/
model.py
599 lines (424 loc) · 16.6 KB
/
model.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# This is my model file. My class definitions, reused functions,
# etc. will live here.
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, DateTime, Float, Text, Boolean
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship, backref
from datetime import date
from datetime import datetime
ENGINE = create_engine("sqlite:///runfree.db", echo=False)
sqla_session = scoped_session(sessionmaker(bind=ENGINE, autocommit = False, autoflush = False))
Base = declarative_base()
Base.query = sqla_session.query_property()
#
# Classes
#
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key = True)
email = Column(String(64), unique = True, nullable = False)
password = Column(String(64), nullable = True)
first = Column(String(64), nullable = True)
last = Column(String(64), nullable = True)
birthdate = Column(DateTime(timezone = False), nullable = True)
sex = Column(String(15), nullable = True)
zipcode = Column(String(15), nullable = True)
runs = relationship("Run", backref=backref("user"))
goals = relationship("Goal", backref=backref("user"))
def __repr__(self):
return "User with email: %s" % self.email
class Run(Base):
__tablename__ = "runs"
id = Column(Integer, primary_key = True)
user_id = Column(Integer, ForeignKey("users.id"))
date_run = Column(DateTime(timezone = False), nullable = True)
zipcode = Column(String(16), nullable = True)
approx_dist = Column(Float, nullable = True)
approx_time = Column(Integer, nullable = True)
commit_date = Column(DateTime(timezone = False), nullable = False)
route = Column(Integer, ForeignKey("routes.id"), nullable = True)
ratings = relationship("Rating", backref=backref("run"))
def __repr__(self):
return "Run on %s" % datetime.strptime((str(self.date_run)), "%Y-%m-%d %H:%M:%S").strftime("%m-%d-%Y")
class Question(Base):
__tablename__ = "questions"
id = Column(Integer, primary_key = True)
question = Column(String(200), nullable = False)
minimum = Column(Integer, nullable = True)
maximum = Column(Integer, nullable = True)
def __repr__(self):
return "%s" % self.question
class Rating(Base):
__tablename__ = "ratings"
id = Column(Integer, primary_key = True)
user_id = Column(Integer, ForeignKey("users.id"))
run_id = Column(Integer, ForeignKey("runs.id"))
question_id = Column(Integer, ForeignKey("questions.id"))
numeric_ans = Column(Integer, nullable = True)
select_ans = Column(String(100), nullable = True)
text_ans = Column(Text, nullable = True)
def __repr__(self):
return "User ID: %d, Run ID: %d, Question ID: %d" % (self.user_id, self.run_id, self.question_id)
class Goal(Base):
__tablename__ = "goals"
id = Column(Integer, primary_key = True)
user_id = Column(Integer, ForeignKey("users.id"))
description = Column(String(200), nullable = False)
fitness_level = Column(String(200))
run_length_history = Column(String(15))
set_date = Column(DateTime(timezone = False), nullable = False)
zipcode = Column(String(15))
race_url = Column(String(200))
event_date = Column(DateTime(timezone = False))
id_for_api = Column(String(100))
date_completed = Column(DateTime(timezone = False), nullable = True)
def __repr__(self):
return "User: %d, Goal: %s" % (self.user_id, self.description)
class Subgoal(Base):
__tablename__ = "subgoals"
id = Column(Integer, primary_key = True)
goal_id = Column(Integer, ForeignKey("goals.id"))
description = Column(String(200), nullable = True)
date = Column(DateTime(timezone = False), nullable = True)
date_completed = Column(DateTime(timezone = False), nullable = True)
associated_run_id = Column(Integer, nullable = True)
goal = relationship("Goal", backref=backref("subgoals", order_by = id))
def __repr__(self):
return "%s" % self.description
class Route(Base):
__tablename__ = "routes"
id = Column(Integer, primary_key = True)
user_id = Column(Integer, ForeignKey("users.id"))
title = Column(String(200), nullable = False)
location_description = Column(String(500))
notes = Column(Text, nullable = True)
distance = Column(Float, nullable = True)
html_embed = Column(Text, nullable = False)
user = relationship("User", backref = backref("routes", order_by = id.desc()))
def __repr__(self):
return "%s : %f Miles" % (self.location_description, self.distance)
# -----------Classes End--------------------------
#
# functions
#
def insert_new_user(new_user):
"""Will insert a new user into the database when he
or she signs up."""
sqla_session.add(new_user)
sqla_session.commit()
def get_user_by_email(email):
"""Returns the user object associated with an email address."""
user = sqla_session.query(User).filter_by(email=email).first()
return user
def insert_new_run(new_run):
"Will insert a new run into the database for the user."
sqla_session.add(new_run)
sqla_session.commit()
def get_latest_run(user):
"""Will find a run with a particular time stamp."""
run = sqla_session.query(Run).filter_by(user_id = user.id).order_by(Run.commit_date.desc()).first()
return run
def get_run_by_id(run_id):
"""Will find a run with the indicated run_id."""
run = sqla_session.query(Run).filter_by(id=run_id).first()
return run
def find_all_runs(user):
"""Returns a list of all the users runs"""
runs = sqla_session.query(Run).filter_by(user_id = user.id).all()
return runs
def find_all_runs_desc(user):
"""Returns a list of all the users runs newest first."""
runs = sqla_session.query(Run).filter_by(user_id = user.id).order_by(Run.date_run.desc()).all()
return runs
def insert_new_goal(new_goal):
"""Will insert a new goal into the database."""
sqla_session.add(new_goal)
sqla_session.commit()
def insert_new_subgoal(subgoal):
"""Will insert a new subgoal into the database."""
sqla_session.add(subgoal)
sqla_session.commit()
def get_most_recent_goal(user):
"""Finds the most recent goal for the user."""
goal = sqla_session.query(Goal).filter_by(user_id = user.id).order_by(Goal.set_date.desc()).first()
return goal
def get_goal_by_id(goal_id):
"""Will find a goal with the indicated goal_id."""
goal = sqla_session.query(Goal).filter_by(id=goal_id).first()
return goal
def get_subgoals_by_goal_id(goal_id):
"""will get all the subgoals for a given goal."""
subgoals = sqla_session.query(Subgoal).filter_by(goal_id=goal_id).all()
return subgoals
def get_subgoal_by_id(subgoal_id):
""""will get the subgoal object for a given id."""
subgoal = sqla_session.query(Subgoal).filter_by(id = subgoal_id).one()
return subgoal
def get_all_goals(user):
"""returns a lis of all goals for the given user."""
goals = sqla_session.query(Goal).filter_by(user_id = user.id).all()
return goals
def get_outstanding_goals(user):
"""returns a list of outstanding subgoal objects, that is, those that are not complete"""
goals = get_all_goals(user)
outstanding_goals = []
for goal in goals:
if not goal.date_completed:
outstanding_goals.append(goal)
return outstanding_goals
def get_outstanding_subgoals(user):
"""returns a list of outstanding subgoal objects, that is, those that are not complete"""
goals = get_all_goals(user)
outstanding_subgoals = []
for goal in goals:
subgoals = get_subgoals_by_goal_id(goal.id)
for subgoal in subgoals:
if not subgoal.date_completed:
outstanding_subgoals.append(subgoal)
return outstanding_subgoals
def get_outstanding_subgoal_by_goal_id(goal_id):
"""Returns outstanding subgoals for a particular goal."""
subgoals = get_subgoals_by_goal_id(goal_id)
outstanding_subgoals = []
for subgoal in subgoals:
if not subgoal.date_completed:
outstanding_subgoals.append(subgoal)
return subgoals
def get_runs_after_date(user, date):
"""gets all runs for a user after a given date."""
all_runs = find_all_runs(user)
runs_after_date = []
for run in all_runs:
if run.date_run > date:
runs_after_date.append(run)
return runs_after_date
def get_ratings_for_run(run_id):
"""returns all ratings for a given run."""
ratings = sqla_session.query(Rating).filter_by(run_id = run_id).all()
return ratings
def get_location_by_run_id(run_id):
""""returns the location of a run with the provided run id. """
location = sqla_session.query(Rating).filter_by(run_id = run_id, question_id = 6).one()
return location
def get_terrain_by_run_id(run_id):
"""returns the terrain of a run with the provided run id. """
terrain = sqla_session.query(Rating).filter_by(run_id = run_id, question_id = 7).one()
return terrain
def get_route_by_run_id(run_id):
"""returns the route type for a run with the provided run id."""
route = sqla_session.query(Rating).filter_by(run_id = run_id, question_id = 8).one()
return route
def get_instagram(run_id):
"""Returns the instagram html for a given run."""
instagram = sqla_session.query(Rating).filter_by(run_id = run_id, question_id = 10).all()
return instagram
def get_location_ratings(user, runs_to_get = 5):
""" Returns all ratings that deal with location for a
given user. """
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
location_ratings = []
for run in runs:
location_ratings.append(sqla_session.query(Rating).filter_by(question_id = 6, user_id = user.id, run_id = run[3]).one())
return location_ratings
def get_terrain_ratings(user, runs_to_get = 5):
"""Returns all ratings that deal with location for a given user."""
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
terrain_ratings = []
for run in runs:
terrain_ratings.append(sqla_session.query(Rating).filter_by(question_id = 7, user_id = user.id, run_id = run[3]).one())
return terrain_ratings
def get_route_ratings(user, runs_to_get = 5):
"""Returns all ratings that deal with route for a given user."""
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
route_ratings = []
for run in runs:
route_ratings.append(sqla_session.query(Rating).filter_by(question_id = 8, user_id = user.id, run_id = run[3]).one())
return route_ratings
def get_collection_of_runs(user_id, runs_to_get = 5):
"""Returns the date and distance of the latest runs
for a given user. """
runs = sqla_session.query(Run.date_run, Run.approx_dist, Run.id).filter_by(user_id = user_id).order_by(Run.date_run.desc()).limit(runs_to_get).all()
# Untuple-ing
run_list = []
for run in runs:
run_list.append([run[0], run[1], get_run_score(run[2]), run[2]])
return run_list
def get_run_score(run_id):
"""Returns a score that will help me rate the quality of the run."""
during_score = sqla_session.query(Rating.numeric_ans).filter_by(run_id = run_id, question_id = 2).first()
after_score = sqla_session.query(Rating.numeric_ans).filter_by(run_id = run_id, question_id = 3).first()
energy_score = sqla_session.query(Rating.numeric_ans).filter_by(run_id = run_id, question_id = 4).first()
run_score = .50 * during_score[0] + .20 * after_score[0] + .30 * energy_score[0]
return run_score
def get_before_ratings(user, runs_to_get = 5):
"""returns list of all the user inputted ratings about how they felt before the run."""
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
mood_ratings = []
mood_info = []
for run in runs:
mood_ratings.append(sqla_session.query(Rating).filter_by(run_id = run[3], question_id = 1).one())
print mood_ratings
for mood in mood_ratings:
mood_info.append([mood.numeric_ans, mood.run.approx_dist])
return mood_info
def get_during_ratings(user, runs_to_get = 5):
"""return list of all the user inputted ratings about how they felt during the run. """
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
mood_ratings = []
mood_info = []
for run in runs:
mood_ratings.append(sqla_session.query(Rating).filter_by(run_id = run[3], question_id = 2).one())
for mood in mood_ratings:
mood_info.append([mood.numeric_ans, mood.run.approx_dist])
return mood_info
def get_after_ratings(user, runs_to_get = 5):
"""returns list of all the user inputted ratings about how they felt after the run."""
runs = get_collection_of_runs(user.id, runs_to_get = runs_to_get)
mood_ratings = []
for run in runs:
mood_ratings.append(sqla_session.query(Rating).filter_by(run_id = run[3], question_id = 3).one())
mood_info = []
for mood in mood_ratings:
mood_info.append([mood.numeric_ans, mood.run.approx_dist])
return mood_info
def get_user_routes(user):
"""returns all the route objects for the given user. """
routes = sqla_session.query(Route).filter_by(user_id = user.id).all()
return routes
def get_route_by_id(route_id):
"""returns a route object for the given id."""
route = sqla_session.query(Route).filter_by(id=route_id).one()
return route
def create_db():
"""Recreates the database."""
Base.metadata.create_all(ENGINE)
# Dictionaries, etc. that will be helpful in displaying info
route_dictionary = {
"point_to_point": "Point to point",
"out_and_back": "Out and back",
"treadmill": "Treadmill",
"track": "Track",
"random": "Random",
"loop": "Loop"
}
terrain_dictionary = {
"flat": "Flat",
"downhill": "Mostly Downhill",
"uphill": "Mostly Uphill",
"hills": "Rolling Hills"
}
location_dictionary = {
"park": "Park",
"city": "City",
"neighborhood": "Neighborhood",
"trail": "Trail",
"beach": "Beach",
"treadmill": "Treadmill",
"track": "Track"
}
goal_dictionary = {
"run_walk_5k": "Run/Walk 5k",
"run_5k": "Run 5k",
"run_walk_10k": "Run/Walk 10k",
"run_10k": "Run 10k",
"run_walk_half": "Run/Walk Half Marathon (13.1 Miles)",
"run_half": "Run Half Marathon (13.1 Miles)",
"run_walk_1_mile": "Run/Walk 1 mile",
"run_walk_2_miles": "Run/Walk 2 miles",
"run_walk_3_miles": "Run/Walk 3 miles",
"run_2_miles": "Run 2 miles",
"run_walk_5_miles": "Run/Walk 5 miles",
"run_5_miles": "Run 5 miles",
"run_walk_10_miles": "Run/Walk 10 miles",
"run_10_miles": "Run 10 miles"
}
# Used to autopopulate sub goals when a goal is selected.
subgoal_dictionary = {
"run_walk_5k": ["run_walk_1_mile", "run_walk_2_miles"],
"run_5k": ["run_walk_1_mile", "run_walk_2_miles", "run_walk_3_miles", "run_2_miles"],
"run_walk_10k": ["run_walk_5k", "run_walk_5_miles"],
"run_10k": ["run_walk_5k", "run_5k", "run_walk_10k", "run_5_miles"],
"run_walk_half": ["run_walk_5k", "run_walk_10k", "run_walk_10_miles"],
"run_half": ["run_walk_5k", "run_walk_10k", "run_5k", "run_10k", "run_walk_10_miles", "run_10_miles"]
}
distance_dictionary = {
"run_walk_5k": "Distance%20(running):5k",
"run_5k": "Distance%20(running):5k",
"run_walk_10k": "Distance%20(running):10k",
"run_10k": "Distance%20(running):10k",
"run_walk_half":"Distance%20(running):half%20marathon",
"run_half": "Distance%20(running):half%20marathon"
}
distance_int_dictionary = {
"run_walk_5k": 3,
"run_5k": 3,
"run_walk_10k": 6,
"run_10k": 6,
"run_walk_half": 13,
"run_half": 13,
"run_walk_1_mile": 1,
"run_walk_2_miles": 2,
"run_walk_3_miles": 3,
"run_2_miles": 2,
"run_walk_5_miles": 5,
"run_5_miles": 5,
"run_walk_10_miles": 10,
"run_10_miles": 10
}
location_badges_dictionary = {
"park": "/static/images/hayleypark.jpg",
"city": "/static/images/2ndalternatecityscape.jpg",
"neighborhood": "static/images/hayleyneighborhood.jpg",
"trail": "/static/images/hayleymountainterrain.jpg",
"beach": "/static/images/beachbadge.jpg",
"treadmill": "static/images/treadmillbadge.jpg",
"track": "static/images/alternatetrackbadge.jpg"
}
terrain_badges_dictionary = {
"flat": "/static/images/flat.jpg",
"downhill": "/static/images/downhill.jpg",
"uphill": "/static/images/uphill.jpg",
"hills": "/static/images/updownupdown.jpg"
}
route_badges_dictionary = {
"point_to_point": "/static/images/pointtopoint.jpg",
"out_and_back": "/static/images/outandbackroute.jpg",
"treadmill": "/static/images/2ndalternatetreadmillroute.jpg",
"track": "/static/images/routetrack.jpg",
"random": "/static/images/randomroute.jpg",
"loop": "/static/images/looproute.jpg"
}
weekday_badges_dictionary = {
"Monday": "/static/images/monday.jpg",
"Tuesday": "/static/images/tuesday.jpg",
"Wednesday": "/static/images/wednesday.jpg",
"Thursday": "/static/images/thursday.jpg",
"Friday": "/static/images/friday.jpg",
"Saturday": "/static/images/saturday.jpg",
"Sunday": "/static/images/sunday.jpg"
}
location_color_dictionary = {
"park": "#FB2570",
"city": "#9025FB",
"neighborhood": "#2597FB",
"trail": "#FBA825",
"beach": "#FBEA25",
"treadmill": "#e377c2",
"track": "#8c564b"
}
terrain_color_dictionary = {
"flat": "#17becf",
"downhill": "#bcbd22",
"uphill": "#e6550d",
"hills": "#9467bd"
}
route_color_dictionary = {
"point_to_point": "#ff9896",
"out_and_back": "#ffbb78",
"treadmill": "#1f77b4",
"track": "#e377c2",
"random": "#ff7f0e",
"loop": "#c49c94"
}