-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
234 lines (182 loc) · 6.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import datetime
import logging
import uuid
import jwt # pip install PYJWT
from flask import Flask, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
from flask_uploads import (
UploadSet,
configure_uploads,
IMAGES,
) # pip install Flask-Uploads
app = Flask(__name__)
app.config["SECRET_KEY"] = "thisissecret"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///Sample.db"
app.config["UPLOADED_PHOTOS_DEST"] = "uploads"
photos = UploadSet("photos", IMAGES)
configure_uploads(app, photos)
db = SQLAlchemy(app)
db.create_all()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
password = db.Column(db.String(80))
admin = db.Column(db.Boolean)
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
author = db.Column(db.String(20))
publisher = db.Column(db.String(50))
user_id = db.Column(db.Integer) # refers to id of User...
def token_required(f):
@wraps(f)
def decorated(*args, **kwargs):
token = None
if "x-access-token" in request.headers:
token = request.headers["x-access-token"]
if not token:
return jsonify({"message": "Token is missing!"}), 401
try:
data = jwt.decode(token, app.config["SECRET_KEY"])
current_user = User.query.filter_by(public_id=data["public_id"]).first()
except:
return jsonify({"message": "Token is invalid!"}), 401
return f(current_user, *args, **kwargs)
return decorated
@app.route("/upload", methods=["POST"])
def upload():
if "photo" in request.files:
filename = photos.save(request.files["photo"])
return jsonify({"file name": filename})
return jsonify({"message": "Image not uploaded"})
@app.route("/login", methods=["GET"])
def login():
auth = request.authorization
if not auth or not auth.username or not auth.password:
return make_response(
"Could not verify",
401,
{"WWW-Authenticate": 'Basic realm="Login required!"'},
)
user = User.query.filter_by(name=auth.username).first()
if not user:
return make_response(
"Could not verify",
401,
{"WWW-Authenticate": 'Basic realm="Login required!"'},
)
if check_password_hash(user.password, auth.password):
# if auth.password == user.password:
token = jwt.encode(
{
"public_id": user.public_id,
"exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=10),
},
app.config["SECRET_KEY"],
)
return jsonify({"token": token.decode("UTF-8")})
return make_response(
"Could not verify", 401, {"WWW-Authenticate": 'Basic realm="Login required!"'}
)
@app.route("/form_signup", methods=["POST"])
def form_signup():
logging.log(logging.INFO, "Approached!")
return jsonify(
{
"user_name": request.form.get("user_name"),
"user_password": request.form.get("user_password"),
}
)
@app.route("/signup", methods=["POST"])
def signup_user():
data = request.get_json()
hashed_password = generate_password_hash(data["password"], method="sha256")
new_user = User(
public_id=str(uuid.uuid4()),
name=data["name"],
password=hashed_password,
admin=False,
)
db.session.add(new_user)
db.session.commit()
return jsonify({"message": "Account created!"})
@app.route("/")
def home():
return jsonify({"message": "This is home page!"})
@app.route("/book", methods=["GET"])
@token_required
def get_all_books(current_user):
if not current_user.admin:
books = Book.query.filter_by(user_id=current_user.id).all()
output = []
for book in books:
book_data = {}
book_data["id"] = book.id
book_data["name"] = book.name
book_data["author"] = book.author
book_data["publisher"] = book.publisher
output.append(book_data)
else:
books = Book.query.all()
output = []
for book in books:
book_data = {}
book_data["id"] = book.id
book_data["name"] = book.name
book_data["author"] = book.author
book_data["publisher"] = book.publisher
output.append(book_data)
return jsonify(output)
@app.route("/book/<book_id>", methods=["GET"])
@token_required
def get_one_book(current_user, book_id):
book = Book.query.filter_by(id=book_id, user_id=current_user.id).first()
if not book:
return jsonify({"message": "No book found!"})
book_data = {}
book_data["id"] = book.id
book_data["name"] = book.name
book_data["author"] = book.author
book_data["publisher"] = book.publisher
return jsonify(book_data)
@app.route("/book", methods=["POST"])
@token_required
def add_book(current_user):
data = request.get_json()
new_book = Book(
name=data["name"],
author=data["author"],
publisher=data["publisher"],
user_id=current_user.id,
)
db.session.add(new_book)
db.session.commit()
return jsonify({"message": "Book added!"})
@app.route("/book/<book_id>", methods=["PUT"])
@token_required
def update_book(current_user, book_id):
data = request.get_json()
book = Book.query.filter_by(id=book_id, user_id=current_user.id).first()
if not book:
return jsonify({"message": "No book found!"})
book.name = data["name"]
book.author = data["author"]
book.publisher = data["publisher"]
db.session.commit()
return jsonify({"message": "Book item has been updated!"})
@app.route("/book/<book_id>", methods=["DELETE"])
@token_required
def delete_book(current_user, book_id):
book = Book.query.filter_by(id=book_id, user_id=current_user.id).first()
if not book:
return jsonify({"message": "No book found!"})
db.session.delete(book)
db.session.commit()
return jsonify({"message": "Book deleted!"})
if __name__ == "__main__":
# app.run(host='0.0.0.0', debug=True)
app.run()
# Type in terminal to get ip.... ifconfig | grep "inet " | grep -v 127.0.0.1