Added the ability to edit posts, with edited marker on profiles

This commit is contained in:
Socks 2018-03-22 22:27:03 +00:00
parent de15a84c54
commit ba9367b35d
4 changed files with 47 additions and 3 deletions

View file

@ -215,6 +215,7 @@ def like_post(post_id):
elif request.method == "GET": elif request.method == "GET":
pass pass
@app.route("/unlike/<post_id>", methods=["GET", "POST"]) @app.route("/unlike/<post_id>", methods=["GET", "POST"])
def unlike_post(post_id): def unlike_post(post_id):
if 'username' not in session.keys(): return redirect(url_for('login')) if 'username' not in session.keys(): return redirect(url_for('login'))
@ -245,5 +246,21 @@ def mentions():
posts=timeline.get_mentions(logged_in)) posts=timeline.get_mentions(logged_in))
@app.route("/editpost/<post_id>", methods=["GET", "POST"])
def editpost(post_id):
if 'username' not in session.keys(): return redirect(url_for('login'))
logged_in = session['username']
post_obj = timeline.post_details(post_id)
print(post_obj)
if request.method == "GET":
if post_obj['poster'].lower() != logged_in:
return abort(403)
else:
return render_template('editpost.html', post_obj=post_obj)
elif request.method == "POST":
timeline.edit_status(post_obj['_id'], request.form['status'])
return redirect('/view/'+str(post_id))
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

@ -30,12 +30,21 @@ def post_status(username, content, private=False, replyTo=False, location=False)
'mentions': accounts_mentioned, 'mentions': accounts_mentioned,
'location': False or location, 'location': False or location,
'private': False or private, 'private': False or private,
'edited': False
} }
timeline_db.insert_one(status) timeline_db.insert_one(status)
def edit_status(post_id, new_content):
timeline_db.update_one({"_id": ObjectId(post_id)},
{"$set":
{"content": new_content.replace('\n', ' ').replace('\r', ''),
"edited": True}
})
def user_posts_by_username(username): def user_posts_by_username(username):
return timeline_db.find({'posterid': accounts_db.find_one({"username": username})['_id']}).sort('timePosted', pymongo.DESCENDING) return timeline_db.find({'posterid': accounts_db.find_one({"username": username})['_id']}).sort('timePosted', pymongo.DESCENDING)

14
templates/editpost.html Normal file
View file

@ -0,0 +1,14 @@
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div class="card">
<div class="card-header">Editing post</div>
<div class="card-body">
<form method="POST" action="/editpost/{{ post_obj._id }}" class="input-group">
<input type="text" name="status" style="margin-right: 16px;" class="form-control" value="{{ post_obj.content }}" autofocus />
<input type="submit" class="btn btn-primary" value="Do Post" />
</form>
</div>
</div>
</div>
{% endblock %}

View file

@ -4,7 +4,7 @@
<div class="container"> <div class="container">
<div class="jumbotron"> <div class="jumbotron">
{% if user.profile.profile_pic %} {% if user.profile.profile_pic %}
<img src="/files/{{ user.profile.profile_pic }}" class="rounded float-left" style="margin-right: 24px; max-height: 100px;"/> <img src="/files/{{ user.profile.profile_pic }}" class="rounded float-left mr-4" style="max-height: 100px;"/>
{% endif %} {% endif %}
<h1>{% if user.verified %} <i class="fas fa-shield-check" alt="Verified"></i> {% endif %}{{ user.displayname }} <small class="text-muted">on Tweeder</small></h1> <h1>{% if user.verified %} <i class="fas fa-shield-check" alt="Verified"></i> {% endif %}{{ user.displayname }} <small class="text-muted">on Tweeder</small></h1>
<h2> <h2>
@ -21,6 +21,7 @@
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-4 col-xl-4 mb-3"> <div class="col-xs-12 col-sm-12 col-md-12 col-lg-4 col-xl-4 mb-3">
<div class="card"> <div class="card">
<div class="card-header"> <div class="card-header">
<img src="/files/{{ user.profile.profile_pic }}" class="rounded mt-0 mr-1 img-fluid d-inline" style="max-height: 1.7rem;"/>
<h4 style="display: inline">{{ user.displayname }}' Profile</h4> <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 %} {% 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>
@ -38,7 +39,7 @@
{% 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"> <div class="card-header">
<b>{{ post.poster }}</b> at {{ post.timePosted.strftime('%Y-%m-%d %-H:%M') }} <b>{{ post.poster }}</b> at {{ post.timePosted.strftime('%Y-%m-%d %-H:%M') }} {% if post.edited %}<i>(Edited)</i>{% endif %}
<span style="float: right"> <span style="float: right">
{% if logged_in|lower in post.likes %} {% 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> <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>
@ -46,7 +47,10 @@
<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> <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 %} {% endif %}
<a href="/reply/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-reply"></i></a> <a href="/reply/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-reply"></i></a>
{% if logged_in == post.poster %} {% if logged_in|lower == post.poster|lower %}
<a href="/editpost/{{ post._id }}" class="btn btn-primary btn-sm"><i class="far fa-edit"></i></a>
{% endif %}
{% if logged_in|lower == post.poster|lower %}
<a href="/delete/{{ post._id }}" class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a> <a href="/delete/{{ post._id }}" class="btn btn-danger btn-sm"><i class="far fa-trash-alt"></i></a>
{% endif %} {% endif %}
</span> </span>