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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
Flask example project
# Pets info Flask project
### Add pets info in form:
* Animal type;
* Animal breed;
* Name;
* Birthday;
* Pet photo URL.

You see all pets data in table with pet age in years.
8 changes: 7 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def add_animal() -> tuple[Response, int]:
data = AnimalCreate(**request.get_json())
new_animal = Animal(
animal_type=data.animal_type,
animal_breed=data.animal_breed,
name=data.name,
birth_date=data.birth_date
birth_date=data.birth_date,
animal_photo=data.animal_photo
)
db.session.add(new_animal)
db.session.commit()
Expand Down Expand Up @@ -84,6 +86,10 @@ def delete_animal(pk: int) -> Union[Response, tuple[Response, int]]:
return jsonify({"message": "Animal deleted successfully!"})


@app.route('/health')
def health():
return "Ok", 200

def initialize_app():
with app.app_context():
db.create_all()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""empty message

Revision ID: e08fc0218f8b
Revision ID: d537062e2bfc
Revises:
Create Date: 2023-10-04 14:18:48.750677
Create Date: 2025-06-04 23:09:27.819725

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'e08fc0218f8b'
revision = 'd537062e2bfc'
down_revision = None
branch_labels = None
depends_on = None
Expand All @@ -23,6 +23,8 @@ def upgrade():
sa.Column('animal_type', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('birth_date', sa.Date(), nullable=False),
sa.Column('animal_breed', sa.String(), nullable=False),
sa.Column('animal_photo', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
Expand Down
17 changes: 16 additions & 1 deletion models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
from datetime import date
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, computed_field


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


class AnimalResponse(BaseModel):
model_config = ConfigDict(from_attributes=True)

id: int
animal_type: str
animal_breed: str
name: str
birth_date: date
animal_photo: str

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

if (today.month, today.day) < (self.birth_date.month, self.birth_date.day):
years -= 1

return years
2 changes: 2 additions & 0 deletions models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ class Animal(db.Model):

id = db.Column(db.Integer, primary_key=True)
animal_type = db.Column(db.String, nullable=False)
animal_breed = db.Column(db.String, nullable=False)
name = db.Column(db.String, nullable=False)
birth_date = db.Column(db.Date, nullable=False)
animal_photo = db.Column(db.String, nullable=False)
32 changes: 30 additions & 2 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ <h2>Animals in the Shelter</h2>
<label for="animal_type">Animal Type:</label>
<input type="text" class="form-control" id="animal_type" placeholder="Animal Type" required>
</div>
<div class="form-group">
<label for="animal_breed">Animal Breed:</label>
<input type="text" class="form-control" id="animal_breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="name">Animal Name:</label>
<input type="text" class="form-control" id="name" placeholder="Animal Name" required>
Expand All @@ -33,6 +37,10 @@ <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_photo">Photo URL:</label>
<input type="text" class="form-control" id="animal_photo" required>
</div>
<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
</div>
Expand All @@ -44,8 +52,11 @@ <h4 class="mt-4">List of animals:</h4>
<thead>
<tr>
<th>Animal Type</th>
<th>Breed</th>
<th>Name</th>
<th>Birth Date</th>
<th>Age</th>
<th>Photo</th>
<th>Actions</th>
</tr>
</thead>
Expand All @@ -72,6 +83,10 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalType">Animal Type:</label>
<input type="text" class="form-control" id="editAnimalType" required>
</div>
<div class="form-group">
<label for="editAnimalBreed">Animal Breed:</label>
<input type="text" class="form-control" id="editAnimalBreed" required>
</div>
<div class="form-group">
<label for="editAnimalName">Name:</label>
<input type="text" class="form-control" id="editAnimalName" required>
Expand All @@ -80,6 +95,10 @@ <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="editAnimalPhoto">Photo URL:</label>
<input type="text" class="form-control" id="editAnimalPhoto" required>
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -98,17 +117,21 @@ <h5 class="modal-title">Edit Animal</h5>
$.getJSON(`/animal/${id}`, function(data) {
$('#editAnimalId').val(data.animal.id);
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalBreed').val(data.animal.animal_breed);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editAnimalPhoto').val(data.animal.animal_photo);
$('#editAnimalModal').modal('show');
});
}

function submitEditAnimal() {
const updatedAnimal = {
animal_type: $('#editAnimalType').val(),
animal_breed: $('#editAnimalBreed').val(),
name: $('#editAnimalName').val(),
birth_date: $('#editAnimalBirthDate').val()
birth_date: $('#editAnimalBirthDate').val(),
animal_photo: $('#editAnimalPhoto').val()
};

const animalId = $('#editAnimalId').val();
Expand All @@ -135,8 +158,11 @@ <h5 class="modal-title">Edit Animal</h5>
$('#animalsList').append(`
<tr>
<td>${animal.animal_type}</td>
<td>${animal.animal_breed}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${animal.age}</td>
<td><img width="200" height="200" alt="${animal.name}" src="${animal.animal_photo}"/></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 @@ -152,8 +178,10 @@ <h5 class="modal-title">Edit Animal</h5>
e.preventDefault();
const newAnimal = {
animal_type: $('#animal_type').val(),
animal_breed: $('#animal_breed').val(),
name: $('#name').val(),
birth_date: $('#birth_date').val()
birth_date: $('#birth_date').val(),
animal_photo: $('#animal_photo').val()
};
$.ajax({
url: '/animal',
Expand Down