-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathviews.py
More file actions
100 lines (77 loc) · 3.03 KB
/
views.py
File metadata and controls
100 lines (77 loc) · 3.03 KB
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
from flask import Blueprint, render_template, request, flash, redirect, url_for
from flask_login import login_required, current_user
from .models import Post, User, Comment
from . import db
views = Blueprint("views", __name__)
@views.route("/")
@views.route("/home")
@login_required
def home():
posts = Post.query.all()
return render_template("home.html", user=current_user, posts=posts)
@views.route("/create-post", methods=['GET', 'POST'])
@login_required
def create_post():
if request.method == "POST":
text = request.form.get('text')
if not text:
flash('Post cannot be empty', category='error')
else:
post = Post(text=text, author=current_user.id)
db.session.add(post)
db.session.commit()
flash('Post created!', category='success')
return redirect(url_for('views.home'))
return render_template('create_post.html', user=current_user)
@views.route("/delete-post/<id>")
@login_required
def delete_post(id):
post = Post.query.filter_by(id=id).first()
if not post:
flash("Post does not exist.", category='error')
elif current_user.id != post.author:
flash('You do not have permission to delete this post.', category='error')
else:
for comment in post.comments:
db.session.delete(comment)
db.session.delete(post)
db.session.commit()
flash('Post deleted.', category='success')
return redirect(url_for('views.home'))
@views.route("/posts/<username>")
@login_required
def posts(username):
user = User.query.filter_by(username=username).first()
if not user:
flash('No user with that username exists.', category='error')
return redirect(url_for('views.home'))
posts = user.posts
return render_template("posts.html", user=current_user, posts=posts, username=username)
@views.route("/create-comment/<post_id>", methods=['POST'])
@login_required
def create_comment(post_id):
text = request.form.get('text')
if not text:
flash('Comment cannot be empty.', category='error')
else:
post = Post.query.filter_by(id=post_id)
if post:
comment = Comment(
text=text, author=current_user.id, post_id=post_id)
db.session.add(comment)
db.session.commit()
else:
flash('Post does not exist.', category='error')
return redirect(url_for('views.home'))
@views.route("/delete-comment/<comment_id>")
@login_required
def delete_comment(comment_id):
comment = Comment.query.filter_by(id=comment_id).first()
if not comment:
flash('Comment does not exist.', category='error')
elif current_user.id != comment.author and current_user.id != comment.post.author:
flash('You do not have permission to delete this comment.', category='error')
else:
db.session.delete(comment)
db.session.commit()
return redirect(url_for('views.home'))