-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
534 lines (430 loc) · 20 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# Imports
from flask import Flask, render_template, redirect, url_for, flash,request, abort
from flask import jsonify
from flask_login import LoginManager, UserMixin, login_required, current_user, logout_user, login_user
from configs.dbconfig import DBConfig
from env import DB_CONNECTION, SALT_ROUNDS, HASH_METHOD, SECRET_KEY, CLOUDINARY_API_SECRET,CLOUDINARY_API_KEY,CLOUDINARY_CLOUD_NAME, IS_PROD
from models import load_class_User, load_class_Playlist, load_class_Card, get_LIKES_table, get_SAVED_table
from utilities.get_quote import get_quote
from utilities.playlist_utility import PlaylistManager
from werkzeug.security import generate_password_hash, check_password_hash
from appconstants import SAMPLE_IMAGES_AVATAR, MONTHS
import random
import cloudinary
import cloudinary.uploader
import cloudinary.api
import os
from datetime import datetime
from functools import wraps
from custom_decors.card_processor import card_processor
app = Flask(__name__)
# Configurations
app.config['SECRET_KEY'] = SECRET_KEY
# DB configs
dbconfig = DBConfig(app)
db,app = dbconfig.connect(DB_CONNECTION)
# Login Manager configs
login_manager = LoginManager()
login_manager.init_app(app)
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
@login_manager.unauthorized_handler
def unauthorized_callback():
return redirect('/login')
def card_creator_only(f):
@wraps(f)
def creator_only_decor(*args, **kwargs):
params = dict(kwargs)
card = Card.query.get(params["id"])
if(current_user.id==card.creator):
return f(*args,**kwargs,card=card)
else:
return redirect(url_for("errorFunc", error="401:Unauthorized request."))
return creator_only_decor
# Media Manager configs
cloudinary.config(cloud_name = CLOUDINARY_CLOUD_NAME,api_key = CLOUDINARY_API_KEY,
api_secret = CLOUDINARY_API_SECRET,secure = True)
# Load models
get_LIKES_table(db)
get_SAVED_table(db)
User = load_class_User(db)
Playlist = load_class_Playlist(db)
Card = load_class_Card(db)
db.create_all()
# Playlist manager
playlist_manager = PlaylistManager(Playlist)
"""
************* ========================================================= *************
====================== Routes Section ===================
=========================================================
"""
"""
=========================================================
==================== General Routes =====================
=========================================================
"""
@app.route("/")
def home():
if current_user.is_authenticated:
Cards = card_processor(Card.query.all()[::-1], current_user.id)
else:
Cards = card_processor(Card.query.all()[::-1], None)
return render_template("home.html",loggedin=current_user.is_authenticated, all_cards=Cards, user_data=current_user)
"""
=========================================================
=============== Authentication Routes ===================
=========================================================
"""
@app.route("/login", methods=["GET","POST"])
def login():
try:
if not current_user.is_authenticated:
if request.method == "GET":
return render_template("login.html")
elif request.method == "POST":
quser = User.query.filter_by(username=request.form.get('username')).first()
if quser:
if(check_password_hash(quser.password, request.form.get('password'))):
login_user(quser)
del current_user.password
return redirect("/")
else:
flash("Invalid credentials, please try again.")
return redirect(url_for("login"))
else:
flash("Invalid credentials, please try again.")
return redirect(url_for("login"))
else:
return redirect("/")
except:
flash("Something went wrong while logging in, please try again.")
return redirect("/login")
@app.route("/register", methods=["GET","POST"])
def register():
try:
if not current_user.is_authenticated:
if request.method == "GET":
return render_template("register.html")
if request.method == "POST":
if User.query.filter_by(username=request.form.get('username')).first():
flash("There is a user already registered with the username, please try again.")
return redirect(url_for("register"))
else:
if request.files['file']:
f = request.files['file']
f.save(f.filename)
link = cloudinary.uploader.upload(f.filename,folder=f"thatday/user")["url"]
os.remove(f.filename)
else:
link = random.choice(SAMPLE_IMAGES_AVATAR)
name = request.form["name"]
username = request.form["username"]
moto = request.form["moto"]
password = generate_password_hash(request.form["password"],method=HASH_METHOD, salt_length=int(SALT_ROUNDS))
new_user = User(name=name,username=username,password=password,img=link,moto=moto)
db.session.add(new_user)
db.session.commit()
login_user(new_user)
del current_user.password
return redirect("/")
else:
return redirect("/")
except Exception as e:
print(e)
flash("Something went wrong while registering, please try again.")
return redirect("/register")
@app.route("/logout")
@login_required
def logout():
logout_user()
return redirect("/")
"""
=========================================================
================ Playlist Card Routes ===================
=========================================================
"""
# Create card
@app.route("/createcard", methods=["GET","POST"])
@login_required
def createcardroute():
if (request.method == "GET"):
return render_template("createcard.html", loggedin=current_user.is_authenticated, user_data=current_user,hide_create_card_link=True)
elif (request.method=="POST"):
# Validation of inputs
date = request.form["date"]
title = request.form["title"]
if(date and title):
date_format = "%Y-%m-%d"
dateinformat = datetime.strptime(date,date_format)
now = datetime.now()
if(dateinformat<=now and len(title)<31):
# Create playlist
# Playlist
no_error, link1 = playlist_manager.get_playlist(date,db)
if(no_error):
try:
# Get random image and quote if doesnt exist
if request.files['file']:
f = request.files['file']
f.save(f.filename)
link = cloudinary.uploader.upload(f.filename,folder=f"thatday/card")["url"]
os.remove(f.filename)
else:
link = cloudinary.uploader.upload("https://source.unsplash.com/random",folder=f"thatday/card")["url"]
if(not request.form["quote"]):
quote = get_quote(10)
else:
quote = request.form["quote"]
year,m,d = tuple(date.split("-"))
if (d[0]=="0"):
d = d[1]
newCard = Card(creator=current_user.id,img=link,title=title,dateformatted=f"{d} {MONTHS[m]} {year}",date=date,by=current_user.name,playlist=link1,quote=quote,likes=0)
db.session.add(newCard)
db.session.commit()
return redirect("/")
except Exception as e:
return redirect(
url_for("errorFunc", error="500:Internal server error, please try again later."))
else:
return redirect(
url_for("errorFunc", error="500:Internal server error, please try again later."))
else:
return redirect(url_for("errorFunc",error="422:Invalid input,date should be in present or past . "))
else:
return redirect(url_for("errorFunc", error="422:Invalid input,missing date or title. "))
# Get card
@app.route("/card/<id>")
def get_card(id):
try:
if(current_user.is_authenticated):
card = card_processor([Card.query.get(id)], current_user.id)
else:
card = card_processor([Card.query.get(id)], None)
if(not card):
return redirect(url_for("errorFunc",error="404:Couldn't find playlist card for this ID"))
else:
return render_template("cardpage.html", all_cards=card,loggedin=current_user.is_authenticated, user_data=current_user)
except Exception as e:
print(e)
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
# Delete card
@app.route("/deletecard/<id>", methods=["DELETE"])
@login_required
@card_creator_only
def delete_card(id, card):
try:
db.session.delete(card)
db.session.commit()
return redirect("/")
except Exception as e:
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
# Edit Card
@app.route("/editcard/<id>", methods=["GET","POST"])
@login_required
@card_creator_only
def edit_card(id, card):
if(request.method=="GET"):
try:
if (not card):
return redirect(url_for("errorFunc", error="404:Couldn't find playlist card for this ID"))
else:
return render_template("editcardpage.html", card=card, loggedin=current_user.is_authenticated,
user_data=current_user)
except Exception as e:
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
elif (request.method == "POST"):
try:
# Check if title has been changed and if new title is valid
if(request.form["title"] and len(request.form["title"])<31):
if(request.form["title"]!=card.title):
card.title = request.form["title"]
else:
return redirect(url_for("errorFunc", error="422:Invalid input,Length of title too long. "))
if (request.form["quote"] and len(request.form["quote"])<251):
if (request.form["quote"] != card.quote):
card.quote = request.form["quote"]
else:
return redirect(url_for("errorFunc", error="422:Invalid input,Length of quote too long. "))
date = request.form["date"]
if (date):
if (date != card.date):
date_format = "%Y-%m-%d"
dateinformat = datetime.strptime(date, date_format)
now = datetime.now()
if (dateinformat <= now):
no_error, link1 = playlist_manager.get_playlist(date, db)
if(not no_error):
return redirect(url_for("errorFunc", error="500:Internal server error, please try again later."))
else:
card.playlist = link1
card.date = date
year, m, d = tuple(date.split("-"))
if (d[0] == "0"):
d = d[1]
card.dateformatted = f"{d} {MONTHS[m]} {year}"
else:
return redirect(
url_for("errorFunc", error="422:Invalid input,date should be in present or past . "))
if request.files['file']:
f = request.files['file']
f.save(f.filename)
link = cloudinary.uploader.upload(f.filename, folder=f"thatday/card")["url"]
os.remove(f.filename)
card.img = link
db.session.commit()
return redirect(f"/card/{card.id}")
except Exception as e:
return redirect(url_for("errorFunc", error="500:Internal server error, please try again later."))
# Like unlike cards
@app.route("/like/<id>", methods=["PUT"])
@login_required
def card_like(id):
if(request.method=="PUT"):
try:
card = Card.query.get(id)
user = User.query.get(current_user.id)
cards_liked = user.liked_cards
if(card in cards_liked):
card.likes = card.likes - 1
user.liked_cards.remove(card)
else:
card.likes = card.likes + 1
user.liked_cards.append(card)
likes = card.likes
db.session.commit()
return jsonify({"message":"Successfully liked card.","status":True,"likes":likes}), 200
except Exception as e:
return jsonify({"message":"Couldn't like card. Internal server error.","status":False,"likes":likes}), 500
# Save unsave cards
@app.route("/save/<id>", methods=["PUT"])
@login_required
def card_save(id):
if(request.method=="PUT"):
try:
card = Card.query.get(id)
user = User.query.get(current_user.id)
cards_saved = user.saved_cards
if(card in cards_saved):
user.saved_cards.remove(card)
else:
user.saved_cards.append(card)
db.session.commit()
return jsonify({"message":"Successfully saved card.","status":True}), 200
except Exception as e:
return jsonify({"message":"Couldn't like card. Internal server error.","status":False}), 500
# Get saved cards
@app.route("/saved")
@login_required
def saved_cards():
try:
user = User.query.get(current_user.id)
cards = user.saved_cards
cards = card_processor(cards,current_user.id)
return render_template("savedcards.html", loggedin=current_user.is_authenticated, all_cards=cards, user_data=current_user)
except Exception as e:
print(e)
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
# Get saved cards
@app.route("/search",methods=["POST"])
def search_cards():
try:
if(request.method=="POST"):
if(request.form["date"]):
term = request.form["query"]
if(term):
cards = Card.query.filter(Card.__ts_vector__.match(term) | Card.date.like(request.form["date"])).all()
else:
cards = Card.query.filter(Card.date.like(request.form["date"])).all()
else:
term = request.form["query"]
if(term):
cards = Card.query.filter(Card.__ts_vector__.match(term)).all()
else:
cards = []
if(current_user.is_authenticated):
cards = card_processor(cards,current_user.id)
else:
cards = card_processor(cards,None)
return render_template("searchedcards.html", loggedin=current_user.is_authenticated, all_cards=cards, user_data=current_user,query=term,date=request.form["date"])
except Exception as e:
print(e)
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
"""
=========================================================
=============== Account Routes ===================
=========================================================
"""
@app.route("/myaccount")
@login_required
def myaccount():
try:
user = User.query.get(current_user.id)
cards = Card.query.filter_by(creator=user.id).all()
like_sum = sum([card.likes for card in cards])
if (like_sum == 0):
like_sum = "-"
elif (like_sum > 999):
like_sum = str(like_sum / 1000) + "k"
cards = card_processor(cards,current_user.id)
return render_template("accountpage.html", loggedin=current_user.is_authenticated, all_cards=cards, user_data=current_user, like_sum=like_sum)
except Exception as e:
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
@app.route("/editaccount",methods=["GET","POST"])
@login_required
def editaccount():
try:
user = User.query.get(current_user.id)
if (request.method == "GET"):
return render_template("editaccountpage.html", user_data=current_user)
elif(request.method == "POST"):
if request.files['file']:
f = request.files['file']
f.save(f.filename)
link = cloudinary.uploader.upload(f.filename,folder=f"thatday/user")["url"]
os.remove(f.filename)
user.img = link
user.name = request.form["name"]
user.moto = request.form["moto"]
db.session.commit()
login_user(user)
del current_user.password
return redirect("/myaccount")
except Exception as e:
print(e)
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
@app.route("/changepassword",methods=["GET","POST"])
@login_required
def change_password_page():
try:
user = User.query.get(current_user.id)
if (request.method == "GET"):
return render_template("changepassword.html", user_data=current_user)
elif(request.method == "POST"):
if user:
if(check_password_hash(user.password, request.form.get('oldpassword'))):
user.password = generate_password_hash(request.form["password"],method=HASH_METHOD, salt_length=int(SALT_ROUNDS))
db.session.commit()
login_user(user)
del current_user.password
return redirect("/myaccount")
else:
flash("Invalid credentials, please try again.")
return redirect(url_for("login"))
else:
flash("Something went wrong, user couldn't be found, please try again.")
return redirect(url_for("change_password_page"))
except Exception as e:
print(e)
return redirect(url_for("errorFunc", error="500:Internal server error please try again later."))
"""
=========================================================
=============== Miscellaneous Routes ===================
=========================================================
"""
# Error Handling
@app.route("/error/<error>")
def errorFunc(error):
e_code = error.split(":")[0]
e_msg = error.split(":")[1]
return render_template('error.html',ecode=e_code,msg=e_msg), e_code