-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
773 lines (556 loc) · 21.4 KB
/
hello.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
from flask import Flask ,render_template,flash,request,redirect,url_for
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from werkzeug.security import generate_password_hash,check_password_hash
from datetime import date
from flask_login import UserMixin ,login_user ,LoginManager ,login_required,logout_user,current_user
from WebForms import LoginForm,PostForm,UserForm,PasswordForm,NamerForm,SearchForm,Followforms
from sqlalchemy import MetaData
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_ckeditor import CKEditor
from werkzeug.utils import secure_filename
import uuid as uuid
import os
convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
metadata = MetaData(naming_convention=convention)
#create a Flask Instance
app = Flask(__name__)
ckeditor = CKEditor(app)
#Add Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
#secret Key !
app.config['SECRET_KEY'] ="hello"
app.debug = True
UPLOAD_FOLDER = 'static/images'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
#initalise the databse
db = SQLAlchemy(app, metadata=metadata)
migrate = Migrate(app,db,render_as_batch=True)
#Flask_Login Stuff
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return Users.query.get(int(user_id))
#Create Login page
@app.route('/login',methods=['GET','POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = Users.query.filter_by(username=form.username.data).first()
if user:
#check the hash
if check_password_hash(user.password_hash,form.password.data):
login_user(user)
flash('Yay Logged In!')
return redirect(url_for('dashboard'))
else:
flash('Wrong Password - Try Again !')
else:
flash("User Doesnt Exist Try Again")
return render_template('login.html',form=form)
#create Logout Page
@app.route('/logout',methods=['GET','POST'])
def logout():
logout_user()
flash("You have beeen logged Out !")
return redirect(url_for('login'))
#Create Dashboard page
@app.route('/dashboard',methods=['GET','POST'])
@login_required
def dashboard():
form = UserForm()
id= current_user.id
name_to_update = Users.query.get_or_404(id)
if request.method == 'POST':
name_to_update.name = request.form['name']
name_to_update.email = request.form['email']
name_to_update.favorite_color = request.form['favorite_color']
name_to_update.username = request.form['username']
name_to_update.about_author = request.form['about_author']
# check for porfile pic
if request.files['profile_pic']:
name_to_update.profile_pic = request.files['profile_pic']
# grab Image name
pic_filename = secure_filename(name_to_update.profile_pic.filename)
#set uuis
pic_name = str(uuid.uuid1())+"_"+pic_filename
#save image name
saver = request.files['profile_pic']
# change name to string
name_to_update.profile_pic = pic_name
try:
db.session.commit()
saver.save(os.path.join(app.config['UPLOAD_FOLDER'],pic_name))
flash('User Updated Successfully')
return render_template('dashboard.html',form=form,name_to_update=name_to_update)
except:
flash('Try Again')
return render_template('dashboard.html',form=form,name_to_update=name_to_update)
else:
db.session.commit()
flash('User Updated Successfully')
return render_template('dashboard.html',form=form,name_to_update=name_to_update)
else:
return render_template('dashboard.html',form=form,name_to_update=name_to_update,id= id)
@app.route('/posts/<int:id>')
def post(id):
post = Posts.query.get_or_404(id)
our_users = Users.query.order_by(Users.date_added)
return render_template('post.html',post=post,our_users=our_users)
@app.route('/post/edit/<int:id>',methods=['GET','POST'])
@login_required
def edit_post(id):
post = Posts.query.get_or_404(id)
form = PostForm()
if form.validate_on_submit():
post.title = form.title.data
# post.author = form.author.data
post.slug = form.slug.data
post.content = form.content.data
post.img_url = form.img_url.data
#Update Datavbase
db.session.add(post)
db.session.commit()
flash("Post has Been Updated !")
return redirect(url_for('post',id=post.id))
if current_user.id == post.poster_id:
form.title.data = post.title
# form.author.data =post.author
form.slug.data =post.slug
form.content.data =post.content
form.img_url.data = post.img_url
return render_template('edit_post.html',form=form)
else:
flash("You arent authorized to edit this post!")
return render_template('posts.html',posts=posts)
#add Posts Page
@app.route('/posts')
def posts():
#Grab all the posts from the database
posts = Posts.query.order_by(Posts.date_posted.desc())
our_users = Users.query.order_by(Users.date_added.desc())[0:3]
return render_template("posts.html",posts=posts,our_users=our_users)
#delete post
@app.route('/posts/delete/<int:id>')
@login_required
def delete_post(id):
post_to_delete = Posts.query.get_or_404(id)
id = current_user.id
if(id == post_to_delete.poster.id):
try:
db.session.delete(post_to_delete)
db.session.commit()
#Return a message
flash("Blog Post Was Deleted")
posts = Posts.query.order_by(Posts.date_posted)
return render_template("posts.html",posts=posts)
except:
flash("Whoops Try again")
posts = Posts.query.order_by(Posts.date_posted)
return render_template("posts.html",posts=posts)
else:
flash("You are not authorized to delete this post")
posts = Posts.query.order_by(Posts.date_posted)
return render_template("posts.html",posts=posts)
#add Post Page
@app.route('/add/posts',methods=['GET','POST'])
@login_required
def add_post():
form = PostForm()
if form.validate_on_submit():
poster=current_user.id
post = Posts(
title=form.title.data,
content = form.content.data,
poster_id = poster,
img_url = form.img_url.data,
slug = form.slug.data
)
#clear the form
form.title.data =''
form.content.data=''
# form.author.data=''
form.img_url.data=''
form.slug.data=''
#add post data to database
db.session.add(post)
db.session.commit()
flash("Blog Post Submitted Successfully")
return render_template('add_post.html',form=form)
#Json Thing
@app.route('/date')
def get_current_date():
return {"date": date.today()}
@app.route('/delete/<int:id>')
@login_required
def delete(id):
if id == current_user.id:
name = None
form = UserForm()
user_to_delete = Users.query.get_or_404(id)
try:
db.session.delete(user_to_delete)
db.session.commit()
flash("User Deleted Successfully")
our_users = Users.query.order_by(Users.date_added)
return render_template("add_user.html",form = form,name=name,our_users = our_users)
except:
flash("User was not deleted")
return render_template("add_user.html",form = form,name=name,our_users = our_users)
else:
flash("You are not authorised to delete")
return redirect(url_for('dashboard'))
#Update Database Record
@app.route('/update/<int:id>',methods=['GET','POST'])
@login_required
def update(id):
form = UserForm()
name_to_update = Users.query.get_or_404(id)
if request.method == 'POST':
name_to_update.name = request.form['name']
name_to_update.email = request.form['email']
name_to_update.favorite_color = request.form['favorite_color']
name_to_update.username = request.form['username']
try:
db.session.commit()
flash('User Updated Successfully')
return render_template('update.html',form=form,name_to_update=name_to_update)
except:
flash('Try Again')
return render_template('update.html',form=form,name_to_update=name_to_update)
else:
return render_template('update.html',form=form,name_to_update=name_to_update,id= id)
@app.route('/user/add',methods =['GET',"POST"])
def add_user():
name = None
form = UserForm()
if form.validate_on_submit():
user = Users.query.filter_by(email=form.email.data).first()
if user is None:
hashed_pw = generate_password_hash(form.password_hash.data,"sha256")
user = Users(username=form.username.data , name = form.name.data , email = form.email.data,favorite_color=form.favorite_color.data,password_hash = hashed_pw)
db.session.add(user)
db.session.commit()
name = form.name.data
form.name.data=''
form.email.data=''
form.username.data =''
form.favorite_color=''
form.password_hash=''
flash("User Added Successfully")
our_users = Users.query.order_by(Users.date_added)
return render_template("add_user.html",form = form,name=name,our_users = our_users)
#create a route decorator
@app.route('/')
#safe
#capitalize
#lower
#upper
#title
#trim
#striptags
# def index():
# return "<h1>Hello World<h1>"
def index():
favorite_pizza = ['hello' , 'water ' , 'earth',41]
return render_template('index.html',favorite_pizza = favorite_pizza)
# Create Admin Page
@app.route('/admin',methods=["GET","POSTS"])
@login_required
def admin():
id = current_user.id
if id == 1:
return render_template("admin.html")
else:
flash("sorry you must be the admin to access this page")
return redirect(url_for('dashboard'))
# pass stuff to navbaar
@app.context_processor
def base():
form = SearchForm()
return dict(form=form)
# #create a search function in posts
# @app.route('/search' , methods=["POST"])
# def search():
# form = SearchForm()
# posts = Posts.query
# if form.validate_on_submit():
# post.searched = form.searched.data
# #QUERY THE Database
# posts = posts.filter(Posts.content.like('%'+post.searched+'%'))
# posts = posts.order_by(Posts.title).all()
# return render_template("search.html",form = form, searched = post.searched,posts=posts)
#create a search function in users
@app.route('/search' , methods=["POST"])
def search():
form = SearchForm()
users = Users.query
posts = Posts.query
if form.validate_on_submit():
user.searched = form.searched.data
post.searched = form.searched.data
#QUERY THE Database
users = users.filter(Users.username.like('%'+user.searched+'%'))
users = users.order_by(Users.username).all()
#QUERY THE Database
posts = posts.filter(Posts.content.like('%'+post.searched+'%'))
posts = posts.order_by(Posts.title).all()
# return render_template("search.html",form = form, searched = post.searched,posts=posts)
return render_template("search.html" , form = form , searched = user.searched , users = users,posts=posts)
else:
flash('Try Again')
return render_template('search.html')
#Custom Error Pages
#Invalid URL
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'),404
#Internal Server URL
@app.errorhandler(500)
def page_not_found(e):
return render_template('500.html'),500
#Create Name Page
@app.route('/name',methods=['GET','POST'])
def name():
name = None
form = NamerForm()
#validation
if form.validate_on_submit():
name = form.name.data
form.name.data =''
flash("Form Submitted Successfully")
return render_template('name.html',name = name , form = form)
#Create test_pw
@app.route('/test_pw',methods=['GET','POST'])
def test_pw():
email = None
password=None
pw_to_check=None
passed = None
form = PasswordForm()
#validation
if form.validate_on_submit():
email = form.email.data
password = form.password_hash.data
form.email.data =''
form.password_hash.data =''
pw_to_check = Users.query.filter_by(email=email).first()
#check hashed Password
passed = check_password_hash(pw_to_check.password_hash , password)
flash("Form Submitted Successfully")
return render_template('test_pw.html',email= email,password=password ,pw_to_check = pw_to_check, form = form,passed = passed)
# UserProfile
@app.route('/user/<username>')
def user(username):
user = Users.query.filter_by(username=username).first()
posts = Posts.query.filter_by(poster_id = user.id)
posts= posts.order_by(Posts.date_posted.desc())
return render_template('user.html',posts=posts ,user=user, username = username)
# Followers implementation
@app.route('/follow/<username>', methods=['POST'])
@login_required
def follow(username):
form = Followforms()
if form.validate_on_submit():
user = Users.query.filter_by(username=username).first()
if user is None:
flash('User {} not found.'.format(username))
return redirect(url_for('index'))
if user == current_user:
flash('You cannot follow yourself!')
return redirect(url_for('user', username=username))
current_user.follow(user)
db.session.commit()
flash('You are following {}!'.format(username))
return redirect(url_for('user', username=username))
else:
return redirect(url_for('index'))
@app.route('/unfollow/<username>', methods=['POST'])
@login_required
def unfollow(username):
form = Followforms()
if form.validate_on_submit():
user = Users.query.filter_by(username=username).first()
if user is None:
flash('User {} not found.'.format(username))
return redirect(url_for('index'))
if user == current_user:
flash('You cannot unfollow yourself!')
return redirect(url_for('user', username=username))
current_user.unfollow(user)
db.session.commit()
flash('You are not following {}.'.format(username))
return redirect(url_for('user', username=username))
else:
return redirect(url_for('index'))
@app.route('/feed/<username>', methods=["GET"])
@login_required
def feed(username):
user = Users.query.filter_by(username=username).first()
# we have grabed user
posts = user.followed_posts().filter()
our_users = user.followed.filter()[0:3]
users = Users.query.filter()[0:5]
# posts = Posts.query.order_by(Posts.date_posted.desc())
return render_template("posts.html",posts=posts,our_users=our_users,users=users)
@app.route('/all_users',methods=["GET"])
def all_users():
users = Users.query.filter()
return render_template("all_users.html",users=users)
@app.route('/following/<username>',methods=["GET"])
def following(username):
user = Users.query.filter_by(username=username).first()
users = user.followed
return render_template("all_users.html",users=users)
@app.route('/followers/<username>',methods=["GET"])
def followers(username):
user = Users.query.filter_by(username=username).first()
users = user.followers
return render_template("all_users.html",users=users)
# @app.route('/add/comment',methods=["POST"])
# def add_comment(id):
# comment = Comment(text="Hello world" , author = current_user.username)
# try:
# db.session.add(comment)
# db.session.commit()
# flash("Comment posted")
# except:
# flash("Comment not posted")
#Classes
#Create a Blog Post Model
class Posts(db.Model):
id = db.Column(db.Integer,primary_key=True)
title = db.Column(db.String(255))
content = db.Column(db.Text)
#author=db.Column(db.String(255))
date_posted=db.Column(db.DateTime() ,default =datetime.utcnow)
img_url = db.Column(db.String(2083))
slug= db.Column(db.String(255))
#Foreign Key to Link Users (refer to primary)
poster_id = db.Column(db.Integer,db.ForeignKey("users.id"))
archived_int = db.Column(db.Integer , default=0 )
likes = db.relationship('LikePost', backref='post', lazy='dynamic')
def archive_post(self, post):
if not self.has_archived_post(post):
post.archived_int= 1
def unarchive_post(self, post):
if self.has_archived_post(post):
post.archived_int = 0
def has_archived_post(self, post):
return post.archived_int
# Create followers
followers = db.Table('followers',
db.Column('follower_id', db.Integer, db.ForeignKey('users.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('users.id'))
)
#Create Comment
class Comment(db.Model):
id = db.Column(db.Model);
text = db.Column(db.String(140))
author = db.Column(db.String(32))
date_posted=db.Column(db.DateTime() ,default =datetime.utcnow,index=True)
parent_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
replies = db.relationship(
'Comment', backref=db.backref('parent', remote_side=[id]),
lazy='dynamic')
class LikePost(db.Model):
__tablename__ = 'post_like'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
#create Model
class Users(db.Model,UserMixin):
id = db.Column(db.Integer,primary_key=True)
username = db.Column(db.String(120),nullable=False)
name = db.Column(db.String(120),nullable=False)
email = db.Column(db.String(120),nullable=False,unique=True)
profile_pic = db.Column(db.String(),nullable=True)
favorite_color = db.Column(db.String(120))
about_author = db.Column(db.Text(500),nullable=True)
date_added = db.Column(db.DateTime,default=datetime.utcnow)
#Do password
password_hash = db.Column(db.String(120))
#Users can have many posts
posts= db.relationship('Posts',backref='poster')
#followed relationship
followed = db.relationship(
'Users', secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
def follow(self,user):
if not self.is_following(user):
self.followed.append(user)
def unfollow(self,user):
if self.is_following(user):
self.followed.remove(user)
def is_following(self,user):
return self.followed.filter(followers.c.followed_id == user.id).count()>0
def followed_posts(self):
followed= Posts.query.join(
followers,(followers.c.followed_id == Posts.poster_id)).filter(
followers.c.follower_id == self.id
)
own = Posts.query.filter_by(poster_id = self.id)
return followed.union(own).order_by(Posts.date_posted.desc())
#like relationship
liked = db.relationship(
'LikePost',
foreign_keys='LikePost.user_id',
backref='user', lazy='dynamic')
def like_post(self, post):
if not self.has_liked_post(post):
like = LikePost(user_id=self.id, post_id=post.id)
db.session.add(like)
def unlike_post(self, post):
if self.has_liked_post(post):
LikePost.query.filter_by(
user_id=self.id,
post_id=post.id).delete()
def has_liked_post(self, post):
return LikePost.query.filter(
LikePost.user_id == self.id,
LikePost.post_id == post.id).count() > 0
@property
def password(self):
raise AttributeError('password is not readable attribute')
@password.setter
def password(self,password):
self.password_hash = generate_password_hash(password)
def verify_password(self,password):
return check_password_hash(self.password_hash,password)
#create a String
def __repr__(self):
return '<Name %r>' % self.name
@app.route('/like/<int:post_id>/<action>')
@login_required
def like_action(post_id, action):
post = Posts.query.filter_by(id=post_id).first_or_404()
if action == 'like':
current_user.like_post(post)
db.session.commit()
if action == 'unlike':
current_user.unlike_post(post)
db.session.commit()
return redirect(request.referrer)
@app.route('/archived/<int:post_id>/<action>')
@login_required
def archive_action(post_id, action):
post = Posts.query.filter_by(id=post_id).first_or_404()
if action == 'archive':
post.archive_post(post)
db.session.commit()
if action == 'unarchive':
post.unarchive_post(post)
db.session.commit()
return redirect(request.referrer)