-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
809 lines (714 loc) · 30.6 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template,session,redirect,url_for, jsonify, request
from flask_caching import Cache
from flask_compress import Compress
from config import config
from urllib.parse import quote_plus, urlencode
from authlib.integrations.flask_client import OAuth
from dataclasses import dataclass
from content import suggestion_content
import discord_webhook
import json
import uuid
from datetime import date
from functools import wraps
def to_pretty_json(obj: dict) -> str:
return json.dumps(obj, default=lambda o: o.__dict__, indent=4)
def page_not_found(e):
return render_template('404.html'), 404
def requires_auth(f):
"""
Use on routes that require a valid session, otherwise it aborts with a 403
"""
@wraps(f)
def decorated(*args, **kwargs):
admin = False
moderator = False
if session.get('user'):
userinfo = session.get('user').get("userinfo")
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
banned = Ban.query.filter_by(userid=user.userid).first()
if banned:
return "Your account has been locked. Contact the PortMaster discord crew for more details. This may have happened if you did not follow the <a href='https://discord.com/channels/1122861252088172575/1232622554984878120/1232623360211423344'>suggestion guidelines</a>. Click <a href='/'>here</a> to go back to home page."
if Admin.query.filter_by(userid=user.userid).first():
admin = True
if Moderator.query.filter_by(userid=user.userid).first():
moderator = True
kwargs["userinfo"] = userinfo
kwargs["admin"] = admin
kwargs["moderator"] = moderator
return f(*args, **kwargs)
else:
return redirect(url_for('login',next=request.full_path))
return decorated
"""
Configuration of the app
"""
app = Flask(__name__)
app.secret_key = config["WEBAPP"]["SECRET_KEY"]
app.jinja_env.filters['to_pretty_json'] = to_pretty_json
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///db.db'
app.config['SECRET_KEY']=config["FLASKCONFIG"]["SECRET_KEY"]
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
app.config['CACHE_TYPE']="SimpleCache"
app.config['CACHE_DEFAULT_TIMEOUT']=300
db = SQLAlchemy(app)
compress = Compress(app)
cache = Cache(app)
@dataclass
class Suggestion(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
title:str = db.Column(db.String(100))
weburl:str = db.Column(db.String(200))
imageurl:str = db.Column(db.String(200))
license:str = db.Column(db.String(10))
dependencies:str = db.Column(db.String(300))
content:str = db.Column(db.String(100))
comment:str = db.Column(db.String(300))
engine:str = db.Column(db.String(50))
feasibility:str = db.Column(db.String(50))
status:str = db.Column(db.String(50))
category:str = db.Column(db.String(50))
tags:str = db.Column(db.String(200))
userid:str = db.Column(db.String(100))
date:str = db.Column(db.String(50))
language:str = db.Column(db.String(50))
@dataclass
class Admin(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
userid:str = db.Column(db.String(100))
@dataclass
class Moderator(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
userid:str = db.Column(db.String(100))
@dataclass
class User(db.Model):
userid:str = db.Column(db.String(200),primary_key=True)
idpid:str = db.Column(db.String(100))
idpname:str = db.Column(db.String(100))
displayname:str = db.Column(db.String(100))
contact:str = db.Column(db.String(100))
lastlogin:str = db.Column(db.String(20))
status:str = db.Column(db.String(20))
@dataclass
class Ban(db.Model):
userid:str = db.Column(db.String(100),primary_key=True)
date:str = db.Column(db.String(20))
@dataclass
class PortRating(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
port:str = db.Column(db.String(100))
rating:str = db.Column(db.String(100))
@dataclass
class SuggestionVote(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
userid:str = db.Column(db.String(200))
suggestionid:str = db.Column(db.String(100))
@dataclass
class AuditHistory(db.Model):
id:str = db.Column(db.String(200),primary_key=True)
source:str = db.Column(db.String(100))
target:str = db.Column(db.String(100))
type:str = db.Column(db.String(50))
date:str = db.Column(db.String(20))
@cache.cached(timeout=50,key_prefix='get_suggestions')
def getSuggestions():
return Suggestion.query.all()
@cache.cached(timeout=50,key_prefix='get_admins')
def getAdmins():
return Admin.query.all()
@cache.cached(timeout=50,key_prefix='get_moderators')
def getModerators():
return Moderator.query.all()
def add_user(userinfo):
isUser = User.query.filter_by(idpid=userinfo["sub"]).first()
if not isUser:
user = User(userid=uuid.uuid4().hex,idpid=userinfo["sub"],contact=userinfo["email"],displayname=userinfo["name"],idpname=userinfo["name"],lastlogin=date.today(),status="Active")
db.session.add(user)
db.session.commit()
else:
isUser.idpname=userinfo["name"]
isUser.lastlogin=date.today()
db.session.commit()
def check_priviledges_new():
username = None
userid = None
loggedin = False
isadmin = False
ismoderator = False
if session.get('user'):
loggedin = True
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
username = user.idpname
userid = user.userid
for admin in getAdmins():
if admin.userid == userid:
isadmin = True
break
for moderator in getModerators():
if moderator.userid == userid:
ismoderator = True
break
return (loggedin,username,userid,ismoderator,isadmin)
def check_priviledges():
username = None
userid = None
loggedin = False
admin = False
moderator = False
if session.get('user'):
loggedin = True
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
username = user.idpname
userid = user.userid
if Admin.query.filter_by(userid=user.userid).first():
admin = True
if Moderator.query.filter_by(userid=user.userid).first():
moderator = True
return (loggedin,username,userid,moderator,admin)
def log_change(source,target,type):
if config["WEBAPP"].getboolean("LOG_HOSTORY"):
log = AuditHistory(id=uuid.uuid4().hex,source=source,target=target,type=type,date=date.today())
db.session.add(log)
db.session.commit()
def has_session():
if session.get('user'):
return True
else:
return False
auth0_config = config['AUTH0']
oauth = OAuth(app)
domain = auth0_config["DOMAIN"]
client_id = auth0_config["CLIENT_ID"]
client_secret = auth0_config["CLIENT_SECRET"]
oauth.register(
"auth0",
client_id=client_id,
client_secret=client_secret,
client_kwargs={
"scope": "openid profile email",
},
server_metadata_url=f'https://{domain}/.well-known/openid-configuration'
)
# Auth related views
@app.route("/login")
def login():
"""
Redirects the user to the Auth0 Universal Login (https://auth0.com/docs/authenticate/login/auth0-universal-login)
"""
return oauth.auth0.authorize_redirect(
redirect_uri=url_for("callback", _external=True,next=request.args.get('next'))
)
@app.route("/signup")
def signup():
"""
Redirects the user to the Auth0 Universal Login (https://auth0.com/docs/authenticate/login/auth0-universal-login)
"""
return oauth.auth0.authorize_redirect(
redirect_uri=url_for("callback", _external=True),
screen_hint="signup"
)
@app.route("/callback", methods=["GET", "POST"])
def callback():
"""
Callback redirect from Auth0
"""
redirecturl = "/"
if request.args.get('next'):
redirecturl = request.args.get('next')
try:
token = oauth.auth0.authorize_access_token()
except Exception as e:
return redirect("/")
session["user"] = token
add_user(token["userinfo"])
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
banned = Ban.query.filter_by(userid=user.userid).first()
if banned:
session.clear()
return "Your account has been locked. Contact the PortMaster discord crew for more details. This may have happened if you did not follow the <a href='https://discord.com/channels/1122861252088172575/1232622554984878120/1232623360211423344'>suggestion guidelines</a>. Click <a href='/'>here</a> to go back to home page"
return redirect(redirecturl)
@app.route("/logout")
def logout():
"""
Logs the user out of the session and from the Auth0 tenant
"""
session.clear()
return redirect(
"https://" + domain
+ "/v2/logout?"
+ urlencode(
{
"returnTo": url_for("home", _external=True),
"client_id": client_id,
},
quote_via=quote_plus,
)
)
@app.route("/")
def home():
suggestions = []
loggedin,username,userid,moderator,admin = check_priviledges_new()
canEdit = False
if moderator or admin:
canEdit = True
for suggestion in Suggestion.query.all():
user = User().query.filter_by(userid=suggestion.userid).first()
if user:
suggestion = jsonify(suggestion).json
votes = SuggestionVote.query.filter_by(suggestionid=suggestion["id"]).all()
suggestion["voteCount"] = len(votes)
if loggedin:
idpid = session.get('user').get("userinfo")["sub"]
currentUser = User.query.filter_by(idpid=idpid).first()
vote = SuggestionVote.query.filter_by(userid=currentUser.userid,suggestionid=suggestion["id"]).first()
if vote:
suggestion["voted"] = True
else:
suggestion["voted"] = False
#suggestion["author"] = user.displayname
suggestion["author"] = ""
suggestions.append(suggestion)
return render_template("suggestions.html", suggestions=suggestions,userid=userid,loggedin=loggedin,username=username,canEdit=canEdit,moderator=moderator,admin=admin)
@app.route("/rate-port")
@requires_auth
def rate_port(userinfo,admin,moderator):
port = request.args.get('port') + ".zip"
loggedin,username,userid,moderator,admin = check_priviledges()
print(request.referrer)
return render_template('rate_port.html',loggedin=loggedin,admin=admin,moderator=moderator,port=port,userid=userid)
@app.route("/post-rating",methods=['POST'])
@requires_auth
def post_rating(userinfo,admin,moderator):
id = request.form['id']
suggestion = Suggestion.query.filter_by(id=id).first()
loggedin,username,userid,moderator,admin = check_priviledges()
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
if admin or moderator or suggestion.userid == user.userid :
if request.form['action'] == "delete":
Suggestion.query.filter_by(id=id).delete()
else:
suggestion.title = request.form['title']
suggestion.weburl = request.form['weburl']
suggestion.imageurl = request.form['imageurl']
suggestion.license = request.form['license']
suggestion.content = request.form['content']
suggestion.dependencies = ",".join(request.form.getlist('dependencies'))
suggestion.comment = request.form['comment']
suggestion.engine = request.form['engine']
suggestion.category = request.form['category']
suggestion.language = request.form['language']
db.session.commit()
return redirect('/')
@app.route("/suggestion-details")
def suggestion_details():
id = request.args.get('id')
loggedin,username,userid,moderator,admin = check_priviledges()
if not id:
return redirect('/')
suggestion = Suggestion.query.filter_by(id=id).first()
user = User.query.filter_by(userid=suggestion.userid).first()
suggestion.author = user.displayname
votes = SuggestionVote.query.filter_by(suggestionid=suggestion.id).all()
suggestion.votes = len(votes)
return render_template('suggestion_details.html',suggestion=suggestion,userid=userid,loggedin=loggedin,username=username,moderator=moderator,admin=admin)
@app.route("/suggestion-moderator")
@requires_auth
def suggestion_moderator(userinfo,admin,moderator):
if admin or moderator:
id = request.args.get('id')
loggedin = True
suggestion = Suggestion.query.filter_by(id=id).first()
canEdit = True
if session.get('user'):
user = User.query.filter_by(userid=suggestion.userid).first()
return render_template('suggestion_moderator.html',user=user,admin=admin,moderator=moderator,suggestion=suggestion,loggedin=loggedin,suggestion_content=suggestion_content,canEdit=canEdit)
@app.route("/api/backup")
def api_backup():
loggedin,username,userid,moderator,admin = check_priviledges()
if admin:
backup = {}
backup["Suggestion"] = jsonify(Suggestion.query.all()).json
backup["SuggestionVote"] = jsonify(SuggestionVote.query.all()).json
backup["User"] = jsonify(User.query.all()).json
backup["Ban"] = jsonify(Ban.query.all()).json
backup["Admin"] = jsonify(Admin.query.all()).json
backup["Moderator"] = jsonify(Moderator.query.all()).json
backup["PortRating"] = jsonify(PortRating.query.all()).json
backup["AuditHistory"] = jsonify(AuditHistory.query.all()).json
return (jsonify(backup))
else:
return redirect('/')
@app.route("/api/suggestions")
def api_suggestions():
suggestions = []
loggedin = False
#loggedin,username,userid,moderator,admin = check_priviledges_new()
if session.get('user'):
loggedin = True
for suggestion in getSuggestions():
user = User().query.filter_by(userid=suggestion.userid).first()
if user:
suggestion = jsonify(suggestion).json
votes = SuggestionVote.query.filter_by(suggestionid=suggestion["id"]).all()
suggestion["voteCount"] = len(votes)
if loggedin:
idpid = session.get('user').get("userinfo")["sub"]
currentUser = User.query.filter_by(idpid=idpid).first()
vote = SuggestionVote.query.filter_by(userid=currentUser.userid,suggestionid=suggestion["id"]).first()
if vote:
suggestion["voted"] = True
else:
suggestion["voted"] = False
#suggestion["author"] = user.displayname
suggestion["author"] = ""
suggestions.append(suggestion)
return (suggestions)
@app.route("/api/suggestions_new")
def api_suggestions_new():
currentUser = None
if has_session():
idpid = session.get('user').get("userinfo")["sub"]
currentUser = User.query.filter_by(idpid=idpid).first()
suggestions = []
for suggestion in getSuggestions():
user = User().query.filter_by(userid=suggestion.userid).first()
if user:
suggestion = jsonify(suggestion).json
votes = SuggestionVote.query.filter_by(suggestionid=suggestion["id"]).all()
suggestion["voteCount"] = len(votes)
if has_session():
vote = SuggestionVote.query.filter_by(userid=currentUser.userid,suggestionid=suggestion["id"]).first()
if vote:
suggestion["voted"] = True
else:
suggestion["voted"] = False
#suggestion["author"] = user.displayname
suggestion["author"] = ""
suggestions.append(suggestion)
return (suggestions)
@app.route("/suggestion")
@requires_auth
def suggestion(userinfo,admin,moderator):
id = request.args.get('id')
loggedin = True
suggestion = Suggestion.query.filter_by(id=id).first()
canEdit = False
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
admin = Admin.query.filter_by(userid=user.userid).first()
moderator = Moderator.query.filter_by(userid=user.userid).first()
if user.userid == suggestion.userid or admin or moderator:
canEdit = True
return render_template('suggestion.html',suggestion=suggestion,loggedin=loggedin,suggestion_content=suggestion_content,canEdit=canEdit)
@app.route("/new-suggestion")
@requires_auth
def new_suggestion(userinfo,admin,moderator):
loggedin = True
suggestions = Suggestion.query.all()
if admin or moderator:
return render_template('new_suggestion.html',admin=admin,moderator=moderator,loggedin=loggedin,suggestion_content=suggestion_content,suggestions=suggestions)
else:
return redirect('/')
@app.route("/remove-privilege",methods=['GET'])
@requires_auth
def remove_privilege(userinfo,admin,moderator):
loggedin,username,userid,moderator,admin = check_priviledges()
if admin:
privilegeType = request.args.get('privilege')
targetuserid = request.args.get('userid')
if privilegeType == "admin":
Admin.query.filter_by(userid=targetuserid).delete()
db.session.commit()
log_change(userid,targetuserid,"Remove Admin")
if privilegeType == "moderator":
Moderator.query.filter_by(userid=targetuserid).delete()
db.session.commit()
log_change(userid,targetuserid,"Remove Moderator")
return redirect('/admin')
@app.route("/ban-user",methods=['GET'])
@requires_auth
def ban_user(userinfo,admin,moderator):
if admin:
userid = request.args.get('userid')
ban = Ban(userid=userid,date=date.today())
user = User.query.filter_by(userid=userid).first()
idpid = userinfo["sub"]
source_user = User.query.filter_by(idpid=idpid).first()
user.status = "Banned"
db.session.add(ban)
db.session.commit()
log_change(source_user.userid,userid,"Ban User")
return redirect('/moderator')
@app.route("/unban-user",methods=['GET'])
@requires_auth
def unban_user(userinfo,admin,moderator):
if admin or moderator:
userid = request.args.get('userid')
Ban.query.filter_by(userid=userid).delete()
user = User.query.filter_by(userid=userid).first()
idpid = userinfo["sub"]
source_user = User.query.filter_by(idpid=idpid).first()
user.status = "Active"
db.session.commit()
log_change(source_user.userid,userid,"Unban User")
return redirect('/moderator')
@app.route("/add-privilege",methods=['GET'])
@requires_auth
def add_privilege(userinfo,admin,moderator):
loggedin,username,sourceuserid,moderator,admin = check_priviledges()
if admin:
privilegeType = request.args.get('privilege')
userid = request.args.get('userid')
if privilegeType == "admin":
existingAdmin = Admin.query.filter_by(userid=userid).first()
if not existingAdmin:
admin = Admin(id=uuid.uuid4().hex,userid=userid)
db.session.add(admin)
db.session.commit()
log_change(sourceuserid,userid,"Add Admin")
if privilegeType == "moderator":
existingModerator = Moderator.query.filter_by(userid=userid).first()
if not existingModerator:
moderator = Moderator(id=uuid.uuid4().hex,userid=userid)
db.session.add(moderator)
db.session.commit()
log_change(sourceuserid,userid,"Add Moderator")
return redirect('/admin')
@app.route("/vote-suggestion",methods=['POST'])
@requires_auth
def vote_suggestion(userinfo,admin,moderator):
userid = request.get_json()["userid"]
suggestionid = request.get_json()["suggestionid"]
idpid = userinfo["sub"]
user = User.query.filter_by(idpid=idpid).first()
if user.userid == userid:
vote = SuggestionVote.query.filter_by(userid=userid,suggestionid=suggestionid).first()
if not vote:
upvote = SuggestionVote(id=uuid.uuid4().hex,userid=userid,suggestionid=suggestionid)
db.session.add(upvote)
db.session.commit()
count = SuggestionVote.query.filter_by(suggestionid=suggestionid).all()
return json.dumps({'count':len(count)}), 200, {'ContentType':'application/json'}
else:
SuggestionVote.query.filter_by(userid=userid,suggestionid=suggestionid).delete()
db.session.commit()
count = SuggestionVote.query.filter_by(suggestionid=suggestionid).all()
return json.dumps({'count':len(count)}), 200, {'ContentType':'application/json'}
else:
return json.dumps({'success':False}), 404, {'ContentType':'application/json'}
@app.route("/edit-suggestion-moderator",methods=['POST'])
@requires_auth
def edit_suggestion_moderator(userinfo,admin,moderator):
loggedin,username,userid,moderator,admin = check_priviledges()
if admin or moderator:
id = request.form['id']
if request.form['action'] == "delete":
Suggestion.query.filter_by(id=id).delete()
#log_change(userid,request.form['id'],"Moderator Delete Suggestion")
else:
suggestion = Suggestion.query.filter_by(id=id).first()
suggestion.title = request.form['title']
suggestion.weburl = request.form['weburl']
suggestion.imageurl = request.form['imageurl']
suggestion.license = request.form['license']
suggestion.content = request.form['content']
suggestion.dependencies = ",".join(request.form.getlist('dependencies'))
suggestion.comment = request.form['comment']
suggestion.engine = request.form['engine']
suggestion.category = request.form['category']
suggestion.language = request.form['language']
suggestion.feasibility = request.form['feasibility']
suggestion.status = request.form['status']
#log_change(userid,request.form['id'],"Moderator Edit Suggestion")
db.session.commit()
return redirect('/')
@app.route("/edit-suggestion",methods=['POST'])
@requires_auth
def edit_suggestion(userinfo,admin,moderator):
id = request.form['id']
suggestion = Suggestion.query.filter_by(id=id).first()
loggedin,username,userid,moderator,admin = check_priviledges()
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
if admin or moderator or suggestion.userid == user.userid :
if request.form['action'] == "delete":
Suggestion.query.filter_by(id=id).delete()
#log_change(userid,request.form['id'],"User Delete Suggestion")
else:
suggestion.title = request.form['title']
suggestion.weburl = request.form['weburl']
suggestion.imageurl = request.form['imageurl']
suggestion.license = request.form['license']
suggestion.content = request.form['content']
suggestion.dependencies = ",".join(request.form.getlist('dependencies'))
suggestion.comment = request.form['comment']
suggestion.engine = request.form['engine']
suggestion.category = request.form['category']
suggestion.language = request.form['language']
#log_change(userid,request.form['id'],"User Edit Suggestion")
db.session.commit()
return redirect('/')
@app.route("/add-suggestion",methods=['POST'])
@requires_auth
def add_suggestion(userinfo,admin,moderator):
title = request.form['title']
exisistingSugegstion = Suggestion.query.filter_by(title=title).first()
if not exisistingSugegstion:
weburl = request.form['weburl']
imageurl = request.form['imageurl']
license = request.form['license']
content = request.form['content']
dependencies = ",".join(request.form.getlist('dependencies'))
comment = request.form['comment']
engine = request.form['engine']
feasibility = "low"
status = "Pending"
category = request.form['category']
tags = ""
dateadded = date.today()
language = request.form['language']
id = uuid.uuid4().hex
idpid = userinfo["sub"]
user = User.query.filter_by(idpid=idpid).first()
userid = user.userid
suggestion = Suggestion(id=id,userid=userid,title=title,weburl=weburl, imageurl=imageurl,license=license,dependencies=dependencies,comment=comment,engine=engine,feasibility=feasibility,status=status,category=category,tags=tags,date=dateadded,language=language,content=content)
db.session.add(suggestion)
db.session.commit()
if "127.0.0.1" not in request.host:
discord_webhook.post_message(title=title,link= "https://" + request.host + "/suggestion-moderator?id="+id,image=imageurl,author=user.displayname,comment=comment,profileimage=userinfo["picture"])
#log_change(user.userid,id,"Add Suggestion")
return redirect('/')
else:
return redirect('/')
@app.route("/update-profile",methods=['POST'])
@requires_auth
def update_profile(userinfo,admin,moderator):
userid = request.form['userid']
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
if user.userid == userid:
displayname = request.form['displayname']
contact = request.form['contact']
user.displayname = displayname
user.contact = contact
db.session.commit()
return redirect('/profile')
else:
return redirect('/')
@app.route("/view-user")
@requires_auth
def view_user(userinfo,admin,moderator):
loggedin = has_session()
if admin or moderator:
userid = request.args.get('userid')
user = User.query.filter_by(userid=userid).first()
canEdit = False
if moderator or admin:
canEdit = True
return render_template('view_user.html', user_profile=userinfo,user=user,canEdit=canEdit,moderator=moderator,admin=admin,loggedin=loggedin)
else:
return redirect('/')
@app.route("/edit-profile")
@requires_auth
def edit_profile(userinfo,admin,moderator):
loggedin = has_session()
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
canEdit = False
if moderator or admin:
canEdit = True
return render_template('edit_profile.html',user_profile=userinfo,user=user,canEdit=canEdit,moderator=moderator,admin=admin,loggedin=loggedin)
@app.route("/profile")
@requires_auth
def profile(userinfo,admin,moderator):
loggedin,username,userid,moderator,admin = check_priviledges()
suggestions = []
idpid = session.get('user').get("userinfo")["sub"]
user = User.query.filter_by(idpid=idpid).first()
canEdit = False
if moderator or admin:
canEdit = True
for suggestion in Suggestion.query.all():
if suggestion.userid == user.userid:
suggestion = jsonify(suggestion).json
votes = SuggestionVote.query.filter_by(suggestionid=suggestion["id"]).all()
suggestion["voteCount"] = len(votes)
if loggedin:
idpid = session.get('user').get("userinfo")["sub"]
currentUser = User.query.filter_by(idpid=idpid).first()
vote = SuggestionVote.query.filter_by(userid=currentUser.userid,suggestionid=suggestion["id"]).first()
if vote:
suggestion["voted"] = True
else:
suggestion["voted"] = False
#suggestion["author"] = user.displayname
suggestion["author"] = ""
suggestions.append(suggestion)
return render_template('profile.html',userid=userid,loggedin=loggedin,user_profile=userinfo,user=user,suggestions=suggestions,canEdit=canEdit,moderator=moderator,admin=admin)
@app.route("/moderator")
@requires_auth
def moderator(userinfo,admin,moderator):
loggedin,username,userid,moderator,admin = check_priviledges()
if moderator or admin:
admins = Admin.query.all()
moderators = Moderator.query.all()
suggestions = Suggestion.query.all()
for suggestion in suggestions:
if "https" not in suggestion.imageurl:
suggestion.brokenimage = True
users = User.query.all()
for user in users:
isAdmin = Admin.query.filter_by(userid=user.userid).first()
if isAdmin:
user.admin = True
for suggestion in suggestions:
user = User().query.filter_by(userid=suggestion.userid).first()
if user:
suggestion.creator = user.idpname
bans = Ban.query.all()
for ban in bans:
user = User().query.filter_by(userid=ban.userid).first()
if user:
ban.idpname = user.idpname
ban.displayname = user.displayname
ban.contact = user.contact
return render_template('moderator.html',admin=admin,moderator=moderator,loggedin=loggedin,admins=admins,moderators=moderators,suggestions=suggestions,users=users,bans=bans)
else:
return redirect('/')
@app.route("/admin")
@requires_auth
def admin(userinfo,admin,moderator):
if not admin:
return redirect('/')
loggedin = has_session()
admins = Admin.query.all()
for dbadmin in admins:
user = User.query.filter_by(userid=dbadmin.userid).first()
if user:
dbadmin.idpname = user.idpname
dbadmin.displayname = user.displayname
dbadmin.contact = user.contact
moderators = Moderator.query.all()
for dbmoderator in moderators:
user = User.query.filter_by(userid=dbmoderator.userid).first()
if user:
dbmoderator.idpname = user.idpname
dbmoderator.displayname = user.displayname
dbmoderator.contact = user.contact
users = User.query.all()
history = AuditHistory.query.all()
for item in history:
source_user = User.query.filter_by(userid=item.source).first()
item.source_idpname = source_user.idpname
target_user = User.query.filter_by(userid=item.target).first()
item.target_idpname = target_user.idpname
return render_template('admin.html',admin=admin,moderator=moderator,loggedin=loggedin,admins=admins,moderators=moderators,users=users,history=history)
if __name__ == '__main__':
app.run(host='0.0.0.0',debug=True,port=4040)
print("test")