def _updateConferenceObject(self, request): # preload necessary data items user, userId, userDisplayName, profileKey = currentUser() conference, conferenceKey = self._getConferenceFromWebsafeKey(request.websafeKey) # Verify if the user is the organizer of the conference if profileKey != conference.key.parent(): raise endpoints.UnauthorizedException("You must be the owner of the conference to update it.") # update existing conference 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": conference.month = data.month setattr(conference, field.name, data) conference.put() return self._copyConferenceToForm(conference, userDisplayName)
def getConferencesCreated(self, request): """ Return conferences created by user.""" user, userId, userDisplayName, userProfileKey = currentUser() conferences = Conference.query(ancestor=userProfileKey) # return set of ConferenceForm objects per Conference return ConferenceForms(items=[self._copyConferenceToForm(conf, userDisplayName) for conf in conferences])
def _createConferenceObject(self, request): """Create a Conference object, returning ConferenceForm/request.""" if not request.name: raise endpoints.BadRequestException("Conference 'name' field required") # preload necessary data items user, userId, userDisplayName, profileKey = currentUser() # copy ConferenceForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} del data["websafeKey"] del data["organizerDisplayName"] del data["seatsAvailable"] # add default values for those missing (both data model & outbound Message) for df in CONFERENCE_DEFAULTS: if data[df] in (None, []): data[df] = CONFERENCE_DEFAULTS[df] setattr(request, df, CONFERENCE_DEFAULTS[df]) # convert dates from strings to Date objects; set month based on start_date if data["startDate"]: data["startDate"] = datetime.strptime(data["startDate"][:10], "%Y-%m-%d").date() data["month"] = data["startDate"].month else: data["month"] = 0 if data["endDate"]: data["endDate"] = datetime.strptime(data["endDate"][:10], "%Y-%m-%d").date() # set seatsAvailable to be the same as maxAttendees on creation # both for data model & outbound Message if data["maxAttendees"] > 0: data["seatsAvailable"] = data["maxAttendees"] setattr(request, "seatsAvailable", data["maxAttendees"]) # allocate new Conference ID with profileKey as parent conferenceId = Conference.allocate_ids(size=1, parent=profileKey)[0] # make Conference key from ID conferenceKey = ndb.Key(Conference, conferenceId, parent=profileKey) data["key"] = conferenceKey data["organizerUserId"] = request.organizerUserId = userId # create Conference, send email to organizer confirming # creation of Conference & return (modified) ConferenceForm Conference(**data).put() taskqueue.add( params={"email": user.email(), "conferenceInfo": repr(request)}, url="/tasks/send_confirmation_email" ) return self._copyConferenceToForm(conferenceKey.get(), userDisplayName)
def _createSessionObject(self, request): """ Create or update Session object, returning SessionForm/request.""" if not request.name: raise endpoints.BadRequestException("Session 'name' field required") # preload necessary data items user, userId, userDisplayName, profileKey = currentUser() conference, conferenceKey = self._getConferenceFromWebsafeKey(request.websafeKey) # Verify if the user is the organizer of the conference if profileKey != conference.key.parent(): raise endpoints.UnauthorizedException("You must be the organizer of the conference to create its session.") # copy SessionForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} # add default values for those missing (both data model & outbound Message) if data["startTime"] and not data["endTime"]: data["endTime"] = data["startTime"] for df in SESSION_DEFAULTS: if data[df] in (None, []): data[df] = SESSION_DEFAULTS[df] setattr(request, df, SESSION_DEFAULTS[df]) # convert dates from strings to Date and Time objects; data["date"] = datetime.strptime(data["date"], "%Y-%m-%d") data["startTime"] = datetime.strptime(data["startTime"], "%H:%M:%S") data["endTime"] = datetime.strptime(data["endTime"], "%H:%M:%S") if data["startTime"] > datetime.strptime("19:00:00", "%H:%M:%S"): data["lateSession"] = True else: data["lateSession"] = False del data["speakerKey"] del data["websafeKey"] del data["duration"] # allocate session's id by setting conference key as a parent sessionId = Session.allocate_ids(size=1, parent=conferenceKey)[0] sessionKey = ndb.Key(Session, sessionId, parent=conferenceKey) data["key"] = sessionKey # Update the one to many relationship between speaker and session speaker, speakerKey = self._getSpeakerKey(request.speakerKey) data["speaker"] = speaker.key speaker.sessions.append(sessionKey) speaker.put() # create Session object and put it into DB Session(**data).put() # Add to a task queue the task to set memcache about featured speakers taskqueue.add( params={"speaker_websafeKey": request.speakerKey, "conference_websafeKey": request.websafeKey}, url="/tasks/setFeaturedSpeaker", ) # return SessionForm object return self._copySessionToForm(sessionKey.get())