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)