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
39 changes: 38 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import requests
from flask import Flask, jsonify, request, Response, render_template

from models.pydantic.models import AnimalCreate, AnimalResponse
Expand All @@ -12,6 +13,18 @@
db = init_db(app)


def get_random_dog_photo():
response = requests.get('https://dog.ceo/api/breeds/image/random')
data = response.json()
return data['message']


def get_random_cat_photo():
response = requests.get('https://api.thecatapi.com/v1/images/search')
data = response.json()
return data[0]['url']


@app.route('/')
def home() -> str:
return render_template('home.html')
Expand All @@ -26,10 +39,24 @@ def index() -> Response:
@app.route('/animal', methods=['POST'])
def add_animal() -> tuple[Response, int]:
data = AnimalCreate(**request.get_json())

if not data.photo_url:
if data.animal_type.lower() == 'dog':
photo_url = get_random_dog_photo()
elif data.animal_type.lower() == 'cat':
photo_url = get_random_cat_photo()
else:
photo_url = None
else:
photo_url = data.photo_url

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=photo_url,
animal_age=data.age
)
db.session.add(new_animal)
db.session.commit()
Expand All @@ -51,6 +78,11 @@ 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

if data.photo_url:
animal.photo_url = data.photo_url

db.session.commit()
return jsonify(
{
Expand Down Expand Up @@ -84,6 +116,11 @@ def delete_animal(pk: int) -> Union[Response, tuple[Response, int]]:
return jsonify({"message": "Animal deleted successfully!"})


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


def initialize_app():
with app.app_context():
db.create_all()
Expand Down
23 changes: 19 additions & 4 deletions models/pydantic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
from pydantic import BaseModel, ConfigDict


class AnimalCreate(BaseModel):
class AnimalAge:
birth_date: date

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


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


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

id: int
animal_type: str
name: str
birth_date: date
breed: str
photo_url: str
animal_age: int
3 changes: 3 additions & 0 deletions models/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,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=True)
animal_age = db.Column(db.Integer, nullable=False)
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=True)
animal_age = db.Column(db.Integer, nullable=False)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pydantic = "^2.4.2"
flask-sqlalchemy = "^3.1.1"
flask-migrate = "^4.0.5"
pydantic-settings = "^2.0.3"
requests = "^2.31.0"


[build-system]
Expand Down
26 changes: 24 additions & 2 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ <h2>Animals in the Shelter</h2>
<div class="form-group">
<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 of Animal:</label>
<input type="text" class="form-control" id="breed" placeholder="Animal Breed" required>
</div>
<div class="form-group">
<label for="photo_url">Photo of your Animal (URL):</label>
<input type="text" class="form-control" id="photo_url" placeholder="A random URL with a photo will be generated if this field is empty!">
</div>
<button type="submit" class="btn btn-primary">Add Animal</button>
</form>
Expand Down Expand Up @@ -80,6 +88,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="editPhotoUrl">Photo of your Animal (URL):</label>
<input type="text" class="form-control" id="editPhotoUrl">
</div>
</form>
</div>
<div class="modal-footer">
Expand All @@ -100,6 +116,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.breed);
$('#editPhotoUrl').val(data.animal.photo_url);
$('#editAnimalModal').modal('show');
});
}
Expand All @@ -108,7 +126,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(),
photo_url: $('#editPhotoUrl').val()
};

const animalId = $('#editAnimalId').val();
Expand Down Expand Up @@ -153,7 +173,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