def addWishList(self, request): """Adds a session to a user wishlist""" user = endpoints.get_current_user() # User is logged in if not user: raise endpoints.UnauthorizedException('Authorization required') # Check if parameters were provided if not request.websafeSessionKey: raise endpoints.BadRequestException('websafeSessionKey are required') # check that session exists session_key = ndb.Key(urlsafe=request.websafeSessionKey) session = session_key.get() if not session: raise endpoints.NotFoundException('No session found with key: %s' % request.websafeSessionKey) new_wishlist_session = UserWishList( userId=getUserId(user), websafeSessionKey=session_key.urlsafe() ) new_wishlist_session.put() return UserWishListForm( userId=getUserId(user), websafeSessionKey=session_key.urlsafe() )
def _addSessionToUserWishList(self, request): """Add a Session to a User's WishList""" # preload necessary data items user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Check the required fields are present if not request.sessionKey: raise endpoints.BadRequestException("Session 'session Key' field required") # Obtain the session key # Then obtain the session using the session key # If session does not exist, return session does not exist try: s_key = ndb.Key(urlsafe=request.sessionKey) if s_key is not None: session = s_key.get() except ProtocolBufferDecodeError: raise endpoints.BadRequestException("Session key Invalid") if not session: raise endpoints.BadRequestException("Session does not exist") # copy from user wish list from to dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} data['userID'] = user_id # generate Profile Key based on user ID and User Wish list # ID based on Profile key get user wish list key from ID p_key = ndb.Key(Profile, user_id) userwishlist_id = UserWishList.allocate_ids(size=1, parent=p_key)[0] userwishlist_key = ndb.Key(UserWishList, userwishlist_id, parent=p_key) # Save the key and conference web safe key data['key'] = userwishlist_key data['conferenceWsk'] = session.websafeConferenceKey # create UserWishList, send email to user confirming # creation of User wish list & return (modified) User wish list Form userwishlist = UserWishList(**data) userwishlist.put() return self._copyUserWishListToForm(userwishlist)
def getSessionsInWishList(self, request): """Get all sessions of a user""" # Obtain user information and check user is logged in user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Obtain all user wish list matching the user id userwishlist_sessions = UserWishList.query(UserWishList.userID == user_id) return UserWishListForms(items=[self._copyUserWishListToForm(session) for session in userwishlist_sessions])
def deleteSessionInWishlist(self, request): """Delete session(s) from a user wish list matching the session key""" # Check the required field is provided if not request.sessionKey: raise endpoints.BadRequestException("Session 'session Key' field required") # Obtain all user wish list session matching the given sessionKey # and delete all of them userwishlist_session = UserWishList.query(UserWishList.sessionKey == request.sessionKey) for u in userwishlist_session: u.key.delete() retval = True # return deletion is sucessful return BooleanMessage(data=retval)
def getWishList(self, request): """Retrieves a list of wishlisted sessions for the current user""" user = endpoints.get_current_user() # User is logged in if not user: raise endpoints.UnauthorizedException('Authorization required') sessions_in_wishlist = UserWishList.query().filter(UserWishList.userId == getUserId(user)) session_keys = [ndb.Key(urlsafe=wishlist.websafeSessionKey) for wishlist in sessions_in_wishlist] sessions = ndb.get_multi(session_keys) return UserWishListSessionForms(sessions=[self._copySessionToForm(session) for session in sessions])
def deleteWishList(self, request): """Deletes a UserWishList object""" user = endpoints.get_current_user() # User is logged in if not user: raise endpoints.UnauthorizedException('Authorization required') session_key = ndb.Key(urlsafe=request.websafeSessionKey) session = session_key.get() if not session: raise endpoints.NotFoundException('Session not found') wishlist_entity = UserWishList.query().filter(UserWishList.userId == getUserId(user), UserWishList.websafeSessionKey == request.websafeSessionKey).fetch() for wish in wishlist_entity: wish.key.delete() return BooleanMessage(data=True)