Added most of the code for private messages, although something doesn't work at the moment
This commit is contained in:
parent
fc5100c515
commit
a09225a523
3 changed files with 88 additions and 2 deletions
31
Tweeder.py
31
Tweeder.py
|
@ -1,5 +1,5 @@
|
|||
from flask import Flask, render_template, request, redirect, url_for, session, make_response, abort
|
||||
from backend import accounts, timeline, files
|
||||
from backend import accounts, timeline, files, messages
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = "eVZ4EmVK70iETb03KqDAXV5sBHb3T73t"
|
||||
|
@ -215,6 +215,7 @@ def like_post(post_id):
|
|||
elif request.method == "GET":
|
||||
pass
|
||||
|
||||
|
||||
@app.route("/unlike/<post_id>", methods=["GET", "POST"])
|
||||
def unlike_post(post_id):
|
||||
if 'username' not in session.keys(): return redirect(url_for('login'))
|
||||
|
@ -245,5 +246,33 @@ def mentions():
|
|||
posts=timeline.get_mentions(logged_in))
|
||||
|
||||
|
||||
@app.route("/messages", methods=["GET", "POST"])
|
||||
def messages_blank():
|
||||
logged_in = session['username'] if ('username' in session.keys()) else False
|
||||
if 'username' not in session: return redirect(url_for('login'))
|
||||
if request.method == "GET":
|
||||
return render_template('messages.html', logged_in=logged_in)
|
||||
elif request.method == "POST":
|
||||
return redirect('/messages/'+request.form['messageuser'])
|
||||
|
||||
|
||||
@app.route("/messages/<user>", methods=["GET", "POST"])
|
||||
def messaging(user):
|
||||
logged_in = session['username'] if ('username' in session.keys()) else False
|
||||
if 'username' not in session: return redirect(url_for('login'))
|
||||
if request.method == "GET":
|
||||
return render_template(
|
||||
"messages.html",
|
||||
messaging=accounts.get_display_name(user),
|
||||
messages=messages.get_messages(logged_in, user.lower())
|
||||
)
|
||||
elif request.method == "POST":
|
||||
messages.send_message(
|
||||
accounts.get_display_name(logged_in),
|
||||
accounts.get_display_name(user),
|
||||
request.form['message_content']
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host="127.0.0.1", debug=True)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from pymongo import MongoClient
|
||||
from bson.objectid import ObjectId
|
||||
from backend import accounts
|
||||
import datetime
|
||||
import datetime, pymongo
|
||||
|
||||
client = MongoClient()
|
||||
db = client.tweeder
|
||||
|
@ -34,3 +34,22 @@ def send_message(msg_from, msg_to, msg_content):
|
|||
}
|
||||
|
||||
messages_db.insert_one(message)
|
||||
|
||||
|
||||
def get_messages(user1, user2):
|
||||
user1_id = accounts_db.find_one({"username": user1.lower()})['_id']
|
||||
user2_id = accounts_db.find_one({"username": user2.lower()})['_id']
|
||||
|
||||
messages = messages_db.find(
|
||||
{"$or": [
|
||||
{
|
||||
"from": user1_id,
|
||||
"to" : user2_id
|
||||
},
|
||||
{
|
||||
"from": user2_id,
|
||||
"to" : user1_id
|
||||
}
|
||||
]}
|
||||
).sort('timeSent', pymongo.DESCENDING)
|
||||
return messages
|
||||
|
|
38
templates/messages.html
Normal file
38
templates/messages.html
Normal file
|
@ -0,0 +1,38 @@
|
|||
{% extends 'layout.html' %}
|
||||
{% block nav_messages %}active{% endblock %}
|
||||
{% block content %}
|
||||
<div class="container">
|
||||
<div class="card mb-3">
|
||||
<div class="card-header">
|
||||
<form method="POST" action="/messages" class="input-group">
|
||||
<input type="text" name="messageuser" class="form-control mr-3" placeholder="User to message..." value="{% if messaging %}{{ messaging }}{% endif %}" />
|
||||
<button type="submit" class="btn btn-primary">Message user</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if messages %}
|
||||
{% for message in messages %}
|
||||
<div class="card mb-3" style="word-wrap: break-word; width: 100%;">
|
||||
<div class="card-header"><b>{{ message.poster }}</b> at {{ message.timePosted.strftime('%Y-%m-%d %-H:%M') }}</div>
|
||||
<div class="card-body">
|
||||
{{ message.content }}
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if messaging %}
|
||||
<div class="card">
|
||||
<div class="card-footer">
|
||||
<form method="POST" action="/messages/{{ messaging }}" class="input-group">
|
||||
<input type="text" name="message_content" class="form-control mr-3" placeholder="Write a new message to {{ messaging }}" />
|
||||
<button type="submit" class="btn btn-primary">Send</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
Loading…
Add table
Reference in a new issue