Now with the ability to pin posts to profile

This commit is contained in:
Socks 2018-04-05 01:06:11 +01:00
parent 7ac5463f64
commit 1346dcfa45
4 changed files with 78 additions and 2 deletions

View file

@ -70,6 +70,10 @@ def profile(name=None):
name = name.lower()
logged_in = accounts.get_display_name(session['username']) if 'username' in session.keys() else False
posts = list(timeline.user_posts_by_username(name))
if 'pinned' in accounts.account_details(name).keys():
pinned = timeline.post_details(accounts.account_details(name)['pinned'])
else:
pinned=False
return render_template('profile.html',
title=accounts.get_display_name(name)+"'s profile",
user=accounts.account_details(name),
@ -77,7 +81,8 @@ def profile(name=None):
theme=accounts.get_theme(logged_in),
following=accounts.is_following(logged_in, name),
followers=accounts.get_followers(name),
posts=posts)
posts=posts,
pinned=pinned)
@app.route('/logout')
@ -318,5 +323,19 @@ def messaging(user):
return redirect(request.referrer)
@app.route('/pin/<post_id>', methods=['GET'])
def pin(post_id):
if timeline.get_poster(post_id).lower() == session['username'].lower():
accounts.set_pinned(session['username'], post_id)
return redirect(request.referrer)
@app.route('/unpin/<post_id>', methods=['GET'])
def unpin(post_id):
if timeline.get_poster(post_id).lower() == session['username'].lower():
accounts.unset_pinned(session['username'], post_id)
return redirect(request.referrer)
if __name__ == '__main__':
app.run(host="127.0.0.1", debug=True)

View file

@ -1,5 +1,6 @@
from pymongo import MongoClient
import bcrypt
from bson.objectid import ObjectId
client = MongoClient()
accounts_db = client.tweeder.accounts
@ -52,6 +53,20 @@ def update_profile(username, details):
{'$set': {'profile': details}}, upsert=True)
def set_pinned(username, post_id):
post_id = ObjectId(post_id)
accounts_db.update_one({'username': username},
{'$set': {'pinned': post_id}}, upsert=True)
def unset_pinned(username, post_id):
username = username.lower()
post_id = ObjectId(post_id)
if accounts_db.find_one({'username': username})['pinned'] == post_id:
accounts_db.update_one({'username': username},
{'$set': {'pinned': None}}, upsert=True)
def get_followers(username):
if not account_exists(username):
return False

View file

@ -112,3 +112,7 @@ def unlike_post(post_id, user):
def get_mentions(username):
return timeline_db.find({"mentions": username.lower()}).sort('timePosted', pymongo.DESCENDING)
def post_details(post_id):
return timeline_db.find_one({'_id': ObjectId(post_id)})

View file

@ -38,12 +38,50 @@
</div>
</div>
<div class="col-xs-12 col-md-12 col-lg-8" style="float: right;">
{% if pinned %}
<div class="card" style="word-wrap: break-word; margin-bottom: 16px; width: 100%;">
<div class="card-header">
<b><i class="fas fa-thumbtack"></i> <a href="/profile/{{ pinned.poster }}">{{ pinned.poster }}</a></b> at {{ pinned.timePosted.strftime('%Y-%m-%d %-H:%M') }} {% if pinned.edited %}<i>(Edited)</i>{% endif %}
<span style="float: right">
{% if logged_in|lower == pinned.poster|lower %}
{% if user.pinned == pinned._id %}
<a href="/unpin/{{ pinned._id }}" class="btn btn-primary btn-sm"><i class="far fa-thumbtack" data-fa-transform="rotate-45"></i></a>
{% else %}
<a href="/pin/{{ pinned._id }}" class="btn btn-outline-primary btn-sm"><i class="far fa-thumbtack" data-fa-transform="rotate-45"></i></a>
{% endif %}
{% endif %}
{% if logged_in|lower in pinned.likes %}
<form style="display: inline;" method="POST" action="/unlike/{{ pinned._id }}"><button type="submit" class="btn btn-danger btn-sm"><i class="fas fa-heart"></i> {{ pinned.likes|count }}</button></form>
{% else %}
<form style="display: inline;" method="POST" action="/like/{{ pinned._id }}"><button type="submit" class="btn btn-outline-danger btn-sm"><i class="fas fa-heart"></i> {{ pinned.likes|count }}</button></form>
{% endif %}
<a href="/reply/{{ pinned._id }}" class="btn btn-primary btn-sm"><i class="far fa-reply"></i></a>
{% if logged_in|lower == pinned.poster|lower %}
<a href="/editpost/{{ pinned._id }}" class="btn btn-primary btn-sm"><i class="far fa-edit"></i></a>
{% endif %}
{% if logged_in|lower == pinned.poster|lower %}
<a href="/delete/{{ pinned._id }}" class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a>
{% endif %}
</span>
</div>
<div class="card-body">
{{ pinned.content }}
</div>
</div>
{% endif %}
{% if posts %}
{% for post in posts if not post.hidden %}
<div class="card" style="word-wrap: break-word; margin-bottom: 16px; width: 100%;">
<div class="card-header">
<b><a href="/profile/{{ post.poster }}">{{ post.poster }}</a></b> at {{ post.timePosted.strftime('%Y-%m-%d %-H:%M') }} {% if post.edited %}<i>(Edited)</i>{% endif %}
<span style="float: right">
{% if logged_in|lower == post.poster|lower %}
{% if user.pinned == post._id %}
<a href="/unpin/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-thumbtack" data-fa-transform="rotate-45"></i></a>
{% else %}
<a href="/pin/{{ post._id }}" class="btn btn-outline-primary btn-sm"><i class="far fa-thumbtack" data-fa-transform="rotate-45"></i></a>
{% endif %}
{% endif %}
{% 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 %}