def setUpClass(cls): db.session.rollback() Pet.query.delete() db.session.commit() Specie.query.delete() db.session.commit() cat = Specie(species="cat") dog = Specie(species="dog") por = Specie(species="porcupine") db.session.add_all([cat, dog, por]) db.session.commit() pet1 = Pet(name="Woofly", species_id=2, photo_url="http://127.0.0.1:5000/static/images/woofly.png", age=1, notes="woofly-notes") pet2 = Pet( name="Taylor", species_id=1, photo_url="http://127.0.0.1:5000/static/images/taylor.jpg", age=8, notes= "A truly lovable, friendly cat!! Any adoptee will be lucky to have her.", available=False) db.session.add(pet1) db.session.commit() db.session.add(pet2) db.session.commit()
def post(self): """ASWP-API Species POST Returns: dict: Dictionary with success message """ data = self.parse_request() Specie.add(data["name"], data["description"], data["price"]) return {"message": "Specie added successfully"}
def get(self): """ASWP-API Species GET Returns: type[Response]: Flask JSON Response with list of Species """ species = [Specie.to_dict(s) for s in Specie.get_all()] species_safe = [{ "id": s["id"], "name": s["name"], "animals_count": Animal.get_count_by_specie(s["id"]) } for s in species] return jsonify(species=species_safe)
def setUpClass(cls): db.session.rollback() Pet.query.delete() db.session.commit() Specie.query.delete() db.session.commit() cat = Specie(species="cat") dog = Specie(species="dog") por = Specie(species="porcupine") db.session.add_all([cat, dog, por]) db.session.commit()
def get(self, specie_id): """ASWP-API Specie GET Args: specie_id (int): Specie ID Returns: type[Response]: Flask JSON Response with Specie data tuple: Tuple with error message and status code """ specie = Specie.get_by_id(specie_id) if specie: specie = Specie.to_dict(specie) else: return {"error": "Specie not found"}, 404 specie_animals = [ Animal.to_safe_dict(a) for a in Animal.get_all_by_specie(specie_id) ] specie["animals"] = specie_animals return jsonify(specie=specie)
def get(self): """ASWP-API Animals GET Returns: type[Response]: Flask JSON Response with list of Animals """ animals = [Animal.to_dict(a) for a in Animal.get_all()] if animals: for animal in animals: if animal["price"] == None: animal["price"] = Specie.get_price(animal["specie"]) return jsonify(animals=animals)
def post(self): """ASWP-API Animals POST Returns: dict: Dictionary with success message tuple: Tuple with error message and status code """ data = self.parse_request() price = data.get("price", None) description = data.get("description", None) center_id = get_jwt_identity()["center_id"] if not Specie.exists(data["specie"]): return {"error": "Specified specie does not exist"}, 409 Animal.add(center_id, data["name"], description, data["age"], data["specie"], price) return {"message": "Animal added successfully"}
def get(self, animal_id): """ASWP-API Animal GET Args: animal_id (int): Animal ID Returns: type[Response]: Flask JSON Response with Animal data tuple: Tuple with error message and status code """ animal = Animal.get_by_id(animal_id) if animal: animal = Animal.to_dict(animal) if animal["price"] == None: animal["price"] = Specie.get_price(animal["specie"]) return jsonify(animal) else: return {"error": "Animal not found"}, 404
def put(self, animal_id): """ASWP-API Animal PUT Args: animal_id (int): ID of Animal to update Returns: dict: Dictionary with success message tuple: Tuple with error message and status code """ data = self.parse_request() center_id = get_jwt_identity()["center_id"] if not Animal.exists(animal_id): return {"error": "Animal not found"}, 404 if not Animal.is_owner(animal_id, center_id): return {"error": "You are not allowed to update this animal"}, 403 if "specie" in data: if not Specie.exists(data["specie"]): return {"error": "Specified specie does not exist"}, 409 Animal.update(animal_id, data) return {"message": "Animal updated successfully"}
from app import db from models import Specie, Pet db.drop_all() db.create_all() cat = Specie(species="cat") dog = Specie(species="dog") por = Specie(species="porcupine") db.session.add_all([cat, dog, por]) db.session.commit() woof = Pet(name="Woofly", species_id=2, photo_url="/static/images/woofly.png", age=1) tay = Pet( name="Taylor", species_id=1, photo_url="/static/images/taylor.jpg", age=8, notes= "A truly lovable, friendly cat!! Any adoptee will be lucky to have her.", available=False) porc = Pet(name="Porchetta", species_id=3, photo_url="/static/images/porchetta.png", age=1, available=True) snar = Pet(name="Snargle", species_id=1,
"""Seed file to make sample data for adopt db.""" from models import Pet, Specie, PetSpeciesTag, db from app import app # Create all tables db.session.rollback() db.drop_all() db.create_all() ####### add species ####### pet = Specie(species="Cat") pet2 = Specie(species="Porcupine") pet3 = Specie(species="Dog") pets = [pet, pet2, pet3] db.session.add_all(pets) db.session.commit() ####### add Pets ####### whiskers = Pet( name="Whiskers", specie_id=1, image_url="https://static01.nyt.com/images/2019/12/20/arts/00cats-1/00cats-1-videoSixteenByNineJumbo1600-v3.jpg", age=2, notes="fun and playful cat...", available=True, ) supercat = Pet(