-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathdb.py
287 lines (223 loc) · 8.13 KB
/
db.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
"""
This module contains all database interfacing methods for the MFlix
application. You will be working on this file for the majority of M220P.
Each method has a short description, and the methods you must implement have
docstrings with a short explanation of the task.
Look out for TODO markers for additional help. Good luck!
"""
import bson
from flask import current_app, g
from werkzeug.local import LocalProxy
from flask_pymongo import PyMongo
from pymongo.errors import DuplicateKeyError, OperationFailure
from bson.objectid import ObjectId
from bson.errors import InvalidId
def get_db():
"""
Configuration method to return db instance
"""
db = getattr(g, "_database", None)
if db is None:
db = g._database = PyMongo(current_app).db
return db
# Use LocalProxy to read the global db instance with just `db`
db = LocalProxy(get_db)
def get_movies_by_country(countries):
"""
Finds and returns movies by country.
Returns a list of dictionaries, each dictionary contains a title and an _id.
"""
try:
"""
Ticket: Projection
Write a query that matches movies with the countries in the "countries"
list, but only returns the title and _id of each movie.
Remember that in MongoDB, the $in operator can be used with a list to
match one or more values of a specific field.
"""
# Find movies matching the "countries" list, but only return the title
# and _id. Do not include a limit in your own implementation, it is
# included here to avoid sending 46000 documents down the wire.
print(f" c: {countries}")
return list(db.movies.find({},{"country" : 1}))
except Exception as e:
return e
def get_movies_faceted(filters, page, movies_per_page):
"""
Returns movies and runtime and ratings facets. Also returns the total
movies matched by the filter.
Uses the same sort_key as get_movies
"""
sort_key = "tomatoes.viewer.numReviews"
pipeline = []
if "cast" in filters:
pipeline.extend([{
"$match": {"cast": {"$in": filters.get("cast")}}
}, {
"$sort": {sort_key: -1}
}])
else:
raise AssertionError("No filters to pass to faceted search!")
counting = pipeline[:]
count_stage = {"$count": "count"}
counting.append(count_stage)
skip_stage = {"$skip": movies_per_page * page}
limit_stage = {"$limit": movies_per_page}
facet_stage = {
"$facet": {
"runtime": [{
"$bucket": {
"groupBy": "$runtime",
"boundaries": [0, 60, 90, 120, 180],
"default": "other",
"output": {
"count": {"$sum": 1}
}
}
}],
"rating": [{
"$bucket": {
"groupBy": "$metacritic",
"boundaries": [0, 50, 70, 90, 100],
"default": "other",
"output": {
"count": {"$sum": 1}
}
}
}],
"movies": [{
"$addFields": {
"title": "$title"
}
}]
}
}
try:
movies = list(db.movies.aggregate(pipeline, allowDiskUse=True))[0]
count = list(db.movies.aggregate(counting, allowDiskUse=True))[
0].get("count")
return (movies, count)
except OperationFailure:
raise OperationFailure(
"Results too large to sort, be more restrictive in filter")
def build_query_sort_project(filters):
"""
Builds the `query` predicate, `sort` and `projection` attributes for a given
filters dictionary.
"""
query = {}
# The field "tomatoes.viewer.numReviews" only exists in the movies we want
# to display on the front page of MFlix, because they are famous or
# aesthetically pleasing. When we sort on it, the movies containing this
# field will be displayed at the top of the page.
sort = [("tomatoes.viewer.numReviews", -1)]
project = None
if filters:
if "text" in filters:
query = {"$text": {"$search": filters["text"]}}
meta_score = {"$meta": "textScore"}
sort = [("score", meta_score)]
project = {"score": meta_score}
elif "cast" in filters:
query = {"cast": {"$in": filters["cast"]}}
elif "genres" in filters:
"""
Ticket: Text and Subfield Search
Given a genre in the "filters" object, construct a query that
searches MongoDB for movies with that genre.
"""
# TODO: Text and Subfield Search
# Construct a query that will search for the chosen genre.
query = {}
return query, sort, project
def get_movies(filters, page, movies_per_page):
"""
Returns a cursor to a list of movie documents.
Based on the page number and the number of movies per page, the result may
be skipped and limited.
The `filters` from the API are passed to the `build_query_sort_project`
method, which constructs a query, sort, and projection, and then that query
is executed by this method (`get_movies`).
Returns 2 elements in a tuple: (movies, total_num_movies)
"""
query, sort, project = build_query_sort_project(filters)
if project:
cursor = db.movies.find(query, project).sort(sort)
else:
cursor = db.movies.find(query).sort(sort)
total_num_movies = 0
if page == 0:
total_num_movies = db.movies.count_documents(query)
movies = cursor.limit(movies_per_page)
return (list(movies), total_num_movies)
def get_movie(id):
"""
Given a movie ID, return a movie with that ID, with the comments for that
movie embedded in the movie document. The comments are joined from the
comments collection using expressive $lookup.
"""
try:
pipeline = [
{
"$match": {
"_id": ObjectId(id)
}
}
]
movie = db.movies.aggregate(pipeline).next()
return movie
# TODO: Error Handling
# If an invalid ID is passed to `get_movie`, it should return None.
except (StopIteration) as _:
return None
except Exception as e:
return {}
def get_all_genres():
"""
Returns list of all genres in the database.
"""
return list(db.movies.aggregate([
{"$unwind": "$genres"},
{"$group": {"_id": None, "genres": {"$addToSet": "$genres"}}}
]))[0]["genres"]
"""
Ticket: Create/Update Comments
For this ticket, you will need to implement the following two methods:
- add_comment
- update_comment
You can find these methods below this docstring. Make sure to read the comments
to better understand the task.
"""
def add_comment(movie_id,name , email, comment, date):
"""
Inserts a comment into the comments collection, with the following fields:
- "name"
- "email"
- "movie_id"
- "text"
- "date"
Name and email must be retrieved from the "user" object.
"""
comment_doc = { 'movie_id' : movie_id, 'name' : name, 'email' : email,'text' : comment, 'date' : date}
return db.comments.insert_one(comment_doc)
def update_comment(comment_id, user_email, text, date):
"""
Updates the comment in the comment collection. Queries for the comment
based by both comment _id field as well as the email field to doubly ensure
the user has permission to edit this comment.
"""
# TODO: Create/Update Comments
# Use the user_email and comment_id to select the proper comment, then
# update the "text" and "date" of the selected comment.
response = db.comments.update_one(
{ "comment_id": comment_id },
{ "$set": { "text ": text, "date" : date } }
)
return response
def delete_comment(comment_id, user_email):
"""
Given a user's email and a comment ID, deletes a comment from the comments
collection
"""
response = db.comments.delete_one( { "_id": ObjectId(comment_id) } )
return response