def addSessionToWishlist(self, request): """Add a Session to a wish list. the session is taken by its websafeKey and inserted in the WishList entity""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) safe_key = request.websafeSessionKey # Get the session entity session = ndb.Key(urlsafe=safe_key).get() # define the required fields from the session in the wishlist. data = {'name': '', 'speaker': ''} data['name'] = session.name data['speaker'] = session.speaker p_key = ndb.Key(Profile, user_id) # Create the WishList entity wish = WishList( sessionName=data['name'], sessionSpeaker=data['speaker'], sessionKey=safe_key, parent=p_key ) wish.put() return WishListForm( sessionName=data['name'], sessionSpeaker=data['speaker'], sessionKey=safe_key )
def addToWishList(self, userID, product, productID): try: user = User.objects.get(pk=userID) p = Product.objects.get(Q(pk=productID), Q(name=product)) if WishList.objects.filter(Q(owner=user), Q( product=p)).count() != 0: #check if item already exist return dataBaseModel.ERR_WISHLIST_ALREADY_EXIST newOne = WishList(owner=user, product=p) # create a new wishlist item newOne.save() return dataBaseModel.SUCCESS except: return dataBaseModel.ERR_BAD_PRODUCT
def _createWishListObject(self, request): """Create or update Session object, returning SessionForm/request.""" # preload necessary data items user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) if not request.session_key: raise endpoints.BadRequestException("'session_key' field required") data = {} # generate Profile Key based on user ID and Conference # ID based on Profile key get Conference key from ID u_key = ndb.Key(Profile, user_id) w_sid = WishList.allocate_ids(size=1, parent=u_key)[0] w_key = ndb.Key(WishList, w_sid, parent=u_key) data['key'] = w_key data['user_id'] = user_id data['session_key'] = request.session_key WishList(**data).put() return self._copyWishListToForm(w_key.get())