Skip to content

Commit 25371ed

Browse files
committed
Like Post View
1 parent e2483fb commit 25371ed

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

website/views.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask import Blueprint, render_template, request, flash, redirect, url_for
22
from flask_login import login_required, current_user
3-
from .models import Post, User, Comment
3+
from .models import Post, User, Comment, Like
44
from . import db
55

66
views = Blueprint('views', __name__)
@@ -96,4 +96,24 @@ def delete_comment(comment_id):
9696
db.session.commit()
9797
flash('Comment deleted.', category='success')
9898

99-
return redirect(url_for(dirHome))
99+
return redirect(url_for(dirHome))
100+
101+
102+
@views.route("/like-post/<post_id>", methods=['POST'])
103+
@login_required
104+
def like(post_id):
105+
post = Post.query.filter_by(id=post_id)
106+
like = Like.query.filter_by(author=current_user.id, post_id=post_id).first()
107+
108+
if not post:
109+
flash("Post does not exist.", category='error')
110+
elif like:
111+
db.session.delete(like)
112+
db.session.commit()
113+
else:
114+
like = Like(author=current_user.id, post_id=post_id)
115+
db.session.delete(like)
116+
db.session.commit()
117+
118+
return redirect(url_for(dirHome))
119+

0 commit comments

Comments
 (0)