def createSessionObject(self, request): """Create or update Session object, returning SessionForm/request.""" # check if user is logged in user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # get conference object using websafeConferenceKey conf = ndb.Key(urlsafe=request.websafeConferenceKey).get() if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % request.websafeConferenceKey ) # check that user is owner of conference if user_id != conf.organizerUserId: raise endpoints.ForbiddenException( 'Only the owner can update the conference.') # convert date string to date object date_convert = request.date if date_convert: date_convert = datetime.strptime(date_convert[:10], "%Y-%m-%d").date() # saving variables in the request to Session() sess = Session() sess.name = request.name sess.highlights = request.highlights sess.speaker = request.speaker sess.duration = request.duration sess.typeOfSession = request.typeOfSession sess.date = date_convert #sess.websafeConferenceKey = request.websafeConferenceKey #sess.conferenceName = conf.name sess.startTime = request.startTime sess.endTime = request.endTime # getting sessionkey with conference as parent 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) sess.key = s_key sess.websafeKey = s_key.urlsafe() sess.put() # calling cacheFeaturedSpeaker using taskqueue taskqueue.add( params={ 'speaker': request.speaker, 'websafeConferenceKey': request.websafeConferenceKey }, url='/tasks/set_featured_speaker' ) return self._copySessionToForm(sess)
def do_login(login, password): try: user = User.objects.get(login=login) except User.DoesNotExist: return None hashed_pass = salt_and_hash(password) if user.password != hashed_pass: return None session = Session() session.key = generate_long_random_key() session.user = user session.expires = datetime.now() + timedelta(days=5) session.save() return session.key
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 _createSessionObject(self, request): """Creates a new Session object from the request.""" # fetch the user user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required.') user_id = getUserId(user) # build the conference key wsck = request.websafeConferenceKey conference = ndb.Key(urlsafe=wsck).get() if not conference: raise endpoints.BadRequestException('The conference does not exist.') p_key = ndb.Key(Profile, user_id) # make sure the conference was created by the current user if p_key != conference.key.parent(): raise endpoints.ForbiddenException('You may only create sessions for your own conferences.') # create the session and add the required and optional fields session = Session() if not request.name: raise endpoints.BadRequestException("Session 'name' field required.") session.name = request.name if not request.speaker: raise endpoints.BadRequestException("Session 'speaker' field required.") session.speaker = request.speaker if not request.date: raise endpoints.BadRequestException("Session 'date' field required") session.date = datetime.strptime(request.date, '%Y-%m-%d').date() if not request.startTime: raise endpoints.BadRequestException("Session 'startTime' field required.") session.startTime = datetime.strptime(request.startTime, '%H:%M').time() if not request.duration: raise endpoints.BadRequestException("Session 'duration' field required.") session.duration = int(request.duration) if request.typeOfSession: session.typeOfSession = request.typeOfSession if request.highlights: session.highlights = request.highlights # generate a key based on the parent conference s_id = Session.allocate_ids(size=1, parent=conference.key)[0] s_key = ndb.Key(Session, s_id, parent=conference.key) session.key = s_key # store the new session session.put() # add a new task to update the featured speaker taskqueue.add(params={'speaker': session.speaker, 'websafeConferenceKey': wsck}, url='/tasks/update_featured_speaker' ) return self._copySessionToForm(session)
def post(self): user = users.get_current_user() session = Session() session.host(user) domain = self.request.headers.get('host', 'no host') self.response.out.write(render('host.html', options({'id': session.key().id(), 'domain': domain})))