From 3f3c07e2ae315bb6f1c5c0dd4ec547a692fd71dd Mon Sep 17 00:00:00 2001 From: maxizdev Date: Sun, 15 Jun 2025 21:58:13 +0300 Subject: [PATCH] 1. Add endpoint /health to check server responsibility. 2. Add Breed and Photo to Animal model. 3. Add computed field Age to Animal. 4. Change home.html according to mentioned updates. --- app.py | 13 +++++-- ...4344dc_added_breed_and_photo_to_animals.py | 34 +++++++++++++++++++ models/pydantic/models.py | 16 ++++++++- models/sqlalchemy/models.py | 2 ++ pyproject.toml | 1 + templates/home.html | 33 ++++++++++++++++-- 6 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 migrations/versions/8804934344dc_added_breed_and_photo_to_animals.py diff --git a/app.py b/app.py index 11e2013..867358a 100644 --- a/app.py +++ b/app.py @@ -17,10 +17,15 @@ def home() -> str: return render_template('home.html') +@app.route('/health') +def health() -> tuple[Response, int]: + return jsonify({"message": "Health is OK!"}), 200 + + @app.route('/animals', methods=['GET']) def index() -> Response: animals = Animal.query.all() - return jsonify({"animals": [AnimalResponse.model_validate(animal).model_dump(mode='json') for animal in animals]}) + return jsonify({"animals": [AnimalResponse.model_validate(animal).model_dump(mode='json', exclude_none=True) for animal in animals]}) @app.route('/animal', methods=['POST']) @@ -29,7 +34,9 @@ def add_animal() -> tuple[Response, int]: new_animal = Animal( animal_type=data.animal_type, name=data.name, - birth_date=data.birth_date + birth_date=data.birth_date, + animal_breed=data.animal_breed, + photo_url=data.photo_url, ) db.session.add(new_animal) db.session.commit() @@ -51,6 +58,8 @@ 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.animal_breed = data.animal_breed + animal.photo_url = data.photo_url db.session.commit() return jsonify( { diff --git a/migrations/versions/8804934344dc_added_breed_and_photo_to_animals.py b/migrations/versions/8804934344dc_added_breed_and_photo_to_animals.py new file mode 100644 index 0000000..30f67ea --- /dev/null +++ b/migrations/versions/8804934344dc_added_breed_and_photo_to_animals.py @@ -0,0 +1,34 @@ +"""added breed and photo to animals + +Revision ID: 8804934344dc +Revises: e08fc0218f8b +Create Date: 2025-06-15 00:47:59.705621 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8804934344dc' +down_revision = 'e08fc0218f8b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.add_column(sa.Column('animal_breed', sa.String(), nullable=False)) + batch_op.add_column(sa.Column('photo_url', sa.String(), nullable=False)) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('animal', schema=None) as batch_op: + batch_op.drop_column('photo_url') + batch_op.drop_column('animal_breed') + + # ### end Alembic commands ### diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..a7251e3 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,11 +1,13 @@ +from pydantic import BaseModel, computed_field, ConfigDict from datetime import date -from pydantic import BaseModel, ConfigDict class AnimalCreate(BaseModel): animal_type: str name: str birth_date: date + animal_breed: str + photo_url: str class AnimalResponse(BaseModel): @@ -15,3 +17,15 @@ class AnimalResponse(BaseModel): animal_type: str name: str birth_date: date + animal_breed: str + photo_url: str + + @computed_field + @property + def age(self) -> int: + today = date.today() + res = today.year - self.birth_date.year - ( + (today.month, today.day) < (self.birth_date.month, self.birth_date.day) + ) + return res + diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..89ee100 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -9,3 +9,5 @@ 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) + animal_breed = db.Column(db.String, nullable=False) + photo_url = db.Column(db.String, nullable=False) diff --git a/pyproject.toml b/pyproject.toml index 156df2c..5567089 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,7 @@ version = "0.1.0" description = "" authors = ["marcustas "] readme = "README.md" +package-mode = false [tool.poetry.dependencies] python = "^3.11" diff --git a/templates/home.html b/templates/home.html index 80b942e..def7063 100644 --- a/templates/home.html +++ b/templates/home.html @@ -33,6 +33,14 @@

Animals in the Shelter

+
+ + +
+
+ + +
@@ -46,6 +54,9 @@

List of animals:

Animal Type Name Birth Date + Breed + Age + Photo Actions @@ -80,6 +91,14 @@ +
+ + +
+
+ + +