-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
286 lines (258 loc) · 13 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from db_utils import get_month_emotions, get_user_id, today_emotion, add_new_user, check_email, check_username, get_password, check_entry_journal, check_entry, add_journal, get_records
from flask import Flask, render_template, request, flash, redirect, session, jsonify
from config import SECRET_KEY
from helper_oop import QuoteAPI, JokeAPI, MoodDict
from registration_form import RegistrationForm
from datetime import datetime, timedelta
from flask_bcrypt import Bcrypt
app = Flask(__name__)
# Setting session secret key and a session length of 15 minutes
app.config['SECRET_KEY'] = SECRET_KEY
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=15)
# Settings to remove the whitespaces added by jinja blocks
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
# Sets up encryption for passwords:
bcrypt = Bcrypt(app)
# Homepage displays gifs from the api for the user to select
@app.route('/', methods=['GET'])
def mood_checkin():
if 'mood_dict' not in session: # First check if the session has already a saved dictionary
try:
emotions_api = MoodDict()
emotions_dict = emotions_api.make_dict()
session['mood_dict'] = emotions_dict # saves the gif url dict to the session
except Exception as e:
print('Mood endpoint: ', e)
session.pop('_flashes', None)
flash("Something went wrong. Please try again later", "error")
return render_template("mood.html", emotions=session['mood_dict'])
# After choosing a feeling, user is redirected here where they can choose between getting a joke or a quote
@app.route('/choice/<id>', methods=['GET', 'POST'])
def choice(id):
try: # saves selected mood and gif url into the session
session['emotion'] = id
session['mood_url'] = session['mood_dict'][id]
except Exception as e:
print('Choice endpoint: ', e)
session.pop('_flashes', None)
flash("Something went wrong! Please submit a new choice", "error")
return redirect('/')
return render_template("choice.html", emotion=id)
# Page displaying the quote of the day
@app.route('/quote', methods=['GET', 'POST'])
def quote_of_the_day():
quote_api = QuoteAPI()
result = quote_api.unpack()
quote = result[0]
author = result[1]
if request.method == "POST": # triggered when the user tries to save the quote
# following logic checks if they are able to save a quote (are they logged in -> have they already saved an entry for today -> did the entry save?)
if 'user' not in session:
return redirect('/login')
try:
validation_check = check_entry(session['user_id'], session['date'])
if validation_check == True:
session.pop('_flashes', None)
flash("You have already saved an entry for today", "notification")
elif validation_check == False:
today_emotion(session['user_id'], session['emotion'], session['mood_url'], session['date'], 'Quote', quote)
validation_check_two = check_entry(session['user_id'], session['date'])
if validation_check_two:
session.pop('_flashes', None)
flash("Your entry has been saved.", "notification")
return redirect('/journal')
else:
session.pop('_flashes', None)
flash("Something went wrong. Please try again later", "error")
except Exception as e:
print('Quote endpoint: ', e)
session.pop('_flashes', None)
flash("Something went wrong. Please try again later", "error")
return render_template("quote.html", quote=quote, author=author)
# Page displaying joke of the day. Follows the same logic:
@app.route('/joke', methods=['GET', 'POST'])
def joke_generator():
if 'joke' not in session: # First check if the session has already a saved joke
joke_api = JokeAPI()
result = joke_api.unpack()
session['joke'] = result
if request.method == "POST":
if 'user' not in session:
return redirect('/login')
try:
v_check = check_entry(session['user_id'], session['date'])
if v_check == True:
session.pop('_flashes', None)
flash("You have already saved an entry for today", "notification")
elif v_check == False:
print(session['joke'])
today_emotion(session['user_id'], session['emotion'], session['mood_url'], session['date'], 'Joke', session['joke'])
vc_two = check_entry(session['user_id'], session['date'])
if vc_two:
session.pop('_flashes', None)
flash("Your entry has been saved.", "notification")
return redirect('/journal')
else:
session.pop('_flashes', None)
flash("Something went wrong. Please try again later", "error")
except Exception as e:
print('Joke endpoint: ', e)
session.pop('_flashes', None)
flash("Something went wrong. Please try again later", "error")
return render_template("joke.html", joke=session['joke'])
# Page allowed the user to write and save a journal entry
@app.route('/journal', methods=['GET', 'POST'])
def add_journal_entry():
if request.method == 'POST':
content = request.form.get('textarea')
# Series of validation checks before saving the entry
# (is the user logged in? -> is there content in the journal? -> is the content too long? -> have they saved their mood choice yet? -> have they saved a diary entry already?)
if 'user' not in session:
return redirect('/login')
elif not content:
session.pop('_flashes', None)
flash('Journal is empty', "notification-error")
elif len(content) > 350:
session.pop('_flashes', None)
flash("Oops! Journal entries must be 350 characters or less...", "error")
else:
try:
validation_check = check_entry(session['user_id'], session['date'])
if validation_check == False:
session.pop('_flashes', None)
flash("You need to save today's emotion first!", "notification")
elif validation_check == True:
validation_check_two = check_entry_journal(session['user_id'], session['date'])
if validation_check_two == True:
session.pop('_flashes', None)
flash('You have already submitted a diary entry for this date', "notification")
elif validation_check_two == False:
add_journal(content, session['user_id'], session['date'])
validation_three = check_entry_journal(session['user_id'], session['date'])
if validation_three:
session.pop('_flashes', None)
flash("Your entry has been saved.", "notification")
return redirect('/overview')
else:
session.pop('_flashes', None)
flash('Something went wrong. Please try again later.', "error")
except Exception as e:
print('Journal endpoint: ', e)
session.pop('_flashes', None)
flash('Something went wrong. Please try again later.', "error")
return render_template("journal.html")
# This page gets calendar view of your entries + stats of moods
@app.route('/overview', methods=['GET', 'POST'])
def show_overview():
if 'user' not in session: # checks if the user is logged in, redirects if not
return redirect('/login')
if request.method == "POST": # triggered by the calendar changing month
session.pop('_flashes', None)
try:
date = request.form.get('month')
sliced_date = date[0:15]
if sliced_date:
date_object = datetime.strptime(sliced_date, "%a %b %d %Y")
# Get full month name:
month_dt = str(date_object.month)
month_object = datetime.strptime(month_dt, "%m")
month_name = month_object.strftime("%B")
# Get month and year as integers:
month = int(date_object.month)
year = int(date_object.year)
# Get array for user's emotions for that month/year
myList = get_month_emotions(session['user_id'], month, year)
return jsonify({'output': myList, 'label': f'Your moods for {month_name} {year}...'})
except Exception as e:
print('Overview endpoint: ', e)
return render_template("overview.html")
# Shows the saved records for a chosen date
@app.route('/archive/<date>')
def show_archive_by_date(date):
if 'user' not in session: # checks if the user is logged in, redirects if not
return redirect('/login')
# Get the records from the database
saved_records = get_records(session['user_id'], date)
if saved_records is None:
flash(f"No records saved on {date}", 'notification')
return redirect('/overview')
record = {}
record['emotion'] = saved_records[0]
record['gif_url'] = saved_records[1]
record['choice'] = saved_records[2]
record['quote_joke'] = saved_records[3]
record['diary'] = saved_records[4]
if record['diary'] is None:
record['diary'] = f"You didn't feel like journaling on {date} and that's okay!"
return render_template("archive.html", date=date, record=record)
# Page to register a new user
@app.route('/register', methods=['GET', 'POST'])
def register_user():
form = RegistrationForm(request.form)
if request.method == 'POST': # Triggered when the form is submitted
content = {}
# Collect user input from the form:
for item in ["FirstName", "LastName", "Username", "email", "password", "confirm", "accept_tos"]:
content[item] = request.form.get(item)
# A series of validation checks:
if content['password'] != content['confirm']:
session.pop('_flashes', None)
flash('Password and Password Confirmation do not match', "error")
elif check_email(content['email']):
session.pop('_flashes', None)
flash('Email already registered')
elif check_username(content['Username']):
session.pop('_flashes', None)
flash('Username already in use', "error")
else:
# If checks passed, it now tries to create a hashed_password
try:
hashed_password = bcrypt.generate_password_hash(content['password']).decode('utf-8')
content['hashed_password'] = hashed_password
if add_new_user(content) == 'New user added.':
session.pop('_flashes', None)
flash("Your account has been created. Please login.", "notification")
return redirect('/login')
else:
session.pop('_flashes', None)
flash('We were unable to register you at this time. Please try again later', "error")
except Exception as e:
print('New user endpoint: ', e)
session.pop('_flashes', None)
flash('We were unable to register you at this time. Please try again later', "error")
return render_template("register.html", form=form)
# Page for user to login
@app.route('/login', methods=['GET', 'POST'])
def user_login():
if request.method == 'POST': # Triggered on form submission
session.clear()
username = request.form.get('uname')
password = request.form.get('password')
if not check_username(username): # Validation check for username
session.pop('_flashes', None)
flash("This username does not exist", "error")
else:
try:
# Verifies password matches hashed password
stored_password = get_password(username)
if not bcrypt.check_password_hash(stored_password, password):
session.pop('_flashes', None)
flash("Username and Password do not match", "error")
else: # If successful, username, id, and date added to the session:
session['user'] = username
session['user_id'] = get_user_id(username)
session['date'] = datetime.today().strftime('%Y-%m-%d')
return redirect('/')
except Exception as e:
print('Login endpoint: ', e)
flash("Something has gone wrong. Please try again later", "error")
return render_template("login.html")
# Log out and clear the session
@app.route('/logout')
def user_logout():
session.clear()
flash("You have been logged out. See you soon!", "notification")
return redirect('/')
if __name__ == '__main__':
app.run(debug=True, port=5500)