diff --git a/app.py b/app.py index 11e2013..7108663 100644 --- a/app.py +++ b/app.py @@ -1,3 +1,4 @@ +import requests from flask import Flask, jsonify, request, Response, render_template from models.pydantic.models import AnimalCreate, AnimalResponse @@ -12,6 +13,18 @@ db = init_db(app) +def get_random_dog_photo(): + response = requests.get('https://dog.ceo/api/breeds/image/random') + data = response.json() + return data['message'] + + +def get_random_cat_photo(): + response = requests.get('https://api.thecatapi.com/v1/images/search') + data = response.json() + return data[0]['url'] + + @app.route('/') def home() -> str: return render_template('home.html') @@ -26,10 +39,24 @@ def index() -> Response: @app.route('/animal', methods=['POST']) def add_animal() -> tuple[Response, int]: data = AnimalCreate(**request.get_json()) + + if not data.photo_url: + if data.animal_type.lower() == 'dog': + photo_url = get_random_dog_photo() + elif data.animal_type.lower() == 'cat': + photo_url = get_random_cat_photo() + else: + photo_url = None + else: + photo_url = data.photo_url + new_animal = Animal( animal_type=data.animal_type, name=data.name, - birth_date=data.birth_date + birth_date=data.birth_date, + breed=data.breed, + photo_url=photo_url, + animal_age=data.age ) db.session.add(new_animal) db.session.commit() @@ -51,6 +78,11 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]: animal.animal_type = data.animal_type animal.name = data.name animal.birth_date = data.birth_date + animal.breed = data.breed + + if data.photo_url: + animal.photo_url = data.photo_url + db.session.commit() return jsonify( { @@ -84,6 +116,11 @@ def delete_animal(pk: int) -> Union[Response, tuple[Response, int]]: return jsonify({"message": "Animal deleted successfully!"}) +@app.route('/health') +def health(): + return '', 200 + + def initialize_app(): with app.app_context(): db.create_all() diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..95c0fdf 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -2,16 +2,31 @@ from pydantic import BaseModel, ConfigDict -class AnimalCreate(BaseModel): +class AnimalAge: + birth_date: date + + @property + def age(self) -> int: + today = date.today() + animal_age = today.year - self.birth_date.year + if (today.month, today.day) < (self.birth_date.month, self.birth_date.day): + animal_age -= 1 + return animal_age + + +class AnimalCreate(BaseModel, AnimalAge): animal_type: str name: str - birth_date: date + breed: str + photo_url: str -class AnimalResponse(BaseModel): +class AnimalResponse(BaseModel, AnimalAge): model_config = ConfigDict(from_attributes=True) id: int animal_type: str name: str - birth_date: date + breed: str + photo_url: str + animal_age: int diff --git a/models/sqlalchemy/__init__.py b/models/sqlalchemy/__init__.py index 4fee0e2..3c4da88 100644 --- a/models/sqlalchemy/__init__.py +++ b/models/sqlalchemy/__init__.py @@ -6,3 +6,6 @@ class Animal(db.Model): animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + breed = db.Column(db.String, nullable=False) + photo_url = db.Column(db.String, nullable=True) + animal_age = db.Column(db.Integer, nullable=False) diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..74e35dc 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -9,3 +9,6 @@ class Animal(db.Model): animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + breed = db.Column(db.String, nullable=False) + photo_url = db.Column(db.String, nullable=True) + animal_age = db.Column(db.Integer, nullable=False) diff --git a/pyproject.toml b/pyproject.toml index 156df2c..c1c63f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ pydantic = "^2.4.2" flask-sqlalchemy = "^3.1.1" flask-migrate = "^4.0.5" pydantic-settings = "^2.0.3" +requests = "^2.31.0" [build-system] diff --git a/templates/home.html b/templates/home.html index 80b942e..5427ebd 100644 --- a/templates/home.html +++ b/templates/home.html @@ -32,6 +32,14 @@