def _copySessionToForm(self, session, name=None): """Copy relevant fields from Session to SessionForm.""" sf = SessionForm() for field in sf.all_fields(): if hasattr(session, field.name): if field.name == "dateTime": s_date = getattr(session,field.name) if s_date: setattr(sf, 'dateTime', s_date.strftime('%y-%m-%d')) setattr(sf, 'startTime', s_date.strftime('%H:%M')) elif field.name == "speaker": speakerID = getattr(session,field.name) # get speaker object if there is one assigend if speakerID == None: setattr(sf, 'speakerDisplayName', 'NONE ASSIGNED') else: speak = Speaker.get_by_id(speakerID) setattr(sf, 'speakerDisplayName', speak.name) #set typeOfSession #if there is no typeOfSession then set it to 'NOT_SPECIFIED' elif field.name == "typeOfSession": currentType = getattr(session,field.name) if currentType: setattr(sf, field.name, getattr(SessionType, str(currentType))) else: setattr(sf, field.name, getattr(SessionType, 'NOT_SPECIFIED')) else: setattr(sf, field.name, getattr(session,field.name)) sf.check_initialized() return sf
def _copySessionToForm(self, session, name=None): """Copy relevant fields from Session to SessionForm.""" sf = SessionForm() for field in sf.all_fields(): if hasattr(session, field.name): if field.name == "dateTime": s_date = getattr(session, field.name) if s_date: setattr(sf, 'dateTime', s_date.strftime('%y-%m-%d')) setattr(sf, 'startTime', s_date.strftime('%H:%M')) elif field.name == "speaker": speakerID = getattr(session, field.name) # get speaker object if there is one assigend if speakerID == None: setattr(sf, 'speakerDisplayName', 'NONE ASSIGNED') else: speak = Speaker.get_by_id(speakerID) setattr(sf, 'speakerDisplayName', speak.name) #set typeOfSession #if there is no typeOfSession then set it to 'NOT_SPECIFIED' elif field.name == "typeOfSession": currentType = getattr(session, field.name) if currentType: setattr(sf, field.name, getattr(SessionType, str(currentType))) else: setattr(sf, field.name, getattr(SessionType, 'NOT_SPECIFIED')) else: setattr(sf, field.name, getattr(session, field.name)) sf.check_initialized() return sf
def post(self): """Check and Set as Featured Speaker if appropriate""" speaker = self.request.get('speakerId') # Gather all sessions by this speaker fsessions = Session.query(Session.speakerId==str(speaker))\ .fetch(projection=[Session.name]) #If more than 1 session, then make featured speaker if len(fsessions) > 1: # Lookup Speaker speaker = Speaker.get_by_id(int(speaker),) # Set the featured speaker and sessions in memcache announcement = FEATURED_SPEAKER_TPL % (speaker.displayName, ', '.join(session.name for session in fsessions)) memcache.set(MEMCACHE_FEATURED_SPEAKER, announcement)
def _copySessionToForm(self, sesh): """Copy relevant fields from Session to SessionForm.""" sf = SessionForm() for field in sf.all_fields(): if hasattr(sesh, field.name): # convert date and startTime to string; just copy others if field.name == 'date' or field.name == 'startTime': setattr(sf, field.name, str(getattr(sesh, field.name))) else: setattr(sf, field.name, getattr(sesh, field.name)) elif field.name == "websafeKey": setattr(sf, field.name, sesh.key.urlsafe()) # query speaker by the speaker_id and add values to form if sesh.speakerId: speaker = Speaker.get_by_id(sesh.speakerId) sf.speaker_name = speaker.name sf.speaker_email = speaker.email sf.speaker_gender = speaker.gender sf.check_initialized() return sf
def _createSessionObject(self, request): """Create or update a Session 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") # fetch and check conference conf = ndb.Key(urlsafe=request.websafeConferenceKey).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.') data = {field.name: getattr(request, field.name) for field in request.all_fields()} # convert date and time fields to the correct types if data['dateTime']: s_date = datetime.strptime(data['dateTime'], '%Y-%m-%d %H:%M') data['dateTime'] = s_date data['startTime'] = datetime.strftime(s_date, '%H:%M') # set type of session if it's not supplied set it to 'NOT_SPECIFIED' if data['typeOfSession']: data['typeOfSession'] = str(data['typeOfSession']) else: data['typeOfSession'] = 'NOT_SPECIFIED' # delete the websafeConferenceKey it will be the parent and does # not need to be saved as an entity del data['websafeConferenceKey'] # delete speakerDisplayName it is not stored in te database del data['speakerDisplayName'] # add default values for those missing (both data model & outbound Message) for df in SESSION_DEFAULTS: if data[df] in (None, []): data[df] = SESSION_DEFAULTS[df] setattr(request, df, SESSION_DEFAULTS[df]) s_id = Conference.allocate_ids(size=1, parent=conf.key)[0] s_key = ndb.Key(Session, s_id, parent=conf.key) data['key'] = s_key Session(**data).put() # - - - Task 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - # after adding the data check if the speaker has mre than one session # at the conference. If they do add a Memcache entry for them # first get all sesiions for the conference then filter by speaker # if there is more than one session add to Memcache confSessions = Session.query(ancestor=conf.key) speakerSessions = confSessions.filter(Session.speaker == data['speaker']) # speakerSessions = confSessions sessionCount = speakerSessions.count() if sessionCount > 1: # get the speaker object from id speak = Speaker.get_by_id(data['speaker']) taskqueue.add( params={'speakerName': speak.name}, url='/tasks/add_featured_speaker' ) return request
def _createSessionObject(self, request): """Create or update a Session 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") # fetch and check conference conf = ndb.Key(urlsafe=request.websafeConferenceKey).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.') data = { field.name: getattr(request, field.name) for field in request.all_fields() } # convert date and time fields to the correct types if data['dateTime']: s_date = datetime.strptime(data['dateTime'], '%Y-%m-%d %H:%M') data['dateTime'] = s_date data['startTime'] = datetime.strftime(s_date, '%H:%M') # set type of session if it's not supplied set it to 'NOT_SPECIFIED' if data['typeOfSession']: data['typeOfSession'] = str(data['typeOfSession']) else: data['typeOfSession'] = 'NOT_SPECIFIED' # delete the websafeConferenceKey it will be the parent and does # not need to be saved as an entity del data['websafeConferenceKey'] # delete speakerDisplayName it is not stored in te database del data['speakerDisplayName'] # add default values for those missing (both data model & outbound Message) for df in SESSION_DEFAULTS: if data[df] in (None, []): data[df] = SESSION_DEFAULTS[df] setattr(request, df, SESSION_DEFAULTS[df]) s_id = Conference.allocate_ids(size=1, parent=conf.key)[0] s_key = ndb.Key(Session, s_id, parent=conf.key) data['key'] = s_key Session(**data).put() # - - - Task 4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - # after adding the data check if the speaker has mre than one session # at the conference. If they do add a Memcache entry for them # first get all sesiions for the conference then filter by speaker # if there is more than one session add to Memcache confSessions = Session.query(ancestor=conf.key) speakerSessions = confSessions.filter( Session.speaker == data['speaker']) # speakerSessions = confSessions sessionCount = speakerSessions.count() if sessionCount > 1: # get the speaker object from id speak = Speaker.get_by_id(data['speaker']) taskqueue.add(params={'speakerName': speak.name}, url='/tasks/add_featured_speaker') return request