def _createSpeakerObject(self, request): speaker = Speaker(name=request.name, mainEmail=request.mainEmail) speaker.put() speaker.websafeKey = speaker.key.urlsafe() speaker.put() return self._copySpeakerToForm(speaker)
def _createSessionObject(self, request): """Create a new session object, and if necessary, a new speaker object""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) if not request.name: raise endpoints.BadRequestException( "Session 'name' field required") # get key of the conference where the session will be held conf_key = ndb.Key(urlsafe=request.websafeConferenceKey) conf = conf_key.get() # check that conference exists if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % request.websafeConferenceKey) # check that user is owner if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can update the conference.') # copy SessionForm/ProtoRPC Message into dict data = { field.name: getattr(request, field.name) for field in request.all_fields() } # If session creator has provided a speaker key, check that it is valid and retrieve # his/her name if possible # Otherwise create a new speaker (if a speaker name is provided) and assign a a key otherwise continue # building the session without a speaker if data['websafeSpeakerKey'] is not None: try: speaker_key = ndb.Key(urlsafe=request.websafeSpeakerKey) speaker = speaker_key.get() data['speakerName'] = speaker.name data['speakerEmail'] = speaker.mainEmail except ProtocolBufferDecodeError: raise endpoints.NotFoundException( 'No speaker found with websafeSpeakerKey: %s. Create a new speaker or enter the key again' % data['websafeSpeakerKey']) else: # Create a new speaker object if a name is provided in the SessionForm if data['speakerName'] is not None: speaker = Speaker(name=data['speakerName'], mainEmail=data['speakerEmail']) speaker.put() data['websafeSpeakerKey'] = speaker.key.urlsafe() speaker.websafeKey = speaker.key.urlsafe() # add default values for those missing (both data model & outbound Message) for df in SESS_DEFAULTS: if data[df] in (None, []): data[df] = SESS_DEFAULTS[df] setattr(request, df, SESS_DEFAULTS[df]) # convert Date from string to Date object if data['date'] is not None: data['date'] = datetime.datetime.strptime(data['date'][:10], "%Y-%m-%d").date() # convert startTime from string to Time object if data['startTime'] is not None: data['startTime'] = datetime.datetime.strptime( data['startTime'][:], "%H:%M").time() # websafeKey only used for return messages del data['websafeKey'] # Assign the new session a conference parent data['parent'] = conf_key # Create the sesion and put to the database sess = Session(**data) sess_key = sess.put() # update speaker (if necessary) so that this session key will be added to his list of session keys if speaker is not None: speaker.sessionKeys.append(sess_key) speaker.put() # Check the speaker's sessions to see if he/she already has a session at the same conference # If so, set a featured speaker memcache entry to list all of his or her sessions parent_keys = [ sess_key.parent() for sess_key in speaker.sessionKeys ] if sess_key.parent() in parent_keys: featured_sessions = [ sess_key.get() for sess_key in speaker.sessionKeys ] featured_speaker_string = FEATURED_SPEAKER_TPL % ( speaker.name, ', '.join(sess.name for sess in featured_sessions)) memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, featured_speaker_string) return self._copySessionToForm(sess)
def _createSessionObject(self, request): """Create a new session object, and if necessary, a new speaker object""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) if not request.name: raise endpoints.BadRequestException("Session 'name' field required") # get key of the conference where the session will be held conf_key = ndb.Key(urlsafe=request.websafeConferenceKey) conf = conf_key.get() # check that conference exists if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % request.websafeConferenceKey) # check that user is owner if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can update the conference.') # copy SessionForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} # If session creator has provided a speaker key, check that it is valid and retrieve # his/her name if possible # Otherwise create a new speaker (if a speaker name is provided) and assign a a key otherwise continue # building the session without a speaker if data['websafeSpeakerKey'] is not None: try: speaker_key = ndb.Key(urlsafe=request.websafeSpeakerKey) speaker = speaker_key.get() data['speakerName'] = speaker.name data['speakerEmail'] = speaker.mainEmail except ProtocolBufferDecodeError: raise endpoints.NotFoundException( 'No speaker found with websafeSpeakerKey: %s. Create a new speaker or enter the key again' % data['websafeSpeakerKey']) else: # Create a new speaker object if a name is provided in the SessionForm if data['speakerName'] is not None: speaker = Speaker(name=data['speakerName'], mainEmail=data['speakerEmail']) speaker.put() data['websafeSpeakerKey'] = speaker.key.urlsafe() speaker.websafeKey = speaker.key.urlsafe() # add default values for those missing (both data model & outbound Message) for df in SESS_DEFAULTS: if data[df] in (None, []): data[df] = SESS_DEFAULTS[df] setattr(request, df, SESS_DEFAULTS[df]) # convert Date from string to Date object if data['date'] is not None: data['date'] = datetime.datetime.strptime(data['date'][:10], "%Y-%m-%d").date() # convert startTime from string to Time object if data['startTime'] is not None: data['startTime'] = datetime.datetime.strptime(data['startTime'][:], "%H:%M").time() # websafeKey only used for return messages del data['websafeKey'] # Assign the new session a conference parent data['parent'] = conf_key # Create the sesion and put to the database sess = Session(**data) sess_key = sess.put() # update speaker (if necessary) so that this session key will be added to his list of session keys if speaker is not None: speaker.sessionKeys.append(sess_key) speaker.put() # Check the speaker's sessions to see if he/she already has a session at the same conference # If so, set a featured speaker memcache entry to list all of his or her sessions parent_keys = [sess_key.parent() for sess_key in speaker.sessionKeys] if sess_key.parent() in parent_keys: featured_sessions = [sess_key.get() for sess_key in speaker.sessionKeys] featured_speaker_string = FEATURED_SPEAKER_TPL % (speaker.name, ', '.join(sess.name for sess in featured_sessions)) memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, featured_speaker_string) return self._copySessionToForm(sess)