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 createSpeaker(self, request):
     """ Create a speaker """
     # Make sure the user is logged in.
     user = endpoints.get_current_user()
     if not user:
         raise endpoints.UnauthorizedException('Authorization required.')
     user_id = getUserId(user)
     user_key = ndb.Key(Profile, user_id)
     # Create the Speaker object
     speaker = Speaker()
     # Copy the fields from the request to the Speaker
     speaker = message_to_ndb(request, speaker)
     # Allocate the Speaker id and set the key with the User as parent
     speaker_id = speaker.allocate_ids(size=1, parent=user_key)[0]
     speaker.key = ndb.Key(Speaker, speaker_id, parent=user_key)
     # Write the speaker to the db
     speaker.put()
     # Create a SpeakerForm and copy the fields from the request
     speaker_form = SpeakerForm()
     speaker_form = ndb_to_message(speaker, speaker_form)
     # Send back the SpeakerForm including the websafe key
     return speaker_form