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)