From 320ccb73165e7e6f0c3841871ef9c902345ce11c Mon Sep 17 00:00:00 2001 From: Kate Biliak Date: Sun, 14 Apr 2024 20:44:33 -0400 Subject: [PATCH 1/6] endpoint 'health' was added --- app.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 11e2013..7bd8856 100644 --- a/app.py +++ b/app.py @@ -20,7 +20,14 @@ def home() -> str: @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') for + animal in animals]}) + + +@app.route('/health') +def health(): + return Response(status=200) @app.route('/animal', methods=['POST']) From 500ce140f1326eb666fae36e9a817f944a7ac8f8 Mon Sep 17 00:00:00 2001 From: Kate Biliak Date: Sun, 14 Apr 2024 21:15:56 -0400 Subject: [PATCH 2/6] `breed` and `photo_url` columns were added --- README.md | 9 ++++- .../versions/f67eaa69e91f_add_columns.py | 34 +++++++++++++++++++ models/sqlalchemy/models.py | 3 ++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/f67eaa69e91f_add_columns.py diff --git a/README.md b/README.md index 41c0b3e..2ed2b00 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -Flask example project \ No newline at end of file +Flask example project + + +## run flask migration +1. `flask db init` - generate migration folder +2. `flask db stamp head` - update database +3. `flask db migrate -m 'comment'` - autogenerate migration script +4. `flask db upgrade` - apply migration \ No newline at end of file diff --git a/migrations/versions/f67eaa69e91f_add_columns.py b/migrations/versions/f67eaa69e91f_add_columns.py new file mode 100644 index 0000000..44fe4d0 --- /dev/null +++ b/migrations/versions/f67eaa69e91f_add_columns.py @@ -0,0 +1,34 @@ +"""add columns + +Revision ID: f67eaa69e91f +Revises: e08fc0218f8b +Create Date: 2024-04-14 21:07:50.074349 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'f67eaa69e91f' +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)) + 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('breed') + + # ### end Alembic commands ### diff --git a/models/sqlalchemy/models.py b/models/sqlalchemy/models.py index dc6e14a..cfa983f 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=False) + From d592b1e8dffe1307174bf0d920d000a2f840d613 Mon Sep 17 00:00:00 2001 From: Kate Biliak Date: Sun, 14 Apr 2024 21:45:56 -0400 Subject: [PATCH 3/6] Added possibility to create an animal with breed and photo url --- app.py | 4 +++- models/pydantic/models.py | 4 ++++ templates/home.html | 21 ++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 7bd8856..dc0bb35 100644 --- a/app.py +++ b/app.py @@ -36,7 +36,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, + breed=data.breed, + photo_url=data.photo_url, ) db.session.add(new_animal) db.session.commit() diff --git a/models/pydantic/models.py b/models/pydantic/models.py index 91e66db..7f84b5b 100644 --- a/models/pydantic/models.py +++ b/models/pydantic/models.py @@ -6,6 +6,8 @@ class AnimalCreate(BaseModel): animal_type: str name: str birth_date: date + breed: str + photo_url: str class AnimalResponse(BaseModel): @@ -15,3 +17,5 @@ class AnimalResponse(BaseModel): animal_type: str name: str birth_date: date + breed: str + photo_url: str diff --git a/templates/home.html b/templates/home.html index 80b942e..8f49aeb 100644 --- a/templates/home.html +++ b/templates/home.html @@ -33,6 +33,15 @@

Animals in the Shelter

+
+ + +
+
+ + +
+ @@ -46,6 +55,8 @@

List of animals:

Animal Type Name Birth Date + Breed + Photo URL Actions @@ -80,6 +91,10 @@ +
+ + +
+
+ + +