def deleteConference(self, request): """Delete conference.""" wsck = request.websafeConferenceKey conf = self._retrieveConference(wsck) if not conf: raise endpoints.NotFoundException('No conference found with key: %s' % wsck) # Remove the link between the sessions and this conference conf_sess = ConferenceSession.query(ancestor=conf.key) if conf_sess: for sess in conf_sess: wssk = sess.key.urlsafe() # Get all the wishlists that have this session in them and remove this session from them wishes = Wishlist.query(ndb.AND(Wishlist.sessions == wssk)) for wish in wishes: if wish and wssk in wish.sessions: wish.sessions.remove(wssk) wish.put() sess.key.delete() # Unregister the users from this conference if they are registered for it registered_users = Profile.query(ndb.AND(Profile.conferenceKeysToAttend == wsck)) if registered_users: for reg_user in registered_users: if reg_user and wsck in reg_user.conferenceKeysToAttend: reg_user.conferenceKeysToAttend.remove(wsck) reg_user.put() conf.key.delete() return BooleanMessage(data=True)
def getSessionsFromSpeakerAndConference(self, request): '''Given a speaker and a conference return all sessions that have that speaker''' # get speaker key and make sure we have a speaker speaker_key = self.get_websafe_key( request.websafeSpeakerKey, "ConferenceSpeaker") speaker = speaker_key.get() if not speaker: raise endpoints.NotFoundException( "The speaker you are looking for was not found!") # get conference key and make sure we have a conference conf_key = self.get_websafe_key( request.websafeConferenceKey, "Conference") conference = conf_key.get() if not conference: raise endpoints.NotFoundException( "The conference you are looking for was not found!") q = ConferenceSession.query(ancestor=conf_key) q = q.filter(ConferenceSession.speakerKey == speaker_key) sessions = q.fetch(50) return mm.ConferenceSessionForms( items=[sessions[i].to_form(speaker) for i in range(len(sessions))])
def getAllSessionsByType(self, request): """Given a conference, return all sessions of a specified type (eg lecture, keynote, workshop)""" sessions = ConferenceSession.query(ndb.AND(ConferenceSession.typeOfSession == request.sessionType)) # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs) for cs in sessions] )
def getConferenceSessionsByType(self, request): ''' Create Session to Conference, open only to the conference Organizer''' # Limit the amount of types one can include: if len(request.typeOfSession) > 5: raise endpoints.BadRequestException( "Maximum number of typeOfSession to include is 5") # get the conference confKey = self.get_websafe_key( request.websafeConferenceKey, "Conference") conf = confKey.get() if not conf: raise endpoints.BadRequestException( "This conference does not exist: %s" % confKey) # query ConferenecSession q = ConferenceSession.query(ancestor=confKey) types = request.typeOfSession if types: q = q.filter(ConferenceSession.type.IN(types)) q = q.order(ConferenceSession.name) sessions = q.fetch(100) speaker_keys = [] for sess in q: speaker_keys.append(sess.speakerKey) speakers = ndb.get_multi(speaker_keys) return mm.ConferenceSessionForms( items=[sessions[i].to_form(speakers[i]) for i in range(len(sessions))])
def _cacheSpeakerAndSession(speaker_key, conf_key): """Assign Speaker and their Session to memcache; used by memcache cron job """ session_message = "" # Make sure the conference and speaker are both valid if conf_key and speaker_key: conf = ndb.Key(urlsafe=conf_key).get() speaker = ndb.Key(urlsafe=speaker_key).get() if conf and speaker: # Now retrieve all sessions for that conference at which this speaker will speak sessions = ConferenceSession.query(ancestor=conf.key)\ .filter(ndb.AND(ConferenceSession.speakerUserId == speaker_key))\ .fetch(projection=[Conference.name]) # If this speaker is supposed to speak at multiple sessions, # throw a summary of all their sessions into the task queue if sessions and len(sessions) > 1: session_message = '%s %s %s' % ( speaker.displayName, 'will be speaking at the following sessions: ', ', '.join(sess.name for sess in sessions)) memcache.set(speaker.mainEmail, session_message) else: memcache.delete(speaker.mainEmail) elif not conf: logging.error('No conference found with key: %s' % conf_key) elif not speaker: logging.error('No speaker found with key: %s' % speaker_key) elif not conf_key: logging.error('No conference key was provided') elif not speaker_key: logging.error('No speaker key was provided') return session_message
def getSessionsBySpeaker(self, request): """Query for sessions with a given speaker""" q = ConferenceSession.query(ConferenceSession.speakers.name == request.websafeSpeaker) q.order(ConferenceSession.start_time) return ConferenceSessionForms( items=[self._copySessionToForm(sess) for sess in q] )
def getConferenceSessionsByFilters(self, request): """Query for the sessions that are not specified type and are not running after specified time """ # first get the sessions that don't have the specified type # then get the sessions that don't run past the specified time type_f = request.websafeType print type_f print '------------------------------' time_f = request.websafeTime print time_f print '------------------------------' eq_f = request.websafeOperator print eq_f print '------------------------------' q = ConferenceSession.query(ConferenceSession.type != type_f) print q sessions=[] for sess in q: if sess.start_time is not None: if eq_f == '<': if sess.start_time < datetime.strptime(time_f, '%H:%M').time(): # add the session to the sessions sessions.append(sess) elif eq_f == '=': if sess.start_time == datetime.strptime(time_f, '%H:%M').time(): # add the session to the sessions sessions.append(sess) elif eq_f == '>': if sess.start_time > datetime.strptime(time_f, '%H:%M').time(): # add the session to the sessions sessions.append(sess) return ConferenceSessionForms( items=[self._copySessionToForm(s) for s in sessions] )
def getSessionsBySpeaker(self, request): """Return a list of sessions associated with this conference limited by session speaker.""" sessions = ConferenceSession.query(ConferenceSession.speaker == request.speakerPath) # return all matched Sessions as SessionForms return SessionForms( items=[self._copySessionToForm(sess, sess.websafeConferenceKey) for sess in sessions] )
def getSessionsBySpeaker(self, request): """Retrieves sessions matching the request query""" q = ConferenceSession.query( ConferenceSession.speaker == request.speaker) return ConferenceSessionForms( items=[self._copyConferenceSessionToForm(cs) for cs in q] )
def getSessionsBySpeaker(self, request): """Given a speaker, return all sessions given by this particular speaker, across all conferences""" conf_sess = ConferenceSession.query(ndb.AND(ConferenceSession.speakerUserId == request.speakerUserId)) speaker = self._getSpeaker(request.speakerUserId) # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs, getattr(speaker, 'displayName')) for cs in conf_sess] )
def getAllSessions(self, request): """return all sessions""" conf_sess = ConferenceSession.query() # return set of ConferenceSessionForm objects return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs) for cs in conf_sess] )
def getConferenceSessions(self, request): """Query for sessions, given a conference""" # query for the conference, and then query for the sessions by conference key conf = ndb.Key(urlsafe=request.websafeConferenceKey).get() q = ConferenceSession.query(ancestor=conf.key) q.order(ConferenceSession.start_time) return ConferenceSessionForms( items=[self._copySessionToForm(sess) for sess in q] )
def getConferenceSessionsByType(self, request): """Query for sessions, given the type""" conf = ndb.Key(urlsafe=request.websafeConferenceKey).get() q = ConferenceSession.query(ancestor=conf.key) q = q.filter(ConferenceSession.type == request.websafeType) q.order(ConferenceSession.start_time) return ConferenceSessionForms( items=[self._copySessionToForm(sess) for sess in q] )
def getConferenceSessionsByConfId(self, request): """Retrieves sessions matching the request query""" q = ConferenceSession.query(ancestor=ndb.Key( urlsafe=request.conferenceKey)) return ConferenceSessionForms( items=[self._copyConferenceSessionToForm(cs) for cs in q] )
def getConferenceSessionsByHighlight(self, request): """Query for sessions with the given highlight""" q = ConferenceSession.query(ConferenceSession.highlights.IN([request.websafeHighlight])) if q is not None: return ConferenceSessionForms( items=[self._copySessionToForm(sess) for sess in q] ) return ConferenceSessionForms( items=[] )
def post(self): """Establish if a speaker is duplicated and adds it to the Featured Speaker memcache if it is.""" confKey = ndb.Key(urlsafe=self.request.get('websafeConferenceKey')) sessions = ConferenceSession.query(ancestor=confKey).filter(ConferenceSession.speaker == self.request.get('speaker')) if(sessions.count() > 1): featured_speakers = '%s %s %s' % ( 'The featured speaker ', self.request.get('speaker'), ' is hosting the following sessions:' ' '.join(sess.name for sess in sessions)) memcache.set(MEMCACHE_FEATURED_SPEAKERS_KEY, featured_speakers)
def getConferenceSessionsByMaxDuration(self, request): """Return a list of sessions associated with this conference limited by MAX duration.""" sessions = ConferenceSession.query(ndb.AND(ConferenceSession.websafeConferenceKey == request.websafeConferenceKey,ConferenceSession.duration <= request.duration)) if not sessions: raise endpoints.NotFoundException( 'No sessions found with key: %s' % request.websafeConferenceKey) # return all matched Sessions as SessionForms return SessionForms( items=[self._copySessionToForm(sess, request.websafeConferenceKey) for sess in sessions] )
def getConferenceSessions(self, request): """Return a list of sessions associated with this conference.""" confKey = ndb.Key(urlsafe=request.websafeConferenceKey) sessions = ConferenceSession.query(ancestor=confKey) if not sessions: raise endpoints.NotFoundException( 'No sessions found with key: %s' % request.websafeConferenceKey) # return all matched Sessions as SessionForms return SessionForms( items=[self._copySessionToForm(sess, request.websafeConferenceKey) for sess in sessions] )
def getConferenceSessionsByType(self, request): """Retrieves sessions matching the request query""" q = ConferenceSession.query(ancestor=ndb.Key( urlsafe=request.conferenceKey)) q = q.filter( ConferenceSession.typeOfSession == str( request.typeOfSession)) return ConferenceSessionForms( items=[self._copyConferenceSessionToForm(cs) for cs in q] )
def getConferenceSessionsByType(self, request): """Given a conference, return all sessions of a specified type (eg lecture, keynote, workshop)""" conf = self._retrieveConference(request.websafeConferenceKey) if not conf: raise endpoints.NotFoundException('No conference found with key: %s' % request.websafeConferenceKey) sessions = ConferenceSession.query(ancestor=conf.key)\ .filter(ndb.AND(ConferenceSession.typeOfSession == request.sessionType)) # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs) for cs in sessions] )
def getConferenceSessions(self, request): """Given a conference, return all sessions""" profile = self._getUserProfile() conf = self._retrieveConference(request.websafeConferenceKey) if not conf: raise endpoints.NotFoundException('No conference found with key: %s' % request.websafeConferenceKey) conf_sess = ConferenceSession.query(ancestor=conf.key) # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs) for cs in conf_sess] )
def _setFeaturedSpeaker(conference_key, speaker_name): """ Checks whether the speaker is a featured speaker or not and if they are, will set them into memcache with the sessions they speak in :param conference_key: the conference key :param speaker_name: the speaker name """ conf = ndb.Key(urlsafe=conference_key).get() q = ConferenceSession.query(ancestor=conf.key) q = q.filter(ConferenceSession.speakers.name.IN([speaker_name])) session_names=[sess.name for sess in q] if q.count(limit=2) > 1: memcache.set(MEMCACHE_FEATURED_SPEAKER_KEY, speaker_name) memcache.set(MEMCACHE_FEATURED_SESSIONS_KEY, session_names)
def _updateFeaturedSpeaker(speaker, conferenceKey): """Updates the featured speaker in memcache.""" conf = ndb.Key(urlsafe=conferenceKey) if conf.kind() != "Conference" or not conf.get(): raise endpoints.NotFoundException( 'No conference found with key: %s' % conferenceKey) q = ConferenceSession.query(ancestor=ndb.Key( urlsafe=conferenceKey)) q = q.filter(ConferenceSession.speaker == speaker) if q.count() > 1: fskey = 'featuredSpeaker-' + conferenceKey memcache.set(fskey, (speaker, [qi.name for qi in q]))
def getConferenceSessionsCreated(self, request): """Return conference sessions created by user.""" # make sure user is authed user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # create ancestor query for all key matches for this user confs = ConferenceSession.query(ancestor=ndb.Key(Profile, user_id)) prof = ndb.Key(Profile, user_id).get() # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self._copyConferenceSessionToForm(conf) for conf in confs] )
def getConferenceSpeakers(self, request): """Gets all speakers at the desired conference.""" conf = ndb.Key(urlsafe=request.conferenceKey) if conf.kind() != "Conference" or not conf.get(): raise endpoints.NotFoundException( 'No conference found with key: %s' % request.conferenceKey) q = ConferenceSession.query( ancestor=ndb.Key( urlsafe=request.conferenceKey)) q = q.fetch(projection=[ConferenceSession.speaker]) return GetConferenceSpeakersResponse( speakers=list({s.speaker for s in q}) )
def getDaytimeNonWorkshopSessions(self, request): """Get sessions before 7pm and non-worksop""" conf_sess = ConferenceSession.query(ConferenceSession.typeOfSession.IN([ str(SessionType.UNKNOWN), str(SessionType.LECTURE), str(SessionType.KEYNOTE), str(SessionType.MEETUP) ])) no_early_workshop = conf_sess.filter(ConferenceSession.startTime < time(19,00)) if no_early_workshop: # return set of ConferenceSessionForm objects per ConferenceSession return ConferenceSessionForms( items=[self.toConferenceSessionForm(cs) for cs in no_early_workshop] ) else: return ConferenceSessionForms( items=[self.toConferenceSessionForm()] )
def _getFeaturedSpeaker(speaker, websafeConferenceKey): # Check to see if Speaker exists in other sessions in this conference qry1 = ConferenceSession.query() qry2 = qry1.filter(ConferenceSession.speaker == speaker) # Filter on speaker qry3 = qry2.filter(ConferenceSession.websafeConferenceKey == websafeConferenceKey) # Filter on websafeConferenceKey if qry3: # If speaker is in another session in this conference # format sessionSpeakerList and set it in memcache sessionSpeakerList = SESSIONS_TPL % (speaker, ', '.join(conf.name for conf in qry3)) memcache.set(MEMCACHE_SESSIONS_KEY, sessionSpeakerList) else: # If no speaker is in another session in this conference # delete the memcache announcements entry sessionSpeakerList = "" memcache.delete(MEMCACHE_SESSIONS_KEY) return sessionSpeakerList
def _queryproblem(self, request): ''' session query method to search for unavailable after a certain time (in int hour blocks) and exclude up to 3 types of sessions ''' # to reduce friction we will only allow 3 excludes if len(request.exclude) > 3: raise endpoints.BadRequestException( "You are only allowed to exclude up to 3 types of Sessions.") # list of all allowed timeslots # ideally this list is created in order from the most popular session # times allowed_timeslots = [i for i in range(24)] # compose a list of unavailable times if request.afterTime: dissalowed_timeslots = [i for i in xrange(request.afterTime, 24)] else: dissalowed_timeslots = [] # exclude dissalowedtimeslots query_times = [ i for i in allowed_timeslots if i not in dissalowed_timeslots] q = ConferenceSession.query() q = q.filter(ConferenceSession.startTimeSlot.IN(query_times)) # filter out all excludes for s_type in request.exclude: q = q.filter(ConferenceSession.type != s_type) # order by conference type first since that is the inequality filter q.order(ConferenceSession.type) q.order(ConferenceSession.startTime) # fetch max 100 records sessions = q.fetch(100) speaker_keys = [] for sess in q: speaker_keys.append(sess.speakerKey) # get speakers for every session in order speakers = ndb.get_multi(speaker_keys) return mm.ConferenceSessionForms( items=[sessions[i].to_form(speakers[i]) for i in range(len(sessions))])
def removeSpeaker(self, request): """Remove the Speaker.""" wssk = request.websafeSpeakerKey spkr = ndb.Key(urlsafe=wssk).get() if not spkr: raise endpoints.NotFoundException('No speaker found with key: %s' % wssk) sessions = ConferenceSession.query(ndb.AND( ConferenceSession.speakerUserId == request.websafeSpeakerKey) ).fetch() # Remove the association this speaker has with any sessions if sessions: for session in sessions: session.speakerUserId = None session.put() spkr.key.delete() return BooleanMessage(data=True)
def getConferenceSpeakers(self, request): """Return users assigned as speakers for a given conference's sessions.""" conf = self._retrieveConference(request.websafeConferenceKey) if not conf: raise endpoints.NotFoundException('No conference found with key: %s' % request.websafeConferenceKey) # get all the sessions for this conference conf_sess = ConferenceSession.query(ancestor=conf.key) if not conf_sess: raise endpoints.NotFoundException('No sessions were found for this conference') # get all the speakers for this conference speaker_ids = [sess.speakerUserId for sess in conf_sess if sess is not None and sess.speakerUserId is not None] if not len(speaker_ids): raise endpoints.NotFoundException('No speakers are listed for any of this conference sessions') return SpeakerForms( speakers=[self.toSpeakerForm( self._getSpeaker(speaker) ) for speaker in set(speaker_ids)] )