Skip to content

Commit

Permalink
Implemented message deletion (with AJAX) and fixed message editing
Browse files Browse the repository at this point in the history
  • Loading branch information
etrian-dev committed Mar 6, 2022
1 parent baf3b23 commit bba9c34
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
40 changes: 20 additions & 20 deletions chatroom/Msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, data: str, stamp: int):
from flask import (
Blueprint, flash, g, redirect, render_template, request, session, url_for, make_response
)
from flask.json import jsonify

blueprint = Blueprint('msg', __name__, url_prefix='/msg')

Expand Down Expand Up @@ -64,36 +65,35 @@ def edit_message(msg_id):
sender_recipient = cur.fetchone()
newmsg = request.get_json()['msg']
cur.execute('''
UPDATE Messages SET msg_data = ?;
''', [newmsg.encode(encoding='utf-8')])
UPDATE Messages SET msg_data = ? WHERE msg_id = ?;
''', [newmsg.encode(encoding='utf-8'), msg_id])
db.get_db().commit()
flash(f"Message {msg_id} updated successfully")
url = url_for('chat.display_chat', user=sender_recipient['sender'], other=sender_recipient['recipient'])
print(url)
return redirect(url)
return jsonify({"url": url})
# Otherwise render the webpage containing the message to be modified
msg_data = dict()
msg_data['msg_id'] = msg_id
db_conn = db.get_db()
cur = db_conn.execute('''
SELECT * FROM Messages WHERE msg_id = ?;''', [msg_id])
msg_row = cur.fetchone()

msg_data['old_msg'] = msg_row['msg_data'].decode(encoding='utf-8')
if request.method == 'POST':
new_msg = request.form['message']
cur.execute('''
UPDATE OR ROLLBACK Messages
SET msg_data = ?
WHERE msg_id = ?;
''', [new_msg.encode(encoding='utf-8'), msg_id])
db_conn.commit()

return redirect(url_for('chat.display_chat', user=msg_row['sender'], other=msg_row['recipient']))
else:
return render_template('edit_message.html', **msg_data)
return render_template('edit_message.html', **msg_data)


@blueprint.route('/<int:msg_id>', methods=['DELETE'])
# TODO: delete
def delete_message(sender, receiver, msg_id):
return f"DELETEed msg from {sender} to {receiver}"
def delete_message(msg_id):
if request.method == 'DELETE':
# Build the url of the chat where the user will be redirected after deletion
cur = db.get_db().execute('''
SELECT sender,recipient FROM Messages WHERE msg_id = ?;
''', [msg_id])
sender_recipient = cur.fetchone()
url = url_for('chat.display_chat', user=sender_recipient['sender'], other=sender_recipient['recipient'])
# Delete the message
cur.execute('''
DELETE FROM Messages WHERE msg_id = ?;
''', [msg_id])
db.get_db().commit()
return jsonify({"url": url})
2 changes: 1 addition & 1 deletion chatroom/templates/edit_message.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<div class="card">
<form action="{{ url_for('msg.edit_message', msg_id=msg_id) }}">
<form action="{{ url_for('msg.edit_message', msg_id=msg_id) }}" id="msg-edit-form">
<input class='form-input' type='textarea' id="message" name='message' value='{{ old_msg }}'>
<button class='btn btn-primary' id="submit-btn" type='submit'>Ok</button>
</form>
Expand Down
4 changes: 3 additions & 1 deletion chatroom/templates/messages.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
</span>
</div>
<div class='card-body'>{{ msg['data'] }}</div>
{% if msg['sender'] is eq this_user %}
<div class="card-footer">
<a href="{{ url_for('msg.edit_message', msg_id=msg['msg_id'])}}">
Edit
</a>
<a href="{{ url_for('msg.delete_message', msg_id=msg['msg_id'])}}">
<a id="msg-delete" href="{{ url_for('msg.delete_message', msg_id=msg['msg_id'])}}">
Delete
</a>
</div>
{% endif %}
</div>
</div>
{% endfor %}
Expand Down

0 comments on commit bba9c34

Please sign in to comment.