def get_attending(self, request): """ Get Conferences the calling user is attending (i.e. registered for) :param request: :return: ConferenceForms """ if not isinstance(request, message_types.VoidMessage): raise endpoints.BadRequestException() prof = ProfileApi.profile_from_user() # get user Profile conf_keys = prof.conferencesToAttend # Changed from original code if len(conf_keys) == 0: # user hasn't registered for anything, so bail out of this method return ConferenceForms() conferences = ndb.get_multi(conf_keys) # get organizers organisers = [ndb.Key(Profile, conf.organizerUserId) for conf in conferences] profiles = ndb.get_multi(organisers) # put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # return set of ConferenceForm objects per Conference return ConferenceForms(items=[conf.to_form(names[conf.organizerUserId]) for conf in conferences] )
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 _register(request, reg=True): """ Register or unregister user for selected conference. :param request: RPC Message Request with a urlsafe Conference Key :param reg: whether to register (True) or unregister (False) the requesting User :return: BooleanMessage - True if successful, False if failure """ prof = ProfileApi.profile_from_user() # get user Profile # check if conf exists given websafeConfKey # get conference; check that it exists c_key = ndb.Key(urlsafe=request.websafeConferenceKey) conf = c_key.get() if not conf: raise endpoints.NotFoundException('No conference found for key') # register if reg: # check if user already registered otherwise add if c_key in prof.conferencesToAttend: raise ConflictException( 'Already registered for this conference') # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException('There are no seats available.') # register user, take away one seat prof.conferencesToAttend.append(c_key) conf.seatsAvailable -= 1 # update datastore prof.put() conf.put() # un-register else: # check if user already registered if c_key in prof.conferencesToAttend: # unregister user, add back one seat prof.conferencesToAttend.remove(c_key) conf.seatsAvailable += 1 # update datastore prof.put() conf.put() else: return BooleanMessage(data=False) 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))