/
app.py
927 lines (650 loc) · 27.9 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
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
import mysql.connector
from flask import (Flask, flash, redirect, render_template, request, session,
url_for)
import datetime
from Helper import DataSizeRange
from Helper import Generator as Gen
from Helper import Validator as Val
app = Flask(__name__)
app.debug = True
app.secret_key = "12345" # use a better secret_key
app.templates_auto_reload = True
app.cache_type = "null"
app.permanent_session_lifetime = datetime.timedelta(hours=1)
DATABASE_NAME = "pill_code_db" # mysql db_name
USERNAME = "root"
HOST = "localhost"
PASSWORD = ""
# function to read the article content and title
def read_title_and_post(article_id):
try:
id = article_id
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `post_title`, `post_content` FROM `articles` WHERE `post_id`=%s"
cur = db_conn.cursor()
cur.execute(sql_query, (id, ))
if not cur:
return False
data = cur.fetchone()
db_conn.close()
return data
except (mysql.connector.Error, Exception) as e:
print(str(e))
return False
# this function does the massive work in updating a users firstname, lastname and bio
# call this function then pass in the user email
# , the name of the field to update. the value to pass there is obtain from the form.
# also we take the field name and the name of the button to that form
def update_user_profile(email, set_field, field_name, btn_name):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
if not email:
return False
if not request.method == 'POST':
return False
if email != session.get('email'):
return False
field_data = request.form.get(field_name)
field_btn_val = request.form.get(btn_name)
if not field_name or not field_btn_val:
message = 'There is an empty field, please fill it out'
cat_filter = 'danger'
return False
sql_query = f"UPDATE `users` SET {set_field}=%s WHERE `user_email`=%s"
values = (field_data, email)
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
cur = db_conn.cursor()
cur.execute(sql_query, values)
if not cur:
message = "update unsuccessful"
cat_filter = 'danger'
db_conn.close()
return False
message = "update successful"
cat_filter = 'success'
db_conn.commit()
db_conn.close()
flash(message, cat_filter)
return True
except (mysql.connector.Error, Exception) as e:
print(str(e))
return False
# index/home page
@app.route('/')
def index():
articles = []
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `post_id`, `post_title`, `post_content`, `user_email`, `post_date` FROM `articles` ORDER BY `post_id` DESC"
cur = db_conn.cursor()
cur.execute(sql_query)
if cur:
articles = cur.fetchall()
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
return render_template("index.html", articles=articles)
# static pages
@app.route('/about')
def about():
return render_template("about.html")
# static page + form
@app.route('/contact', methods=['GET', 'POST'])
def contact():
return render_template('contact.html')
# account settings
@app.route('/setting/', methods=['GET', 'POST'])
@app.route('/setting/profile', methods=['GET', 'POST'])
def user_profile():
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
try:
email = session.get('email')
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `user_first_name`, `user_last_name`, `user_bio`, `user_email` FROM `users` WHERE `user_email`=%s"
cur = db_conn.cursor()
cur.execute(sql_query, (email,))
if not cur:
return redirect(url_for('logout'))
user_data = cur.fetchone()
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('logout'))
if not user_data:
return redirect(url_for('login'))
return render_template('user_profile.html', user_data=user_data)
# update firstname
@app.route('/setting/firstname/<string:email>', methods=['GET', 'POST'])
def update_first_name(email=''):
if update_user_profile(email, 'user_first_name', 'update_first_name', 'update_first_name_btn'):
return redirect(url_for('user_profile'))
else:
return redirect(url_for('logout'))
# update lastname
@app.route('/setting/lastname/<string:email>', methods=['GET', 'POST'])
def update_last_name(email=''):
if update_user_profile(email, 'user_last_name', 'update_last_name', 'update_last_name_btn'):
return redirect(url_for('user_profile'))
else:
return redirect(url_for('logout'))
# update bio
@app.route('/setting/bio/<string:email>', methods=['GET', 'POST'])
def update_bio(email=''):
if update_user_profile(email, 'user_bio', 'update_bio', 'update_bio_btn'):
return redirect(url_for('user_profile'))
else:
return redirect(url_for('logout'))
@app.route('/setting/email/<string:email>', methods=['GET', 'POST'])
def email_token(email=''):
# return render_template('email_token_field.html')
return redirect(url_for('index'))
@app.route('/setting/password/<string:email>', methods=['GET', 'POST'])
def password_token(email=''):
# return render_template('password_token_field.html')
return redirect(url_for('index'))
@app.route('/setting/forget_password', methods=['GET', 'POST'])
def forget_password():
# return render_template('forget_password.html')
return redirect(url_for('index'))
@app.route('/setting/reset_password/<string:email>', methods=['GET', 'POST'])
def reset_password(email=''):
# return render_template('reset_password.html')
return redirect(url_for('index'))
# add a comment
@app.route('/comment/write/<string:article_id>', methods=['GET', 'POST'])
def write_comment(article_id):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
message = ''
cat_filter = "danger"
if request.method != 'POST':
message = 'At least of something to say ...'
flash(message, cat_filter)
return redirect(url_for('read_article', article_id=article_id))
comment_text = request.form.get('comment_box')
add_comment_btn = request.form.get('add_comment_btn')
if not comment_text or not add_comment_btn:
message = 'Comment is empty'
flash(message, cat_filter)
return redirect(url_for('read_article', article_id=article_id))
try:
user_email = session.get('email')
comment_date = Gen().get_current_date_time()
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "INSERT INTO `comments`(`post_id`, `comment_text`, `comment_date`, `user_email`) VALUES(%s, %s, %s, %s)"
values = (article_id, comment_text, comment_date, user_email)
cur = db_conn.cursor()
cur.execute(sql_query, values)
if cur:
message = "Comment added successfully"
cat_filter = 'success'
db_conn.commit()
else:
message = "could not add comment"
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('read_article', article_id=article_id))
flash(message, cat_filter)
return redirect(url_for('read_article', article_id=article_id))
# delete comment
@app.route('/comment/delete/<string:comment_id>', methods=['GET', 'POST'])
def delete_comment(comment_id):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
try:
user_email = session.get('email')
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `user_email`, `post_id` FROM `comments` WHERE `comment_id`=%s"
select_cur = db_conn.cursor()
select_cur.execute(sql_query, (comment_id,))
if not select_cur:
return redirect(url_for('articles'))
result = select_cur.fetchone()
author_email = result[0]
post_id = result[1]
if user_email == author_email:
sql_query = "DELETE FROM `comments` WHERE `comment_id`=%s"
delete_cur = db_conn.cursor()
delete_cur.execute(sql_query, (comment_id,))
if delete_cur:
message = "comment deleted successfully"
cat_filter = "success"
db_conn.commit()
else:
message = "could not delete comment"
cat_filter = "danger"
db_conn.close()
flash(message, cat_filter)
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('read_article', article_id=post_id))
# read comment
def read_comment(comment_id):
"""
return a tuple of the post_id, comment_text and user_email
"""
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `comment_id`, `post_id`, `comment_text`, `user_email` FROM `comments` WHERE `comment_id`=%s"
cur = db_conn.cursor()
cur.execute(sql_query, (comment_id, ))
if not cur:
return False
result = cur.fetchone()
db_conn.close()
return result
except (mysql.connector.Error, Exception) as e:
print(str(e))
return False
# update a comment
@app.route('/comment/update/<string:comment_id>', methods=['GET', 'POST'])
def update_comment(comment_id):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
user_email = session.get('email')
data = read_comment(comment_id)
if not data:
return redirect(url_for('all_articles'))
result_comment_id = data[0]
result_post_id = data[1]
result_comment_text = data[2]
result_email = data[3]
cat_filter = 'danger'
message = ''
if str(result_comment_id) != comment_id:
return redirect(url_for('all_articles'))
if result_email != user_email:
message = "edit an article you wrote"
return redirect(url_for('all_articles'))
if request.method != 'POST':
message = 'Request method is not POST'
flash(message, cat_filter)
comment = [comment_id, result_comment_text]
return render_template('update_comment.html', comment=comment)
comment_text = request.form.get('update_comment_content')
update_comment_submit_btn = request.form.get(
'update_comment_submit_button')
if not comment_text or not update_comment_submit_btn:
message = 'Select a comment you wrote'
return redirect(url_for('read_article', article_id=result_post_id))
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "UPDATE `comments` SET `comment_text`=%s WHERE `comment_id`=%s AND `user_email`=%s"
values = (comment_text, comment_id, user_email)
cur = db_conn.cursor()
cur.execute(sql_query, values)
if not cur:
message = "could not update the comment"
else:
message = "comment updated successfully"
cat_filter = "success"
db_conn.commit()
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
flash(message, cat_filter)
return redirect(url_for('read_article', article_id=result_post_id))
# signup, login, Logout and delete account
@app.route('/account/signup/', methods=['GET', 'POST'])
@app.route('/account/signup/<string:email>', methods=['GET', 'POST'])
def signup(email=''):
if 'token' in session or 'email' in session:
return redirect(url_for('logout'))
message = ''
cat_filter = "danger"
if not request.form:
message = "There is no request form"
else:
if request.method != 'POST':
message = 'Request method is not POST'
else:
first_name = request.form.get('sign_up_first_name')
last_name = request.form.get('sign_up_last_name')
bio = request.form.get('sign_up_user_bio')
email = request.form.get('sign_up_email')
sign_up_password = request.form.get('sign_up_password')
confirm_password = request.form.get('sign_up_confirm_password')
register_button = request.form.get('register_button')
if (
not first_name or not last_name or not email or not sign_up_password or
not confirm_password or not bio or not register_button):
message = 'There are empty fields, please fill them out'
else:
if Val().is_email_valid(email):
if not Val().is_valid_password(sign_up_password) or not Val().is_valid_password(confirm_password):
message = "Invalid password format"
else:
if not Val().validate_size(sign_up_password, DataSizeRange(6, 20)):
message = "Check password length, 6 - 20"
else:
hashed_passwd = Gen().get_bcrypt_hashed_passwd(sign_up_password)
registered_date = Gen().get_current_date_time()
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT user_email FROM `users` WHERE `user_email`=%s"
select_cur = db_conn.cursor()
result = select_cur.execute(sql_query, (email, ))
if result:
return redirect(url_for('login', email=email))
sql_query = "INSERT INTO `users`(`user_first_name`, `user_last_name`, `user_email`, `user_password`, `user_bio`, `user_register_date`) VALUES(%s, %s, %s, %s, %s, %s)"
values = (first_name, last_name, email,
hashed_passwd, bio, registered_date)
insert_cur = db_conn.cursor()
try:
result = insert_cur.execute(sql_query, values)
# create a session
session["token"] = Gen().generate_token()
session["email"] = email
print(session)
# success
message = "signup successful"
cat_filter = "success"
# stamp the database - comNoblessemit the changes and close connection
db_conn.commit()
db_conn.close()
# redirect to index page
return redirect(url_for('index'))
except (mysql.connector.Error, Exception) as e:
print(str(e))
message = "signup unsuccessful"
cat_filter = "danger"
return render_template('signup.html', email=email)
else:
message = "Invalide email format"
flash(message, cat_filter)
return render_template('signup.html', email=email)
@app.route('/account/login/', methods=['GET', 'POST'])
@app.route('/account/login/<string:email>', methods=['GET', 'POST'])
def login(email=''):
message = ''
cat_filter = 'danger'
if 'token' in session or 'email' in session:
return redirect(url_for('logout'))
if request.method != 'POST':
message = 'Enter the needed credentials'
flash(message, cat_filter)
return render_template('login.html', email=email)
email = request.form.get('login_email')
password = request.form.get('login_password')
login_button = request.form.get('login_button')
if not email or not password or not login_button:
message = 'Email and Password are required for a login, please...'
flash(message, cat_filter)
return render_template('login.html', email=email)
if (
not Val().is_email_valid(email) or
not Val().validate_size(password, DataSizeRange(6, 20)) or
not Val().is_valid_password(password)):
message = "Invalide credentials, check and enter again"
flash(message, cat_filter)
return render_template('login.html', email=email)
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `user_password` FROM `users` WHERE `user_email`=%s"
cur = db_conn.cursor()
cur.execute(sql_query, (email,))
if not cur:
return redirect(url_for('signup', email=email))
result = cur.fetchone()
hashed_password = result[0]
if not Val().is_valid_hash(password, hashed_password):
message = "Invalid credentials, please check and enter again ..."
flash(message, cat_filter)
return render_template('login.html', email=email)
except (mysql.connector.Error, Exception) as e:
print(str(e))
message = "There was an error when loging in, please try again in a few seconds later..."
flash(message, cat_filter)
return render_template('login.html', email=email)
session["token"] = Gen().generate_token()
session["email"] = email
# success
message = "login successfull"
cat_filter = "success"
return redirect(url_for('index'))
@app.route("/account/logout")
def logout():
if 'token' in session:
flash("Logout successfull", "success")
session.clear()
return redirect(url_for("login"))
@app.route('/account/delete/<string:email>', methods=['GET', 'POST'])
def delete_user(email=''):
return redirect(url_for('user_profile'))
# articles
@app.route('/article/') # /article/unknow_id - return articles
@app.route('/articles')
def all_articles():
articles = []
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `post_id`, `post_title`, `user_email`, `post_date` FROM `articles` ORDER BY `post_id` DESC"
cur = db_conn.cursor()
cur.execute(sql_query)
if cur:
articles = cur.fetchall()
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
return render_template('all_articles.html', articles=articles)
@app.route('/article/read/<string:article_id>')
def read_article(article_id):
article = []
comments = []
try:
post_id = article_id
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT * FROM `articles` WHERE `post_id`=%s"
articles_cur = db_conn.cursor()
articles_cur.execute(sql_query, (post_id, ))
if not articles_cur:
return redirect(url_for('all_articles'))
article = articles_cur.fetchone()
print('0000000 article')
print(article)
# select comments
sql_query = "SELECT * FROM `comments` WHERE `post_id`=%s ORDER BY `comment_date` DESC"
comments_cur = db_conn.cursor()
comments_cur.execute(sql_query, (post_id, ))
if comments_cur:
comments = comments_cur.fetchall()
print('0000000 comments')
print(comments)
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print('error', str(e))
return render_template("article.html", article=article, comments=comments)
@app.route("/article/delete/<string:article_id>")
def delete_article(article_id):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `user_email` FROM `articles` WHERE `post_id`=%s"
select_article_cur = db_conn.cursor()
select_article_cur.execute(sql_query, (article_id,))
if select_article_cur:
author_email = select_article_cur.fetchone()[0]
user_email = session.get('email')
if author_email == user_email:
sql_query = "DELETE FROM `articles` WHERE `post_id`=%s AND `user_email`=%s"
values = (article_id, user_email)
delete_articles_cur = db_conn.cursor()
delete_articles_cur.execute(sql_query, values)
if delete_articles_cur:
sql_query = "DELETE FROM `comments` WHERE `post_id`=%s"
delete_comments_cur = db_conn.cursor()
delete_comments_cur.execute(sql_query, (article_id,))
flash("Article deleted successfully", 'success')
else:
flash("Article deletion unsuccessful", 'danger')
db_conn.commit()
db_conn.close()
return redirect(url_for('all_articles'))
db_conn.close()
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('read_article', article_id=article_id))
@app.route('/article/update/<string:article_id>', methods=['GET', 'POST'])
def update_article(article_id):
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
if request.method != 'POST':
try:
post_id = article_id
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
sql_query = "SELECT `post_title`, `post_content` FROM `articles` WHERE `post_id`=%s"
select_cur = db_conn.cursor()
select_cur.execute(sql_query, (post_id, ))
if not select_cur:
return redirect(url_for('all_articles'))
article = select_cur.fetchone()
db_conn.close()
return render_template('update_article.html', article=article)
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('all_articles'))
else:
try:
post_id = article_id
post_content = request.form.get('update_post_content')
post_title = request.form.get('update_post_title')
post_btn = request.form.get('update_post_submit_button')
if not post_content or not post_title or not post_btn:
return render_template('update_article.html', article=article)
sql_query = "UPDATE `articles` SET `post_content`=%s, `post_title`=%s WHERE `post_id`=%s"
values = (post_content, post_title, post_id)
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
update_cur = db_conn.cursor()
update_cur.execute(sql_query, values)
if update_cur:
db_conn.commit()
db_conn.close()
flash("article updated successfully", 'success')
return redirect(url_for('read_article', article_id=post_id))
db_conn.close()
flash('update unsuccessful', 'danger')
return render_template('update_article.html', article=article)
except (mysql.connector.Error, Exception) as e:
print(str(e))
return redirect(url_for('all_articles'))
@app.route('/article/write', methods=['GET', 'POST'])
def write_article():
if not 'token' in session or not 'email' in session:
return redirect(url_for('logout'))
message = ''
cat_filter = "danger"
if request.method != 'POST':
return render_template('write_article.html')
post_title = request.form.get('post_title')
post_content = request.form.get('post_content')
post_submit_button = request.form.get('post_submit_button')
if not post_title or not post_content or not post_submit_button:
message = 'There are empty fields, please fill them out'
return render_template(
'write_article.html', post_title=post_title, post_content=post_content)
# need some validation and verification
try:
db_conn = mysql.connector.connect(
host=HOST, user=USERNAME,
password=PASSWORD, database=DATABASE_NAME, buffered=True)
user_email = session.get('email')
post_date = Gen().get_current_date_time()
sql_query = "INSERT INTO `articles`(`post_title`, `post_content`, `user_email`, `post_date`) VALUES(%s, %s, %s, %s)"
values = (post_title, post_content, user_email, post_date)
cur = db_conn.cursor()
cur.execute(sql_query, values)
if not cur:
message = "An error while creating an article, please try again in a few seconds later"
db_conn.close()
flash(message, cat_filter)
return render_template(
'write_article.html', post_title=post_title, post_content=post_content)
db_conn.commit()
db_conn.close()
message = "article added successfully"
cat_filter = "success"
flash(message, cat_filter)
return redirect(url_for('index'))
except (mysql.connector.Error, Exception) as e:
print(str(e))
return render_template(
'write_article.html', post_title=post_title, post_content=post_content)
# test request
with app.test_request_context():
# index
print(url_for('index'))
# about
print(url_for('about'))
# contact
print(url_for('contact'))
# email token
print(url_for('email_token', email='hello@hmail.com'))
# password token
print(url_for('password_token', email='hello@hmail.com'))
# user profile
print(url_for('user_profile'))
# forget password
print(url_for('forget_password'))
# delete user
print(url_for('delete_user', email='hello@hmail.com'))
# write comment
print(url_for('write_comment', article_id='1'))
# delete comment
print(url_for('delete_comment', comment_id='1'))
# update comment
print(url_for('update_comment', comment_id='1'))
# signup
print(url_for('signup'))
print(url_for('signup', email="A@amail.com"))
# login
print(url_for('login'))
print(url_for('login', email='A@amail.com'))
# logout
print(url_for('logout'))
# article
print(url_for('all_articles'))
# read article
print(url_for('read_article', article_id='1'))
# update article
print(url_for('update_article', article_id='1'))
# write article
print(url_for('write_article'))
# delete article
print(url_for('delete_article', article_id='1'))
# print(url_for())
# print(url_for())
if __name__ == "__main__":
app.run()