-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
574 lines (496 loc) · 23.1 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
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
# Import all required models and Frameworks
import json
from functools import wraps
from flask import Flask, render_template, redirect, url_for, flash, abort, request
from flask_bootstrap import Bootstrap
from flask_ckeditor import CKEditor
from datetime import date
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError
from flask_login import login_required, login_user, logout_user, current_user, LoginManager, UserMixin
from forms import *
from flask_gravatar import Gravatar
import os
from datetime import datetime
from tables import create_tables
from authentication import Authentication
# Create flask app
app = Flask(__name__)
# Add the secret key
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', os.urandom(20).hex())
# init CkEditor
ckeditor = CKEditor(app)
# init Bootstrap
Bootstrap(app)
# Create a object from the LoginManager class
login_manager = LoginManager()
login_manager.init_app(app=app)
# Int Gravatar
gravatar = Gravatar(app,
size=100,
rating='g',
default='retro',
force_default=False,
force_lower=False,
use_ssl=False,
base_url=None)
# CONNECT TO DB
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Create tables
User, NotConfirmedAccount, BlogPost, Comment = create_tables(db, UserMixin=UserMixin)
db.create_all()
# Admin user account
db_admin_emails = User.query.filter_by(is_admin=True).all()
admin_emails_len = len(db_admin_emails)
admin_emails = json.loads(os.environ.get('ADMIN_EMAILS'))
admin_names = json.loads(os.environ.get('ADMIN_NAMES'))
admin_passwords = json.loads(os.environ.get('PASSWORDS'))
def add_admin_to_db():
if len(admin_names) > 0:
while True:
admin1 = User()
admin1.id = os.urandom(20).hex()
admin1.name = admin_names[0]
admin1.email = admin_emails[0]
admin1.password = generate_password_hash(admin_passwords[0], salt_length=int(os.environ.get('SALT_LENGTH')))
admin1.is_email_confirmed = True
admin1.is_admin = True
admin1.email_confirmed_time = datetime.now()
admin1.is_account_active = True
# 2
admin2 = User()
admin2.id = os.urandom(20).hex()
admin2.name = admin_names[1]
admin2.email = admin_emails[1]
admin2.password = generate_password_hash(admin_passwords[1], salt_length=int(os.environ.get('SALT_LENGTH')))
admin2.is_email_confirmed = True
admin2.is_admin = True
admin2.email_confirmed_time = datetime.now()
admin2.is_account_active = True
db.session.add(admin1)
db.session.add(admin2)
try:
db.session.commit()
except IntegrityError:
pass
else:
break
if admin_emails_len == 1 or admin_emails_len == 2:
for db_admin_email in db_admin_emails:
if db_admin_email.email not in admin_emails:
db.drop_all()
db.create_all()
add_admin_to_db()
elif admin_emails_len == 0:
add_admin_to_db()
authentication = Authentication()
@login_manager.user_loader
def load_user(user_id):
return User.query.filter_by(id=user_id).first()
def admin_only(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not current_user.is_admin:
return abort(403, "Access denied")
else:
return f(*args, **kwargs)
return decorated_function
@app.context_processor
def inject_now():
return {'now': datetime.now().utcnow()}
@app.route('/')
def get_all_posts():
posts: list = BlogPost.query.all()
# Revers the post list
posts.reverse()
return render_template("index.html", all_posts=posts)
@app.route('/register', methods=['GET', 'POST'])
def register():
global authentication
# Create a object from RegisterForm
register_form = RegisterForm()
if register_form.validate_on_submit():
if User.query.filter_by(email=register_form.email.data).first():
flash("You've already signed up that email, log in instead!", "error")
return redirect(url_for('login'))
else:
not_confirmed_user = NotConfirmedAccount()
while True:
not_confirmed_user.id = os.urandom(20).hex()
not_confirmed_user.name = register_form.name.data
not_confirmed_user.email = register_form.email.data
not_confirmed_user.password = generate_password_hash(
register_form.password.data,
salt_length=int(os.environ.get('SALT_LENGTH')))
db.session.add(not_confirmed_user)
try:
db.session.commit()
except IntegrityError:
pass
else:
break
text_message_body = " this is your account confirmation email, please copy and past in to your web " \
"browser or click on the below link."
# HTML
subheading = "Email confirmation to confirm your email and your account is created yourself in my blog."
description = "please copy and past in to your web browser url bar or click once on the link blow "
authentication.back_to_default(not_confirmed_user)
authentication.email_confirmation(text_message_body, subheading, description, subject='Email confirmation',
end_point='account-confirmation/email')
flash(
"We are send confirmation email to your email address, please follow the instruction to activate your "
"account.",
"Warning")
return redirect(url_for('get_all_posts'))
return render_template("register.html", form=register_form)
@app.route('/account-confirmation/email/<jwt_token>')
def confirm_account_as_email(jwt_token):
if not current_user.is_authenticated:
email = ''
print(authentication)
if authentication is not None:
email = authentication.check_email_confirmation(jwt_token)
if email:
# Find user from Not Authenticated table.
not_confirmed_accounts = NotConfirmedAccount.query.filter_by(email=email).all()
if len(not_confirmed_accounts) == 1:
user = not_confirmed_accounts[0]
else:
user = not_confirmed_accounts[-1]
# Add authenticated user in to User table
confirmed_user = User()
confirmed_user.id = user.id
confirmed_user.name = user.name
confirmed_user.email = user.email
confirmed_user.password = user.password
confirmed_user.account_created_time = user.create_time
confirmed_user.is_email_confirmed = True
confirmed_user.is_account_active = True
confirmed_user.email_confirmed_time = datetime.now()
confirmed_user.email_confirmed_time = datetime.now()
db.session.add(confirmed_user)
db.session.commit()
# Remove authenticated user entry in the Not Authenticated table.
for not_confirmed_account in not_confirmed_accounts:
db.session.delete(not_confirmed_account)
db.session.commit()
login_user(confirmed_user, True)
flash("You are successfully authenticated now. thank you for visiting my blog.", "success")
return redirect(url_for("get_all_posts"))
else:
return redirect(url_for('resend_verification'))
else:
return redirect(url_for('get_all_posts'))
@app.route('/account-confirmation/email/resend/', methods=['GET', 'POST'])
def resend_verification():
global authentication
if not current_user.is_authenticated:
resend_email_form = ResendEmailFrom()
if resend_email_form.validate_on_submit():
user_email_data = resend_email_form.email.data
not_confirmed_accounts = NotConfirmedAccount.query.filter_by(email=user_email_data).all()
not_confirmed_accounts_length = len(not_confirmed_accounts)
not_confirmed_account = None
if not_confirmed_accounts_length == 0:
not_confirmed_account = not_confirmed_accounts[0]
else:
for not_confirmed_account_index in range(0, not_confirmed_accounts_length - 1):
db.session.delete(not_confirmed_accounts[not_confirmed_account_index])
db.session.commit()
not_confirmed_account = not_confirmed_accounts[-1]
if not_confirmed_account.email == user_email_data:
text_message_body = " this is your account confirmation email, please copy and past in to your web " \
"browser or click on the below link."
# HTML
subheading = "Email confirmation to confirm your email and your account is created yourself in my blog."
description = "please copy and past in to your web browser url bar or click once on the link blow "
authentication.back_to_default(not_confirmed_account)
authentication.email_confirmation(text_message_body, subheading, description,
subject='Email confirmation',
end_point='account-confirmation/email')
flash("We are send confirmation email to your email address, please follow the instruction to activate "
"your "
"account.",
"Warning")
return redirect(url_for('get_all_posts'))
elif not (not_confirmed_account.email == user_email_data):
flash("You are not author of this account. you cant access any ones account.")
return redirect(url_for('login'))
else:
flash("You are already confirmed your email address. please log in to your account normally")
return redirect(url_for('login'))
else:
return render_template('email_confirmation.html', form=resend_email_form)
else:
return redirect('/')
@app.route('/login', methods=['GET', 'POST'])
def login():
login_form = LoginForm()
if login_form.validate_on_submit():
email_data = login_form.email.data
user = User.query.filter_by(email=email_data).first()
not_confirmed_account = NotConfirmedAccount.query.filter_by(email=email_data).all()
if user is not None:
if user.is_account_active:
if check_password_hash(user.password, login_form.password.data):
login_user(user, True)
return redirect(url_for('get_all_posts'))
else:
flash("Password incorrect. please try again", "error")
return redirect(url_for('login'))
else:
flash("Your account is not activating. please try again or contact me.", "error")
return redirect(url_for('resend_verification'))
else:
if len(not_confirmed_account) != 0:
flash("Your account is not activating. please try again or contact me.", "error")
return redirect(url_for('resend_verification'))
else:
flash("Email is does not exist, please try again.", "error")
return redirect(url_for("login"))
return render_template("login.html", form=login_form)
@app.route('/change_email/<new_email>')
@login_required
def change_email(new_email):
global authentication
user = User.query.filter_by(id=current_user.id).first()
user.is_changing_email = True
user.is_email_confirmed = False
db.session.commit()
text_message_body = "this is your email confirmation, please copy and past in to your " \
"web browser or click on the " \
"billow link. "
# HTML
subheading = "Email confirmation to change current email address to you provided new email address."
description = "please copy and past in to your web browser url bar or click once on the link blow "
authentication.back_to_default(user)
authentication.email_confirmation(text_message_body, subheading, description, subject='Email confirmation',
end_point='change_email/check', another_data={'new_email': new_email})
flash("A confirmation email now sent to your email. please follow it to change you email.")
return redirect(url_for('user_profile'))
@app.route('/change_email/check/<jwt_token>')
def change_email_now(jwt_token):
global authentication
if authentication is not None:
data = authentication.check_email_confirmation(jwt_token)
if data:
new_email = data['another_data']['new_email']
user = User.query.filter_by(id=current_user.id).first()
if len(User.query.filter_by(email=new_email).all()) == 0:
user.email = new_email
user.is_email_confirmed = True
user.is_changing_email = False
user.last_email_changed_time = datetime.now()
user.is_account_active = True
db.session.commit()
flash("Your email is successfully changed.")
else:
user.is_changing_email = False
user.is_email_confirmed = True
user.is_account_active = True
flash("Your email is changing is unsuccessful.")
return redirect(url_for('user_profile'))
else:
flash("This is not your email")
return redirect('/')
else:
flash("Email Changing is field, because server error.")
return redirect('/')
@app.route('/change_password/<user_id>', methods=['GET', 'POST'])
@login_required
def change_password(user_id):
global authentication
if current_user.id == user_id:
form = ChangePasswordForm()
if form.validate_on_submit():
new_password = form.new_password.data
if not check_password_hash(current_user.password, new_password):
if new_password == form.confirm_new_password.data:
user = User.query.filter_by(id=current_user.id).first()
if user:
requested_ip = request.remote_addr
text_message_body = "Your password change request"
# HTML
subheading = " if you are requested change your password to new password."
description = f"""
<h1Request from</h1>
<p>ip:{requested_ip}</p>
<p>Time: {datetime.now()}</p>
<p>Name: {user.name} </p>
<p>email: {user.email} </p> <br> <h2 style='color: blue;'>If this is only request
you, click billow like to change user password</h2>
"""
authentication.back_to_default(user)
authentication.email_confirmation(text_message_body, subheading, description,
subject='Email confirmation',
end_point='change_password/check',
another_data={'new_password': new_password})
flash("A confirmation email now sent to your email. please follow it to change you email.")
user.is_changing_password = True
db.session.commit()
return redirect(url_for('user_profile', user_id=user_id))
else:
flash("You can't change password because this account is not your own.")
return redirect(url_for('user_profile', user_id=user_id))
else:
flash("New password is not maths to confirm new password")
return redirect(url_for('change_password', user_id=user_id))
else:
flash("Current password is not valid")
return redirect(url_for('change_password', user_id=user_id))
else:
return render_template('change_password.html', form=form)
@app.route('/change_password/check/<token>')
def check_change_password(token):
global authentication
if authentication is not None:
data = authentication.check_email_confirmation(token)
if data:
user = User.query.filter_by(email=data['email']).first()
user.is_changing_password = False
user.password = generate_password_hash(data['another_data']['new_password'], salt_length=10)
user.last_password_changed_time = datetime.now()
db.session.commit()
flash("Password change is successful.")
return redirect(url_for('user_profile'))
else:
flash('Password is change is unsuccessful.')
return redirect(url_for('change_password'))
else:
return redirect('/')
@login_required
@app.route('/profile', methods=['GET', 'POST'])
def user_profile():
user = User.query.filter_by(id=current_user.id).first()
user_profile_form = UserProfileFrom(name=user.name, email=user.email, password=user.password)
if user_profile_form.validate_on_submit():
if user_profile_form.email.data != user.email:
return redirect(url_for('change_email', new_email=user_profile_form.email.data))
elif not check_password_hash(user.password,
user_profile_form.password.data) and user_profile_form.password.data != '':
return redirect(url_for('change_password', user_id=user.id))
elif user_profile_form.name.data != user.name:
user.name = user_profile_form.name.data
db.session.commit()
flash("Your name is successful changed.")
else:
return redirect(url_for('user_profile'))
return redirect(url_for('user_profile'))
return render_template('user_profile.html', form=user_profile_form)
@app.route('/logout')
@login_required
def logout():
global authentication
authentication = Authentication()
logout_user()
return redirect(url_for('get_all_posts'))
@app.route("/post/<post_id>", methods=['GET', 'POST'])
def show_post(post_id):
comment_form = CommentForm()
if comment_form.validate_on_submit():
if current_user.is_authenticated:
parent_post = BlogPost.query.get(post_id)
while True:
comment = Comment(id=os.urandom(20).hex(), comment_author=current_user, parent_post=parent_post,
text=comment_form.text.data)
db.session.add(comment)
try:
db.session.commit()
except IntegrityError:
pass
else:
break
flash("Your Post is created now")
return redirect(url_for('show_post', post_id=post_id))
else:
flash("You need to login or register to comment")
return redirect(url_for('login'))
else:
requested_post = BlogPost.query.get(post_id)
return render_template("post.html", post=requested_post, form=comment_form, gravatar=gravatar)
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/new-post", methods=['GET', 'POST'])
@login_required
@admin_only
def add_new_post():
form = CreatePostForm()
if form.validate_on_submit():
while True:
new_post = BlogPost(
id=os.urandom(20).hex(),
title=form.title.data,
subtitle=form.subtitle.data,
body=form.body.data,
img_url=form.img_url.data,
author=current_user,
date=date.today().strftime("%B %d, %Y")
)
db.session.add(new_post)
try:
db.session.commit()
except IntegrityError:
pass
else:
break
return redirect(url_for("get_all_posts"))
return render_template("make-post.html", form=form)
@app.route("/edit-post/<post_id>", methods=['GET', 'POST'])
@login_required
@admin_only
def edit_post(post_id):
post = BlogPost.query.get(post_id)
edit_form = CreatePostForm(
title=post.title,
subtitle=post.subtitle,
img_url=post.img_url,
body=post.body
)
if edit_form.validate_on_submit():
post.title = edit_form.title.data
post.subtitle = edit_form.subtitle.data
post.img_url = edit_form.img_url.data
post.body = edit_form.body.data
db.session.commit()
return redirect(url_for("show_post", post_id=post.id))
return render_template("make-post.html", form=edit_form)
@app.route("/delete/<post_id>")
@login_required
@admin_only
def delete_post(post_id):
post_to_delete = BlogPost.query.get(post_id)
db.session.delete(post_to_delete)
db.session.commit()
return redirect(url_for('get_all_posts'))
@app.route('/delete/comment/<post_id>/<comment_id>/<user_id>')
@login_required
def delete_comment(post_id, comment_id, user_id):
comments = Comment.query.filter_by(author_id=user_id).all()
for comment in comments:
if comment.post_id == post_id and comment.id == comment_id:
db.session.delete(comment)
db.session.commit()
return redirect(url_for('show_post', post_id=post_id))
flash("Comment deleting is failed. please try again.")
return redirect(url_for('show_post', post_id=post_id))
@app.route('/delete/user/<user_id>', methods=['GET', 'POST'])
@login_required
def delete_user(user_id):
if not current_user.is_admin:
user = User.query.filter_by(id=user_id).first()
db.session.delete(user)
db.session.commit()
flash("Your account is successes delete")
return redirect(url_for('get_all_posts'))
else:
flash("I don't want to delete_my_account because I am the admin of this Website / Blog.")
return redirect(url_for('user_profile'))
if __name__ == "__main__":
app.run()