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
7 changes: 6 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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,
image_url=data.image_url
)
db.session.add(new_animal)
db.session.commit()
Expand All @@ -51,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(
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Add image column to animal table

Revision ID: c77d65e56e96
Revises: ed59229175d1
Create Date: 2025-06-03 22:50:20.353785

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'c77d65e56e96'
down_revision = 'ed59229175d1'
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('image_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('image_url')

# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -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 ###
13 changes: 11 additions & 2 deletions models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from datetime import date
from pydantic import BaseModel, ConfigDict
from datetime import date, timedelta
from pydantic import BaseModel, ConfigDict, computed_field


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


class AnimalResponse(BaseModel):
Expand All @@ -15,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))
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)
breed = db.Column(db.String, nullable=False)
image_url = db.Column(db.String, nullable=False)
57 changes: 50 additions & 7 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ <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"
placeholder="Animal Breed" required
>
</div>
<div class="form-group">
<label for="image_url">Image:</label>
<input type="text" class="form-control"
placeholder="Animal Pic" id="image_url">
</div>
<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
</div>
Expand All @@ -44,8 +55,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>Image</th>
<th>Actions</th>
</tr>
</thead>
Expand Down Expand Up @@ -80,6 +94,17 @@ <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"
placeholder="Animal Breed" required
>
</div>
<div class="form-group">
<label for="editAnimalImage_url">Image:</label>
<input type="text" class="form-control"
placeholder="Animal Pic" id="editAnimalImage_url">
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -95,11 +120,16 @@ <h5 class="modal-title">Edit Animal</h5>
function updateAnimal(id) {
// Fetch the current details of the animal to pre-populate the form.
// This assumes you have a backend route to get details of a single animal.

$.getJSON(`/animal/${id}`, function(data) {
console.log(data)
$('#editAnimalId').val(data.animal.id);
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editAnimalBreed').val(data.animal.breed);
$('#editAnimalImage_url').val(data.animal.image_url);

$('#editAnimalModal').modal('show');
});
}
Expand All @@ -108,7 +138,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: $('#editAnimalBreed').val(),
image_url: $('#editAnimalImage_url').val(),
};

const animalId = $('#editAnimalId').val();
Expand All @@ -121,8 +153,9 @@ <h5 class="modal-title">Edit Animal</h5>
success: function() {
$('#editAnimalModal').modal('hide');
fetchAnimals();
}
}
});

}


Expand All @@ -131,12 +164,20 @@ <h5 class="modal-title">Edit Animal</h5>
$.getJSON('/animals', function(data) {
$('#animalsList').empty();
data.animals.forEach(animal => {
console.log(animal)
const {animal_type, breed, name, birth_date, image_url, age} = animal
$('#animalsList').append(`
<tr>
<td>${animal.animal_type}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${animal_type}</td>
<td>${breed}</td>
<td>${name}</td>
<td>${birth_date}</td>
<td>${age}</td>
<td>
<img
src="${image_url}"
alt="${name}"
style="width: 50px; height: 50px;"
>
<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 +194,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(),
image_url: $('#image_url').val()
};
$.ajax({
url: '/animal',
Expand Down