Пример #1
0
 def addSessionToWishlist(self, request):
     #TASK 2
     """Adds sessions to a user wishlist & returns the sessions added"""
     #Check if the user is logged in
     user = endpoints.get_current_user()
     if not user:
         raise endpoints.UnauthorizedException('Authorization Required')
     user_id = getUserId(user)
     #Validate that the SessionKey (urlsafe key) is provided
     if not request.SessionKey:
         raise endpoints.BadRequestException("SessionKey field required") 
     #Validate whether the requested SessionKey is already in the user's wishlist
     q = SessionWishlist.query()
     if (q.filter(SessionWishlist.sessionKey == request.SessionKey).count() > 0):
             raise endpoints.BadRequestException("SessionKey is already in %s's wishlist" % user)
     #Generate a Wishlist key to store the user wishlist. The wishlist will be created as
     #a child of Profile
     p_key = ndb.Key(Profile, user_id)
     w_id = SessionWishlist.allocate_ids(size=1, parent=p_key)[0]
     w_key = ndb.Key(SessionWishlist, w_id, parent=p_key)
     #Add the wishlist to the DS
     wishlist = SessionWishlist( key = w_key, sessionKey = request.SessionKey)
     wl_key = wishlist.put()
     #Return the session associated with the created entry
     session = ndb.Key(urlsafe=request.SessionKey).get()
     return self._copySessionToForm(session)
Пример #2
0
    def getSessionsInWishlist(self, request):
        """Get user wishlist sessions"""
        #TASK 2
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization Required')
        user_id = getUserId(user)
        p_key = ndb.Key(Profile, user_id)
        #Query the list of wishlist sessions based on ancestor key p_key
        wishlist = SessionWishlist.query(ancestor=p_key).fetch()
        #generate a list of sessions using the sessionKey stored in the wishlist
        sessions = []
        for wish_session in wishlist:
            sessions.append(ndb.Key(urlsafe=wish_session.sessionKey).get())

        return SessionForms(
                items=[self._copySessionToForm(session) for session in sessions]
                )
    def addSessionToWishlist(self, request):
        """adds the session to the user's list of sessions they are interested in attending

        """
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')

        # retrieve the session
        sess = ndb.Key(urlsafe=request.session_key).get()

        if sess is None:
            raise endpoints.NotFoundException(
                "No session for the given key was found"
            )

        # retrieve the user's wishlist if one exists
        wl = ndb.Key(SessionWishlist, getUserId(user)).get()
        if wl is None:
            wl_key = ndb.Key(SessionWishlist, getUserId(user))
            wl = SessionWishlist(
                key=wl_key,
                session_keys=[request.session_key]
            )
            wl.put()
            return request
        # if the wishlist exists then just add the sess to it
        wl_sess_keys = wl.session_keys
        if request.session_key not in wl.session_keys:
            wl_sess_keys.append(request.session_key)
            wl.session_keys = wl_sess_keys
            wl.put()
        wl.key.delete()
        return SessionWishlistForm(
            session_key=request.session_key
        )