def _cacheFeaturedSpeaker(self, request): """Finds the featured Speaker and adds the speaker to the memcache.""" # Retrieve the Conference key try: c_key = ndb.Key(urlsafe=request.websafeConferenceKey) except Exception: raise endpoints.BadRequestException( 'The websafeConferenceKey is invalid.') # Verify that the conference exists conference = c_key.get() checkObj(conference, 'Conference') # Store the Sessions that are ancestors of the Conference sessions = Session.query(ancestor=c_key) # Retrieve Speaker Keys from each Session in a Conference speakerKeys = set() #memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, featured) return featured
def getConferenceSessions(self, request): """Given a conference, return all sessions.""" # Getting and Verifying current user user = getUser() # Retrieve the Conference key try: c_key = ndb.Key(urlsafe=request.websafeConferenceKey) except Exception: raise endpoints.BadRequestException( 'The websafeConferenceKey given is invalid.') # Verify that the Conference exists conf = c_key.get() checkObj(conf, 'Conference') # Store Sessions that are ancestors sessions = Session.query(ancestor=c_key) # Return a SessionForm for each Session return SessionForms( items = [self._copyConferenceSessionToForm( sess) for sess in sessions])
def getConference(self, request): """Return requested conference (by websafeConferenceKey).""" # get Conference object from request; bail if not found conf = ndb.Key(urlsafe=request.websafeConferenceKey).get() checkObj(conf, 'Conference') prof = conf.key.parent().get() # return ConferenceForm return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
def getSpeakersByConference(self, request): """Given a websafeConferenceKey, return all speakers.""" # Getting and Verifying current user user = getUser() # Retrieve the Conference key try: c_key = ndb.Key(urlsafe=request.websafeConferenceKey) except Exception: raise endpoints.BadRequestException( 'The websafeConferenceKey is invalid.') # Verify that the conference exists conference = c_key.get() checkObj(conference, 'Conference') # Save the ancestors the Conference obj sessions = Session.query(ancestor=c_key) # Retrieve Speaker Keys from each Session in a Conference speakerKeys = set() for sess in sessions: for webSafeKey in sess.speakerKey: speaker_key = ndb.Key(urlsafe=webSafeKey) # add to set speakerKeys.add(speaker_key) # Container to keep speaker objects speakers = [] # Get each speaker and add to the set for spk in speakerKeys: speaker = spk.get() speakers.append(speaker) # Return one or many SpeakerForms for Speakers return SpeakerForms( items = [self._copySpeakerToForm( spkr) for spkr in speakers])
def _updateConferenceObject(self, request): """Update a Conference object, returning the updated ConferenceForm(). """ # Getting and Verifying current user user = getUser() # get the user_id (email) user_id = getUserId(user) # copy data from ProtoRPC Message into dict data = ({field.name: getattr(request, field.name) for field in request.all_fields()}) # update existing conference conf = ndb.Key(urlsafe=request.websafeConferenceKey).get() # check that conference exists checkObj(conf, 'Conference') # To modify you must be the owner if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can modify the Conference.') # copy relevant fields from ConferenceForm to Conference object for field in request.all_fields(): data = getattr(request, field.name) # only copy fields where we get data if data not in (None, []): # special handling for dates (convert string to Date) if field.name in ('startDate', 'endDate'): data = datetime.strptime(data, "%Y-%m-%d").date() if field.name == 'startDate': conf.month = data.month # write to Conference object setattr(conf, field.name, data) conf.put() prof = ndb.Key(Profile, user_id).get() return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None # Get the Profile prof = self._getProfileFromUser() # Check if the conference exists given a websafeConferenceKey wsck = request.websafeConferenceKey # get the conference object. conf = ndb.Key(urlsafe=wsck).get() # Check if the conf was retrieved checkObj(conf, 'Conference') # register if reg: # check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have 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.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if wsck in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)