def getConferencesWithWishlistedSessions(self, request): """Returns conferences which have sessions the user has wishlisted.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) session_keys = [ wl.sessionKey for wl in SessionWishlistItem.query( SessionWishlistItem.userId == user_id)] conf_keys = [ndb.Key(urlsafe=k).parent().urlsafe() for k in session_keys] conf_counts = {} for ck in conf_keys: if ck in conf_counts: conf_counts[ck] += 1 else: conf_counts[ck] = 1 confs = [] for (k,v) in sorted(conf_counts.items(), key=operator.itemgetter(1)): confs.append(ConferenceWithWishlistSession( conference=self._copyConferenceToForm( ndb.Key(urlsafe=k).get(), None), wishlistedSessions=v)) return ConferencesWithWishlistSessionResponse( conferences=list(reversed(confs)) )
def _createSessionWishlistObject(self, request): """Wishlists a session for the current user.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # check that session exists sessionKey = ndb.Key(urlsafe=request.sessionKey) if sessionKey.kind() != "ConferenceSession" or not sessionKey.get(): raise endpoints.NotFoundException( 'No session found with key: %s' % request.sessionKey) csession = sessionKey.get() q = SessionWishlistItem.query() q = q.filter(SessionWishlistItem.userId == user_id) q = q.filter(SessionWishlistItem.sessionKey == request.sessionKey) # if session has already been wishlisted by this user, simply return # success if q.count() > 0: return BooleanMessage(data=True) wlItem = SessionWishlistItem() wlItem.userId = user_id wlItem.sessionKey = request.sessionKey wlItem.put() return BooleanMessage(data=True)
def addSessionToWishlist(self, request): """adds the session to the user's list of sessions wishlist""" # get current user's Profile user = self._getCurrentUserProfile() # retrieve Session entity from request # to be added to the user's wishlist session_websafe_key = request.session_websafe_key session_to_add = self._getEntityByWebSafeKey(session_websafe_key) # check if user already added this Session to wishlist session_in_wishlist = SessionWishlistItem.query( ancestor=user.key).filter( SessionWishlistItem.session_websafe_key == session_websafe_key).get() if session_in_wishlist: raise ConflictException('Session already found in wishlist.') # create new key for SessionWishlistItem entity # with user.key as parent session_wishlist_id = SessionWishlistItem.allocate_ids( size=1, parent=user.key)[0] session_wishlist_key = ndb.Key( SessionWishlistItem, session_wishlist_id, parent=user.key) # prepare data to be used for creating new SessionWishlistItem entity data = dict() # overwrite the automatically-generated key # with our custom parental key data['key'] = session_wishlist_key data['session_websafe_key'] = session_websafe_key # the denormalized parent conference websafe key of this session # used for easy query filtering in getSessionWishlistItem endpoint data['parent_wsck'] = session_to_add.key.parent().get().key.urlsafe() SessionWishlistItem(**data).put() return request
def getSessionsInWishlist(self, request): """Gets all sessions the current user has wishlisted""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) session_keys = [ wl.sessionKey for wl in SessionWishlistItem.query( SessionWishlistItem.userId == user_id)] wl_sessions = [ndb.Key(urlsafe=k).get() for k in session_keys] return ConferenceSessionForms( items=[self._copyConferenceSessionToForm(cs) for cs in wl_sessions] )
def getMostWishlistedSessions(self, request): """Returns the most wishlisted session""" # get all wishlist entities wishlisted_sessions = SessionWishlistItem.query() print wishlisted_sessions # extract Session keys from all SessionWishlistItem entities # to be used for in _mostCommonItem(session_keys) # which uses set() and 'Model' is not immutable session_keys = [sess.session_websafe_key for sess in wishlisted_sessions] # get most common item in session_keys list; # AKA, the most wishlisted session. most_wishlisted_session = ndb.Key(urlsafe=self._mostCommonItem(session_keys)).get() return self._copySessionToForm(most_wishlisted_session)
def getSessionsInWishlist(self, request): """Given a conference, find all user's SessionWishlistItem in that conference """ # get user and conference entities from request user = self._getCurrentUserProfile() conference = self._getEntityByWebSafeKey(request.wsck) # query for SessionWishlistItem entities # using user as ancestor # and wsck as parent conference wishlist = SessionWishlistItem.query( ancestor=user.key).filter( SessionWishlistItem.parent_wsck == conference.key.urlsafe()).fetch() # get Session entities from wishlist sessions = [ndb.Key(urlsafe=wishlist_item.session_websafe_key).get() for wishlist_item in wishlist] return SessionForms(items=[self._copySessionToForm(session) for session in sessions])