def _createWishlistObject(self, request): """ Create or update Wishlist object, returning WishlistForm """ # Preload and validate necessary data items user = self.get_authed_user() user_id = getUserId(user) prof_key = ndb.Key(Profile, user_id) if not request.websafeKey: raise endpoints.BadRequestException('Session websafeKey required') session = ndb.Key(urlsafe=request.websafeKey) if not session: raise endpoints.NotFoundException( 'No session found with key: {}'.format(request.websafeKey)) # copy WishlistForm/ProtoRPC Message into dict data = { field.name: getattr(request, field.name) for field in request.all_fields() } del data['websafeKey'] sess_key = ndb.Key(urlsafe=request.websafeKey) data['session'] = sess_key wishlist_id = Wishlist.allocate_ids(size=1, parent=prof_key)[0] wishlist_key = ndb.Key(Wishlist, wishlist_id, parent=prof_key) data['key'] = wishlist_key Wishlist(**data).put() return self._copyWishlistToForm(wishlist_key.get())
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 _createWishlistObject(self, request): """Create Wishlist object, returning WishlistForm/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.websafeSessionKey: raise endpoints.BadRequestException( "Session 'websafeSessionKey' field required") # copy WishlistForm/ProtoRPC Message into dict data = { field.name: getattr(request, field.name) for field in request.all_fields() } data['user'] = user_id p_key = ndb.Key(Profile, user_id) 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 Wishlist(**data).put() return request
def _createWishlistObject(self, request): """Create or update Wishlist object, returning WishlistForm/request.""" user_id = self._getUser() p_key = self._getUserProfileKey(user_id) wssk = request.websafeSessionKey sess = ndb.Key(urlsafe=wssk).get() if not sess: raise endpoints.NotFoundException("No session found with key: %s" % wssk) w = {} # Create a unique key w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) w["key"] = w_key # Check for an existing wishlist wish = Wishlist.query(ancestor=p_key).get() # If there is already a wishlist record, append this session to it, # otherwise create a new wishlist (unless its already in the wishlist) if not wish: w["sessions"] = [wssk] Wishlist(**w).put() elif wssk in wish.sessions: raise ConflictException("You have already placed this session in your wishlist") else: wish.sessions.append(wssk) wish.put() w["sessions"] = [self._retrieveSession(cs_id) for cs_id in wish.sessions] return self.toWishlistForm(w)
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 _createWishlistObject(self, request): """ Create or update Wishlist object, returning WishlistForm """ # Preload and validate necessary data items user = self.get_authed_user() user_id = getUserId(user) prof_key = ndb.Key(Profile, user_id) if not request.websafeKey: raise endpoints.BadRequestException('Session websafeKey required') session = ndb.Key(urlsafe=request.websafeKey) if not session: raise endpoints.NotFoundException( 'No session found with key: {}'.format(request.websafeKey) ) # copy WishlistForm/ProtoRPC Message into dict data = {field.name: getattr(request, field.name) for field in request.all_fields()} del data['websafeKey'] sess_key = ndb.Key(urlsafe=request.websafeKey) data['session'] = sess_key wishlist_id = Wishlist.allocate_ids(size=1, parent=prof_key)[0] wishlist_key = ndb.Key(Wishlist, wishlist_id, parent=prof_key) data['key'] = wishlist_key Wishlist(**data).put() return self._copyWishlistToForm(wishlist_key.get())
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 _createWishlistObject(self, request): """Create or update Wishlist object, returning WishlistForm/request.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # check sessionName exists if not request.sessionName: raise endpoints.UnauthorizedException('Name required') # get userId user_id = getUserId(user) # get session key this_session = Session.query(Session.name == request.sessionName).get() # populate dict data = { 'userId': user_id, 'sessionName': request.sessionName, 'sessionKey': this_session.key, 'typeOfSession': this_session.typeOfSession } # generate wishlist key from session key p_key = this_session.key c_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] c_key = ndb.Key(Wishlist, c_id, parent=p_key) data['key'] = c_key # query wishlist, filter by userId and sessionName, then count q = Wishlist.query() q = q.filter(Wishlist.userId == user_id) # if this session already in user's wishlist, bounce for i in q: if i.sessionKey == this_session.key: raise endpoints.UnauthorizedException( 'Session already added to wishlist') # save to wishlist Wishlist(**data).put() return request
def addSessionToWishlist(self, request): """Copy Session to Wishlist""" # first of all there is a need to check if this session is # already in wishlist: sessInWishlist = Wishlist.query( Wishlist.sessionKey == request.SessionKey).count() if sessInWishlist != 0: raise endpoints.ForbiddenException( "Denied, you can't create one session in wishlist twice") else: # get session and key: session_key = ndb.Key(urlsafe=request.SessionKey) session = session_key.get() # allocating id for session in wishlist: sess_wish_id = Wishlist.allocate_ids(size=1, parent=session.key)[0] # making key for session in wish, parent is just session: sess_wish_key = ndb.Key(Wishlist, sess_wish_id, parent=session.key) # in order not to deal with converting # dateandtime objects to string setting new # variables: date, startTime = None, None if session.date is not None: date = session.date del session.date if session.startTime is not None: startTime = session.startTime del session.startTime # making form from session to make dict and then give # that dict into Wishlist: session_form = self._copySessionToForm(session) # making dict with all data: data = {field.name: getattr(session_form, field.name) for field in session_form.all_fields()} data['date'] = date data['startTime'] = startTime data['sessionKey'] = session_key.urlsafe() # adding session_wish key to dict: data['key'] = sess_wish_key Wishlist(**data).put() return session_form
def _addSessionToWishlist(self, request): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') s_keys = [] user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) # Get the session keys and verify if the exist try: sessionKeys = request.sessionKeys for sess in sessionKeys: s_keys.append(ndb.Key(urlsafe=sess).get().key) except: raise endpoints.BadRequestException("Invalid 'session' value") # check and see if user currently has wish list wl = Wishlist.query(ancestor=ndb.Key(Profile, user_id)).get() # if wishlist exists, check to see if session keys exists in wishlist # if not, add if wl: for key in s_keys: if key not in wl.sessionKeys: wl.sessionKeys.append(key) wl.put() return self._copyWishlistToForm(wl) # else create Wishlist object else: # copy WishlistForm/ProtoRPC Message into dict data = {'sessionKeys': s_keys} 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 Wishlist(**data).put() return request
def _createWishlistObject(self, request): """Create or update Wishlist object, returning WishlistForm/request.""" # check auth user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # check sessionName exists if not request.sessionName: raise endpoints.UnauthorizedException('Name required') # get userId user_id = getUserId(user) # get session key this_session = Session.query(Session.name == request.sessionName).get() # populate dict data = {'userId': user_id, 'sessionName': request.sessionName, 'sessionKey': this_session.key, 'typeOfSession': this_session.typeOfSession} # generate wishlist key from session key p_key = this_session.key c_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] c_key = ndb.Key(Wishlist, c_id, parent=p_key) data['key'] = c_key # query wishlist, filter by userId and sessionName, then count q = Wishlist.query() q = q.filter(Wishlist.userId == user_id) # if this session already in user's wishlist, bounce for i in q: if i.sessionKey == this_session.key: raise endpoints.UnauthorizedException('Session already added to wishlist') # save to wishlist Wishlist(**data).put() return request
def addSessionToWishlist(self, request): user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) p_key = ndb.Key(Profile, user_id) # see if there is a wishlist already query = Wishlist.query(ancestor=p_key) session_key = ndb.Key(urlsafe=request.websafeSessionKey) if query.count(): wishlist = query.get() wishlist.sessions.append(session_key) wishlist.put() else: w_id = Wishlist.allocate_ids(size=1, parent=p_key)[0] w_key = ndb.Key(Wishlist, w_id, parent=p_key) data = {} data['key'] = w_key data['sessions'] = [] data['sessions'].append(session_key) w_key = Wishlist(**data).put() print(w_key) return BooleanMessage(data=True)