def _wishlist(self, request, add=True): """ Transaction to add or remove Session from a ConferenceWishlist given by a WishlistRequest :param request: Wishlist RPC Request [VoidMessage, session key in query string] :param add: whether to add (True) to the wishlist or remove from a wishlist (False) :return: BooleanMessage - True if successful, False if failure """ prof = ProfileApi.profile_from_user() # get user Profile session = get_from_webkey(request.websafeSessionKey) # get session if not session or not isinstance(session, Session): raise endpoints.NotFoundException('Not a valid session') # see if the wishlist exists wishlist = ConferenceWishlist().query(ancestor=prof.key) \ .filter(ConferenceWishlist.conferenceKey == session.conferenceKey) \ .get() # User requested to add to the wishlist, so create if needed if not wishlist: if add: # need to create the wishlist first conf_key = session.key.parent() wishlist = ConferenceWishlist(conferenceKey=conf_key, parent=prof.key) else: # remove request, but no wishlist! raise endpoints.NotFoundException( 'Nothing wishlisted for Conference') # update wishlist by adding/removing session key s_key = session.key if s_key not in wishlist.sessionKeys: if add: # add the key wishlist.sessionKeys.append(s_key) wishlist.put() else: # can't remove a nonexistant key raise endpoints.NotFoundException( 'Session not in wishlist for Conference') else: # key already exists if add: # session already wishlisted, so return error return BooleanMessage(data=False) else: # remove key from wishlist wishlist.sessionKeys.remove(s_key) # if wishlist is empty, remove the wishlist. else just update. if len(wishlist.sessionKeys) == 0: wishlist.key.delete() else: wishlist.put() return BooleanMessage(data=True)
def get_wishlists(self, request): """ Endpoint for retrieving all wishlists for a requesting user :param request: :return: """ if not isinstance(request, VoidMessage): raise endpoints.BadRequestException() prof = ProfileApi.profile_from_user() # get user Profile wishlists = ConferenceWishlist.query(ancestor=prof.key).fetch() return WishlistForms( items=[wishlist.to_form() for wishlist in wishlists])
def get_wishlists(self, request): """ Endpoint for retrieving all wishlists for a requesting user :param request: :return: """ if not isinstance(request, VoidMessage): raise endpoints.BadRequestException() prof = ProfileApi.profile_from_user() # get user Profile wishlists = ConferenceWishlist.query(ancestor=prof.key).fetch() return WishlistForms( items=[wishlist.to_form() for wishlist in wishlists] )
def get_wishlist(self, request): """ Gets the list of sessions wishlisted by a User given a Conference. :param request: Conference GET Request [VoidMessage, conference key in query string] :return: SessionForms """ prof = ProfileApi.profile_from_user() # get user Profile conf_key = ndb.Key(urlsafe=request.websafeConferenceKey) wishlist = ConferenceWishlist().query(ancestor=prof.key) \ .filter(ConferenceWishlist.conferenceKey == conf_key) \ .get() if not wishlist: error = 'No wishlist for conference with key %s' % \ request.websafeConferenceKey raise endpoints.NotFoundException(error) sessions = ndb.get_multi(wishlist.sessionKeys) return SessionForms(items=SessionApi.populate_forms(sessions))