Added like/unlike buttons to posts on profiles

This commit is contained in:
SecretlyTaco 2018-02-23 22:14:17 +00:00
parent 5cfc400a8b
commit d674bc7acf
3 changed files with 49 additions and 1 deletions

View file

@ -190,5 +190,25 @@ def view_thread(post_id):
posts=posts) posts=posts)
@app.route("/like/<post_id>", methods=["GET", "POST"])
def like_post(post_id):
if 'username' not in session.keys(): return redirect(url_for('login'))
logged_in = session['username']
if request.method == "POST":
timeline.like_post(post_id, logged_in)
return redirect(str('/view/' + post_id))
elif request.method == "GET":
pass
@app.route("/unlike/<post_id>", methods=["GET", "POST"])
def unlike_post(post_id):
if 'username' not in session.keys(): return redirect(url_for('login'))
logged_in = session['username']
if request.method == "POST":
timeline.unlike_post(post_id, logged_in)
return redirect(str('/view/' + post_id))
elif request.method == "GET":
pass
if __name__ == '__main__': if __name__ == '__main__':
app.run(host="127.0.0.1", debug=True) app.run(host="127.0.0.1", debug=True)

View file

@ -68,6 +68,11 @@ def get_parent(post_id):
else: else:
return False return False
def get_poster(post_id):
return timeline_db.find_one({"_id": ObjectId(post_id)})['poster']
def get_full_replies(post_id): def get_full_replies(post_id):
replies = [] replies = []
replies.append(post_details(post_id)) replies.append(post_details(post_id))
@ -76,3 +81,13 @@ def get_full_replies(post_id):
replies.append(get_parent(post_id)) replies.append(get_parent(post_id))
post_id = get_parent(post_id)['_id'] post_id = get_parent(post_id)['_id']
return replies[::-1] return replies[::-1]
def like_post(post_id, user):
timeline_db.update_one({"_id": ObjectId(post_id)},
{"$push": {"likes": user.lower()}})
def unlike_post(post_id, user):
timeline_db.update_one({"_id": ObjectId(post_id)},
{"$pull": {"likes": user.lower()}})

View file

@ -30,7 +30,20 @@
{% if posts %} {% if posts %}
{% for post in posts if not post.hidden %} {% for post in posts if not post.hidden %}
<div class="card" style="word-wrap: break-word; margin-bottom: 16px; width: 100%;"> <div class="card" style="word-wrap: break-word; margin-bottom: 16px; width: 100%;">
<div class="card-header"><b>{{ post.poster }}</b> at {{ post.timePosted.strftime('%Y-%m-%d %-H:%M') }} <span style="float: right"><a href="/reply/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-reply"></i></a> {% if logged_in == post.poster %} <a href="/delete/{{ post._id }}" class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a>{% endif %}</span></div> <div class="card-header">
<b>{{ post.poster }}</b> at {{ post.timePosted.strftime('%Y-%m-%d %-H:%M') }}
<span style="float: right">
{% if logged_in|lower in post.likes %}
<form style="display: inline;" method="POST" action="/unlike/{{ post._id }}"><button type="submit" class="btn btn-danger btn-sm"><i class="fas fa-heart"></i> {{ post.likes|count }}</button></form>
{% else %}
<form style="display: inline;" method="POST" action="/like/{{ post._id }}"><button type="submit" class="btn btn-outline-danger btn-sm"><i class="fas fa-heart"></i> {{ post.likes|count }}</button></form>
{% endif %}
<a href="/reply/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-reply"></i></a>
{% if logged_in == post.poster %}
<a href="/delete/{{ post._id }}" class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a>
{% endif %}
</span>
</div>
<div class="card-body"> <div class="card-body">
{{ post.content }} {{ post.content }}
</div> </div>