def post(self): ''' Add a FilmFinder to your Banned List. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() is_valid_integer(request.json['userID']) session = Session() query = session.query(User).filter( User.userID == request.json['userID']).one_or_none() if not query: raise NotFound query = session.query(BannedList).filter( BannedList.userID == g.userID, BannedList.bannedUserID == request.json['userID']).one_or_none() if query or g.userID == request.json['userID']: # If the FilmFinder being banned is already in the user's Banned List, # or if a user is attempting to ban himself, raise an Exception. raise Forbidden # Remove a user's subscription to a FilmFinder when banning that FilmFinder. session.query(Subscription).filter( Subscription.userID == g.userID, Subscription.subscribedUserID == request.json['userID']).delete() session.add(BannedList(g.userID, request.json['userID'])) session.commit() session.close() return {'message': 'Reviewer banned.'}, 201
def post(self, id): ''' Leave a movie review ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() is_valid_integer(id) validate_rating(request.json['rating']) validate_review(request.json['review']) session = Session() movie = session.query(Movie).filter(Movie.movieID == id).one_or_none() if not movie: raise NotFound query = session.query(MovieReview).filter( MovieReview.movieID == id, MovieReview.userID == g.userID).one_or_none() if query: # We disallow a user from leaving more than one review for the same movie. raise Forbidden query = session.query(Watchlist).filter( Watchlist.movieID == id, Watchlist.userID == g.userID).one_or_none() if not query: # When a user leaves a review for a film, we add that # film to his Watchlist if it isn't there already. session.add(Watchlist(id, g.userID)) request.json['rating'] = float(request.json['rating']) session.add( MovieReview(id, g.userID, request.json['rating'], request.json['review'])) movie.ratings_sum += request.json['rating'] movie.review_count += 1 session.commit() session.close() return {'message': 'Review saved.'}, 201
def post(self, userID): TokenAuthenticator(request.headers.get('Authorization')).authenticate() session = Session() is_valid_integer(userID) # User not Found if not session.query(User).filter(User.userID == userID).first(): raise NotFound cID = g.userID # Can't subscribe to oneself. if cID == userID: raise BadRequest new = set(session.query(Wishlist.movieID).filter(Wishlist.userID == userID)) cur = set(session.query(Wishlist.movieID).filter(Wishlist.userID == cID)) # Add only absent movies toAdd = new.difference(cur) if toAdd: for movie in toAdd: for m in movie: wl = Wishlist(m, cID) session.add(wl) session.commit() session.close() response = {'message': 'Wishlisted Movies imported'} return response, 201 else: response = {'message': 'No Movies were imported'} return response, 200
def post(self): # Get params username = request.json.get('username') username = cleanString(username) yob = request.json.get('yob') email = request.json['email'] email = cleanString(email) isValidEmail(email) password = request.json['password'] session = Session() if session.query(User).filter(User.email == email).first(): raise BadRequest hashed_password = HashGenerator(password).generate() new_user = User(username, email, hashed_password, yob) session.add(new_user) session.commit() session.close() response = {'message': 'Registration successful.'} return response, 201
def delete(self, id): ''' Remove a FilmFinder from your Banned List. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() is_valid_integer(id) session = Session() if not session.query(BannedList).filter(BannedList.userID == g.userID, BannedList.bannedUserID == id).delete(): # If the FilmFinder being unbanned is not in # the user's Banned List, raise an Exception. session.commit() raise NotFound session.commit() session.close() return {'message': 'Reviewer unbanned.'}, 200
def delete(self, movieID): ''' Removes said movie from current user's Wishlist. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() session = Session() is_valid_integer(movieID) affectedRows = session.query(Wishlist).filter(Wishlist.movieID == movieID)\ .filter(Wishlist.userID == g.userID).delete() # When 0, it means either of movieID or userID are not present in database. if affectedRows == 0: raise NotFound else: session.commit() session.close() return 204
def post(self): ''' Adds a movie to the users' Watchlist. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() movieID = request.json.get('movieID') is_valid_integer(movieID) new_watchlist = Watchlist(movieID, g.userID) session = Session() session.add(new_watchlist) try: session.commit() session.close() except IntegrityError: #If Watchlist already present session.rollback() raise BadRequest response = {'message': 'Movie added to Watchlist.'} return response, 201
def delete(self, userID): ''' Unsubscribe to said user. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() session = Session() curUserID = g.userID # Can't subscribe to oneself. if curUserID == int(userID): raise BadRequest affectedRows = session.query(Subscription).filter(Subscription.userID == curUserID) \ .filter(Subscription.subscribedUserID == userID).delete() # When 0, it means userIDs are not present in database. if affectedRows == 0: raise NotFound else: session.commit() return 204
def post(self): ''' Subscribe to the user. ''' TokenAuthenticator(request.headers.get('Authorization')).authenticate() subscribedUserID = request.json.get('userID') curUserID = g.userID # Can't subscribe to oneself. if curUserID == int(subscribedUserID): raise BadRequest subscribe = Subscription(curUserID, subscribedUserID) session = Session() session.add(subscribe) try: session.commit() except IntegrityError: #If subscription already present session.rollback() raise BadRequest response = {'message': 'Subscribed to User'} return response, 201