-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMongoCalls.py
More file actions
303 lines (233 loc) · 6.81 KB
/
MongoCalls.py
File metadata and controls
303 lines (233 loc) · 6.81 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
"""Handle calls to and from mongodb"""
from pymongo import MongoClient
from random import randint
import datetime
#client = MongoClient()
client = MongoClient('mongodb://duck_hacker:ssw690@34.230.77.217/ssw690spring2018')
db = client.ssw690spring2018
def add_user(id, email, uname, photo):
try:
db.Users.insert_one({
'uuid':id,
'email':email,
'username':uname,
'photo':photo
})
except Exception as e:
print('ERROR ADDING USER', e)
def get_user():
try:
result = db.Users.find()
except Exception as e:
print('ERROR READING FILE', e)
users = [post for post in result]
return users
def get_specific_user(id):
try:
result = db.Users.find_one({'uuid': id})
except Exception as e:
print('ERROR READING FILE', e)
user = result
return user
def get_userbyid(username):
try:
result = db.Users.find_one({'username': username})
except Exception as e:
print('ERROR READING FILE', e)
user = result
return user
def get_question():
"""retrieve all the questions contained in Interview collection"""
try:
questions = db.Interview.find()
except:
print('ERROR READING FILE')
brokendownexp = [post for post in questions]
return brokendownexp
def insert_questions(question, title, topic, userid):
"""insert questions into the question collection"""
id = randint(0, 9999)
try:
db.Interview.insert_one(
{
'_id': id,
'title': title,
"voteup": 0,
"votedown":0,
"question": question,
'topic': topic,
'userid': userid,
'time': get_time()
}
)
except Exception as e:
print('Error submitting solution: {}'.format(e))
def get_specific_ques(id):
question = None
result = {}
try:
question = db.Interview.find_one({'_id': int(id)})
except Exception as e:
print('Error finding element : {}'.format(e))
for key in question:
result[key] =question[key]
return result
def insert_experience(userid, exp):
"""insert user experience into Experience collection"""
id = randint(0, 9999)
try:
db.Experience.insert_one(
{
'_id': id,
'userid':userid,
'experience': exp,
'time': get_time(),
"voteup":0,
"votedown":0
}
)
except Exception as e:
print('Error submitting experience: {}'.format(e))
def get_experience():
"""retrieve all the experiences contained in Experience collection"""
try:
experience = db.Experience.find()
except Exception as e:
print('ERROR READING FILE:', e)
result =[exp for exp in experience]
return result
def get_solution():
"""retrieve all the solutions conatined in solutions collection"""
try:
solution = db.Solution.find()
except:
print('Error retrieving file')
result = [sol for sol in solution]
return result
def get_solution_by_id(id):
solution = None
table = db.Solution
listofsol = []
try:
solution = table.find({'quesid': id})
except Exception as e:
print('Error finding element : {}'.format(e))
for r in solution:
listofsol.append(r)
return listofsol
def insert_solution(solution, quesid,userid, files=None):
"""insert solutions into the solution collection"""
id = randint(0, 9999)
try:
db.Solution.insert_one(
{
'_id': id,
"userid": userid,
'quesid':quesid,
"files": files,
"voteup": 0,
"votedown":0,
"solution": solution,
'time': get_time()
}
)
except Exception as e:
print('Error submitting solution: {}'.format(e))
def find_by_topic(topic):
table = db.Interview
try:
questions = table.find({'topic': topic})
except Exception as e:
print("Error finding element: {}".format(e))
result = [ques for ques in questions]
return result
def increase_count(post_id):
"""increase count of votes by 1"""
votes=None
table = db.Solution
try:
solution = table.find_one({'_id': int(post_id)})
votes = solution['voteup']
except Exception as e:
print('Error finding element : {}'.format(e))
new_vote = int(votes)+1
try:
db.Solution.update_one(
{'_id': int(post_id)},
{"$set":{"voteup" : new_vote}
}
)
except Exception as e:
print('Error updating count: {}'.format(e))
def decrease_count(post_id):
"""decrease count of votes by 1"""
votes = None
table = db.Solution
try:
solution = table.find_one({'_id': int(post_id)})
votes = solution['votedown']
except Exception as e:
print('Error finding element : {}'.format(e))
new_vote = int(votes) + 1
try:
db.Solution.update_one(
{'_id': int(post_id)},
{"$set": {"votedown" : new_vote}
}
)
except Exception as e:
print('Error updating count: {}'.format(e))
def ex_increase_count(post_id):
"""increase count of votes by 1"""
votes=None
table = db.Experience
try:
Experience = table.find_one({'_id': int(post_id)})
votes = Experience['voteup']
except Exception as e:
print('Error finding element : {}'.format(e))
new_vote = int(votes)+1
try:
db.Experience.update_one(
{'_id': int(post_id)},
{"$set":{"voteup" : new_vote}
}
)
except Exception as e:
print('Error updating count: {}'.format(e))
def ex_decrease_count(post_id):
"""decrease count of votes by 1"""
votes = None
table = db.Experience
try:
Experience = table.find_one({'_id': int(post_id)})
votes = Experience['votedown']
except Exception as e:
print('Error finding element : {}'.format(e))
new_vote = int(votes) + 1
try:
db.Experience.update_one(
{'_id': int(post_id)},
{"$set": {"votedown" : new_vote}
}
)
except Exception as e:
print('Error updating count: {}'.format(e))
def download_file(filename):
"""download file uploaded to mongodb"""
try:
post = db.solution.find({"file":filename})
except Exception as e:
print('Error downloading file: {}'.format(e))
return post.file
def get_ex_votes(post_id):
votes = None
table = db.Experience
try:
Experience = table.find_one({'_id': int(post_id)})
votes = Experience['votes']
except Exception as e:
print('Error finding element : {}'.format(e))
return votes
def get_time():
return datetime.datetime.now()