def _createSpeakerObject(self, request): """Create a Speaker object, returning SpeakerForm/request.""" # Getting and Verifying current user user = getUser() # Confirm the field is filled out checkField(request.name, 'name') # Copy SpeakerForm/ProtoRPC Message into dict data = ({field.name: getattr(request, field.name) for field in request.all_fields()}) # Create a key for the Speaker s_id = Session.allocate_ids(size=1)[0] s_key = ndb.Key(Speaker, s_id) # Update stored session with session keys data['key'] = s_key # Create and update session and return the form Speaker(**data).put() taskqueue.add( params = { 'email' : user.email(), 'subject' : 'You Added %s as a Speaker!' % data['name'], 'body' : 'Here are the details for the added speaker:', 'info' : repr(request)}, url = '/tasks/send_confirmation_email') return request
def _createSessionObject(self, request): """Create a Session object, returning SessionForm/request.""" # Getting and Verifying current user user = getUser() # get the user_id (email) user_id = getUserId(user) # Check that the 'name' field if filed checkField(request.name, 'name') # Check that the 'parentKey' field if filed checkField(request.parentKey, 'parentKey') # Attempt to retrieve the Conference or raise an exception try: _key = ndb.Key(urlsafe=request.parentKey) except Exception: raise endpoints.BadRequestException( 'The parentKey given is invalid.') # Retrieve the Conference Obj conf = _key.get() # Verify that the current user created the conference if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the conference creator can add a session to it.') # Check speakerKey and save them speakers = [] if request.speakerKey: for speakerKey in request.speakerKey: try: speaker = ndb.Key(urlsafe=speakerKey).get() speakers.append(speaker) except Exception: raise endpoints.BadRequestException( 'Check the speakerKey it is invalid.') # Copy SessionForm/ProtoRPC Message into dict data = ({field.name: getattr(request, field.name) for field in request.all_fields()}) # If values not given for Session defaults, add defaults for df in SESSION_DEFAULTS: if data[df] in (None, []): data[df] = SESSION_DEFAULTS[df] setattr(request, df, SESSION_DEFAULTS[df]) # Converting the date info from strings to Date objects # setting the object month to the start dates month if data['date']: data['date'] = (datetime.strptime( data['date'][:10], "%Y-%m-%d").date()) data['month'] = data['date'].month else: data['month'] = conf.month # Convert startTime from string to Time object if data['startTime']: data['startTime'] = (datetime.strptime( data['startTime'][:5], "%H:%M").time()) # Convert typeOfSession to string if data['typeOfSession']: data['typeOfSession'] = str(data['typeOfSession']) # Create a session id for the Session, # create the relationship with parent key. s_id = Session.allocate_ids(size=1, parent=_key)[0] # Create the session key s_key = ndb.Key(Session, s_id, parent=_key) # Fill the session key data['key'] = s_key # Check that the speaker was passed if speakers: # Query the session for instance of speakers s = Session.query( ancestor=ndb.Key(urlsafe=request.parentKey)) # Setting none as featured featured = None # Number of session instance_count = 0 for spkr in data['speakerKey']: i_count = s.filter(Session.speakerKey == spkr).count() if i_count >= instance_count: featured = spkr minSessions = i_count # Advise of the featured Speaker using the taskQueue if featured: taskqueue.add( params = { 'websafeConferenceKey': request.parentKey, 'websafeSpeakerKey' : featured}, url = '/tasks/set_featured_speaker', method = 'GET') # Store in the DataStore Session(**data).put() # Send an email to the conference organizer taskqueue.add( params = { 'email' : user.email(), 'subject' : 'You Created a New Session for %s!' % conf.name, 'body' : 'Here are the details for new Session:', 'info' : repr(request)}, url = '/tasks/send_confirmation_email') return request