From 9ec227c1954fcca71e6ab5bea3b93c3280bd028e Mon Sep 17 00:00:00 2001 From: Andrii Ostapenko Date: Wed, 4 Jun 2025 23:55:53 +0300 Subject: [PATCH 1/2] Added /health endpoin, also added pet breed and photo url fields and age method in pydantic model --- app.py | 8 ++++- .../{e08fc0218f8b_.py => d537062e2bfc_.py} | 8 +++-- models/pydantic/models.py | 17 +++++++++- models/sqlalchemy/models.py | 2 ++ templates/home.html | 32 +++++++++++++++++-- 5 files changed, 60 insertions(+), 7 deletions(-) rename migrations/versions/{e08fc0218f8b_.py => d537062e2bfc_.py} (77%) diff --git a/app.py b/app.py index 11e2013..8a37133 100644 --- a/app.py +++ b/app.py @@ -28,8 +28,10 @@ def add_animal() -> tuple[Response, int]: data = AnimalCreate(**request.get_json()) new_animal = Animal( animal_type=data.animal_type, + animal_breed=data.animal_breed, name=data.name, - birth_date=data.birth_date + birth_date=data.birth_date, + animal_photo=data.animal_photo ) db.session.add(new_animal) db.session.commit() @@ -84,6 +86,10 @@ def delete_animal(pk: int) -> Union[Response, tuple[Response, int]]: return jsonify({"message": "Animal deleted successfully!"}) +@app.route('/health') +def health(): + return "Ok", 200 + def initialize_app(): with app.app_context(): db.create_all() diff --git a/migrations/versions/e08fc0218f8b_.py b/migrations/versions/d537062e2bfc_.py similarity index 77% rename from migrations/versions/e08fc0218f8b_.py rename to migrations/versions/d537062e2bfc_.py index 9a2228e..37121e9 100644 --- a/migrations/versions/e08fc0218f8b_.py +++ b/migrations/versions/d537062e2bfc_.py @@ -1,8 +1,8 @@ """empty message -Revision ID: e08fc0218f8b +Revision ID: d537062e2bfc Revises: -Create Date: 2023-10-04 14:18:48.750677 +Create Date: 2025-06-04 23:09:27.819725 """ from alembic import op @@ -10,7 +10,7 @@ # revision identifiers, used by Alembic. -revision = 'e08fc0218f8b' +revision = 'd537062e2bfc' down_revision = None branch_labels = None depends_on = None @@ -23,6 +23,8 @@ def upgrade(): sa.Column('animal_type', sa.String(), nullable=False), sa.Column('name', sa.String(), nullable=False), sa.Column('birth_date', sa.Date(), nullable=False), + sa.Column('animal_breed', sa.String(), nullable=False), + sa.Column('animal_photo', sa.String(), nullable=False), sa.PrimaryKeyConstraint('id') ) # ### end Alembic commands ### diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..9c7f067 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,11 +1,13 @@ from datetime import date -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, computed_field class AnimalCreate(BaseModel): animal_type: str + animal_breed: str name: str birth_date: date + animal_photo: str class AnimalResponse(BaseModel): @@ -13,5 +15,18 @@ class AnimalResponse(BaseModel): id: int animal_type: str + animal_breed: str name: str birth_date: date + animal_photo: str + + @computed_field + @property + def age(self) -> int: + today = date.today() + years = today.year - self.birth_date.year + + if (today.month, today.day) < (self.birth_date.month, self.birth_date.day): + years -= 1 + + return years diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..6b78e04 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -7,5 +7,7 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) animal_type = db.Column(db.String, nullable=False) + animal_breed = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + animal_photo = db.Column(db.String, nullable=False) diff --git a/templates/home.html b/templates/home.html index 80b942e..5f9f78a 100644 --- a/templates/home.html +++ b/templates/home.html @@ -25,6 +25,10 @@

Animals in the Shelter

+
+ + +
@@ -33,6 +37,10 @@

Animals in the Shelter

+
+ + +
@@ -44,8 +52,11 @@

List of animals:

Animal Type + Breed Name Birth Date + Age + Photo Actions @@ -72,6 +83,10 @@ +
+ + +
@@ -80,6 +95,10 @@
+
+ + +