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 _getProfileFromUser(self): """Return user Profile from datastore, creating new one if non-existent.""" # make sure user is authed user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # get Profile from datastore user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) profile = p_key.get() # create new Profile if not there if not profile: profile = Profile( key = p_key, displayName = user.nickname(), mainEmail= user.email(), teeShirtSize = str(TeeShirtSize.NOT_SPECIFIED), ) w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) wishlist = Wishlist( key = w_key, sessionKeys = [] ) profile.put() wishlist.put() return profile # return Profile
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 _getWithlistByUserId(self, user_id): wishlist_key = ndb.Key(Wishlist, user_id) wishlist = wishlist_key.get() if not wishlist: wishlist = Wishlist(key=wishlist_key) wishlist.put() return wishlist
def _makeWishlist(self): user = self._getProfileFromUser() parent_key = user.key new_wish_list = Wishlist(parent=parent_key) # set user_id (mainEmail) for ease of query new_wish_list.userId = user.mainEmail # set wish list as child of current profile new_wish_list.put()
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 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 _getWishlistFromUser(self): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) w_key = ndb.Key(Wishlist, user_id) wishlist = w_key.get() if not wishlist: wishlist = Wishlist( key=w_key, ownerId=user_id, public=True, ) wishlist.put() print wishlist return wishlist
def _creatWishlist(self, request): "Create a Wishlist, add the session to it." # Get the current user, and the its name and key. user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = _getUserId() user_key = ndb.Key(Profile, user_id) user_name = user_key.get().displayName # Get the session key and session name. session_key =ndb.Key(urlsafe=request.websafeSessionKey) session_name = session_key.get().sessionName # Store the user and session data in a wishlist entity. wishlist = Wishlist( userName=user_name, userKey=user_key, sessionKey=session_key, sessionName=session_name) wishlist.put() # Return user and session name as a tuple. return user_name, session_name