def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None # Get user profile prof = self._getProfileFromUser() # Check if conference given in the websafeConferenceKey exists wsck = request.websafeConferenceKey conf = _getEntityByWebsafeKey(wsck, 'Conference') # Register if reg: # Check if user already registered, otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference.") # Check if seats available if conf.seatsAvailable <= 0: raise ConflictException("There are no seats available.") # Register user, deduct one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # Unregister else: # Check if user already registered if wsck in prof.conferenceKeysToAttend: # Unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # Update the datastore and return prof.put() conf.put() return BooleanMessage(data=retval)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None prof = self._getProfileFromUser() # get user Profile # check if conf exists given websafeConfKey # get conference; check that it exists wsck = request.websafeConferenceKey conf = ndb.Key(urlsafe=wsck).get() if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % wsck) # register if reg: # check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException("There are no seats available.") # register user, take away one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if wsck in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) # remove sessions from wishlist seshs = Session.query(ancestor=ndb.Key(urlsafe=wsck)) for session in seshs: s_key = session.key.urlsafe() if s_key in prof.sessionWishlist: prof.sessionWishlist.remove(s_key) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)
def _register(request, reg=True): """ Register or unregister user for selected conference. :param request: RPC Message Request with a urlsafe Conference Key :param reg: whether to register (True) or unregister (False) the requesting User :return: BooleanMessage - True if successful, False if failure """ prof = ProfileApi.profile_from_user() # get user Profile # check if conf exists given websafeConfKey # get conference; check that it exists c_key = ndb.Key(urlsafe=request.websafeConferenceKey) conf = c_key.get() if not conf: raise endpoints.NotFoundException('No conference found for key') # register if reg: # check if user already registered otherwise add if c_key in prof.conferencesToAttend: raise ConflictException( 'Already registered for this conference') # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException('There are no seats available.') # register user, take away one seat prof.conferencesToAttend.append(c_key) conf.seatsAvailable -= 1 # update datastore prof.put() conf.put() # un-register else: # check if user already registered if c_key in prof.conferencesToAttend: # unregister user, add back one seat prof.conferencesToAttend.remove(c_key) conf.seatsAvailable += 1 # update datastore prof.put() conf.put() else: return BooleanMessage(data=False) return BooleanMessage(data=True)
def create_speaker(self, request): """Create new speaker. Args: request (SpeakerForm) Returns: SpeakerForm updated with the new object's key Raises: endpoints.BadRequestException if the speaker info is invalid models.ConflictException if a speaker with same email already exists """ # Validate fields if not request.name: raise endpoints.BadRequestException("Speaker 'name' field required") if not request.email: raise endpoints.BadRequestException("Speaker 'email' field required") # Check that speaker with same email does not exist speaker = Speaker.query(Speaker.email == request.email).get() if speaker: raise ConflictException("Speaker already exists with email: %s" % request.email) # Allocate speaker ID and generate speaker key s_id = Speaker.allocate_ids(size = 1)[0] s_key = ndb.Key(Speaker, s_id) # Create new speaker speaker = Speaker.to_object(request) speaker.key = s_key # set the key since this is a new object speaker.put() # Returm form back return speaker.to_form()
def addSessionToWishlist(self, request): """Add a session to the user's wishlist.""" user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') # check if session exists given websafeSessionKey wssk = request.websafeSessionKey session = ndb.Key(urlsafe=wssk).get() if not session: raise endpoints.NotFoundException( 'No session found with key: %s' % wssk) prof = self._getProfileFromUser() # get user Profile # check if user already has this session in wishlist, otherwise add if wssk in prof.wishlistSessionKeys: raise ConflictException( "You have already added this session to the wishlist") # add session prof.wishlistSessionKeys.append(wssk) # write things back to the datastore & return prof.put() return self._copySessionToForm(session)
def participateGame(self, request): """ Sign a player(identifiable by player_name) up for a game (identifiable by key); can not play against self """ # get the specified game game_key = ndb.Key(urlsafe=request.websafeGameKey) game = game_key.get() if not game: raise endpoints.NotFoundException( 'No game found with key: {}'.format(request.websafeGameKey)) # get the requested player player = Player.query(Player.displayName == request.player_name).get() if not player: raise endpoints.NotFoundException( 'No player with name {} found'.format(request.player_name)) # check if the player has already signed up elif game_key.urlsafe() in player.gamesInProgress: raise ConflictException( "You have already registered for this game") # update the specified game elif game.playerOne is None: setattr(game, 'playerOne', request.player_name) player.gamesInProgress.append(game_key.urlsafe()) game.seatsAvailable -= 1 elif game.playerTwo is None: game.playerTwo = request.player_name player.gamesInProgress.append(game_key.urlsafe()) game.seatsAvailable -= 1 else: raise endpoints.UnauthorizedException('Sorry, both spots taken!') game.put() player.put() return game._copyGameToForm
def _sessionWishlist(self, request, add=True): """Add or delete a session from a user's wishlist""" profile = self._getProfileFromUser() session_websafekey = request.websafeSessionKey session = ndb.Key(urlsafe=session_websafekey).get() if not session: raise endpoints.NotFoundException( 'No session found with key: %s' % session_websafekey ) if add: if session_websafekey in profile.sessionWishlist: raise ConflictException( "You already have this session in your wishlist" ) else: profile.sessionWishlist.append(session_websafekey) return_value = True else: if session_websafekey in profile.sessionWishlist: profile.sessionWishlist.remove(session_websafekey) return_value = True else: return_value = False profile.put() return BooleanMessage(data=return_value)
def _addToWishlist(self, request, add=True): """Add/remove sessions from user wishlist""" retval = None prof = self._getProfileFromUser() # get user Profile wssk = request.websafeSessionKey session = ndb.Key(urlsafe=wssk).get() if not session: raise endpoints.NotFoundException('No session found with key: %s' % wssk) # Add to wishlist if add: # Check if user already added session to wishlist if wssk in prof.sessionKeysWishlist: raise ConflictException( 'You have already added this session to your wishlist') # Add session to user wishlist prof.sessionKeysWishlist.append(wssk) retval = True # Remove from wishlist else: if wssk in prof.sessionKeysWishlist: prof.sessionKeysWishlist.remove(wssk) retval = True else: retval = False # Write the profile back to the datastore. prof.put() return BooleanMessage(data=retval)
def _sessionWishlist(self, request, addTo=True): """Add or remove session from users wishlist. """ retval = None # Fetch user profile prof = self._getProfileFromUser() # Fetch session from websafeSessionKey and check if exists wsck = request.websafeSessionKey sess = ndb.Key(urlsafe=wsck).get() if not sess: raise endpoints.NotFoundException( 'No session found with key: %s' % wsck) # Check if session is already in wishlist otherwise add if addTo: if wsck in prof.wishlistSessionKeys: raise ConflictException( "You have already added this session to your wishlist") prof.wishlistSessionKeys.append(wsck) retval = True # Check if session is in wishlist then remove else: if wsck in prof.wishlistSessionKeys: prof.wishlistSessionKeys.remove(wsck) retval = True else: retval = False # Put back in datastore and return prof.put() return BooleanMessage(data=retval)
def addSessionToWishlist(self, request): """Adds the session to the user's list of sessions they are interested in attending""" # verify that the weafekey points to a session entity. s_key = ndb.Key(urlsafe=request.websafeSessionKey) if s_key.kind() != 'Session': raise endpoints.BadRequestException( 'websafeKey must point to a Session entity.') # check to see if session exists. session = s_key.get() if not session: raise endpoints.NotFoundException('Session not found.') user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required.') user_id = getUserId(user) prof = ndb.Key(Profile, user_id).get() # Check is session is already on the wishlist. if request.websafeSessionKey in prof.wishList: raise ConflictException('The session is already on your wishlist.') prof.wishList.append(request.websafeSessionKey) prof.put() return self._copyProfileToForm(prof)
def addSessionToWishlist(self, request): """Add a session to user's wishlist""" user = endpoints.get_current_user() #check if logged in if not user: raise endpoints.UnauthorizedException('Authorization required') #grab user profile prof = self._getProfileFromUser() #grab session using urlsafe key wsck = request.websafeSessionKey sess = ndb.Key(urlsafe=wsck).get() #check if session exists if not sess: raise endpoints.NotFoundException('No Session found with key: %s' % wsck) # check if user already registered otherwise add if wsck in prof.sessionsKeysToAttend: raise ConflictException( "You have already registered for this session") # register user, take away one seat prof.sessionsKeysToAttend.append(wsck) prof.put() return self._copySessionToForm(sess)
def _addSessionWishListObject(self, request): """Add Session in WishList, returning True is success otherwise False.""" retval = None # preload necessary data items prof = self._getProfileFromUser() # check if session exists given websafeConfKey # get session; check that it exists wssk = request.websafeSessionKey sess = ndb.Key(urlsafe=wssk).get() if not sess: raise endpoints.NotFoundException('No Session found with key: %s' % wssk) # check if session is already added to wishist otherwise add if wssk in prof.sessionKeysInWishList: raise ConflictException( "This session is already added to your wishlist") # add session in the wishlist if wssk not in prof.sessionKeysInWishList: prof.sessionKeysInWishList.append(wssk) retval = True else: retval = False # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def deleteSessionInWishlist(self, request): """Delete session from wishlist (by websafeSessionKey).""" retval = None # preload necessary data items prof = self._getProfileFromUser() # check if session exists given websafeConfKey # get session; check that it exists wssk = request.websafeSessionKey sess = ndb.Key(urlsafe=wssk).get() if not sess: raise endpoints.NotFoundException('No Session found with key: %s' % wssk) # check if session is not in the wishist if wssk not in prof.sessionKeysInWishList: raise ConflictException( "This session is not added to your wishlist") # remove session from the wishlist prof.sessionKeysInWishList.remove(wssk) retval = True # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def addSessionToWishlist(self, request): """Adds a session to a user's list of sessions they are interested in attending.""" retval = None prof = self._getProfileFromUser() # get user Profile # check if session exists given websafeSessionKey # get session; check that it exists wssk = request.websafeSessionKey sess_key = ndb.Key(urlsafe=wssk) if sess_key.kind() != "Session": raise endpoints.NotFoundException( 'Not a session found with key: %s' % wssk) sess = sess_key.get() if not sess: raise endpoints.NotFoundException('No session found with key: %s' % wssk) # check if user already registered otherwise add if wssk in prof.sessionKeysWishlist: raise ConflictException("You have already added this session") # add to user's wishlist prof.sessionKeysWishlist.append(wssk) retval = True # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def _sessionRegistration(self, request): """Transaction to register or unregister user for selected session.""" retval = None # get user Profile prof = self._getProfileFromUser() # check if session exists given websafeSessionfKey # get conference; check that it exists wssk = request.websafeSessionKey sess = ndb.Key(urlsafe=wssk).get() if not sess: raise endpoints.NotFoundException( 'No session found with key: %s' % wssk) # check if user already registered otherwise add if wssk in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this session") # check if user already registered if wssk in prof.sessionKeysToAttend: retval = False else: # register user to attend session prof.sessionKeysToAttend.append(wssk) retval = True # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def deleteSession(self, request): """Delete a session""" # Get current user user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization required') user_id = getUserId(user) # Get Session Key session_key = ndb.Key(urlsafe=request.websafeSessionKey) # check that session exists if not session_key: raise endpoints.NotFoundException('No session found with key: %s' % request.websafeSessionKey) # Check that user matches conference organizer conference_key = session_key.get().conferenceKey if user_id != conference_key.get().organizerUserId: raise ConflictException( 'Only the conference organizer can delete sessions for the conference' ) session_key.delete() # Delete session_key from profile wishlists profiles = Profile.query() for profile in profiles: if session_key in profile.sessionWishlist: profile.sessionWishlist.remove(session_key) profile.put() return StringMessage(data='Session deleted')
def _wishlistAdding(self, request, add=True): """Add or delete the session from wishlist""" retval = None prof = self._getProfileFromUser() #get user profile wssk = request.websafeSessionKey session = ndb.Key(urlsafe=wssk).get() if not session: raise endpoints.NotFoundException("No Session found with key: %s" % wssk) wsck = session.confKey if wsck not in prof.conferenceKeysToAttend: raise endpoints.UnauthorizedException( "You should register for the conference") #add to wishlist if add: if wssk in prof.sessionKeysToAdd: raise ConflictException("You have already added this session") prof.sessionKeysToAdd.append(wssk) retval = True else: if wssk in prof.sessionKeysToAdd: prof.sessionKeysToAdd.remove(wssk) retval = True else: retval = False prof.put() return BooleanMessage(data=retval)
def _addToWishlist(self, request, add=True): """Adds or delete from the user's wishlist sessions.""" #get profile from user and sessionKey from request prof = self._getProfileFromUser() s_key = request.sessionKey #check to see if session is valid # and if user already added this session session = ndb.Key(urlsafe=s_key).get() if not session: raise endpoints.NotFoundException('No session found with key: %s' % s_key) if add: if s_key in prof.sessionsInWishlist: raise ConflictException( "You have already added this session in your wishlist") else: prof.sessionsInWishlist.append(s_key) else: if s_key in prof.sessionsInWishlist: prof.sessionsInWishlist.remove(s_key) prof.put() session_keys = [ ndb.Key(urlsafe=wssk) for wssk in prof.sessionsInWishlist ] sessions = ndb.get_multi(session_keys) return SessionForms( items=[self._copySessionToForm(session) for session in sessions])
def _sessionWishlist(self, request, add=True): """Add or remove a session to a User's wishlist.""" retval = None # Ensure that user is authed user = endpoints.get_current_user() if not user: raise endpoints.UnauthorizedException('Authorization Required') # Get user's profile prof = self._getProfileFromUser() # Get Session being passed try: sesh_key = ndb.Key(urlsafe=request.webSafeSessionKey) except Exception: raise endpoints.BadRequestException( 'The websafeSessionKey given is invalid.') session = sesh_key.get() # Throw Not Found Error if no Session found if not sesh_key: raise endpoints.NotFoundException( 'No session found with key: {0}'.format(sesh_key)) # Get Session parent key and associated Conference conference = session.key.parent().get() conf_key = conference.key.urlsafe() # Ensure that conference is in User's conferenceKeysToAttend if conf_key not in prof.conferenceKeysToAttend: raise ConflictException( "You must be register for the parent confernce before adding " "a session to your wishlist.") if add: # Check if session is already in wishlist if request.webSafeSessionKey in prof.sessionWishList: raise ConflictException( "This Session is already in your wishlist.") # Add session to User's wishlist prof.sessionWishList.append(request.webSafeSessionKey) retval = True else: # Check if session is already in wishlist if request.webSafeSessionKey in prof.sessionWishList: # Remove Session from User's wishlist prof.sessionWishList.remove(request.webSafeSessionKey) retval = True else: retval = False prof.put() return BooleanMessage(data=retval)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None # Get the Profile prof = self._getProfileFromUser() # Check if the conference exists given a websafeConferenceKey wsck = request.websafeConferenceKey # get the conference object. conf = ndb.Key(urlsafe=wsck).get() # Check if the conf was retrieved checkObj(conf, 'Conference') # register if reg: # check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException( "There are no seats available.") # register user, take away one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if wsck in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)
def _conference_registration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None prof = self._get_profile_from_user() # get user Profile # Check if conf exists given websafeConfKey # Get conference; check that it exists wsck = request.websafeConferenceKey conf = ndb.Key(urlsafe=wsck).get() if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % wsck) # Register if reg: # Check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # Check if seats available if conf.seatsAvailable <= 0: raise ConflictException("There are no seats available.") # Register user, take away one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # Unregister else: # Check if user already registered if wsck in prof.conferenceKeysToAttend: # Unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # Write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)
def _sessionRegistration(self, request, reg=True): """ Given a session, either put it in or remove it from a user's wishlist. Arg: reg (Default=True): If true, add the session to user's wishlist If false, remove the session from user's wishlit. Return: retval(Boolean): If ture, the operation (addition or removal of session from user's wishlist) is successful """ retval = None # User authentication prof = self._getProfileFromUser() # get session object from Datastore wssk = request.websafeSessionKey session = self._getDataStoreObject(wssk) # check whether user has registered for conference where session belongs c_key = session.key.parent() if c_key not in prof.conferenceKeysToAttend: raise ConflictException( "You have yet to register for the conference where this " "session will take place") # put session in wishlist if reg: if session.key in prof.sessionKeysToAttend: raise ConflictException( "You have already placed this session in your wishlist") prof.sessionKeysToAttend.append(session.key) retval = True # remove session from wishlist else: if session.key not in prof.sessionKeysToAttend: raise ConflictException( "This session was not in your wishlist. No action taken.") prof.sessionKeysToAttend.remove(session.key) retval = True prof.put() return BooleanMessage(data=retval)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None prof = self._getProfileFromUser() wsck = request.websafeConferenceKey conf = ndb.Key(urlsafe=wsck).get() if not conf: raise endpoints.NotFoundException( 'No conference found with key: %s' % wsck) if reg: if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") if conf.seatsAvailable <= 0: raise ConflictException( "There are no seats available.") prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True else: if wsck in prof.conferenceKeysToAttend: prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False prof.put() conf.put() return BooleanMessage(data=retval)
def _sessionRegistration(self, request, reg=True): """Add or remove session from wishlist for selected user.""" retval = None prof = self._getProfileFromUser() # get user Profile # check if session exists given sessionKey # get session; check that it exists wssk = request.sessionKey c_key = ndb.Key(urlsafe=wssk).parent() websafe_conference_key = c_key.urlsafe() if not c_key: raise endpoints.NotFoundException( 'No session found with key: %s' % wssk) if c_key.kind() != 'Conference': raise endpoints.BadRequestException( 'Please provide correct session key.\n' 'Error key: %s' % wssk) # register if reg: # check if user already registered otherwise add if wssk in prof.sessionKeysWishlist: raise ConflictException( "The session have already in your wishlist.") # check if user in the conference otherwise add if websafe_conference_key not in prof.conferenceKeysToAttend: raise ConflictException( "You are not register the conference.") # add session to wish list prof.sessionKeysWishlist.append(request.sessionKey) retval = True # unregister else: # check if user already registered if wssk in prof.sessionKeysWishlist: # unregister user, add back one seat prof.sessionKeysWishlist.remove(wssk) retval = True else: retval = False # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None prof = self._getProfileFromUser() # get user Profile # check if conf exists given websafeConfKey # get conference; check that it exists wsck = request.websafeConferenceKey c_key = validate_conf_key(wsck) conf = c_key.get() # register if reg: # check if user already registered otherwise add if wsck in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException( "There are no seats available.") # register user, take away one seat prof.conferenceKeysToAttend.append(wsck) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if wsck in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(wsck) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)
def _updateSessionWishlist(self, request, reg=True): """ It updates the wishlist attribute of the profile entity of user. Stores the session keys It adds the session in wishlist if the reg parameter is true, otherwise removes the session from wishlist This method is transactional so that in case of any failure, the partial changes are reverted """ retval = None prof = self._getProfileFromUser() # get user Profile # check if session exists given websafeSessionKey # get session; check that it exists wssk = request.websafeSessionKey session_key = ndb.Key(urlsafe=wssk) # Assure the websafe key is only for Session and raise exception if # key is provided for non Session kind if session_key.kind() != "Session": raise endpoints.NotFoundException( 'wrong websafeSessionKey provided') session = session_key.get() if not session: raise endpoints.NotFoundException( 'No conference found with key: %s' % wssk) # add session to wishlist if reg: # check if user already has the session in wishlist, otherwise add if session_key in prof.sessionsWishList: raise ConflictException( "This session is already in the wishlist") # register user, take away one seat prof.sessionsWishList.append(session_key) retval = True # remove session from wishlist else: # check if session is already in wishlist if session_key in prof.sessionsWishList: # remove session from wishlist prof.sessionsWishList.remove(session_key) retval = True else: retval = False # write things back to the datastore & return prof.put() return BooleanMessage(data=retval)
def _doWishlist(self, request, add=True): """ add and remove sessions from user's wishlist. """ # get user Profile prof = self._getProfileFromUser() # check if session exists given websafeSessionKey wssk = request.websafeSessionKey s_key = ndb.Key(urlsafe=wssk) # session key session = s_key.get() if not session: raise endpoints.NotFoundException("No session found with key: %s" % wssk) # find the conference key it belongs to wsck = s_key.parent().urlsafe() # check if user has registered this conference if wsck not in prof.conferenceKeysToAttend: raise ConflictException( "You did not regiester for this conference") # add if add: # check if the session has already been added if wssk in prof.wishlist: raise ConflictException( "The session key %s has already in the wishlist" % wssk) # add this session prof.wishlist.append(wssk) # delete else: # check if the session is in the wishlist if wssk not in prof.wishlist: raise ConflictException( "The session key %s is not in the wishlist" % wssk) # remove this session prof.wishlist.remove(wssk) # update the profile in the cloud prof.put() return BooleanMessage(data=True)
def _conferenceRegistration(self, request, reg=True): """Register or unregister user for selected conference.""" retval = None # get user Profile prof = self._getProfileFromUser() # get conference object from Datastore conf = self._getDataStoreObject(request.websafeConferenceKey) # register if reg: # check if user already registered otherwise add if conf.key in prof.conferenceKeysToAttend: raise ConflictException( "You have already registered for this conference") # check if seats avail if conf.seatsAvailable <= 0: raise ConflictException( "There are no seats available.") # register user, take away one seat prof.conferenceKeysToAttend.append(conf.key) conf.seatsAvailable -= 1 retval = True # unregister else: # check if user already registered if conf.key in prof.conferenceKeysToAttend: # unregister user, add back one seat prof.conferenceKeysToAttend.remove(conf.key) conf.seatsAvailable += 1 retval = True else: retval = False # write things back to the datastore & return prof.put() conf.put() return BooleanMessage(data=retval)
def addSessionToWishlist(self, request): """Add a session to the user's wishlist""" # get Session object from request; bail if not found wssk = request.websafeSessionKey sess_key = ndb.Key(urlsafe=wssk) session = sess_key.get() print session if not session: raise endpoints.NotFoundException('No session found with key: %s' % wssk) elif sess_key.kind() != 'Session': raise endpoints.BadRequestException( 'The websafeKey: %s does not belong to a Session object' % wssk) # noqa # Get the session's ancestor conference conference = sess_key.parent().get() # get user Profile prof = self._getProfileFromUser() # Check if user is registered to the conference if not conference.key.urlsafe() in prof.conferenceKeysToAttend: raise ConflictException( 'You need to be registered to the conference in order to join a session' ) # noqa # We don't want to have duplicates if wssk in prof.sessionKeysWishlist: raise ConflictException( 'You have already added this session to your wishlist') # Add the session key to the user's wishlist prof.sessionKeysWishlist.append(wssk) prof.put() # return ProfileForm return self._copyProfileToForm(prof)
def add_session_to_wishlist(self, request): """Add a session to a user's wishlist.""" profile = self._getProfileFromUser() session = self._get_entity_by_key(request.session) # Make sure session hasn't already been added if session.key in profile.sessions_wishlist: raise ConflictException( "You have already added this session to your wishlist.") profile.sessions_wishlist.append(session.key) profile.put() # Return the new, complete wishlist return self._get_wishlist_sessions_as_message(profile)