Ejemplo n.º 1
0
    def addSessionToWishList(self, request):
        """Add a new session to the user's wish list."""
        # make sure user is authed
        user = endpoints.get_current_user()
        if not user:
            raise endpoints.UnauthorizedException('Authorization required')
        user_id = getUserId(user)

        # need to check to make sure session key refers to actual session,
        # also want to check that the user is registered for the relevant
        # conference.
        sesh = ndb.Key(urlsafe = request.sessionKey)
        sesh_key = sesh.get()


        if not sesh:
            raise endpoints.NotFoundException('Session with that key does not exist!')
        conf_for_sesh_key = sesh_key.conference
        conf_for_sesh = conf_for_sesh_key.get()

        confs = Conference.query(ancestor=ndb.Key(Profile, user_id))

        if conf_for_sesh not in confs:
            endpoints.UnauthorizedException('You are not attending the conference with that session!')

        sesh_query = ndb.Key(SessionWishList, user_id).get()
        if not sesh_query:
            # make UserID the key
            new_sesh = SessionWishList(userID = user_id, wishlistSessions = [sesh])
            new_sesh.key = ndb.Key(SessionWishList, new_sesh.userID)
            new_sesh.put()
            return self._copySessionToForm(sesh_key)
        if sesh in sesh_query.wishlistSessions:
            raise endpoints.UnauthorizedException('That session is already in your wishlist!')
        sesh_query.wishlistSessions.append(sesh)
        sesh_query.put()
        return self._copySessionToForm(sesh_key)