Added the capability to edit messages

This commit is contained in:
Yusur 2019-10-14 19:53:33 +02:00
parent 5e7c6097d4
commit 755e7b70be
9 changed files with 85 additions and 5 deletions

View file

@ -5,6 +5,7 @@
* Removed `type` and `info` fields from `Message` table and merged `privacy` field, previously into a separate table, into that table. In order to make the app work, when upgrading you should run the `migrate_0_4_to_0_5.py` script. * Removed `type` and `info` fields from `Message` table and merged `privacy` field, previously into a separate table, into that table. In order to make the app work, when upgrading you should run the `migrate_0_4_to_0_5.py` script.
* Added flask-login dependency. Now, user logins can be persistent up to 365 days. * Added flask-login dependency. Now, user logins can be persistent up to 365 days.
* Rewritten `enrich` filter, correcting a serious security flaw. The new filter uses a tokenizer and escapes all non-markup text. Plus, now the `+` of the mention is visible, but weakened; newlines are now visible in the message. * Rewritten `enrich` filter, correcting a serious security flaw. The new filter uses a tokenizer and escapes all non-markup text. Plus, now the `+` of the mention is visible, but weakened; newlines are now visible in the message.
* Now you can edit or change privacy to messages after they are published. After a message it's edited, the date and time of the message is changed.
## 0.4.0 ## 0.4.0

42
app.py
View file

