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 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 delete(self, id=None): # An unauthenticated user is DEFINITELY NOT ALLOWED to do deletions httpcodes.write_forbidden(self) return
def put(self, id=None): httpcodes.write_forbidden(self) return
def patch(self, id=None): # No modifications to pokemon are allowed, since client is not # authenticated here. httpcodes.write_forbidden(self) return
def post(self, id=None): # Pokemon cannot be created through this handler # since users are not authenticated httpcodes.write_forbidden(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