From de1666ccfb645d58b973c59b5a862d67e1df9bac Mon Sep 17 00:00:00 2001 From: Nataliia Date: Sat, 7 Jun 2025 20:40:17 +0300 Subject: [PATCH 1/2] Breed and health check added --- app.py | 9 ++++++ database.py | 4 +-- ...f2aafb7fe882_add_breed_column_to_animal.py | 32 +++++++++++++++++++ models/pydantic/models.py | 2 ++ models/sqlalchemy/__init__.py | 1 + models/sqlalchemy/models.py | 1 + templates/home.html | 13 ++++++++ 7 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py diff --git a/app.py b/app.py index 11e2013..a99cf4f 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,13 @@ db = init_db(app) +@app.route('/health', methods=['GET']) +def health(): + return jsonify( + { + "Work" + } + ), 200 @app.route('/') def home() -> str: @@ -29,6 +36,7 @@ def add_animal() -> tuple[Response, int]: new_animal = Animal( animal_type=data.animal_type, name=data.name, + breed=data.breed, birth_date=data.birth_date ) db.session.add(new_animal) @@ -50,6 +58,7 @@ def update_animal(pk: int) -> Union[Response, tuple[Response, int]]: animal.animal_type = data.animal_type animal.name = data.name + animal.breed = data.breed animal.birth_date = data.birth_date db.session.commit() return jsonify( diff --git a/database.py b/database.py index 7c8817d..54188d6 100644 --- a/database.py +++ b/database.py @@ -1,7 +1,7 @@ -from flask_sqlalchemy import SQLAlchemy +import flask_sqlalchemy from flask_migrate import Migrate -db = SQLAlchemy() +db = flask_sqlalchemy.SQLAlchemy() migrate = Migrate() diff --git a/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py b/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py new file mode 100644 index 0000000..a35af21 --- /dev/null +++ b/migrations/versions/f2aafb7fe882_add_breed_column_to_animal.py @@ -0,0 +1,32 @@ +"""Add breed column to animal + +Revision ID: f2aafb7fe882 +Revises: e08fc0218f8b +Create Date: 2025-06-07 16:04:35.525625 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f2aafb7fe882' +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('breed', 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('breed') + + # ### end Alembic commands ### diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..068483c 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -5,6 +5,7 @@ class AnimalCreate(BaseModel): animal_type: str name: str + breed: str birth_date: date @@ -14,4 +15,5 @@ class AnimalResponse(BaseModel): id: int animal_type: str name: str + breed: str birth_date: date diff --git a/models/sqlalchemy/__init__.py b/models/sqlalchemy/__init__.py index 4fee0e2..c60fcde 100644 --- a/models/sqlalchemy/__init__.py +++ b/models/sqlalchemy/__init__.py @@ -5,4 +5,5 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) + breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..bcf1083 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -8,4 +8,5 @@ class Animal(db.Model): id = db.Column(db.Integer, primary_key=True) animal_type = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) + breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) diff --git a/templates/home.html b/templates/home.html index 80b942e..eb56f07 100644 --- a/templates/home.html +++ b/templates/home.html @@ -29,6 +29,10 @@

Animals in the Shelter

+
+ + +
@@ -45,6 +49,7 @@

List of animals:

Animal Type Name + Animal Breed Birth Date Actions @@ -76,6 +81,10 @@
+
+ + +
@@ -99,6 +108,7 @@ $('#editAnimalId').val(data.animal.id); $('#editAnimalType').val(data.animal.animal_type); $('#editAnimalName').val(data.animal.name); + $('#editAnimalBreed').val(data.animal.breed); $('#editAnimalBirthDate').val(data.animal.birth_date); $('#editAnimalModal').modal('show'); }); @@ -108,6 +118,7 @@ const updatedAnimal = { animal_type: $('#editAnimalType').val(), name: $('#editAnimalName').val(), + breed: $('#editAnimalBreed').val(), birth_date: $('#editAnimalBirthDate').val() }; @@ -136,6 +147,7 @@ ${animal.animal_type} ${animal.name} + ${animal.breed} ${animal.birth_date} @@ -153,6 +165,7 @@ const newAnimal = { animal_type: $('#animal_type').val(), name: $('#name').val(), + breed: $('#breed').val(), birth_date: $('#birth_date').val() }; $.ajax({ From 8d45ac857c93fa7bc560bb5c9cd74ba93c9d1f0a Mon Sep 17 00:00:00 2001 From: Nataliia Date: Sun, 8 Jun 2025 10:58:04 +0300 Subject: [PATCH 2/2] Add age --- app.py | 5 ++++- models/pydantic/models.py | 7 ++++++- models/sqlalchemy/__init__.py | 1 + models/sqlalchemy/models.py | 1 + templates/home.html | 10 +++++++++- 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index a99cf4f..b2da9fa 100644 --- a/app.py +++ b/app.py @@ -33,12 +33,14 @@ def index() -> Response: @app.route('/animal', methods=['POST']) def add_animal() -> tuple[Response, int]: data = AnimalCreate(**request.get_json()) + new_animal = Animal( animal_type=data.animal_type, name=data.name, breed=data.breed, - birth_date=data.birth_date + birth_date=data.birth_date, ) + db.session.add(new_animal) db.session.commit() return jsonify( @@ -101,3 +103,4 @@ def initialize_app(): if __name__ == '__main__': initialize_app() app.run(debug=True) + diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 068483c..9584cde 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -1,5 +1,5 @@ from datetime import date -from pydantic import BaseModel, ConfigDict +from pydantic import BaseModel, ConfigDict, computed_field class AnimalCreate(BaseModel): @@ -17,3 +17,8 @@ class AnimalResponse(BaseModel): name: str breed: str birth_date: date + + @computed_field + #@property + def age(self) -> int: + return date.today().year - self.birth_date.year diff --git a/models/sqlalchemy/__init__.py b/models/sqlalchemy/__init__.py index c60fcde..722174c 100644 --- a/models/sqlalchemy/__init__.py +++ b/models/sqlalchemy/__init__.py @@ -7,3 +7,4 @@ class Animal(db.Model): name = db.Column(db.String, nullable=False) breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + #age = db.Column(db.Integer, nullable=False) diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index bcf1083..2ecb98c 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -10,3 +10,4 @@ class Animal(db.Model): name = db.Column(db.String, nullable=False) breed = db.Column(db.String, nullable=False) birth_date = db.Column(db.Date, nullable=False) + #age = db.Column(db.Integer, nullable=True) diff --git a/templates/home.html b/templates/home.html index eb56f07..3c9130d 100644 --- a/templates/home.html +++ b/templates/home.html @@ -51,6 +51,7 @@

List of animals:

Name Animal Breed Birth Date + Age Actions @@ -82,13 +83,17 @@
- +
+
+ + +