def getConferencesCreated(self, request): """Return conferences 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 = Conference.query(ancestor=ndb.Key(Profile, user_id)) prof = ndb.Key(Profile, user_id).get() # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, getattr(prof, 'displayName')) for conf in confs ])
def getUpcomingConferences(self, request): """Get upcoming conferences""" conferences = Conference.query(Conference.startDate >= datetime.today()) \ .order(Conference.startDate) \ .fetch(limit=10) # limit number of conferences to 10 organizer_keys = [ndb.Key(Profile, conference.organizerUserId) for conference in conferences] profiles = ndb.get_multi(organizer_keys) organizer_dict = {profile.key.id(): profile.displayName for profile in profiles} return ConferenceForms( items=[self._copyConferenceToForm(conference, organizer_dict[conference.organizerUserId]) for conference in conferences] )
def getConferencesCreated(self, requset): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization Required') #Make Profile Key p_key = ndb.Key(Profile, getUserID(user)) #create ancestor query for this user conferences = Conference.query(ancestor=p_key) # get the user profile and display name prof = p_key.get() displayName = getattr(prof, 'displayName') # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, displayName) for conf in conferences ])
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" # TODO: # step 1: get user profile prof = self._getProfileFromUser() # step 2: get conferenceKeysToAttend from profile. # to make a ndb key from websafe key you can use: # ndb.Key(urlsafe=my_websafe_key_string) keys = [ndb.Key(urlsafe=k) for k in prof.conferenceKeysToAttend] # step 3: fetch conferences from datastore. # Use get_multi(array_of_keys) to fetch all keys at once. # Do not fetch them one by one! conferences = ndb.get_multi(keys) # return set of ConferenceForm objects per Conference return ConferenceForms(items=[self._copyConferenceToForm(conf, "")\ for conf in conferences] )
def queryConferences(self, request): """Query for conferences.""" conferences = self._getQuery(request) # need to fetch organiser displayName from profiles # get all keys and use get_multi for speed organisers = ([(ndb.Key(Profile, conf.organizerUserId)) for conf in conferences]) profiles = ndb.get_multi(organisers) # put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # return individual ConferenceForm object per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, names[conf.organizerUserId]) for conf in conferences ])
def queryConferences(self, request): conferences = self._getQuery(request) organisers = [(ndb.Key(Profile, conf.organizerUserId)) for conf in conferences] profiles = ndb.get_multi(organisers) # put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # return individual ConferenceForm object per Conference return ConferenceForms( items=[self._copyConferenceToForm(conf, names[conf.organizerUserId]) for conf in \ conferences] )
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" # step 1: get user profile prof = self._getProfileFromUser() # step 2: get conferenceKeysToAttend from profile. wscks = prof.conferenceKeysToAttend # step 3: fetch conferences from datastore. ds_keys = [ndb.Key(urlsafe=wsck) for wsck in wscks] conferences = ndb.get_multi(ds_keys) # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, "") for conf in conferences ])
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" prof = self._getProfileFromUser() keys = prof.conferenceKeysToAttend listofkeys = [] for i in keys: confFromNdb = ndb.Key(urlsafe=i) listofkeys.append(confFromNdb) conferences = ndb.get_multi(listofkeys) return ConferenceForms(items=[self._copyConferenceToForm(conf, "")\ for conf in conferences] )
def getlowReg(self, request): """Return conferences that are less than half full.""" # create list to stored conferences to be returned and then # fetch all conferences confsToReturn = [] confs = Conference.query() # loop through all conferences adding matching entries to the list for c in confs: maxAttendees = c.maxAttendees seatsAvailable = c.seatsAvailable if seatsAvailable > maxAttendees / 2: confsToReturn += [c] return ConferenceForms(items=[ self._copyConferenceToForm( conf, getattr(conf.key.parent().get(), 'displayName')) for conf in confsToReturn ])
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" prof = self._getProfileFromUser() # get user Profile conf_keys = [ndb.Key(urlsafe=wsck) for wsck in prof.conferenceKeysToAttend] conferences = ndb.get_multi(conf_keys) # get organizers organisers = [ndb.Key(Profile, conf.organizerUserId) for conf in conferences] profiles = ndb.get_multi(organisers) # put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # return set of ConferenceForm objects per Conference return ConferenceForms(items=[self._copyConferenceToForm(conf, names[conf.organizerUserId]) for conf in conferences])
def filterPlayground(self, request): q = Conference.query() # simple filter usage: # q = q.filter(Conference.city == "Paris") q = q.filter(Conference.city == "London") #q = q.filter(Conference.topics == "Medical Innovations") #q = q.filter(Conference.month == 6) # TODO # add 2 filters: # 1: city equals to London # 2: topic equals "Medical Innovations" return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])
def getConferencesByTopicSearch(self, request): """Get list of conferences matching one or more of the given topics.""" conferences = self._getConferencesByTopicSearch(request) # Need to fetch organiser displayName from profiles # Get all keys and use get_multi for speed organisers = [(ndb.Key(Profile, conf.organizerUserId)) for conf in conferences] profiles = ndb.get_multi(organisers) # Put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # Return individual ConferenceForm object per Conference # Return individual ConferenceForm object per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, names[conf.organizerUserId]) for conf in conferences ])
def filterPlayground(self, request): q = Conference.query() # city equals to London q = q.filter(Conference.city == "London") # order by conference name q = q.order(Conference.name) # advanced filter building and usage # field = "city" # operator = "=" # value = "London" # f = ndb.query.FilterNode(field, operator, value) # q = q.filter(f) return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" # Get user profile prof = self._getProfileFromUser() # Get conferenceKeysToAttend from profile. array_of_keys = [] for i in (prof.conferenceKeysToAttend): # make a ndb key from websafe key array_of_keys.append(ndb.Key(urlsafe=i)) # Fetch conferences from datastore. conferences = ndb.get_multi(array_of_keys) # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, "") for conf in conferences ])
def getNoSessions(self, request): """Return conferences that have no sessions.""" # create list to stored conferences to be returned and then # fetch all conferences confsToReturn = [] confs = Conference.query() # loop through all conferences adding matching conferences # with no sessions for c in confs: sessions = Session.query(ancestor=c.key) if sessions.count() == 0: confsToReturn += [c] return ConferenceForms(items=[ self._copyConferenceToForm( conf, getattr(conf.key.parent().get(), 'displayName')) for conf in confsToReturn ])
def getConferencesCreated(self, request): """Return conferences created by user.""" # Getting and Verifying current user user = getUser() # get the user_id (email) user_id = getUserId(user) # create ancestor query for all key matches for this user conferences = Conference.query(ancestor=ndb.Key(Profile, user_id)) prof = ndb.Key(Profile, user_id).get() # return one or many ConferenceForm objects return ConferenceForms( items = [self._copyConferenceToForm( conf, getattr(prof, 'displayName')) for conf in conferences])
def getConferencesCreated(self, request): """Return conferences created by user.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') p_key = ndb.Key(Profile, getUserId(user)) conferences = Conference.query(ancestor=p_key) prof = p_key.get() displayName = getattr(prof, 'displayName') return ConferenceForms( items=[self._copyConferenceToForm(conf, displayName) for conf in conferences] )
def filterPlayground(self, request): """Return conferences created by user.""" # make sure user is authed user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') #delete everything in users wishlist prof = self._getProfileFromUser() # get user Profile #print "******* %s" ,prof.wishListSessionKeys prof.wishListSessionKeys[:] = [] conferences = Conference.query() conferences = conferences.filter(Conference.city == "London") # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, "") for conf in conferences ])
def filterPlayground(self, request): q = Conference.query() # TODO # add 2 filters: # 1: city equals to London q = q.filter(Conference.city == "London") # 2: topic equals "Medical Innovations" q = q.filter(Conference.topics == "Medical Innovations") # 3: order by conference name q = q.order(Conference.name) # first for month of June q = q.filter(Conference.maxAttendees > 10) return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])
def getConferencesByTopic(self, request): """Return all conferences on a given topic""" confs = Conference.query() confs = confs.filter(Conference.topics == request.topic) # get organizers organisers = [ndb.Key(Profile, conf.organizerUserId) for conf in confs] profiles = ndb.get_multi(organisers) # put display names in a dict for easier fetching names = {} for profile in profiles: names[profile.key.id()] = profile.displayName # return set of ConferenceForm objects per Conference return ConferenceForms(items=[self._copyConferenceToForm( conf, names[conf.organizerUserId])\ for conf in confs] )
def get_conferences_created(self, request): """Return conferences created by user.""" # make sure user is authed user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # make profile key p_key = ndb.Key(Profile, get_user_id(user)) # create ancestor query for this user conferences = Conference.query(ancestor=p_key) # get the user profile and display name prof = p_key.get() displayName = getattr(prof, 'displayName') # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copy_conference_to_form(conf, displayName) for conf in conferences ])
def getConferencesCreated(self, request): """Return conferences 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 an instance of a Key for an id(user email) of a kind(Profile) p_key = ndb.Key(Profile, user_id) # query conferences with ancestor user conferences = Conference.query(ancestor=p_key) # get the user profile and display name prof = p_key.get() displayName = getattr(prof, 'displayName') # return set of ConferenceForm objects per Conference return ConferenceForms( items=[self._copyConferenceToForm(conf, displayName) for conf in conferences] )
def getConferencesBySpeaker(self, request): """Return conferences that feature a certain speaker""" # Get Speaker and his/her sessions from websafeSpeakerKey speaker = ndb.Key(urlsafe=request.websafeSpeakerKey).get() sessions = [sessionKey.get() for sessionKey in speaker.sessionKeys] # Get all the unique conference keys and get the conference objects conf_keys = [] for sess in sessions: if sess.websafeConferenceKey not in conf_keys: conf_keys.append(sess.websafeConferenceKey) confs = [ndb.Key(urlsafe=key).get() for key in conf_keys] # return set of ConferenceForm objects per Conference return ConferenceForms(items=[ self._copyConferenceToForm(conf, displayName='') for conf in confs ])
def query(self, request): """ Query Conferences in Datastore :param request: ConferenceQueryForms with one or many ConferenceQueryForm's :return: ConferenceForms with matching ConferenceForm's, if any """ # convert message types for now until we fix the js client side logic query_filters = [ queryutil.QueryFilter( field=f.field, operator=queryutil.QueryOperator.lookup_by_name(f.operator), value=f.value) for f in request.filters] query_form = queryutil.QueryForm( target=queryutil.QueryTarget.CONFERENCE, filters=query_filters) conferences = queryutil.query(query_form) # need to fetch organiser displayName from profiles # get all keys and use get_multi for speed organisers = [(ndb.Key(Profile, conf.organizerUserId)) for conf in conferences] profiles = ndb.get_multi(organisers) if not profiles or profiles == [None]: # bootstrapping issue as the user hasn't created their profile obj profiles = [Profile(key=org) for org in organisers] # put display names in a dict for easier fetching names = {} for profile in profiles: if hasattr(profile, 'displayName'): names[profile.key.id()] = getattr(profile, 'displayName') else: # Chances are someone wasn't forced to set their displayName yet names[profile.key.id()] = '' # return individual ConferenceForm object per Conference return ConferenceForms( items=[conf.to_form(names[conf.organizerUserId]) for conf in conferences] )
def filterPlayground(self, request): q = Conference.query() # simple filter usage: # q = q.filter(Conference.city == "Paris") # advanced filter building and usage # field = "city" # operator = "=" # value = "London" # f = ndb.query.FilterNode(field, operator, value) # q = q.filter(f) q = q.filter(Conference.city == "London") q = q.filter(Conference.maxAttendees > 10) q = q.order(Conference.name) return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])
def filterPlayground(self, request): q = Conference.query() # simple filter usage: q = q.filter(Conference.city == "London") q = q.filter(Conference.topics == "Medical Innovations") q = q.order(Conference.name) # advanced filter building and usage # field = "city" # operator = "=" # value = "London" # f = ndb.query.FilterNode(field, operator, value) # q = q.filter(f) # TODO # add 2 filters: # 1: city equals to London # 2: topic equals "Medical Innovations" return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])
def getConferencesToAttend(self, request): """Get list of conferences that user has registered for.""" # get user profile user = self._getProfileFromUser() # get conferenceKeysToAttend from profile. userConferenceKeyToAttend = user.conferenceKeysToAttend # to make a ndb key from websafe key you can use: conferenceKeys = [] for wsck in userConferenceKeyToAttend: conf_key = ndb.Key(urlsafe=wsck) conferenceKeys.append(conf_key) conferences = ndb.get_multi(conferenceKeys) # return set of ConferenceForm objects per Conference return ConferenceForms(items=[self._copyConferenceToForm(conf, "")\ for conf in conferences] )
def filterPlayground(self, request): q = Conference.query() # simple filter q = q.filter(Conference.city == "London") # Equivalent through ndb # field = "city" # operator = "=" # value = "London" # f = ndb.query.FilterNode(field, operator, value) # q = q.filter(f) q = q.filter(Conference.topics == "Medical Innovations") q = q.order(Conference.name) q = q.filter(Conference.maxAttendees > 10) return ConferenceForms( items=[self._copy_conference_to_form(conf, "") for conf in q])
def filter_playground(self, request): """ Filter Playground method :param request: :return: """ if not isinstance(request, message_types.VoidMessage): raise endpoints.BadRequestException() form = queryutil.QueryForm() form.target = queryutil.QueryTarget.CONFERENCE qfilter = queryutil.QueryFilter( field='CITY', operator=queryutil.QueryOperator.EQ, value='London' ) form.filters = [qfilter] q = queryutil.query(form) return ConferenceForms(items=[conf.to_form() for conf in q])
def filterPlayground(self, request): q = Conference.query() # simple filter usage: # q = q.filter(Conference.city == "Paris") # advanced filter building and usage # field = "city" # operator = "=" # value = "London" # f = ndb.query.FilterNode(field, operator, value) # q = q.filter(f) # # TODO # # add 2 filters: # # 1: city equals to London # q = q.filter(Conference.city == "London") # # 2: topic equals "Medical Innovations" # q = q.filter(Conference.topics == "Medical Innovations") # # 3: order by conference name # q = q.order(Conference.name) # # f = ndb.query.FilterNode("topics", "=", "Medical Innovations") # # q = q.filter(f) # # 4: filter by month # # q = q.filter(Conference.month == 2) # # 5: filter for big conferences # q = q.filter(Conference.maxAttendees > 3) q = Conference.query().\ filter(Conference.city == "London").\ filter(Conference.seatsAvailable >= 1).\ filter(Conference.seatsAvailable <= 9).\ order(Conference.seatsAvailable).\ order(Conference.name).\ order(Conference.month) return ConferenceForms( items=[self._copyConferenceToForm(conf, "") for conf in q])