forked from Garathd/vidx-video-sharing
/
app.py
621 lines (456 loc) · 14.5 KB
/
app.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import os
import pymysql
import db
from jinja2 import Environment, PackageLoader, select_autoescape
from jinja2.ext import Extension, loopcontrols
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
app.secret_key = os.urandom(24)
"""
Adding Jinja Environment so I can use Jinja Extensions
"""
app.jinja_env = Environment(
loader=PackageLoader('app', 'templates'),
autoescape=select_autoescape(['html', 'xml'])
)
# Adding Jinja Loop Controls Extension
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
"""
Login Page
"""
@app.route('/')
def index():
return render_template('login.html')
"""
Welcome Page
"""
@app.route('/welcome')
def welcome():
try:
username = db.getLogin()
if username:
return render_template('welcome.html', username=username)
else:
return redirect('/')
except:
return redirect('/')
"""
User Logout
"""
@app.route('/logout')
def logout():
if db.getLogin():
message = {
"message_type" : "success",
"message_info" : "{0} has logged out!".format(db.getLogin())
}
#Clean the Login Variable
db.setLogin('')
return render_template('login.html', message=message)
else:
return redirect('/')
"""
Registration Page
"""
@app.route('/register')
def register():
return render_template('register.html')
"""
Register the user
"""
@app.route('/register-user/', methods=['GET','POST'])
def regcheck():
# Getting form data
if request.method == 'POST':
form = request.form
username = form['username']
password = form['password']
# Registering User
try:
result = db.register(username,password)
if result:
# Account creation successful
message = {
"message_type" : "success",
"message_info" : "{0}'s account has been created!".format(username)
}
return render_template('login.html', message=message)
else:
# Username has been taken
message = {
"message_type" : "error",
"message_info" : "The username {0} has been taken!".format(username)
}
return render_template('register.html', message=message)
except:
return redirect('/register')
else:
return redirect('/register')
"""
Checking User Credentials
"""
@app.route('/check-user/', methods=['GET','POST'])
def check():
if request.method == 'POST':
form = request.form
username = form['username']
password = form['password']
# Authenticating the user
try:
result = db.authenticate(username,password)
if result:
user = str(result[0])
if username == user:
# Setting Username
db.setLogin(username)
# Getting User ID
userid = db.getUserId(username)
# Checking if user has any videos
videos = db.getMyVideos(userid)
if videos:
return redirect('/videos')
else:
return redirect('/welcome')
else:
message = {
"message_type" : "error",
"message_info" : "Invalid Login Details!"
}
return render_template('login.html', message=message)
except:
return redirect('/')
return render_template('login.html')
"""
User Profile
"""
@app.route('/<username>')
def dashboard(username):
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting list of my videos
videos = db.getMyVideos(userid)
# If user has no videos redirect to welcome screen
if not videos:
return redirect('/welcome')
# Getting the vote information
votes = db.getAllVotes()
return render_template('dashboard.html',
username=username,
videos=videos,
votes=votes,
get_votes=db.calcVotes)
except:
return redirect('/')
"""
Menu Redirect for my profile
"""
@app.route('/profile')
def profile():
try:
# Get Username
user_name = db.getLogin()
return redirect('/{}'.format(user_name))
except:
return redirect('/')
"""
Feed of videos created by other users original videos
"""
@app.route('/videos')
def videos():
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting original videos created by other users
videos = db.getOtherVideos(userid)
# Getting the vote information
votes = db.getAllVotes()
return render_template('videos.html',
videos=videos,
username=user_name,
votes=votes,
userid=userid,
get_votes=db.calcVotes,
check_voted=db.checkVote)
except:
return redirect('/')
"""
Create a new video
"""
@app.route('/new-video')
def newplaylist():
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting list of categories
categories = db.getCategories()
return render_template('new-video.html',
user_name=user_name,
userid=userid,
categories=categories)
except:
return redirect('/')
"""
Add Video to the database
"""
@app.route('/add-video', methods=['GET','POST'])
def addvideo():
# Video's original creator
origin = "true"
if request.method == 'POST':
form = request.form
userid = form['user_id']
title = form['title']
description = form['description']
image = form['image']
video = form['video']
category = form['category_id']
try:
# Get username
user_name = db.getLogin()
# Add the Video to the database
db.addVideo(userid, title, description, image, video, category, origin)
return redirect('/{}'.format(user_name))
except:
return redirect('/')
"""
Edit video by video id
"""
@app.route('/edit/<videoid>')
def edit(videoid):
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting categories list
categories = db.getCategories()
# Getting playlists by id
video = db.getVideoById(videoid)
# Checks to make sure video belongs to the user editing the video.
if int(userid) == int(video['user_id']):
return render_template('edit-video.html',
video=video,
categories=categories)
else:
return redirect('/profile')
except:
return redirect('/')
"""
Edit video
"""
@app.route('/edit-video', methods=['GET','POST'])
def editvideo():
if request.method == 'POST':
form = request.form
videoid = form['video_id']
title = form['title']
description = form['description']
img_source = form['image']
video_source = form['video']
category_id = form['category_id']
try:
# Getting username
user_name = db.getLogin()
# Edit video information in the database
db.edit(videoid, title, description, img_source, video_source, category_id)
return redirect('/{}'.format(user_name))
except:
return redirect('/')
"""
Delete a video
"""
@app.route('/delete/<videoid>', methods=['GET','POST'])
def delete(videoid):
try:
# Get username
user_name = db.getLogin()
# Getting playlists by id
video = db.getVideoById(videoid)
# Getting user id
userid = db.getUserId(user_name)
# Checks to make sure video belongs to the user deleting the video via the url.
if int(userid) == int(video['user_id']):
# Delete the video
db.delete(videoid)
return redirect('/{}'.format(user_name))
else:
return redirect('/profile')
except:
return redirect('/')
"""
Down vote
"""
@app.route('/downvote/<videoid>')
def downvote(videoid):
# Set vote value
vote = -1
# Calculating the votes for video
total_votes = db.calcVotes(videoid)
if total_votes == None:
total_votes = 0
# This deletes a video if it has a total score of -5
if total_votes > -4:
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Make vote
db.vote(videoid, userid, vote)
return redirect('/videos')
except:
redirect('/')
else:
db.delete(videoid)
return redirect('/videos')
"""
Up vote
"""
@app.route('/upvote/<videoid>')
def upvote(videoid):
# Set vote value
vote = 1
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Make vote
db.vote(videoid, userid, vote)
return redirect('/videos')
except:
redirect('/')
"""
Repost a video
"""
@app.route('/repost/<videoid>')
def repost(videoid):
# Video's original creator
origin = "false"
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Get playlist by id
video = db.getVideoById(videoid)
# Setting variables with copied video data
title = video['title']
description = video['description']
img_source = video['img_source']
video_source = video['video_source']
category_id = video['category_id']
# Adding video to database
db.addVideo(userid, title, description, img_source, video_source, category_id, origin)
return redirect('/{}'.format(user_name))
except:
return redirect('/')
"""
Order profile videos by category
"""
@app.route('/my-profile/order-by/category/<category_name>')
def orderprofilecategory(category_name):
# My videos and profile
profile = True
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting category by category name and ordering
category_id = db.getCategoryIdByName(category_name)
# Getting vote information
votes = db.getAllVotes()
# Getting profile video list ordered by category name
ordered = db.orderByCategory(category_id, userid, profile)
return render_template('dashboard.html',
videos=ordered,
username=user_name,
votes=votes,
get_votes=db.calcVotes)
except:
return redirect('/')
"""
Order profile videos by original videos and reposted videos
"""
@app.route('/my-profile/order-by/saved/<status>')
def ordersaved(status):
try:
# Getting username
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting vote information
votes = db.getAllVotes()
# Getting profile video list ordered by original and reposted
ordered = db.orderBySaved(userid, status)
return render_template('dashboard.html',
videos=ordered,
username=user_name,
votes=votes,
get_votes=db.calcVotes)
except:
return redirect('/')
"""
Order videos by category
"""
@app.route('/videos/order-by/category/<category_name>')
def ordercategory(category_name):
# Not my videos or profile page
profile = False
try:
# Getting user name
user_name = db.getLogin()
# Getting user id
userid = db.getUserId(user_name)
# Getting categories by name and ordering
category_id = db.getCategoryIdByName(category_name)
# Getting vote information
votes = db.getAllVotes()
# Getting video list ordered by category name
ordered = db.orderByCategory(category_id, userid, profile)
return render_template('videos.html',
videos=ordered,
username=user_name,
votes=votes,
userid=userid,
get_votes=db.calcVotes,
check_voted=db.checkVote)
except:
return redirect('/')
"""
Ordering videos by username
"""
@app.route('/videos/order-by/user/<user_name>')
def orderuser(user_name):
try:
# Getting my username
my_username = db.getLogin()
# Getting my user id
my_id = db.getUserId(my_username)
# Getting user id to search
users_id = db.getUserId(user_name)
#Getting vote information
votes = db.getAllVotes()
# Getting videos ordered by user name
ordered = db.orderByUser(users_id, my_id)
return render_template('videos.html',
videos=ordered,
username=my_username,
votes=votes,
userid=my_id,
get_votes=db.calcVotes,
check_voted=db.checkVote)
except:
return redirect('/')
if __name__ == '__main__':
app.run(host=os.environ.get('IP'),
port=int(os.environ.get('PORT')),
debug=True)