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
13 changes: 11 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ def home() -> str:
return render_template('home.html')


@app.route('/health')
def health() -> tuple[Response, int]:
return jsonify({"message": "Health is OK!"}), 200


@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', exclude_none=True) for animal in animals]})


@app.route('/animal', methods=['POST'])
Expand All @@ -29,7 +34,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,
animal_breed=data.animal_breed,
photo_url=data.photo_url,
)
db.session.add(new_animal)
db.session.commit()
Expand All @@ -51,6 +58,8 @@ 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.animal_breed = data.animal_breed
animal.photo_url = data.photo_url
db.session.commit()
return jsonify(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""added breed and photo to animals

Revision ID: 8804934344dc
Revises: e08fc0218f8b
Create Date: 2025-06-15 00:47:59.705621

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '8804934344dc'
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('animal_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('animal_breed')

# ### end Alembic commands ###
16 changes: 15 additions & 1 deletion models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from pydantic import BaseModel, computed_field, ConfigDict
from datetime import date
from pydantic import BaseModel, ConfigDict


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


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

@computed_field
@property
def age(self) -> int:
today = date.today()
res = today.year - self.birth_date.year - (
(today.month, today.day) < (self.birth_date.month, self.birth_date.day)
)
return res

2 changes: 2 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ 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)
animal_breed = db.Column(db.String, nullable=False)
photo_url = db.Column(db.String, nullable=False)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
description = ""
authors = ["marcustas <stas.chernov@casafari.com>"]
readme = "README.md"
package-mode = false

[tool.poetry.dependencies]
python = "^3.11"
Expand Down
33 changes: 31 additions & 2 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ <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="animal_breed">Breed:</label>
<input type="text" class="form-control" id="animal_breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="photo_url">Photo:</label>
<input type="url" class="form-control" id="photo_url" placeholder="Photo URL" required>
</div>
<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
</div>
Expand All @@ -46,6 +54,9 @@ <h4 class="mt-4">List of animals:</h4>
<th>Animal Type</th>
<th>Name</th>
<th>Birth Date</th>
<th>Breed</th>
<th>Age</th>
<th>Photo</th>
<th>Actions</th>
</tr>
</thead>
Expand Down Expand Up @@ -80,6 +91,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="editAnimalBreed">Breed:</label>
<input type="text" class="form-control" id="editAnimalBreed" required>
</div>
<div class="form-group">
<label for="editAnimalPhoto">Photo:</label>
<input type="url" class="form-control" id="editAnimalPhoto" required>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -100,6 +119,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);
$('#editAnimalBreed').val(data.animal.animal_breed);
$('#editAnimalPhoto').val(data.animal.photo_url);
$('#editAnimalModal').modal('show');
});
}
Expand All @@ -108,7 +129,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(),
animal_breed: $('#editAnimalBreed').val(),
photo_url: $('#editAnimalPhoto').val()
};

const animalId = $('#editAnimalId').val();
Expand Down Expand Up @@ -137,6 +160,9 @@ <h5 class="modal-title">Edit Animal</h5>
<td>${animal.animal_type}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${animal.animal_breed}</td>
<td>${animal.age}</td>
<td><img src="${animal.photo_url}" alt="Photo" style="max-height: 80px; max-width: 120px;"></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 +179,10 @@ <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(),
animal_breed: $('#animal_breed').val(),
photo_url: $('#photo_url').val()

};
$.ajax({
url: '/animal',
Expand Down