/
app.py
124 lines (85 loc) · 3.02 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
from flask import Flask
from flask import render_template, request, url_for, session, redirect
import auth
import messages
# import json
app = Flask(__name__)
app.secret_key = b'aj(>,m&*@#NxmaiOxH23Kkmlb128($'
def is_logged_in():
'''If 'username' is in the session as a key, return True.
Otherwise, return False.'''
return "username" in session
# if username in session:
# return True
# else:
# return False
# if True:
# show_messages()
# else:
# return redirect(url_for('login.html'))
@app.route("/")
@app.route("/messages/")
def show_messages():
'''To do: If the user is logged in, render the 'messages.html' template
together with the user's messages ('inbox').
If the user is NOT logged in, redirect to the login page.'''
if is_logged_in():
username = session['username']
all_messages = messages.get_messages(username)
return render_template('messages.html', messages=all_messages)
else:
return redirect(url_for('login'))
@app.route("/login/", methods=["POST", "GET"])
def login():
if request.method == 'POST':
username = request.form.get('username', None)
password = request.form.get('password', None)
# Login was successful:
if auth.login(username, password):
session['username'] = username
return redirect(url_for('show_messages'))
else:
return render_template('login.html')
else:
print('login with GET')
return render_template('login.html')
@app.route("/logout/")
def logout():
session.pop('username', None)
return redirect(url_for('login'))
@app.route("/message/<int:id>/")
def show_message(id):
# this is the cup that catches the juice
message = messages.get_message(id)
return render_template('message.html', message = message)
# ask why you have to put message = message... seems a little redundant
'''To do: find the given message by its id.
Then display it in a rendered template.'''
# pass
@app.route("/message/compose/", methods=["GET", "POST"])
def compose():
if request.method == 'GET':
# To do: render the relevant page with no data
# request.form.get('key') why did you type this?
return render_template('compose.html')
# username = session['username']
# all_messages = messages.get_messages(username)
# return render_template('messages.html', messages=all_messages)
# else:
# return redirect(url_for('zzzzzzzzz'))
elif request.method == 'POST':
# create new dict populate with form data from to sub body. once created you want to send in to messages.add and redirect to mesages page.
'''To do: 'unpack' the form data, and process it:
1. Prepare the data in an appropriate data structure
2. Save the data to your data store (Make use of existing functions
you may have)
3. Once the data is saved to file, redirect to the /messages/ page.'''
username = request.form.get('username', None)
password = request.form.get('password', None)
# Login was successful:
if auth.compose(username, password):
session['username'] = username
return redirect(url_for('show_message'(id)))
else:
return render_template('show_messages')
pass