def addSessionToWishlist(self, request): #TASK 2 """Adds sessions to a user wishlist & returns the sessions added""" #Check if the user is logged in user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization Required') user_id = getUserId(user) #Validate that the SessionKey (urlsafe key) is provided if not request.SessionKey: raise endpoints.BadRequestException("SessionKey field required") #Validate whether the requested SessionKey is already in the user's wishlist q = SessionWishlist.query() if (q.filter(SessionWishlist.sessionKey == request.SessionKey).count() > 0): raise endpoints.BadRequestException("SessionKey is already in %s's wishlist" % user) #Generate a Wishlist key to store the user wishlist. The wishlist will be created as #a child of Profile p_key = ndb.Key(Profile, user_id) w_id = SessionWishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(SessionWishlist, w_id, parent=p_key) #Add the wishlist to the DS wishlist = SessionWishlist( key = w_key, sessionKey = request.SessionKey) wl_key = wishlist.put() #Return the session associated with the created entry session = ndb.Key(urlsafe=request.SessionKey).get() return self._copySessionToForm(session)
def getSessionsInWishlist(self, request): """Get user wishlist sessions""" #TASK 2 user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization Required') user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) #Query the list of wishlist sessions based on ancestor key p_key wishlist = SessionWishlist.query(ancestor=p_key).fetch() #generate a list of sessions using the sessionKey stored in the wishlist sessions = [] for wish_session in wishlist: sessions.append(ndb.Key(urlsafe=wish_session.sessionKey).get()) return SessionForms( items=[self._copySessionToForm(session) for session in sessions] )