def getSessionsInWishlistByConference(self, request): """ List the wishlist items for the specified conference. """ # validate the websafe conference key conference_key = validate_websafe_key(request.websafeConferenceKey, 'Conference') # confirm the user user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required.') user_id = getUserId(user) user_key = ndb.Key(Profile, user_id) # get the user's wishlist sessions as a projection q_wishlist = WishList.query(ancestor=user_key) # wl_sessions = q_wishlist.fetch(1, projection=[WishList.sessions]) wishlist = q_wishlist.fetch(1)[0] wishlist_session_keys = [] # for session in wl_sessions: for session in wishlist.sessions: wishlist_session_keys.append(ndb.Key(urlsafe=session)) # query Sessions where the specified Conference is the ancestor session_q = Session.query(ancestor=conference_key) # filter the Sessions to include only the sessions in the wishlist session_q = session_q.filter(Session.key.IN(wishlist_session_keys)) # get the keys of those sessions, which are the ones we're looking for conf_session_keys = session_q.fetch(keys_only=True) # create a wishlist short_wishlist = WishList() # copy the found Session keys into the wishlist as websafe keys for key in conf_session_keys: short_wishlist.sessions.append(key.urlsafe()) # return the reduced wishlist as a message return self._copyWishListToForm(short_wishlist)
def getSpeaker(self, request): """ Get all the information about a speaker. """ # validate the websafe speaker key and retrieve the entity key speaker_key = validate_websafe_key(request.websafeSpeakerKey, 'Speaker') # get the speaker from the db speaker = speaker_key.get() # return a message object with the speaker info return self._copySpeakerToForm(speaker)
def _createSessionObject(self, request): """ :param request: the endpoint request :return: session_form, message of the newly created session """ user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required.') user_id = getUserId(user) # make sure we're given a websafe conference key conference_key = validate_websafe_key(request.websafeConferenceKey, 'Conference') # if we're given a websafe speaker key, make sure it's valid if request.speaker: validate_websafe_key(request.speaker, 'Speaker') # get the conference conference = conference_key.get() # make sure the user can edit this conference if conference.organizerUserId != user_id: raise endpoints.BadRequestException( 'You cannot edit this conference.') # create a session object session = Session() # list the fields we want to exclude exclusions = ['websafeConferenceKey', 'typeOfSession'] # use our handy copy function to copy the other fields session = message_to_ndb(request, session, exclusions) # deal with typeOfSession and get the enum value if request.typeOfSession: session.typeOfSession = str(SessionType(request.typeOfSession)) else: session.typeOfSession = str(SessionType.NOT_SPECIFIED) # allocate an id and create the key session_id = Session.allocate_ids(size=1, parent=conference_key)[0] session.key = ndb.Key(Session, session_id, parent=conference_key) # save the session to ndb session.put() # kick off the featured speaker task taskqueue.add(url='/tasks/set_featured_speaker', params={'conference_key': conference_key.urlsafe()}) # return the newly created session return self._copySessionToForm(session)
def getConferenceFeaturedSpeaker(self, request): """ Gets the featured speaker for a conference """ ws_conference_key = validate_websafe_key(request.websafeConferenceKey, 'Conference', False) # Get the memcache key we're looking for memcache_key = self._featured_speaker_memcache_key(ws_conference_key) # retrieve the message from memcache message = memcache.get(memcache_key) # If there is a message, return it if message is None: message = "" return StringMessage(data=message)
def getSessionsBySpeaker(self, request): """ Get the sessions at which a speaker is speaker across all Conferences. """ # Validate the websafe speaker key and retrieve the entity key speaker_key = validate_websafe_key(request.websafeSpeakerKey, 'Speaker') # query sessions where the speaker is the requested speaker q = Session.query() q = q.filter(Session.speaker == speaker_key.urlsafe()) sessions = q.fetch() # return the list of sessions return SessionForms( items=[self._copySessionToForm(session) for session in sessions])
def updateWishlist(self, request): """ Add or remove sessions to the logged in user's wishlist """ user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required.') # Validate the websafe session key to add ws_add_key = None ws_remove_key = None if request.add: ws_add_key = validate_websafe_key(request.add, 'Session', False) # Validate the websafe session key to remove if request.remove: ws_remove_key = validate_websafe_key(request.remove, 'Session', False) # Get the user wishlist user_id = getUserId(user) user_key = ndb.Key(Profile, user_id) wishlist = WishList.query(ancestor=user_key).fetch(1) # If there wasn't previously a wishlist, create it if not wishlist: wishlist = self._createWishlist(user_key) else: wishlist = wishlist[0] # If there is a session to add, add it if ws_add_key: if ws_add_key not in wishlist.sessions: wishlist.sessions.append(ws_add_key) # If there is a session to remove, remove it if ws_remove_key: if ws_remove_key in wishlist.sessions: wishlist.sessions.remove(ws_remove_key) # Save the wishlist to db wishlist.put() # Create a message of the newly created wishlist wishlist_form = self._copyWishListToForm(wishlist) return wishlist_form
def getConferenceSessions(self, request): """ Get the list of sessions for a conference. """ conference_key = validate_websafe_key(request.websafeConferenceKey, 'Conference') # Get all the sessions where the provided Conference is the ancestor q = Session.query(ancestor=conference_key) # If sessionType is provided as a query string, apply as a filter if request.sessionType: session_type = request.sessionType.upper() if hasattr(SessionType, session_type): q = q.filter(Session.typeOfSession == session_type) # return the list of sessions q = q.order(Session.startTime) return SessionForms( items=[self._copySessionToForm(session) for session in q])
def conferenceQueryProblem(self, request): """ Returns sessions before 7pm that are not workshops """ # Validate the websafe conference key and retrieve the entity key conference_key = validate_websafe_key(request.websafeConferenceKey, 'Conference') # Query for all sessions which are children of the conference q = Session.query(ancestor=conference_key) # Filter for startTime less than 7pm (19:00) startTimeFilter = datetime.strptime('19:00:00', '%H:%M:%S').time() q = q.filter(Session.startTime < startTimeFilter) q = q.filter(Session.startTime != None) q = q.order(Session.startTime) # Get the result with a projection of typeOfSession earlySessions = q.fetch(projection=[Session.typeOfSession]) # Iterate through the results and keep only non-workshop results keys = [s.key for s in earlySessions if s.typeOfSession != 'WORKSHOP'] # Get the db results for the reduced set of keys sessions = ndb.get_multi(keys) # Return the result as a list of sessions return SessionForms( items=[self._copySessionToForm(session) for session in sessions])