def update_animal(id): request_data = request.get_json() center_id = get_id_from_token(request.args.get('token')) if center_id is None: return jsonify({'error': 'invalid token'}), 401 if not validate_animal(request_data): invalid_animal_creation_request_error_message = { "error": "invalid animal", "helpString": "animal request should contain center_id, name, age and species values. " "Price and age should be correct formatted" } response = Response( json.dumps(invalid_animal_creation_request_error_message), 400, mimetype='application/json') return response if Center.get_center_by_id(int(request_data['center_id'])) is None: no_such_center_id_error_message = { "error": "invalid center", "helpString": "center with such id does not exists" } response = Response(json.dumps(no_such_center_id_error_message), 400, mimetype='application/json') return response elif Species.get_species_by_name(request_data['species']) is None: no_such_specie_error_message = { "error": "invalid specie", "helpString": "specie with such name does not exists" } response = Response(json.dumps(no_such_specie_error_message), 400, mimetype='application/json') return response elif Animal.get_animal_by_id(id) is None: no_such_animal_error_message = { "error": "invalid animal", "helpString": "animal with such id does not exists" } response = Response(json.dumps(no_such_animal_error_message), 400, mimetype='application/json') return response else: Animal.update_animal(id, request_data['center_id'], request_data['name'], request_data['description'], int(request_data['age']), request_data['species'], float(request_data['price'])) response = Response("", 204, mimetype='application/json') logging.info('PUT /animals animal_id:{} center_id:{}'.format( id, center_id)) return response
def get_animal_by_id(id): animal = Animal.get_animal_by_id(id) if animal is None: no_such_animal_error_message = { "error": "animal not found", "helpString": "there is no animal with such id in database" } response = Response(json.dumps(no_such_animal_error_message), 404, mimetype='application/json') return response else: return jsonify(Animal.detailed_json(animal))
def delete_animal(id): center_id = get_id_from_token(request.args.get('token')) if center_id is None: return jsonify({'error': 'invalid token'}), 401 animal = Animal.get_animal_by_id(id) if animal.center_id != center_id: wrong_center_id_error_message = { "error": "wrong_center_id", "helpString": "You cant delete animals not from your center" } response = Response(json.dumps(wrong_center_id_error_message), 400, mimetype='application/json') return response if Animal.delete_animal(id): logging.info('DELETE /animals animal_id:{} center_id:{}'.format( id, center_id)) return Response("", status=204) else: return Response("", status=404)
def get_specie_by_id(id): specie = Species.get_species_by_id(id) if specie is None: no_such_specie_error_message = { "error": "specie not found", "helpString": "there is no specie with such id in database" } response = Response(json.dumps(no_such_specie_error_message), 404, mimetype='application/json') return response else: return jsonify(Animal.get_animals_by_species(specie.name))
def get_center_by_id(id): center = Center.get_center_by_id(id) if center is None: no_such_center_error_message = { "error": "center not found", "helpString": "there is no center with such id in database" } response = Response(json.dumps(no_such_center_error_message), 404, mimetype='application/json') return response else: return jsonify({ 'id': center.id, 'login': center.login, 'address': center.address, 'animals': Animal.get_animals_by_center_id(id) })
def get_all_animals(): return jsonify({'animals': Animal.get_all_animals()})
def get_all_species(): species = Species.get_all_species() result = {} for specie in species: result[specie.name] = Animal.get_animals_count_by_species(specie.name) return jsonify(result)