def _updateConferenceObject(self, request): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # copy ConferenceForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} # update existing conference c_key = validate_conf_key(request.websafeConferenceKey) conf = c_key.get() # check that user is owner if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can update the conference.') # Not getting all the fields, so don't create a new object; just # copy relevant fields from ConferenceForm to Conference object 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': conf.month = data.month # write to Conference object setattr(conf, field.name, data) conf.put() prof = ndb.Key(Profile, user_id).get() return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
def getConference(self, request): """Return requested conference (by websafeConferenceKey).""" # get Conference object from request; bail if not found c_key = validate_conf_key(request.websafeConferenceKey) conf = c_key.get() prof = conf.key.parent().get() # return ConferenceForm return self._copyConferenceToForm(conf, getattr(prof, 'displayName'))
def _createSessionObject(self, request): """Create Session object, returning SessionForm/request.""" # check user, if no user, raise exception user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) c_key = validate_conf_key(request.websafeConferenceKey) conf = c_key.get() # check that user is owner if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can create the session.') if not request.name: raise endpoints.BadRequestException("Session 'name' field required") # copy SessionForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} del data['websafeSessionKey'] del data['websafeConferenceKey'] # 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]) # convert dates from strings to Date or time objects; if data['startDateTime']: temp = str(data['startDateTime']) data['startDateTime'] = datetime.strptime(temp, "%Y-%m-%d %H:%M:%S") else: # TODO debug code for query testing data['startDateTime'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S") # generate Profile Key based on user ID and Session # ID based on Profile key get Session key from ID c_key = ndb.Key(urlsafe=request.websafeConferenceKey) s_id = Session.allocate_ids(size=1, parent=c_key)[0] s_key = ndb.Key(Session, s_id, parent=c_key) data['key'] = s_key # create Session, return (modified) SessionForm Session(**data).put() session = s_key.get() wssk = s_key.urlsafe() taskqueue.add(params={'sessionKey': wssk, 'speaker': session.speaker}, url='/tasks/set_speaker_announcement' ) return self._copySessionToForm(session)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None prof = self._getProfileFromUser() # get user Profile # check if conf exists given websafeConfKey # get conference; check that it exists wsck = request.websafeConferenceKey c_key = validate_conf_key(wsck) conf = c_key.get() # register if reg: # check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException( "There are no seats available.") # register user, take away one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if wsck in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)