-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalfachat.py
80 lines (53 loc) · 2.06 KB
/
alfachat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
from flask import abort, escape, Flask, request
from flask import render_template, send_from_directory
from datetime import datetime
from cache_bust import create_hash
from messages import MessageParser
from models import Chat, Users
from config import GUEST_NAME
app = Flask(__name__, static_folder='static', static_url_path='')
@app.route('/robots.txt')
@app.route('/style.css')
@app.route('/favicon.ico')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
@app.route("/<user_id>", methods=['GET'])
def chat_read(user_id):
user = authenticate(user_id)
timestamp = int(datetime.now().timestamp())
with Chat() as chat:
messages = chat.read(150)
latest_pk = messages[-1].pk if messages else 0
return render_template('alfachat.html',
user=user,
timestamp=timestamp,
messages=messages,
guest=GUEST_NAME,
users=Users().all(),
latest_pk=latest_pk)
@app.route("/<user_id>", methods=['POST'])
def chat_write(user_id):
user = authenticate(user_id)
raw_text = escape(request.form['message'])
with Chat() as chat:
chat.write(MessageParser().parse(user, raw_text))
return chat_read(user_id)
@app.route("/archiv/<user_id>")
def archive(user_id):
user = authenticate(user_id)
with Chat() as chat:
messages = chat.read()
return render_template('archive.html', messages=messages, user=user)
@app.route("/latest/<user_id>/<latest_pk>")
def latest(user_id, latest_pk):
user = authenticate(user_id)
with Chat() as chat:
messages = list(filter(lambda m: m.is_visible_to(
user), chat.read_latest(latest_pk)))
return "[{0}]".format(",".join([m.to_json() for m in messages]))
def authenticate(user_id):
return Users().find_by_user_id(user_id) or abort(404)
@app.url_defaults
def hashed_url_for_static_file(endpoint, values):
create_hash(endpoint, values, app)