@ -477,7 +477,6 @@ def create():
text = request.form['text'] text = request.form['text']
privacy = int(request.form.get('privacy', '0')) privacy = int(request.form.get('privacy', '0'))
message = Message.create( message = Message.create(
type='text',
user=user, user=user,
text=text, text=text,
pub_date=datetime.datetime.now(), pub_date=datetime.datetime.now(),
@ -511,6 +510,47 @@ def create():
return redirect(url_for('user_detail', username=user.username)) return redirect(url_for('user_detail', username=user.username))
return render_template('create.html') return render_template('create.html')
@app.route('/edit/<int:id>', methods=['GET', 'POST'])
@login_required
def edit(id):
user = get_current_user()
message = get_object_or_404(Message, Message.id == id)
if message.user != user:
abort(404)
if request.method == 'POST' and (request.form['text'] != message.text or
request.form['privacy'] != message.privacy):
text = request.form['text']
privacy = int(request.form.get('privacy', '0'))
Message.update(
text=text,
privacy=privacy,
pub_date=datetime.datetime.now()
).where(Message.id == id).execute()
# edit uploads (skipped for now)
# create mentions
mention_usernames = set()
for mo in re.finditer(_mention_re, text):
mention_usernames.add(mo.group(1))
# to avoid self mention
mention_usernames.difference_update({user.username})
for u in mention_usernames:
try:
mention_user = User.get(User.username == u)
if privacy in (MSGPRV_PUBLIC, MSGPRV_UNLISTED) or \
(privacy == MSGPRV_FRIENDS and
mention_user.is_following(user) and
user.is_following(mention_user)):
push_notification('mention', mention_user, user=user.id)
except User.DoesNotExist:
pass
flash('Your message has been edited successfully')
return redirect(url_for('user_detail', username=user.username))
return render_template('edit.html', message=message)
#@app.route('/delete/<int:id>', methods=['GET', 'POST'])
#def confirm_delete(id):
# return render_template('confirm_delete.html')
@app.route('/notifications/') @app.route('/notifications/')
@login_required @login_required
def notifications(): def notifications():

View file

@ -88,3 +88,13 @@ function attachFileInput(){
var fileInput = document.getElementById('fileInputContainer'); var fileInput = document.getElementById('fileInputContainer');
fileInput.innerHTML = '<input type="file" accept="image/*" name="file">'; fileInput.innerHTML = '<input type="file" accept="image/*" name="file">';
} }
function showHideMessageOptions(id){
var msgElem = document.getElementById(id);
var options = msgElem.getElementsByClassName('message-options')[0];
if(options.style.display == 'block'){
options.style.display = 'none';
} else {
options.style.display = 'block';
}
}

View file

@ -10,6 +10,8 @@ body{margin:0}
.flash{background-color:#ff9;border:yellow 1px solid} .flash{background-color:#ff9;border:yellow 1px solid}
.weak{opacity:.5} .weak{opacity:.5}
.message-visual img{max-width:100%;max-height:8em} .message-visual img{max-width:100%;max-height:8em}
.message-options-showhide::before{content:'\2026'}
.message-options{display:none}
.create_text{width:100%;height:8em} .create_text{width:100%;height:8em}
.follow_button,input[type="submit"]{background-color:#ff3018;color:white;border-radius:3px;border:1px solid #ff3018} .follow_button,input[type="submit"]{background-color:#ff3018;color:white;border-radius:3px;border:1px solid #ff3018}
.follow_button.following{background-color:transparent;color:#ff3018;border-color:#ff3018} .follow_button.following{background-color:transparent;color:#ff3018;border-color:#ff3018}

17
templates/edit.html Normal file
View file

@ -0,0 +1,17 @@
{% extends "base.html" %}
{% block body %}
<h2>Edit</h2>
<form action="{{ url_for('edit', id=message.id) }}" method="POST" enctype="multipart/form-data">
<dl>
<dt>Message:</dt>
<dd><textarea name="text" required="" class="create_text">{{ message.text }}</textarea></dd>
<dd><select name="privacy">
<option value="0"{% if message.privacy == '0' %} selected{% endif %}>Public - everyone in your profile or public timeline</option>
<option value="1"{% if message.privacy == '1' %} selected{% endif %}>Unlisted - everyone in your profile, hide from public timeline</option>
<option value="2"{% if message.privacy == '2' %} selected{% endif %}>Friends - only people you follow each other</option>
<option value="3"{% if message.privacy == '3' %} selected{% endif %}>Only you</option>
</select></dd>
<dd><input type="submit" value="Save" /></dd>
</dl>
</form>
{% endblock %}

View file

@ -3,7 +3,7 @@
<h2>Explore</h2> <h2>Explore</h2>
<ul> <ul>
{% for message in message_list %} {% for message in message_list %}
<li>{% include "includes/message.html" %}</li> <li id="{{ message.id }}">{% include "includes/message.html" %}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% include "includes/pagination.html" %} {% include "includes/pagination.html" %}

View file

@ -14,4 +14,14 @@
{% endif %} {% endif %}
- -
<time datetime="{{ message.pub_date.isoformat() }}" title="{{ message.pub_date.ctime() }}">{{ message.pub_date | human_date }}</time> <time datetime="{{ message.pub_date.isoformat() }}" title="{{ message.pub_date.ctime() }}">{{ message.pub_date | human_date }}</time>
-
<a href="javascript:void(0);" onclick="showHideMessageOptions({{ message.id }});" class="message-options-showhide"></a>
</p> </p>
<ul class="message-options">
{% if message.user == current_user %}
<li><a href="/edit/{{ message.id }}">Edit or change privacy</a></li>
<!--li><a href="/confirm_delete/{{ message.id }}">Delete</a></li-->
{% else %}
<!--li><a href="/report/{{ message.id }}">Report</a></li-->
{% endif %}
</ul>

View file

@ -3,7 +3,7 @@
<h2>Your Timeline</h2> <h2>Your Timeline</h2>
<ul> <ul>
{% for message in message_list %} {% for message in message_list %}
<li>{% include "includes/message.html" %}</li> <li id="{{ message.id }}">{% include "includes/message.html" %}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% include "includes/pagination.html" %} {% include "includes/pagination.html" %}

View file

@ -27,7 +27,7 @@
{% endif %} {% endif %}
<ul> <ul>
{% for message in message_list %} {% for message in message_list %}
<li>{% include "includes/message.html" %}</li> <li id="{{ message.id }}">{% include "includes/message.html" %}</li>
{% endfor %} {% endfor %}
</ul> </ul>
{% include "includes/pagination.html" %} {% include "includes/pagination.html" %}