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
14 changes: 11 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

db = init_db(app)

@app.route('/health')
def health():
return Response(status=200)

@app.route('/')
def home() -> str:
Expand All @@ -27,9 +30,11 @@ def index() -> Response:
def add_animal() -> tuple[Response, int]:
data = AnimalCreate(**request.get_json())
new_animal = Animal(
animal_type=data.animal_type,
type=data.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 @@ -45,12 +50,15 @@ def add_animal() -> tuple[Response, int]:
def update_animal(pk: int) -> Union[Response, tuple[Response, int]]:
data = AnimalCreate(**request.get_json())
animal = Animal.query.get(pk)

if not animal:
return jsonify({"message": "Animal not found"}), 404

animal.animal_type = data.animal_type
animal.type = data.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
4 changes: 3 additions & 1 deletion migrations/versions/e08fc0218f8b_.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('animal',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('animal_type', sa.String(), nullable=False),
sa.Column('type', sa.String(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('breed', sa.String(), nullable=False),
sa.Column('image_url', sa.String(), nullable=False),
sa.Column('birth_date', sa.Date(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
Expand Down
30 changes: 24 additions & 6 deletions models/pydantic/models.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
from datetime import date
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, computed_field


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

@computed_field
@property
def age(self) -> str:
today = date.today()
years = today.year - self.birth_date.year
months = today.month - self.birth_date.month
days_diff = today.day - self.birth_date.day

class AnimalResponse(BaseModel):
if days_diff < 0:
months -= 1

if months < 0:
years -= 1
months += 12

if years <= 0:
return f"{months} month{'s' if months != 1 else ''}"
return f"{years} year{'s' if years != 1 else ''} {months} month{'s' if months != 1 else ''}"


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

id: int
animal_type: str
name: str
birth_date: date
2 changes: 1 addition & 1 deletion models/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

class Animal(db.Model):
id = db.Column(db.Integer, primary_key=True)
animal_type = db.Column(db.String, nullable=False)
type = db.Column(db.String, nullable=False)
name = db.Column(db.String, nullable=False)
birth_date = db.Column(db.Date, nullable=False)
4 changes: 3 additions & 1 deletion models/sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Animal(db.Model):
__table_args__ = {'extend_existing': True}

id = db.Column(db.Integer, primary_key=True)
animal_type = db.Column(db.String, nullable=False)
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)
43 changes: 34 additions & 9 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ <h2>Animals in the Shelter</h2>
<div class="card-body">
<form id="addAnimalForm">
<div class="form-group">
<label for="animal_type">Animal Type:</label>
<input type="text" class="form-control" id="animal_type" placeholder="Animal Type" required>
<label for="type">Animal Type:</label>
<input type="text" class="form-control" id="type" placeholder="Animal Type" required>
</div>
<div class="form-group">
<label for="name">Animal Name:</label>
<input type="text" class="form-control" id="name" placeholder="Animal Name" required>
</div>
<div class="form-group">
<label for="breed">Animal Breed:</label>
<input type="text" class="form-control" id="breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="image_url">Animal Image Url:</label>
<input type="text" class="form-control" id="image_url" placeholder="Animal Image Url" required>
</div>
<div class="form-group">
<label for="birth_date">Birth Date:</label>
<input type="date" class="form-control" id="birth_date" required>
Expand All @@ -45,6 +53,8 @@ <h4 class="mt-4">List of animals:</h4>
<tr>
<th>Animal Type</th>
<th>Name</th>
<th>Breed</th>
<th>Image Url</th>
<th>Birth Date</th>
<th>Actions</th>
</tr>
Expand Down Expand Up @@ -76,6 +86,14 @@ <h5 class="modal-title">Edit Animal</h5>
<label for="editAnimalName">Name:</label>
<input type="text" class="form-control" id="editAnimalName" 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="editAnimalImageUrl">Image Url:</label>
<input type="text" class="form-control" id="editAnimalImageUrl" required>
</div>
<div class="form-group">
<label for="editAnimalBirthDate">Birth Date:</label>
<input type="date" class="form-control" id="editAnimalBirthDate" required>
Expand All @@ -97,18 +115,22 @@ <h5 class="modal-title">Edit Animal</h5>
// This assumes you have a backend route to get details of a single animal.
$.getJSON(`/animal/${id}`, function(data) {
$('#editAnimalId').val(data.animal.id);
$('#editAnimalType').val(data.animal.animal_type);
$('#editAnimalType').val(data.animal.type);
$('#editAnimalName').val(data.animal.name);
$('#editAnimalBreed').val(data.animal.breed);
$('#editAnimalImageUrl').val(data.animal.image_url);
$('#editAnimalBirthDate').val(data.animal.birth_date);
$('#editAnimalModal').modal('show');
});
}

function submitEditAnimal() {
const updatedAnimal = {
animal_type: $('#editAnimalType').val(),
type: $('#editAnimalType').val(),
name: $('#editAnimalName').val(),
birth_date: $('#editAnimalBirthDate').val()
birth_date: $('#editAnimalBirthDate').val(),
breed: $('#editAnimalBreed').val(),
image_url: $('#editAnimalImageUrl').val(),
};

const animalId = $('#editAnimalId').val();
Expand All @@ -131,12 +153,13 @@ <h5 class="modal-title">Edit Animal</h5>
$.getJSON('/animals', function(data) {
$('#animalsList').empty();
data.animals.forEach(animal => {
console.log(animal)
$('#animalsList').append(`
<tr>
<td>${animal.animal_type}</td>
<td>${animal.type}</td>
<td>${animal.name}</td>
<td>${animal.birth_date}</td>
<td>${animal.breed}</td>
<td><img src='${animal.image_url}' width='150' /></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 @@ -151,8 +174,10 @@ <h5 class="modal-title">Edit Animal</h5>
$('#addAnimalForm').on('submit', function(e) {
e.preventDefault();
const newAnimal = {
animal_type: $('#animal_type').val(),
type: $('#type').val(),
name: $('#name').val(),
breed: $('#breed').val(),
image_url: $('#image_url').val(),
birth_date: $('#birth_date').val()
};
$.ajax({
Expand Down