def delete(self, id=None): try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return # handles requests for trainer/account information if value_is_not_present(id): # Read-Access to all trainer accounts is not allowed httpcodes.write_forbidden(self) return if validate_token_and_id(self, id, id_token) == False: return trainer = TrainerHandler.get_trainer(id, id_token) if not trainer: httpcodes.write_not_found(self) return # Once you have the key, use it to delete all the trainer's pokemon AND # delete the trainer. success_flag = TrainerHandler.prep_delete(trainer) if success_flag == False: httpcodes.write_conflict(self) return trainer.key.delete() httpcodes.write_no_content(self) return
def validate_token_and_id(self, id, id_token): if token_not_valid(self, id_token): # Reject the request print("token invalid") self.response.write("bad token") httpcodes.write_bad_request(self) return False if id != "me": print("THE TRAINER ID IS") print(id) if id_not_valid(self, id, id_token): # If the id doesn't match the OpenId Connect Token info, or if # the token info is simply not valid accordiing to Google, reject the call. httpcodes.write_forbidden(self) return False return True
def delete(self, trainer_id=None, pokemon_id=None): try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return if value_is_not_present(trainer_id): httpcodes.write_bad_request(self) return if validate_token_and_id(self, trainer_id, id_token) == False: return # Get the trainer before doing anything else trainer = TrainerHandler.get_trainer(trainer_id, id_token) if not trainer: httpcodes.write_not_found(self) return if value_is_not_present(pokemon_id): # Then we just need to delete ALL the trainer's pokemon for p_id in trainer.pokemon: p_key = ndb.Key(urlsafe=p_id) p_key.delete() trainer.pokemon = [] else: # Then we have a specific pokemon to delete if pokemon_id not in trainer.pokemon: httpcodes.write_bad_request(self) return ndb.Key(urlsafe=pokemon_id).delete() trainer.pokemon = [p for p in trainer.pokemon if p != pokemon_id] trainer.put() httpcodes.write_no_content(self) return
def get(self, id=None): try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return # handles requests for trainer/account information if value_is_not_present(id): # Read-Access to all trainer accounts is not allowed httpcodes.write_method_not_allowed(self) return if validate_token_and_id(self, id, id_token) == False: print("NOT VALID") return # The id is valid, and we can use it. if id == "me": # use the token to search for and access the account's Trainer Entity trainer = TrainerHandler.get_trainer_by_token(id_token) if not trainer: trainer = TrainerHandler.create_trainer_by_token(id_token) else: # We work under the assumption that the client has already # created a valid account represented by the id. # Hence it is an error if the client tries to access a Trainer # that doesn't exist. try: trainer_key = ndb.Key(urlsafe=id) trainer = trainer_key.get() except: #If trainer key cannot be obtained, reject the request httpcodes.write_bad_request(self) return # Send back the trainer info print(trainer) TrainerHandler.return_trainer_to_client(self, trainer) httpcodes.write_ok(self) return
def patch(self, id=None): try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return # handles requests for trainer/account information if value_is_not_present(id): # Read-Access to all trainer accounts is not allowed print("NOT PRESENT") httpcodes.write_forbidden(self) return if validate_token_and_id(self, id, id_token) == False: print("INVALID") return # Check that the properties to patch are all valid new_data = json.loads(self.request.body) properties = new_data.keys() for p in properties: if p not in TrainerHandler.patch_properties: httpcodes.write_bad_request(self) return trainer = TrainerHandler.get_trainer(id, id_token) if not trainer: httpcodes.write_not_found(self) return # Once trainer is found, patch its properties # using the request data, and then save its data. for p in properties: trainer = update(trainer, new_data, p) trainer.put() TrainerHandler.return_trainer_to_client(self, trainer) httpcodes.write_ok(self) return
def get(self, trainer_id=None, pokemon_id=None): print("TRAINER HANDLER 2") try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return if value_is_not_present(trainer_id): httpcodes.write_bad_request(self) return if validate_token_and_id(self, trainer_id, id_token) == False: return # Get the trainer before doing anything else trainer = TrainerHandler.get_trainer(trainer_id, id_token) if not trainer: print("TRAINER NOT FOUND") httpcodes.write_not_found(self) return if value_is_not_present(pokemon_id): print("GETTING POKEMON") # Then we just have the trainer id, and need to show his pokemon pokemon_list = ndb.get_multi( map(lambda p_id: ndb.Key(urlsafe=p_id), trainer.pokemon)) pokemon_list = map( lambda p: PokemonHandler.prep_pokemon_dict_for_owner( p.to_dict(), p.key.urlsafe()), pokemon_list) self.response.write(json.dumps(pokemon_list)) httpcodes.write_ok(self) else: # Then we have trainer and pokemon id, and need to show the specific pokemon # Verify that the pokemon in fact belongs to the trainer if pokemon_id not in trainer.pokemon: httpcodes.write_bad_request(self) return # If pokemon belongs to trainer, then send it to the trainer! try: pokemon = ndb.Key(urlsafe=pokemon_id).get() except: # Pokemon was somehow accidentally deleted! Let client know to try again trainer.pokemon = [ p for p in trainer.pokemon if p != pokemon_id ] httpcodes.write_conflict(self) return PokemonHandler.return_pokemon_to_owner_client(self, pokemon) httpcodes.write_ok(self) return
def patch(self, trainer_id=None, pokemon_id=None): try: id_token = get_decoded_id_token(self) except: httpcodes.write_bad_request(self) return if value_is_not_present(trainer_id): httpcodes.write_bad_request(self) return if validate_token_and_id(self, trainer_id, id_token) == False: return if value_is_not_present(pokemon_id): # Not allowed to patch the entire list of a trainer's pokemon httpcodes.write_forbiddent(self) return # Get the trainer before doing anything else trainer = TrainerHandler.get_trainer(trainer_id, id_token) if not trainer: httpcodes.write_not_found(self) return # Get the patch data and check it is all valid new_data = json.loads(self.request.body) properties = new_data.keys() for p in properties: if p not in TrainerHandler2.patch_properties: print("PROPERTY NOT FOUND") write_bad_request(self) return # If a specific pokemon is given to patch, then patch it if pokemon_id not in trainer.pokemon: print("POKEMON NOT FOUND") httpcodes.write_bad_request(self) return pokemon = ndb.Key(urlsafe=pokemon_id).get() for p in properties: pokemon = update(pokemon, new_data, p) pokemon.put() PokemonHandler.return_pokemon_to_owner_client(self, pokemon) httpcodes.write_ok(self) return
def post(self, trainer_id=None, pokemon_id=None): try: id_token = get_decoded_id_token(self) except: print("exception in getting token") httpcodes.write_bad_request(self) return # handles requests for trainer/account information if value_is_not_present(trainer_id): httpcodes.write_forbidden(self) return if validate_token_and_id(self, trainer_id, id_token) == False: print("Exception in validating token") return if pokemon_id: # Can't post to a pokemon itself. httpcodes.method_not_allowed(self) return # Check that the properties for the post are all valid new_data = json.loads(self.request.body) properties = new_data.keys() for p in properties: if p not in TrainerHandler2.post_properties: print("bad property") httpcodes.write_bad_request(self) return for required in TrainerHandler2.required_post_properties: if required not in properties: print("bad required property") httpcodes.write_bad_request(self) return # Get the trainer and create a new pokemon for it trainer = TrainerHandler.get_trainer(trainer_id, id_token) print("TRAINER?") print(trainer) print(trainer.pokemon) if not trainer: httpcodes.write_not_found(self) return male = 0 female = 1 gender_int = random.randint(male, female) pokemon = Pokemon(current_owner=trainer.key.urlsafe(), name=new_data["name"], nickname=new_data["name"], level=0, xp=0, friends=[]) if gender_int == male: pokemon.gender = "male" elif gender_int == female: pokemon.gender = "female" pokemon_key = pokemon.put() pokemon_id = pokemon_key.urlsafe() trainer.pokemon.append(pokemon_id) trainer.put() # Return the newly created pokemon to the owner client PokemonHandler.return_pokemon_to_owner_client(self, pokemon) httpcodes.write_created(self) return