forked from yfalcon8/relationship_manager_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
403 lines (286 loc) · 13.6 KB
/
server.py
File metadata and controls
403 lines (286 loc) · 13.6 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""My web app's online structure."""
#################
#### Imports ####
#################
# Jinja is a popular template system for Python, used by Flask.
from jinja2 import StrictUndefined
# Flask: A class that we import. An instance of this class will be the
# WSGI application.
# session: A Flask object (class) that allows you to store information specific to a
# user from one request to the next. It's a dictionary that preserves type.
# It is a customized cookie.
from flask import Flask, render_template, request, session, flash, redirect, url_for, jsonify
from model import connect_to_db, db, User, Recommendation, Relationship, Event
# Imported or 'DEBUG' to prevent from Flask Traceback error showing.
import os
import arrow
from flask_bcrypt import Bcrypt
#######################
#### Configuration ####
#######################
# Instantiates Flask. "__name__" is a special Python variable for the name of
# the current module. This is needed so that Flask knows where to look for
# templates, static files, and so on.
app = Flask(__name__)
# Required to use Flask sessions and the debug DebugToolbarExtension. The user could look at
# the contents of your cookie but not modify it, unless they know the secret key
# used for signing.
app.config['SECRET_KEY'] = os.environ.get("FLASK_SECRET_KEY", "abcdef")
# Another way of generating a secret key:
# >>>import os
# >>>os.urandom(24)
# Raises an error when an undefined variable is used in Jinja2.
app.jinja_env.undefined = StrictUndefined
# Prevents the need to restart server when HTML/CSS is changed.
app.jinja_env.auto_reload = True
bcrypt = Bcrypt(app)
# @app.route('/') is a Python decorator.
# '/' in the decorator maps directly to the homepage.
# The index function is triggered when the URL is visited.
@app.route('/')
def index():
"""Homepage."""
return render_template('homepage.html')
# GET: The browser tells the server to just get the information stored on
# that page and send it.
# POST: The browser tells the server that it wants to post some new
# information to that URL and that the server must ensure the data is stored and
# only stored once. This is how HTML forms usually transmit data to the server.
@app.route('/login')
def display_login():
"""Login page."""
return render_template('login_form.html')
@app.route('/login', methods=['POST'])
def handle_login():
"""Process login. Store user in session."""
# Grab the users input.
email = request.form.get("email")
password = bcrypt.generate_password_hash(request.form.get("password"))
# Check that the user exists.
uq = User.query
user_object = uq.filter_by(email=email).first()
if user_object and bcrypt.check_password_hash(password, user_object.password):
flash("Hello again!")
session["user_email"] = user_object.email
session["user_id"] = user_object.id
return redirect("/landing-page")
else:
flash("Oops! Email / Password mismatch: Try again.")
return redirect("/login")
@app.route('/register')
def register():
"""Registration page."""
fb_id = request.args.get('fb_id')
session["fb_id"] = fb_id
return render_template('registration_form.html')
@app.route('/registration-success', methods=['POST'])
def registration_success():
"""Inform new user that they've been added."""
first_name = request.form.get('fname')
last_name = request.form.get('lname')
email = request.form.get('email')
password = request.form.get('password')
fb_id = request.form.get('fb_id')
# Add the user as long as the email isn't already taken.
email_exists = db.session.query(User).filter_by(email=email).first()
if email_exists is None:
new_user = User(fb_id=fb_id, first_name=first_name, last_name=last_name, email=email, password=password)
db.session.add(new_user)
db.session.commit()
session["email"] = email
else:
flash("Email {} is taken.".format(email))
return redirect('/register')
# Grab the id of the user that just signed in.
user_id = db.session.query(User.id).filter_by(email=email).first()[0]
# Add the user to the session.
email = request.form['email']
password = request.form['password']
user = User.query.filter_by(email=email).first()
session["user_id"] = user.id
return render_template('registration_success.html',
first_name=first_name,
email=email,
user_id=user_id)
@app.route('/check_email_existence')
def query_db_for_email():
"""Hidden route that processes Facebook's API results."""
email = request.args.get('email')
fb_id = request.args.get('id')
# Check to see whether the email address exists in the database.
# If so, return the user_id.
email_exists = db.session.query(User.id).filter_by(email=email).first()
# If the email exists, take the user to the landing page.
if email_exists:
url = '/landing-page/%s' % (email_exists[0])
return redirect(url)
# Otherwise, check for the existence of the fb_id in the database.
else:
# Grab the associated user_id to feed to the landing page url.
fb_id_exists = db.session.query(User.id).filter_by(fb_id=fb_id).first()
# If the fb_id exists, take the user to the landing page.
if fb_id_exists:
url = '/landing-page/%s' % (fb_id_exists[0])
return redirect(url)
# Otherwise, take them to the registration page.
else:
return redirect(url_for('register', fb_id=fb_id))
@app.route('/add-contacts')
def add_contacts():
"""User manually adds contacts and categorizes them as a friend, family, or professional."""
return render_template("add_contact.html")
@app.route('/contact-added', methods=['POST'])
def contact_added():
"""Confirmation page that user has been added."""
first_name = request.form.get('fname')
last_name = request.form.get('lname')
relatp = request.form.get('relatp')
relatp_type = ''
# Change the relapt_type to match the table it will be committed to.
if relatp == 'friend':
relatp_type = 'fr'
elif relatp == 'family':
relatp_type = 'fam'
else:
relatp_type = 'prf'
# Add the new contact to the db.
new_contact = Relationship(user_id=user_id, first_name=first_name, last_name=last_name, relatp_type=relatp_type)
db.session.add(new_contact)
db.session.commit()
# Grab the id of the relationship that was just created.
new_contact_info = db.session.query(Relationship.id).filter_by(user_id=user_id, first_name=first_name, last_name=last_name, relatp_type=relatp_type).all()[0][0]
return render_template("contact_added.html",
first_name=first_name,
last_name=last_name,
relatp=relatp,
user_id=user_id,
relatp_id=new_contact_info)
@app.route('/methods-of-reaching-out/<int:user_id>/<int:relatp_id>')
def specify_methods_of_reaching_out(user_id, relatp_id):
"""User can select methods of reaching out from a list."""
# Given the relatp_id, grab the relationship type (friend, family, or professional).
relatp_type = db.session.query(Relationship.relatp_type).filter_by(id=relatp_id).all()[0][0]
# Grab the recommendation list associated with the relationship type.
rcmdn_list = db.session.query(Recommendation).filter_by(relatp_type=relatp_type).all()
return render_template('reach_out.html',
user_id=user_id,
relatp_type=relatp_type,
relatp_id=relatp_id,
rcmdn_list=rcmdn_list)
@app.route('/methods-success/<int:user_id>/<int:relatp_id>', methods=['POST'])
def method_specification_success(user_id, relatp_id):
"""Add the methods specified to the relationship."""
# Grab the recommendation list specified for the relationship.
desired_list = request.form.getlist('rcmdn')
# Add the customized list to the respective relationship.
update_relatp = Relationship.query.filter_by(user_id=user_id, id=relatp_id).first()
update_relatp.rcmdn_list = desired_list
# The created_at column should be placed in the Relationship table.
created_at = db.session.query(Relationship.created_date).filter_by(id=relatp_id).one()
# Turn the query result (a tuple of datetime) into an Arrow-friendly format.
arrow_created_at = arrow.get(created_at[0])
# The start date of all events will be a month from the date he/she was added.
start_date = arrow_created_at.replace(months=+1)
# Events will be scheduled for a max of a year for demo purposes.
yr_from_now = start_date.replace(years=+1)
# Create events for the duration of the year.
# Friends and family should have an event a month.
# Professional contacts should have an event per quarter.
while start_date < yr_from_now:
for desired_item in desired_list:
if update_relatp.relatp_type == 'fr' or update_relatp.relatp_type == 'fam':
# Convert from arrow format to datetime format for db storage.
new_event = Event(user_id=user_id, relatp_id=relatp_id, rcmdn=desired_item, scheduled_at=start_date.datetime)
db.session.add(new_event)
start_date = start_date.replace(months=+1)
else:
new_event = Event(user_id=user_id, relatp_id=relatp_id, rcmdn=desired_item, scheduled_at=start_date.datetime)
db.session.add(new_event)
start_date = start_date.replace(months=+4)
db.session.commit()
return render_template('reach_out_added.html',
user_id=user_id,
desired_list=desired_list)
@app.route('/landing-page')
def landing_page():
"""Page where users land after logging in or signing up."""
# Display the name of all of the reltionships a user has.
# A list of tuples are returned.
# The relationships id will be used to create a link to their profile.
# The user_id is needed in the query results to pass into my Jinja for loop.
print session["user_id"]
contact_name_and_id = db.session.query(Relationship.id, Relationship.first_name, Relationship.last_name).filter_by(user_id=session["user_id"]).order_by(Relationship.first_name).all()
return render_template("landing_page.html",
contact_name_and_id=contact_name_and_id)
@app.route('/contact-display/<int:relatp_id>')
def contact_display(relatp_id):
"""Display a selected contacts profile."""
# Query for all the data related to a relationship.
# Returns a list of objects.
relatp_info = db.session.query(Relationship).filter_by(id=relatp_id).all()
session['user_id'] = 'user_id'
return render_template("contact_display.html",
relatp_id=relatp_id,
relatp_info=relatp_info)
@app.route('/contact-display-handler', methods=['POST'])
def contact_display_hander():
"""Handle the updates on the relationships."""
relatp_id = request.form.get("id")
# Assign a var to the inputted value of the new information.
value = request.form.get("value")
# Assign a var to the field getting updated.
type_button = request.form.get("typeButton")
# Grab the person getting updated.
relationship = Relationship.query.get(relatp_id)
# Update the existing record for the relationship.
setattr(relationship, type_button, value)
db.session.commit()
user_info = {'typeButton': type_button,
'value': value}
return jsonify(user_info)
@app.route('/event-display')
def event_display():
"""Display a selected contacts profile."""
# Store all of a users events in a list.
all_events = []
# Grab the text and time of all of the users relationship.
# Returns a list of tuples.
rcmdn_and_date = db.session.query(Event.rcmdn, Event.scheduled_at, Event.relatp_id).filter_by(user_id=user_id).order_by(Event.scheduled_at).all()
# Grab the name of the relationship.
# Store the name, text, and time in the all_events list.
for rcmdn in rcmdn_and_date:
relatp_id = rcmdn.relatp_id
relatp_name = db.session.query(Relationship.first_name, Relationship.last_name).filter_by(id=relatp_id).one()
all_events.append([relatp_name.first_name, relatp_name.last_name, rcmdn[0], rcmdn[1].date()])
return render_template("event.html",
all_events=all_events)
@app.route('/logout')
def process_logout():
"""Log user out."""
del session["user_id"]
flash("Logged out.")
return render_template('logout.html')
@app.route("/error")
def error():
raise Exception("Error!")
# App will only run if we ask it to run.
if __name__ == "__main__":
# Setting this to be true so that I can invoke the DebugToolbarExtension
app.debug = True
# Use the DebugToolbar
# DebugToolbarExtension(app)
connect_to_db(app)
# Connection for Heroku.
# connect_to_db(app, os.environ.get("DATABASE_URL"))
# Create the tables we need from our models (if they already
# exist, nothing will happen here, so it's fine to do this each
# time on startup)
# db.create_all(app=app)
DEBUG = "NO_DEBUG" not in os.environ
PORT = int(os.environ.get("PORT", 5000))
# debug=True runs Flask in "debug mode". It will reload my code when it
# changes and provide error messages in the browser.
# The host makes the server publicly available by adding 0.0.0.0. This
# tells my operating system to listen on all public IPs.
# Port 5000 required for Flask.
app.run(host='0.0.0.0', port=PORT, debug=DEBUG)