From d7dc8ddf4629e72bf0399f7f8877f7016798ee0f Mon Sep 17 00:00:00 2001 From: serhii_karakash Date: Tue, 3 Jun 2025 19:07:46 +0300 Subject: [PATCH 1/3] Added breed column/form field --- app.py | 3 +- ...9175d1_add_breed_column_to_animal_table.py | 32 +++++++++++++++++++ models/pydantic/models.py | 1 + models/sqlalchemy/models.py | 1 + templates/home.html | 7 +++- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 migrations/versions/ed59229175d1_add_breed_column_to_animal_table.py diff --git a/app.py b/app.py index 11e2013..19e660a 100644 --- a/app.py +++ b/app.py @@ -29,7 +29,8 @@ 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, + breed=data.breed ) db.session.add(new_animal) db.session.commit() diff --git a/migrations/versions/ed59229175d1_add_breed_column_to_animal_table.py b/migrations/versions/ed59229175d1_add_breed_column_to_animal_table.py new file mode 100644 index 0000000..7c5b4ec --- /dev/null +++ b/migrations/versions/ed59229175d1_add_breed_column_to_animal_table.py @@ -0,0 +1,32 @@ +"""Add breed column to animal table + +Revision ID: ed59229175d1 +Revises: e08fc0218f8b +Create Date: 2025-06-03 18:45:44.575523 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ed59229175d1' +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..7544877 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -6,6 +6,7 @@ class AnimalCreate(BaseModel): animal_type: str name: str birth_date: date + breed: str class AnimalResponse(BaseModel): diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..a56fa31 100644 --- a/models/sqlalchemy/models.py +++ b/models/sqlalchemy/models.py @@ -9,3 +9,4 @@ 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) diff --git a/templates/home.html b/templates/home.html index 80b942e..83f1cc0 100644 --- a/templates/home.html +++ b/templates/home.html @@ -33,6 +33,10 @@

Animals in the Shelter

+
+ + +
@@ -153,7 +157,8 @@ const newAnimal = { animal_type: $('#animal_type').val(), name: $('#name').val(), - birth_date: $('#birth_date').val() + birth_date: $('#birth_date').val(), + breed: $('#breed').val() }; $.ajax({ url: '/animal', From 90c4a31657d3f26fe485dab72cb5338c73126689 Mon Sep 17 00:00:00 2001 From: serhii_karakash Date: Wed, 4 Jun 2025 23:41:48 +0300 Subject: [PATCH 2/3] Added image view of an Animal --- app.py | 6 ++++- models/pydantic/models.py | 12 +++++++-- models/sqlalchemy/models.py | 1 + templates/home.html | 54 +++++++++++++++++++++++++++++++------ 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 19e660a..7c1fb63 100644 --- a/app.py +++ b/app.py @@ -30,7 +30,8 @@ def add_animal() -> tuple[Response, int]: animal_type=data.animal_type, name=data.name, birth_date=data.birth_date, - breed=data.breed + breed=data.breed, + image_url=data.image_url ) db.session.add(new_animal) db.session.commit() @@ -52,6 +53,9 @@ 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 + animal.image_url = data.image_url + db.session.commit() return jsonify( { diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 7544877..4c1eb7e 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 datetime import date, timedelta +from pydantic import BaseModel, ConfigDict, computed_field class AnimalCreate(BaseModel): @@ -7,6 +7,7 @@ class AnimalCreate(BaseModel): name: str birth_date: date breed: str + image_url: str class AnimalResponse(BaseModel): @@ -16,3 +17,10 @@ class AnimalResponse(BaseModel): animal_type: str name: str birth_date: date + breed: str + image_url: str + + @computed_field + def age(self) -> int: + today = date.today() + return today.year - self.birth_date.year - ((today.month, today.day) < (self.birth_date.month, self.birth_date.day)) \ No newline at end of file diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index a56fa31..8577b33 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) birth_date = db.Column(db.Date, nullable=False) breed = db.Column(db.String, nullable=False) + image_url = db.Column(db.String, nullable=False) diff --git a/templates/home.html b/templates/home.html index 83f1cc0..d144a1a 100644 --- a/templates/home.html +++ b/templates/home.html @@ -35,7 +35,14 @@

Animals in the Shelter

- + +
+
+ +
@@ -48,8 +55,11 @@

List of animals:

Animal Type + Breed Name Birth Date + Age + Image Actions @@ -84,6 +94,17 @@ +
+ + +
+
+ + +