-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
315 lines (272 loc) · 11 KB
/
database.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
from user import user
from flask.helpers import flash
import pymongo
from bson.objectid import ObjectId
from pprint import pprint
from supermemo2 import *
from flashcard import *
import secret
import os
# User
# username: String,
# flashcards: Dictionary
# token: String
# Flashcard
# Front: String, Image
# Back: String, Image
# card_id: string,
# set_id: string,
# user_id?: string ?
# learning_info : { ( comes from superMemo2.json() ) }
# e.g.
#
# MongoDB Setup
db_uri = secret.db_uri
client = pymongo.MongoClient(db_uri)
db = client.flashDB
users = db.users
sets = db.sets
flashcards = db.flashcards
'''
NOTE: many of the methods here revolve around the mongoDB created ObjectId, so with creation we may need to change this up
(with creation of users for example)
'''
#User Methods
def db_get_user(user_id):
myquery = { "_id": user_id }
foundUsers = users.find(myquery)
user = None if foundUsers.explain()['executionStats']['nReturned'] <= 0 else foundUsers[0]
user_data = {}
if not user == None:
user_data = user
user_data['flashcard_sets'] = db_get_sets_of_user(user_id)
else:
user_data = {
"error": "No user found."
}
return user_data
def db_create_user(user_name, user_bio, user_flashcards, user_email):
query = {"_id": user_email, "user_name": user_name}
duplicateUsers = users.find(query)
new_user_data = {"error": "User already has this username or email."} if duplicateUsers.explain()['executionStats']['nReturned'] > 0 else {}
if not "error" in new_user_data:
new_user = {
"user_name": user_name,
"user_bio": user_bio,
"flashcard_sets": user_flashcards,
"_id": user_email
}
#created from mongoDB, may need to be changed with OAuth
userid = users.insert(new_user)
return user_email
else:
return new_user_data
'''
For users, sets, and cards, there's both a update_field and update_{object} (ex: update_user)
- the update_field will update a singular field, when given the field (ex: user --> user_name) and new value
- very useful for inside methods, where I may update just one field after making a change
- (ex: deleting flashcard --> change flashcardSet "cards")
- the update_{user, set, card} (ex: db_update_user) will update multiple fields, when given a dictionary of the values ex: { "username": "Chris" }
- Much more useful for API which will send requests in JSON or in {} form
'''
def db_update_user_field(user_id, field, newValue):
if not "error" in user_id:
query = {"_id": user_id}
userUpdated = users.update(query, {"$set": {field: newValue}})
data = {"error": f"User {user_id} wasn't updated"} if userUpdated['nModified'] <= 0 else {"success": f"User {user_id} was updated"}
return data
else:
return {"error": "User "+ user_id + " could not be updated."}
def db_update_user(user_id, newUser):
query = {"_id": user_id}
userUpdated = users.update(query, {"$set": newUser})
if(userUpdated['nModified'] > 0):
return {"success": "User " + user_id + " has been updated"}
else:
return {"error": "User "+ user_id + " could not be updated."}
def db_delete_user(user_id):
myquery = { "_id": user_id }
deletedUser = users.find_one_and_delete(myquery)
data = {"error": f"User {user_id} could not be deleted."} if deletedUser == None else {"success": f"User {str(deletedUser['_id'])} was deleted."}
if not "error" in data:
db_delete_all_sets(user_id)
return data
#flashcard_set Methods
def db_get_set(set_id):
myquery = { "_id": ObjectId(set_id) }
foundSets = sets.find(myquery)
set = None if foundSets.explain()['executionStats']['nReturned'] <= 0 else foundSets[0]
if not set == None:
cards = db_get_flashcards_of_set(set_id)
set_data = {
"title": set["set_name"],
"description": set["set_desc"],
"cards": cards,
"id": str(set["_id"]), #Object id -> str
}
else:
set_data = {
"error": "No set found."
}
return set_data
def db_get_sets_of_user(user_id):
myquery = { "user_email": user_id }
found_sets = sets.find(myquery)
numReturned = found_sets.explain()['executionStats']['nReturned']
sets_in_users = []
for i in range(numReturned):
sets_in_users.append(found_sets[i])
for user_set in sets_in_users:
user_set["_id"] = str(user_set["_id"])
# user_set["user_id"] = str(user_set["user_id"])
pprint(sets_in_users)
return sets_in_users
def db_create_set(user_email, set_name, description, cards):
new_set = {
"user_email": user_email,
"set_name": set_name,
"set_desc": description,
"cards": cards
}
user = db_get_user(user_email)
newuser = user
set_id = sets.insert(new_set)
newuser_sets = user["flashcard_sets"]
newuser_sets.append(set_id)
newuser["flashcard_sets"] = newuser_sets
db_update_user(user_email, newuser)
return set_id
def db_update_set_field(set_id, field, newValue):
query = {"_id": ObjectId(set_id)}
setUpdated = sets.update(query, {"$set": {field: newValue}})
data = {"error": f"Set {set_id} wasn't updated"} if setUpdated['nModified'] <= 0 else {"success": f"Set {set_id} was updated"}
return data
def db_update_set(set_id, newSet):
query = {"_id": ObjectId(set_id)}
setUpdated = sets.update(query, {"$set": newSet})
data = {"error": f"Set {set_id} wasn't updated"} if setUpdated['nModified'] <= 0 else {"success": f"Set {set_id} was updated"}
return data
def db_delete_set(user_id, set_id):
# delete in sets collection
user_sets = db_get_user(user_id)
if "error" in user_sets:
return {"error": f"No set was deleted."}
matching_sets = [set for set in user_sets['flashcard_sets'] if str(set["_id"]) == str(set_id)]
if len(matching_sets) > 0:
query = {"_id": ObjectId(set_id)}
set = sets.remove(query)
if set['n'] > 0:
flashcards_in_set = db_get_flashcards_of_set(set_id)
for flashcard in flashcards_in_set:
db_delete_flashcard(set_id, flashcard["_id"])
if(len(user_sets) > 0):
keep_sets = [set for set in user_sets['flashcard_sets'] if str(set['_id']) != str(set_id)]
db_update_user_field(user_id, "flashcard_sets", keep_sets)
return {"success": f"Set {set_id} was deleted from user {user_id}"}
else:
return {"error": f"No set was deleted."}
else:
return {"error": f"Invalid request: User {user_id} does not own set {set_id}"}
# delete from user object
#finally, delete all flashcards in set
def db_delete_all_sets(user_id):
# deletes all sets belonging to that user_id
user_sets = db_get_sets_of_user(user_id)
query = {"_id": user_id}
# deletes all flashcards underneath said sets (set_id)
for set in user_sets:
db_delete_all_flashcards(set['_id']) # but don't return {"success": f"Set {set_id} was deleted from user {user_id}"} for every set deleted
# clear user flashcard_sets
db_update_user_field(user_id, "flashcard_sets", [])
# one option is sets.delete_many({})
pass
#flashcard Methods
def db_get_flashcard(flashcard_id):
myquery = { "_id": ObjectId(flashcard_id) }
found_flashcards = flashcards.find(myquery)
flashcard = None if found_flashcards.explain()['executionStats']['nReturned'] <= 0 else found_flashcards[0]
if not flashcard == None:
flashcard_data = flashcard
del flashcard_data['set_id']
del flashcard_data['_id']
return flashcard_data
def db_get_flashcards_of_set(set_id):
myquery = { "set_id": ObjectId(set_id) }
found_flashcards = db.flashcards.find(myquery)
numReturned = found_flashcards.explain()['executionStats']['nReturned']
flashcards_in_set = []
for i in range(numReturned):
flashcards_in_set.append(found_flashcards[i])
for flashcard in flashcards_in_set:
flashcard['_id'] = str(flashcard['_id'])
flashcard['set_id'] = str(flashcard['set_id'])
return flashcards_in_set
def db_create_flashcard(set_id, front, back):
# create flashcard_id
superMem = first_review(3)
new_flashcard = {
"set_id": set_id,
"front": front,
"back": back,
"learning_history": superMem.json()
}
flashcard_id = flashcards.insert(new_flashcard)
set_cards = db_get_set(set_id)['cards']
set_cards.append(flashcard_id)
db_update_set_field(set_id, "cards", set_cards)
return flashcard_id
def db_update_flashcard_field(flashcard_id, field, newValue):
query = {"_id": ObjectId(flashcard_id)}
updatedFlashcard = flashcards.update(query, {"$set": {field: newValue}})
return updatedFlashcard
pass
def db_update_flashcard(flashcard_id, newFlashcard):
query = {"_id": ObjectId(flashcard_id)}
updatedFlashcard = flashcards.update(query, {"$set": newFlashcard})
return updatedFlashcard
pass
def db_delete_flashcard(set_id, flashcard_id):
# takes in set_id
set = db_get_set(set_id)
print("SET IS:", set)
# deletes flashcard_id belonging to that set_id
query = {"_id": ObjectId(flashcard_id)}
flashcards.delete_one(query)
# delete from set object
if not "error" in set:
set_cards = set['cards']
set_cards.remove(flashcard_id)
db_update_set_field(set_id, "cards", set_cards)
pass
def db_delete_all_flashcards(set_id):
# takes in set_id
set = db_get_set(set_id)
# deletes all flashcards belonging to that set_id
query = {"set_id": ObjectId(set_id)}
flashcards.remove(query)
db_update_set_field(set_id, "cards", [])
pass
def db_study_flashcard(flashcard_id, time, correct):
# get current learning history (if any)
flashcard_from_db = db_get_flashcard(flashcard_id)
learning_history = flashcard_from_db["learning_history"]
new_flashcard = flashcard(learning_history)
new_flashcard.updateReviewDate(time, correct)
db_update_flashcard_field(flashcard_id, "learning_history", new_flashcard.learning_history)
return new_flashcard.learning_history
def db_get_cards_to_study_today(set_id):
set_from_db = db_get_set(set_id)
cards = set_from_db["cards"]
cards_to_do = []
for card in cards:
curr_card = json.loads(card["learning_history"])
print(curr_card["review_date"])
correct_date = date.fromisoformat(curr_card["review_date"])
if(correct_date <= date.today()):
cards_to_do.append(card)
# cards_to_do = [card for card in cards if card["learning-history"]["review_date"] >= datetime.datetime(2020, 5, 17)]
return cards_to_do
# take data -> create Flashcard (using flashcard class)
# get next_review_day from flashcard.get_next_review_day()
# update db, send to front-end with json
# print(collection.list_collection_names())