Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
Flask example project
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
15 changes: 13 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -29,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()
Expand All @@ -50,6 +59,8 @@ 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.photo_url = data.photo_url
animal.birth_date = data.birth_date
db.session.commit()
return jsonify(
Expand Down
34 changes: 34 additions & 0 deletions migrations/versions/f67eaa69e91f_add_columns.py
Original file line number Diff line number Diff line change
@@ -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 ###
15 changes: 14 additions & 1 deletion models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from datetime import date, timedelta
from pydantic import BaseModel, ConfigDict, computed_field
from datetime import date
from pydantic import BaseModel, ConfigDict


class AnimalCreate(BaseModel):
animal_type: str
name: str
birth_date: date
breed: str
photo_url: str


class AnimalResponse(BaseModel):
Expand All @@ -15,3 +18,13 @@ class AnimalResponse(BaseModel):
animal_type: str
name: str
birth_date: date
breed: str
photo_url: str


@computed_field
@property
def age(self) -> int:
return date.today().year - self.birth_date.year


3 changes: 3 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

33 changes: 31 additions & 2 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ <h2>Animals in the Shelter</h2>
<label for="birth_date">Birth Date:</label>
<input type="date" class="form-control" id="birth_date" required>
</div>
<div class="form-group">
<label for="breed">Breed:</label>
<input type="text" class="form-control" id="breed" required>
</div>
<div class="form-group">
<label for="photo_url">Photo URL:</label>
<input type="text" class="form-control" id="photo_url" required>
</div>

<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
</div>
Expand All @@ -46,6 +55,9 @@ <h4 class="mt-4">List of animals:</h4>
<th>Animal Type</th>
<th>Name</th>
<th>Birth Date</th>
<th>Breed</th>
<th>Photo</th>
<th>Age</th>
<th>Actions</th>
</tr>
</thead>
Expand Down Expand Up @@ -80,6 +92,14 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalBirthDate">Birth Date:</label>
<input type="date" class="form-control" id="editAnimalBirthDate" required>
</div>
<div class="form-group">
<label for="editBreed">Breed:</label>
<input type="text" class="form-control" id="editBreed" required>
</div>
<div class="form-group">
<label for="editPhotoURL">Photo URL:</label>
<input type="text" class="form-control" id="editPhotoURL" required>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -100,6 +120,8 @@ <h5 class="modal-title">Edit Animal</h5>
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editBreed').val(data.animal.breed);
$('#editPhotoURL').val(data.animal.photo_url);
$('#editAnimalModal').modal('show');
});
}
Expand All @@ -108,7 +130,9 @@ <h5 class="modal-title">Edit Animal</h5>
const updatedAnimal = {
animal_type: $('#editAnimalType').val(),
name: $('#editAnimalName').val(),
birth_date: $('#editAnimalBirthDate').val()
birth_date: $('#editAnimalBirthDate').val(),
breed: $('#editBreed').val(),
photo_url: $('#editPhotoURL').val(),
};

const animalId = $('#editAnimalId').val();
Expand Down Expand Up @@ -137,6 +161,9 @@ <h5 class="modal-title">Edit Animal</h5>
<td>${animal.animal_type}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${animal.breed}</td>
<td><img src='${animal.photo_url}' /></td>
<td>${animal.age}</td>
<td>
<button class="btn btn-warning btn-sm" onclick="updateAnimal(${animal.id})">Update</button>
<button class="btn btn-danger btn-sm" onclick="deleteAnimal(${animal.id})">Delete</button>
Expand All @@ -153,7 +180,9 @@ <h5 class="modal-title">Edit Animal</h5>
const newAnimal = {
animal_type: $('#animal_type').val(),
name: $('#name').val(),
birth_date: $('#birth_date').val()
birth_date: $('#birth_date').val(),
breed: $('#breed').val(),
photo_url: $('#photo_url').val(),
};
$.ajax({
url: '/animal',
Expand Down