-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·112 lines (88 loc) · 3.02 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""`main` is the top level module for your Flask application."""
# Import the Flask Framework
from flask import Flask, request, session, redirect, url_for, render_template
from datetime import datetime
from collections import namedtuple
Message = namedtuple('Message', ['sender', 'message', 'date'])
app = Flask(__name__)
app.config.from_object('settings.Production')
MAX_MESSAGES = 3
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
messages = [] # list of Message(s)
def add_message(sender, message):
"""add a new message to the list of all messages
trim list to MAX_MESSAGES if needed
returns newly created Message (or None)
"""
global messages
if message:
msg = Message(sender, message, datetime.now())
messages.append(msg)
if len(messages) > MAX_MESSAGES:
messages = messages[-MAX_MESSAGES:]
return msg
@app.route('/')
def index():
"""Return default page"""
return render_template('master.html')
@app.route('/demo')
def add():
"""adds a test message"""
add_message('demo', 'message')
return 'OK: %d' % len(messages)
@app.route('/send', methods=['GET', 'POST'])
def send_message():
"""allow to send a message"""
global messages
if request.method == 'POST':
message = request.form['text'].strip()
sender = request.form['sender'].strip()
_msg = add_message(sender, message)
return redirect(url_for('show_messages'))
else:
return render_template('send.html')
@app.route('/lst')
def list_messages():
"""list all messages (TXT)"""
return '%d messages: %s' % (len(messages), ",\n".join(messages))
@app.route('/latest')
def latest_message():
"""show latest message (JSON)"""
import json
latest = messages[-1] if len(messages) else None
if latest:
msg = latest.message
if latest.sender:
msg = ("%s: " % latest.sender) + msg
if latest.date:
now = datetime.now()
delta = now-latest.date
if delta.days==0:
dstr = "heute %s" % latest.date.strftime('%H:%M')
elif delta.days==1:
dstr = "gestern %s" % latest.date.strftime('%H:%M')
else:
dstr = latest.date.strftime('%Y-%m-%d %H:%M')
msg = msg + (" (%s)" % dstr)
else:
msg ="keine Nachrichten"
output = {"frames": [{
"index": 0,
"text": msg,
"icon": "i43"
}]
}
return json.dumps(output)
@app.route('/show')
def show_messages():
"""show all messages"""
return render_template('messages.html', messages=messages)
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def application_error(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500