-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrud.py
More file actions
274 lines (165 loc) · 8.16 KB
/
crud.py
File metadata and controls
274 lines (165 loc) · 8.16 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
"""CRUD operations for mental health exercises app."""
from model import db, User, Exercise, Prompt, ResponseToPrompt, PushSubscription, Notification, connect_to_db
from datetime import timedelta
def create_user(email, password, first_name, last_name, is_expert=False, is_consumer=True, pen_name=None):
"""Create and return a new user."""
user = User(email=email,
password=password,
first_name=first_name,
last_name=last_name,
is_expert=is_expert,
is_consumer=is_consumer,
pen_name=pen_name)
return user
def get_users():
"""Return all users from database."""
return User.query.all()
def get_user_by_id(user_id):
"""Return user by `user_id.`"""
return User.query.get(user_id)
def get_user_by_email(email):
"""Return user by `email`."""
return User.query.filter(User.email == email).first()
def create_exercise(title, description, author, frequency=None, time_limit_per_sitting=None):
"""Create and return a new exercise."""
exercise = Exercise(title=title,
description=description,
author=author,
frequency=frequency,
time_limit_per_sitting=time_limit_per_sitting)
return exercise
def get_exercises():
"""Return all exercises from database."""
return Exercise.query.all()
def get_exercise_by_id(exercise_id):
"""Return exercise by `exercise_id`."""
return Exercise.query.get(exercise_id)
def create_prompt(prompt_content, exercise, prompt_type="long answer"):
"""Create and return a new prompt."""
prompt = Prompt(prompt_content=prompt_content,
prompt_type=prompt_type,
exercise=exercise)
return prompt
def get_prompts():
"""Return all prompts from database."""
return Prompt.query.all()
def get_prompt_by_id(prompt_id):
"""Return prompt by `prompt_id`."""
return Prompt.query.get(prompt_id)
def get_prompts_by_exercise(exercise_id):
"""Return prompts by `exercise_id`."""
return Prompt.query.filter(Prompt.exercise_id == exercise_id).all()
def create_response(response_content, prompt, user, time_completed_exercise):
"""Create and return a new response."""
response = ResponseToPrompt(response_content=response_content,
prompt=prompt,
user=user,
time_completed_exercise=time_completed_exercise)
return response
def get_responses():
"""Return all responses from database."""
return ResponseToPrompt.query.all()
def get_response_by_id(response_id):
"""Return response by `response_id`."""
return ResponseToPrompt.query.get(response_id)
def get_responses_by_user_id(user_id):
"""Return responses by `user_id`."""
return ResponseToPrompt.query.filter(ResponseToPrompt.user_id == user_id).all()
def print_exercise_responses_of_user(user_id, exercise_id):
"""Print all of user's responses to prompts for this exercise."""
prompt_response_pairs = []
# List of responses
responses = ResponseToPrompt.query.filter(ResponseToPrompt.user_id == user_id).join(Prompt).filter(Prompt.exercise_id == exercise_id).all()
for response in responses:
tup = (response.prompt.prompt_content, response.response_content)
prompt_response_pairs.append(tup)
for tup in prompt_response_pairs:
# Unpack the tuple to make the print more useful for engineers
(prompt_content, response_content) = tup
print(f'Prompt: {prompt_content} Response: {response_content}')
return prompt_response_pairs
def print_exercises_of_user(user_id):
"""Print all exercises user has responded to."""
exercises = []
# List of responses of user
responses = ResponseToPrompt.query.filter(ResponseToPrompt.user_id == user_id).join(Prompt).all()
for response in responses:
print("Exercise Title:", response.prompt.exercise.title)
exercises.append(response.prompt.exercise)
return exercises
def get_unique_exercises_of_user(user_id):
"""Return unique exercises user has responded to."""
exercises = set() # Note: exercises will be unordered
responses = ResponseToPrompt.query.filter(ResponseToPrompt.user_id == user_id).join(Prompt).all()
for response in responses:
exercises.add(response.prompt.exercise)
return exercises
def create_push_subscription(subscription_json, user):
"""Create and return a new subscription to push notifications."""
subscription = PushSubscription(subscription_json=subscription_json,
user=user)
return subscription
def get_subscriptions():
"""Return all subscriptions from database."""
return PushSubscription.query.all()
def get_first_subscription(subscription_json):
"""Return first subscription to push notifications."""
first_subscription = PushSubscription.query.filter(PushSubscription.subscription_json == subscription_json).first()
return first_subscription
def get_subscription_by_id(subscription_id):
"""Return subscription by `subscription_id`."""
return PushSubscription.query.get(subscription_id)
def create_notification(user, exercise, last_sent):
"""Create and return a new notification."""
notification = Notification(user=user,
exercise=exercise,
last_sent=last_sent)
return notification
def get_notifications():
"""Return all notifications from database."""
return Notification.query.all()
def get_notification_by_id(notification_id):
"""Return notification by `notification_id`."""
return Notification.query.get(notification_id)
# Logic to figure out what notifications to send at each run of the scheduled job.
def get_notifications_to_send(current):
"""Return notifications to send."""
# `current` is when the scheduled `send_push()` function begins to run
notifs_to_send = []
notifications = Notification.query.all()
for notification in notifications:
# Calculate when notification should next be sent to prepare for
# comparison with `current`
next_date = notification.last_sent + timedelta(days=notification.exercise.frequency)
# If `current` is later in time than when the notification should
# next be sent, then this notification is due to be sent again, to
# encourage the user to revisit the exercise
if next_date < current:
notifs_to_send.append(notification)
# print(notifs_to_send)
# TODO: Optimize the database call. Why?
# Because filtering in the database would take advantage of the db's index.
# And returning lots of data from the db to code takes more memory and time.
# Idea: Filter for notifications where the time gap that has transpired
# from the time the notification was last sent to `current` is greater
# than the gap recommended based on how often the exercise should be
# completed.
# Therefore, instead of `Notification.query.all()`:
# Alternative 1:
# `Notification.query.filter((current - Notification.last_sent) > timedelta(hours=Notification.Exercise.frequency)).all())`
# Testing this approach gave errors.
# Alternative 2: Based on SQLAlchemy documentation, I had a hypothesis that
# SQLAlchemy can identify the exercise based on a notification having a
# relationship to an exercise. I tried simplifying Alternative 1 to
# `Notification.query.filter((current - Notification.last_sent) > timedelta(hours=Exercise.frequency)).all())`
# Testing this approach gave errors.
# Next step: Further investigation of how SQLAlchemy works to come up with
# better alternatives.
return notifs_to_send
def get_subscriptions_from_notification(notification):
"""Return push subscriptions of user to whom notification belongs."""
subscriptions = PushSubscription.query.filter(notification.user_id == PushSubscription.user_id).all()
return subscriptions
if __name__ == '__main__':
from server import app
connect_to_db(app)