Created un/follow buttons

This commit is contained in:
SecretlyTaco 2018-02-18 15:15:25 +00:00
parent a07df6d023
commit 22c3193f12
3 changed files with 33 additions and 8 deletions

View file

@ -66,7 +66,7 @@ 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))
return render_template('profile.html', user=accounts.account_details(name), logged_in=logged_in, posts=posts)
return render_template('profile.html', user=accounts.account_details(name), logged_in=logged_in, following=accounts.is_following(logged_in, name), posts=posts)
@app.route('/logout')
@ -140,6 +140,26 @@ def reply_to_post(post_id):
timeline.post_status(logged_in, request.form['status'], replyTo=post_id)
return redirect(url_for('profile'))
@app.route("/follow/<user>", methods=["GET", "POST"])
def follow(user):
if 'username' not in session.keys(): return redirect(url_for('login'))
logged_in = session['username']
if request.method == "POST":
accounts.follow(logged_in, user)
return redirect(str("/profile/" + user))
else:
pass
@app.route("/unfollow/<user>", methods=["GET", "POST"])
def unfollow(user):
if 'username' not in session.keys(): return redirect(url_for('login'))
logged_in = session['username']
if request.method == "POST":
accounts.unfollow(logged_in, user)
return redirect(str("/profile/" + user))
else:
pass
if __name__ == '__main__':
app.run(debug=True)

View file

@ -12,6 +12,8 @@ def get_display_name(username):
def is_verified(username):
return accounts_db.find_one({'username': username})['verified']
def is_following(follower, following):
return bool( accounts_db.find_one({'username': following.lower()})['_id'] in accounts_db.find_one({'username': follower.lower()})['following'] )
def account_exists(username):
return bool(accounts_db.find_one({'username': username}))

View file

@ -15,13 +15,16 @@
<div class="row">
<div class="col-xs-6 col-md-4">
<div class="card">
<h4 class="card-header">{{ user.displayname }}'s profile</h4>
<div class="card-body">
{% if user.profile.location %} <i class="far fa-fw fa-map-marker"></i> {{ user.profile.location }} <br>{% endif %}
{% if user.profile.gender %} <i class="far fa-fw fa-transgender-alt"></i> {{ user.profile.gender }} {% endif %}
</div>
<div class="card">
<div class="card-header">
<h4 style="display: inline">{{ user.displayname }}' Profile</h4>
{% if following %}<form action="/unfollow/{{ user.displayname }}" method="POST" style="display: inline; float: right;"><input type="submit" value="Unfollow" class="btn btn-outline-danger btn-sm"/></form> {% else %}<form action="/follow/{{ user.displayname }}" method="POST" style="display:inline; float: right;"><input type="submit" value="Follow" class="btn btn-outline-primary btn-sm"/></form>{% endif %}
</div>
<div class="card-body">
{% if user.profile.location %} <i class="far fa-fw fa-map-marker"></i> {{ user.profile.location }} <br>{% endif %}
{% if user.profile.gender %} <i class="far fa-fw fa-transgender-alt"></i> {{ user.profile.gender }} {% endif %}
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-8" style="float: right;">
{% if posts %}
@ -39,4 +42,4 @@
</div>
</div>
</div>
{% endblock %}
{% endblock %}