def getSessionWishList(self, request): """This function gets all wishlists of user.""" # check for authentication user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # get Profile from datastore p_key = ndb.Key(Profile, user_id) profile = p_key.get() if not profile: raise endpoints.NotFoundException( 'No profile found') # Output all sessions in profile query = Wishlist.query(ancestor=p_key) query = query.fetch() slist = [] for q in query: s_key = ndb.Key(urlsafe=q.sessionKey) slist.append(self._copySessionToForm(s_key.get())) return SessionForms( items=slist)
def _createWishlistObject(self, request): """Create or update Wishlist object, returning WishlistForm/request.""" user_id = self._getUser() p_key = self._getUserProfileKey(user_id) wssk = request.websafeSessionKey sess = ndb.Key(urlsafe=wssk).get() if not sess: raise endpoints.NotFoundException("No session found with key: %s" % wssk) w = {} # Create a unique key w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) w["key"] = w_key # Check for an existing wishlist wish = Wishlist.query(ancestor=p_key).get() # If there is already a wishlist record, append this session to it, # otherwise create a new wishlist (unless its already in the wishlist) if not wish: w["sessions"] = [wssk] Wishlist(**w).put() elif wssk in wish.sessions: raise ConflictException("You have already placed this session in your wishlist") else: wish.sessions.append(wssk) wish.put() w["sessions"] = [self._retrieveSession(cs_id) for cs_id in wish.sessions] return self.toWishlistForm(w)
def addSessionToWishlist(self, request): """ Add a session to the current user's Wishlist """ # get current user user = endpoints.get_current_user() user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) try: # get the session to be wishlisted session = ndb.Key(urlsafe=request.websafeSessionKey).get() except ProtocolBufferDecodeError: raise endpoints.NotFoundException( 'No session found with key: %s' % request.websafeSessionKey) # first try to find wishlist of the current user wishlist = Wishlist.query(ancestor=p_key).get() if not wishlist: # create a wishlist if nothing was found wishlist = Wishlist(userId = user_id) w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) # Add session key to wishlist wishlist.sessionKey.append(session.key) wishlist.put() # Update the session wishlist counter session.wishlistCount += 1 session.put() return self._copyWishlistToForm(wishlist)
def getSessionsInWishlist(self, request): """Get user's wishlist's sessions in the conference. """ # Get conference key for urlsafekey. c_key = ndb.Key(urlsafe=request.websafeConferenceKey) # Get current user key. user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = _getUserId() user_key = ndb.Key(Profile, user_id) # Get wishlist of the user. user_wish = Wishlist.query(Wishlist.userKey==user_key).fetch() # Put all wishlist sessionkey into wish_s_list. wish_s_keys = [w.sessionKey for w in user_wish] # Add the sessionkeys which belong to the conference to s_list. s_list = [] for s_key in wish_s_keys: if s_key.parent() == c_key: s_list.append(s_key) # Get sesssion entity by session key in s_list, and copy to sessionForm. return SessionForms( items=[self._copySessionToForm(s.get()) for s in s_list])
def _createWishlistObject(self, request): """Create Wishlist object, returning WishlistForm/request.""" # as usual make sure user is logged in user = self._getAuthUser() user_id = getUserId(user) data = {field.name: getattr(request, field.name) for field in request.all_fields()} sess = ndb.Key(urlsafe=request.websafeSessionKey).get() p_key = ndb.Key(Profile, user_id) q = Wishlist.query(ancestor=p_key). \ filter(Wishlist.websafeSessionKey==request.websafeSessionKey).get() if q: raise endpoints.BadRequestException('Session already in Wishlist') data['sessionName'] = sess.name data['userId'] = user_id data['duration'] = sess.duration # create Wishlist key with logged in user as the parent w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) data['key'] = w_key # create Wishlist entry wishlist = Wishlist(**data) wishlist.put() return self._copyWishlistToForm(wishlist)
def _deleteSessionToWishlist(self, request): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # check and see if user currently has wish list wl = Wishlist.query(ancestor=ndb.Key(Profile, user_id)).get() sessionKeys = request.sessionKeys s_keys = [] if wl: # get the session keys and verify if the exist try: sessionKeys = request.sessionKeys for sess in sessionKeys: s_keys.append(ndb.Key(urlsafe=sess).get().key) except: raise endpoints.BadRequestException("Invalid 'session' value") for key in s_keys: if key in wl.sessionKeys: wl.sessionKeys.remove(key) wl.put() return self._copyWishlistToForm(wl) else: raise endpoints.BadRequestException("User does not " "have a Wishlist")
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 orderSessionsInWishlist(self, request): """Filter Wishlist first by startTime""" # get sessions from wishlist wish_sessions = Wishlist.query() # first order by time by startTime: wish_sessions = wish_sessions.order(Wishlist.startTime) return SessionForms( sessions=[ self._copySessionToForm(session) for session in wish_sessions])
def _addSessionToWishlist(self, session_key): user_id = self._getCurrentUserID() key = ndb.Key(Profile, user_id).get() wish_list = Wishlist.query(ancestor=key.key).get() if wish_list and wish_list.sessionKeys: wish_list.sessionKeys.append(session_key) else: wish_list.sessionKeys = [session_key] wish_list.put()
def _getSessionsInWishlist(self): user_id = self._getCurrentUserID() wishlist = Wishlist.query(ancestor=ndb.Key(Profile, user_id)).get() forms = SessionForms() for key in wishlist.sessionKeys: query = Session.query(Session.key == key).get() forms.items += [self._copySessionToForm(session=query)] return forms
def getSessionsInWishlist(self, request): """Get sessions in wishlist.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query wishlist, filter by userId q = Wishlist.query().filter(Wishlist.userId == user_id) return WishlistForms(items=[self._copyWishlistToForm(wish) for wish in q])
def getWishlistBySession(self, request): """ given a specific session, return all users that have this session on their wishlist """ # get wishlists that contain the sessionKey wishlists = Wishlist.query(Wishlist.sessionKey == request.websafeSessionKey) # get the user keys from the wishlists users = [(ndb.Key(Profile, wishlist.userId)) for wishlist in wishlists] # lookup the profiles profiles = ndb.get_multi(users) return ProfileForms( items=[self._copyProfileToForm(profile) for profile in profiles])
def getSessionsInWishlist(self, request): """Get all Wishlist entries for the user.""" user = self._getAuthUser() user_id = getUserId(user) # create ancestor query for all key matches for this user wishlist = Wishlist.query(ancestor=ndb.Key(Profile, user_id)) # return set of WishlistForm objects per Wishlist Entry return WishlistForms( items=[self._copyWishlistToForm(wish) for wish in wishlist] )
def _addToWishlistObject(self, request): """Add a session to a wishlist, return a wishlist form""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Camacho - get the user's wishlist wl = Wishlist.query(ancestor=ndb.Key(Profile,user_id)) wl = wl.get() # Camacho - make sure the wishlist exists if not wl: raise endpoints.NotFoundException( 'Your user does not have a wishlist!') # Camacho - save the current keys try: currKeys = wl.sessionKeys except: currKeys = [] # Camacho - get the key specified to add newKey = getattr(request,'sessionKeys') # Camacho - make sure the key specified corresponds # to an existing session try: session = ndb.Key(urlsafe=newKey).get() except: raise endpoints.NotFoundException( 'Session with this key not found %s' % (newKey)) currKeys.append(newKey) setattr(wl, 'sessionKeys', currKeys) wl.put() sessions = [] # Pass a list of the sessions in the wishlist to the # _copySessionToForm function to return SessionForm # object for k in currKeys: sessions.append(ndb.Key(urlsafe=k).get()) return SessionForms( items=[self._copySessionToForm(sess) for sess in \ sessions] )
def _getSessionsInWishlist(self): """ Helper method to get Sessions from the wishlist """ user = self.get_authed_user() user_id = getUserId(user) prof_key = ndb.Key(Profile, user_id) wish_keys = Wishlist.query(ancestor=prof_key) sess_keys = [wish_key.session for wish_key in wish_keys] if sess_keys in (None, []): raise endpoints.BadRequestException( 'No wishlist found: {}'.format(sess_keys)) return ndb.get_multi(sess_keys)
def getSessionsInMostWishlists(self, request): """Query top five sessions that are in most wishlists""" sessions = Wishlist.query().group_by([Wishlist.sessionKeysToAttend]) Session.query()..filter(Session.typeOfSession == request.typeOfSession) user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') wishlist = self._getWithlistByUserId(getUserId(user)) keys = [ndb.Key(urlsafe=wkey) for wkey in wishlist.sessionKeysToAttend] sessions = ndb.get_multi(keys) return SessionForms(items=[self._copySessionToForm(session) for session in sessions])
def _getSessionsInWishlist(self): """ Helper method to get Sessions from the wishlist """ user = self.get_authed_user() user_id = getUserId(user) prof_key = ndb.Key(Profile, user_id) wish_keys = Wishlist.query(ancestor=prof_key) sess_keys = [wish_key.session for wish_key in wish_keys] if sess_keys in (None, []): raise endpoints.BadRequestException( 'No wishlist found: {}'.format(sess_keys)) return ndb.get_multi(sess_keys)
def getSessionsInWishlist(self, request): """Query for all the sessions in a conference that the user is interested in attending""" p_key = self._getUserProfileKey() wish = Wishlist.query(ancestor=p_key).get() if not wish: raise endpoints.NotFoundException('Your wishlist could not be found') wf = {'key': p_key, 'sessions': []} if len(wish.sessions) and any(s for s in wish.sessions): wf['sessions'] = [self._retrieveSession(cs_id) for cs_id in wish.sessions if cs_id is not None] return self.toWishlistForm(wf)
def getSessionsInWishlist(self, request): """Get sessions in wishlist.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query wishlist, filter by userId q = Wishlist.query().filter(Wishlist.userId == user_id) return WishlistForms( items=[self._copyWishlistToForm(wish) for wish in q])
def getSessionsInWishlist(self, request): """Return all sessions in user Wishlist across all conferences.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) wl = Wishlist.query(ancestor=ndb.Key(Profile, user_id)).get() if not wl: raise endpoints.NotFoundException( 'User does not have a Wishlist!') return self._copyWishlistToForm(wl)
def getSessionsInWishlist(self, request): """Return sessions for a particular speaker across all conferences.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Camacho - get the user's wishlist wl = Wishlist.query(ancestor=ndb.Key(Profile,user_id)).get() # Camacho - make sure the wishlist exists if not wl: raise endpoints.NotFoundException( 'Your user does not have a wishlist!') return self._copyWishlistToForm(wl)
def getWishlistByType(self, request): """Get wishlist by type.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query wishlist, filter by userId and typeOfSession q = Wishlist.query() q = q.filter(Wishlist.userId == user_id) q = q.filter(Wishlist.typeOfSession == request.typeOfSession) return WishlistForms(items=[self._copyWishlistToForm(wish) for wish in q])
def getSessionsInWishlist(self, request): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) # see if there is a wishlist already wishlist = Wishlist.query(ancestor=p_key).get() print(wishlist) sessions = ndb.get_multi(wishlist.sessions) return SessionForms( items=[self._copySessionToForm(session, "") for session in sessions] )
def getWishlistByType(self, request): """Get wishlist by type.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query wishlist, filter by userId and typeOfSession q = Wishlist.query() q = q.filter(Wishlist.userId == user_id) q = q.filter(Wishlist.typeOfSession == request.typeOfSession) return WishlistForms( items=[self._copyWishlistToForm(wish) for wish in q])
def getWishlistSessionsLongerThanDuration(self, request): """Locate all wishlist entries where the duration is equal or longer than the requested duration""" user = self._getAuthUser() user_id = getUserId(user) # create ancestor query for all key matches for this user # and the duration is longer or equal to the specified duration wishls = Wishlist.query(ancestor=ndb.Key(Profile, user_id)). \ filter(Wishlist.duration>=request.duration) # return set of WishlistForm objects per Wishlist return WishlistForms( items=[self._copyWishlistToForm(wish) for wish in wishls] )
def addSessionToWishlist(self, request): """This function adds given session to wishlist.""" # check for authentication user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # check for SessionKey if not request.SessionKey: raise endpoints.BadRequestException( "'SessionKey' field required") # Try and get the session object s_key = ndb.Key(urlsafe=request.SessionKey) sess = s_key.get() if not sess: raise endpoints.NotFoundException( 'No Session found with key: %s' % request.SessionKey) # Add to wishlist # get Profile from datastore p_key = ndb.Key(Profile, user_id) profile = p_key.get() if not profile: raise endpoints.NotFoundException( 'No profile found') # Check that the session does not already exist # Prevent the same session from being added to wishlist query = Wishlist.query(ancestor=p_key) query = query.filter(Wishlist.sessionKey == request.SessionKey) if len(query.fetch()) != 0: raise endpoints.BadRequestException( "That session is already in your wishlist") # generate Wishlist ID based on User ID w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) # save new wishlist to datastore newWish = Wishlist(sessionKey=request.SessionKey) newWish.key = w_key newWish.put() return request
def getWishlists(self, request): """Get all wishlists""" # Query all sessions for thier websafe keys items = [] query = Wishlist.query() for wish in query.fetch(): temp = wish.key.parent().get().displayName items.append([temp, wish.key.urlsafe()]) # Create and send message with all keys and names keys = [] wsk = SessionKeys() for i in items: temp = SessionKeyQuery(name=i[0], key=i[1]) keys.append(temp) wsk.items = keys wsk.check_initialized() return wsk
def _deleteWishlistObject (self, request): """ Delete Wishlist entry """ # as usual make sure user is logged in user = self._getAuthUser() user_id = getUserId(user) # Find the Wishlist Session record for the user q = Wishlist.query(ancestor=ndb.Key(Profile, user_id)). \ filter(Wishlist.websafeSessionKey==request.websafeSessionKey). \ get() # only if it is found should we delete it if q: q.key.delete() return BooleanMessage(data=True)
def getWishlists(self, request): """Get all wishlists""" # Query all sessions for thier websafe keys items = [] query = Wishlist.query() for wish in query.fetch(): temp = wish.key.parent().get().displayName items.append([temp, wish.key.urlsafe()]) # Create and send message with all keys and names keys = [] wsk = SessionKeys() for i in items: temp = SessionKeyQuery(name=i[0], key=i[1]) keys.append(temp) wsk.items = keys wsk.check_initialized() return wsk
def getSessionsInWishlist(self, request): """Query for all the sessions in a conference that the user is interested in.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) wishs = Wishlist.query(Wishlist.user == user_id) session_keys = [ ndb.Key(urlsafe=wish.websafeSessionKey) for wish in wishs ] sessions = ndb.get_multi(session_keys) # return set of SessionForm objects per Session return SessionForms( items=[self._copySessionToForm(session) for session in sessions])
def addSessionToWishlist(self, request): """This function adds given session to wishlist.""" # check for authentication user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # check for SessionKey if not request.SessionKey: raise endpoints.BadRequestException("'SessionKey' field required") # Try and get the session object s_key = ndb.Key(urlsafe=request.SessionKey) sess = s_key.get() if not sess: raise endpoints.NotFoundException('No Session found with key: %s' % request.SessionKey) # Add to wishlist # get Profile from datastore p_key = ndb.Key(Profile, user_id) profile = p_key.get() if not profile: raise endpoints.NotFoundException('No profile found') # Check that the session does not already exist # Prevent the same session from being added to wishlist query = Wishlist.query(ancestor=p_key) query = query.filter(Wishlist.sessionKey == request.SessionKey) if len(query.fetch()) != 0: raise endpoints.BadRequestException( "That session is already in your wishlist") # generate Wishlist ID based on User ID w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) # save new wishlist to datastore newWish = Wishlist(sessionKey=request.SessionKey) newWish.key = w_key newWish.put() return request
def deleteSessionInWishlist(self, request): """Removes the session from the users list of sessions they are interested in attending""" user_id = self._getUser() p_key = self._getUserProfileKey(user_id) wssk = request.websafeSessionKey wish = Wishlist.query(ancestor=p_key).get() if not wish: raise endpoints.NotFoundException('No wishlist found') # Remove this session key from this particular wishlist if wssk in wish.sessions: wish.sessions.remove(wssk) wish.put() retVal = True else: retVal = False return BooleanMessage(data=retVal)
def _createWishlistObject(self, request): """Create or update Wishlist object, returning WishlistForm/request.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # check sessionName exists if not request.sessionName: raise endpoints.UnauthorizedException('Name required') # get userId user_id = getUserId(user) # get session key this_session = Session.query(Session.name == request.sessionName).get() # populate dict data = { 'userId': user_id, 'sessionName': request.sessionName, 'sessionKey': this_session.key, 'typeOfSession': this_session.typeOfSession } # generate wishlist key from session key p_key = this_session.key c_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] c_key = ndb.Key(Wishlist, c_id, parent=p_key) data['key'] = c_key # query wishlist, filter by userId and sessionName, then count q = Wishlist.query() q = q.filter(Wishlist.userId == user_id) # if this session already in user's wishlist, bounce for i in q: if i.sessionKey == this_session.key: raise endpoints.UnauthorizedException( 'Session already added to wishlist') # save to wishlist Wishlist(**data).put() return request
def getSessionsInWishlist(self, request): """ Get all the sessions the current user has wishlisted """ # get current user user = endpoints.get_current_user() user_id = getUserId(user) # get current user's wishlist wishlist = Wishlist.query(Wishlist.userId == user_id).get() if wishlist: # get session keys from current user's wishlist sessionKeys = wishlist.sessionKey # lookup sessions sessions = ndb.get_multi(sessionKeys) else: sessions = [] return SessionForms( items=[self._copySessionToForm(session, getattr(session.key.parent().get(), 'name')) for session in sessions])
def getWishlistBySpeaker(self, request): """Get wishlist by speaker.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query session, filter by speaker q = Session.query().filter(Session.speaker == request.speaker) # query wishlist, filter by userId p = Wishlist.query().filter(Wishlist.userId == user_id) a = [] for s in q: for w in p: if s.key == w.sessionKey: a.append(w) return WishlistForms(items=[self._copyWishlistToForm(wish) for wish in a])
def deleteSessionInWishlist(self, request): """Delete a single session in the user's wishlist.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Camacho - Fetch the user's wishlist wl = Wishlist.query(ancestor=ndb.Key(Profile,user_id)) wl = wl.get() wlForm = WishlistForm() # Camacho - check that conference exists if not wl: raise endpoints.NotFoundException( 'Your user does not have a wishlist!') # Camacho - save the current keys try: currKeys = wl.sessionKeys except: currKeys = [] # Camacho - get the requested key to delete delKey = getattr(request,'sessionKeys') # Camacho - make sure the requested key exists # in the user's wishlist try: currKeys.remove(delKey) except: raise endpoints.NotFoundException( 'Session key not found in wishlist %s' % (delKey)) setattr(wl, 'sessionKeys', currKeys) wl.put() return self._copyWishlistToForm(wl)
def addSessionToWishlist(self, request): """Copy Session to Wishlist""" # first of all there is a need to check if this session is # already in wishlist: sessInWishlist = Wishlist.query( Wishlist.sessionKey == request.SessionKey).count() if sessInWishlist != 0: raise endpoints.ForbiddenException( "Denied, you can't create one session in wishlist twice") else: # get session and key: session_key = ndb.Key(urlsafe=request.SessionKey) session = session_key.get() # allocating id for session in wishlist: sess_wish_id = Wishlist.allocate_ids(size=1, parent=session.key)[0] # making key for session in wish, parent is just session: sess_wish_key = ndb.Key(Wishlist, sess_wish_id, parent=session.key) # in order not to deal with converting # dateandtime objects to string setting new # variables: date, startTime = None, None if session.date is not None: date = session.date del session.date if session.startTime is not None: startTime = session.startTime del session.startTime # making form from session to make dict and then give # that dict into Wishlist: session_form = self._copySessionToForm(session) # making dict with all data: data = {field.name: getattr(session_form, field.name) for field in session_form.all_fields()} data['date'] = date data['startTime'] = startTime data['sessionKey'] = session_key.urlsafe() # adding session_wish key to dict: data['key'] = sess_wish_key Wishlist(**data).put() return session_form
def getWishlistBySpeaker(self, request): """Get wishlist by speaker.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get userId user_id = getUserId(user) # query session, filter by speaker q = Session.query().filter(Session.speaker == request.speaker) # query wishlist, filter by userId p = Wishlist.query().filter(Wishlist.userId == user_id) a = [] for s in q: for w in p: if s.key == w.sessionKey: a.append(w) return WishlistForms( items=[self._copyWishlistToForm(wish) for wish in a])
def _updateSessionsInWishlist(self, request, add=True): """ Add or remove a Session from the Wishlist Whether a session will be added or removed is based on the calling endpoint """ # Preload and validate necessary data items user = self.get_authed_user() session = ndb.Key(urlsafe=request.websafeKey) if not session: raise endpoints.BadRequestException( 'No session found for key: {}'.format(request.websafeKey)) user_id = getUserId(user) prof_key = ndb.Key(Profile, user_id) wishlist = Wishlist.query(ancestor=prof_key) if add: # Check whether the given websafeKey is already in the wishlist if wishlist.filter(Wishlist.session == session).count() > 0: raise endpoints.BadRequestException( 'Session has already been added to your wishlist') self._createWishlistObject(request) return self._copySessionToForm(session.get()) else: sessions = wishlist.filter(Wishlist.session == session).fetch() if len(sessions) != 0: sessions[0].key.delete() updated_wishlist = self._getSessionsInWishlist() return SessionForms(items=[ self._copySessionToForm(item) for item in updated_wishlist ])
def getSessionWishList(self, request): """This function gets all wishlists of user.""" # check for authentication user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # get Profile from datastore p_key = ndb.Key(Profile, user_id) profile = p_key.get() if not profile: raise endpoints.NotFoundException('No profile found') # Output all sessions in profile query = Wishlist.query(ancestor=p_key) query = query.fetch() slist = [] for q in query: s_key = ndb.Key(urlsafe=q.sessionKey) slist.append(self._copySessionToForm(s_key.get())) return SessionForms(items=slist)