-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
274 lines (210 loc) · 7.76 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import json
import os
from flask import Flask, flash, redirect, render_template, request, url_for
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = set(["png", "jpg", "jpeg", "svg"])
app = Flask(__name__, template_folder="./templates", static_folder="./static")
def Product(filename, title, description, price):
product = {
"filename": filename,
"title": title,
"description": description,
"price": price,
}
return product
# products = []
car = []
data = {}
users_data = {}
UPLOAD_FOLDER = "static/assets"
USERS_PATH = "./users_data.json"
PRODUCTS_PATH = "./products.json"
app.secret_key = "secret key"
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 * 1024
logged_users = {} # ip : email
def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
def is_logged(user_ip):
return user_ip in logged_users
@app.route("/api/products", methods=["POST"])
def get_products():
answer = {"products": "", "id": 200} # Everything OK!
if request.method != "POST":
answer["id"] = 405 # Method Not Allowed
return answer
request_data = request.get_json()
begin, end = request_data["begin"], request_data["end"]
if begin < 0 or end <= begin:
answer["id"] = 400 # Bad request
return answer
answer["products"] = products[begin:end] # [begin:end)
return answer
@app.route("/api/register_user", methods=["POST"])
def register_user():
answer = {
"id": 201,
} # User Created!
if request.method != "POST":
answer["id"] = 405 # Method Not Allowed
return answer
request_data = request.get_json()
client_email: str = request_data["email"]
if client_email in users_data:
answer["id"] = 400 # Bad request, repeated user
return answer
users_data[client_email] = request_data
if os.path.isfile(PRODUCTS_PATH):
with open(USERS_PATH, "w") as outfile:
json.dump(users_data, outfile, indent=4)
return answer
@app.route("/api/check_credentials", methods=["POST"])
def check_credentials():
client_ip: str = request.remote_addr
answer = { # credentials aren't OK!
"id": 400,
"correct_email": False,
"correct_password": False,
}
if request.method != "POST":
answer["id"] = 405 # Method Not Allowed
return answer
# if not request.is_json():
# pass # what should we do if the request isn't in json
request_data = request.get_json()
client_email: str = request_data["email"]
if client_email not in users_data:
answer["id"] = 404 # user not found
return answer
user_data = users_data[client_email]
if user_data["email"] == client_email:
answer["correct_email"] = True
client_password: str = request_data["password"]
if user_data["password"] == client_password:
answer["correct_password"] = True
answer["id"] = 200 # credentials are OK!
logged_users[client_ip] = client_email
return answer
@app.route("/")
def index():
client_ip = request.remote_addr
if not is_logged(client_ip):
return redirect(url_for("login"))
data = users_data[logged_users[client_ip]]
context = {"data": data, "products": products}
return render_template("welcome.html", **context, user=data["username"], log=True)
@app.route("/login")
def login():
user_ip = request.remote_addr
if is_logged(user_ip):
return redirect(url_for("profile"))
return render_template("login.html")
@app.route("/register")
def register():
client_ip = request.remote_addr
if is_logged(client_ip):
return redirect("/home")
return render_template("register.html")
@app.route("/profile", methods=["POST", "GET"])
def profile():
client_ip = request.remote_addr
if not is_logged(client_ip):
return redirect("/login")
data = users_data[logged_users[client_ip]]
context = {"data": data}
return render_template("profile.html", **context)
@app.route("/register_product")
def register_product():
client_ip = request.remote_addr
if not is_logged(client_ip):
return redirect("/login")
context = {"data": data}
return render_template("register_product.html", **context)
@app.route("/home", methods=["POST", "GET"])
def upload_image():
user_ip = request.remote_addr
if not is_logged(user_ip):
return redirect("/login")
if request.method == "POST":
if "file" not in request.files:
flash("No file part")
return redirect(request.url)
file = request.files["file"]
if file.filename == "":
flash("No image selected for uploading")
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
new_product = Product(filename, "", "", "")
for key, value in new_product.items():
if key == "filename":
continue
new_product[key] = request.form.get(key)
# print(new_product)
products.append(new_product)
# print(products)
return redirect("/")
# return render_template('welcome.html', **context)
else:
flash("Allowed image types are -> png, jpg, jpeg, gif")
return redirect(request.url)
else:
# return render_template('welcome.html')
return redirect("/")
@app.route("/images/<filename>")
def display_image(filename):
return redirect(url_for("static", filename="assets/" + filename), code=301)
@app.route("/carrito", methods=["POST", "GET"])
def carrito():
user_ip = request.remote_addr
if not is_logged(user_ip):
redirect("/login")
context = {"data": data, "products": car}
if is_logged(user_ip):
return render_template("carrito.html", **context)
@app.route("/carproducts/<filename>", methods=["POST", "GET"])
def carproduct(filename):
for key in products:
if key["filename"] == filename:
print("producto encontrado: ", filename)
car.append(key)
print(car)
return redirect(url_for("index"))
def exit_handler():
print("CTRL + C interruption")
print("SAVING VARS INTO FILES")
with open(PRODUCTS_PATH, "w") as outfile:
json.dump(products, outfile, indent=4)
print(f"users_data : {users_data}")
with open(USERS_PATH, "w") as outfile:
json.dump(users_data, outfile, indent=4)
if __name__ == "__main__":
try:
# load files into local variables
print("LOCAL VARS LOADED")
if os.path.isfile(PRODUCTS_PATH):
products_file = open(PRODUCTS_PATH)
products = json.load(products_file)
print(f"products : {products}")
if os.path.isfile(USERS_PATH):
users_data_file = open(USERS_PATH)
users_data = json.load(users_data_file)
print(f"users_data : {users_data}")
app.run(debug=True)
while True:
pass
# atexit.register(exit_handler)
except KeyboardInterrupt:
# save variables into files
print("CTRL + C interruption")
print("SAVING VARS INTO FILES")
# if(os.path.isfile(PRODUCTS_PATH)):
# with open(PRODUCTS_PATH, "w") as outfile:
# json.dump(products, outfile, indent=4)
# print(f"products : {products}")
# # if(os.path.isfile(USERS_PATH)):
# print(f"before users_data : {users_data}")
# with open(USERS_PATH, "w") as outfile:
# json.dump(users_data, outfile, indent=4)
# print(f"after users_data : {users_data}")