Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
RyjkovAlexey committed Mar 17, 2024
1 parent 965aaac commit d90eb1f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
10 changes: 8 additions & 2 deletions backend/models/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class Animal(DeclarativeBase):
name_ru = Column(String, nullable=False)
name_en = Column(String, nullable=False)

photos = relationship('AnimalPhoto', backref='animal', lazy='joined')
photos = relationship(
'AnimalPhoto',
back_populates='animal',
lazy='joined',
primaryjoin="AnimalPhoto.animal_id == Animal.id"
)

type = Column(String, nullable=False)
sex = Column(String, nullable=False)
age = Column(String, nullable=False)
Expand All @@ -32,7 +38,7 @@ def last_photo(self):
@hybrid_property
def link(self):
settings = Settings()
return f"http://{settings.APP_HOST}:{settings.APP_PORT}/animals/animal/{self.id}"
return f"{settings.HTTP_PROTOCOL}://{settings.APP_HOST}:{settings.APP_PORT}/animals/animal/{self.id}"

@hybrid_property
def img_src(self):
Expand Down
2 changes: 1 addition & 1 deletion backend/routers/animal_photo_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def download_animal_photo(
file_name: str
):
async with request.app.state.db.get_master_session() as session:
path_file = await AnimalPhotoService.get_path_to_photo(file_name)
path_file = AnimalPhotoService.get_path_to_photo(file_name)
return FileResponse(path=path_file, filename=file_name, media_type="application/octet-stream")


Expand Down
2 changes: 1 addition & 1 deletion backend/routers/animal_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
async def get_index_template(
request: Request,
) -> HTMLResponse:
async with request.app.state.db.get_master_session() as session:
async with request.app.state.db.get_master_session() as session:
return await AnimalTemplateService(request, session).get_index_template()


Expand Down
33 changes: 22 additions & 11 deletions backend/services/animal_photo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import uuid
from pathlib import Path

from fastapi import UploadFile
from fastapi import UploadFile, HTTPException, status
from sqlalchemy.ext.asyncio import AsyncSession as Session

from backend.dao.animal_photo_dao import AnimalPhotoDao
from backend.models.animal_photo import AnimalPhoto
from backend.schemas.animal_photo_schemas import AnimalPhotoSchemaCreate
from backend.settings import Settings

Expand All @@ -24,22 +23,34 @@ async def save_file(self, file: UploadFile, animal_id: int):
file_ext = os.path.splitext(file.filename)[1]
file_name = f"{str(uuid.uuid4())}{file_ext}"

path_file = Path(f"{AnimalPhotoService.__path_to_photos}/{file_name}")
path_file.write_bytes(await file.read())
path_file: Path = AnimalPhotoService.get_path_to_photo(file_name)

file_model = AnimalPhotoSchemaCreate(
animal_id=animal_id,
url=file_name
)
if not path_file.exists():
path_file.write_bytes(await file.read())

return await AnimalPhotoDao(self.session).create_one(file_model)
file_model = AnimalPhotoSchemaCreate(
animal_id=animal_id,
url=file_name
)

return await AnimalPhotoDao(self.session).create_one(file_model)
else:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"file {file_name} already exist"
)

@staticmethod
async def get_path_to_photo(file_name: str):
def get_path_to_photo(file_name: str):
return Path(f"{AnimalPhotoService.__path_to_photos}/{file_name}")

async def remove_file(self, file_name: str):
await AnimalPhotoDao(self.session).remove_file_by_name(file_name)
full_path = await AnimalPhotoService.get_path_to_photo(file_name)
full_path = AnimalPhotoService.get_path_to_photo(file_name)
if os.path.exists(full_path):
os.remove(full_path)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"file {file_name} not found"
)
4 changes: 2 additions & 2 deletions backend/services/animal_template_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def _complete_template_path(template_name: str):
return f"pages/index/{template_name}"

async def get_index_template(self) -> HTMLResponse:
animals:list[Animal] = await AnimalDao(self.session).find_all()
cards = [AnimalIndexSchema.from_orm(animal) for animal in animals]
animals: list[Animal] = await AnimalDao(self.session).find_all()
cards = [AnimalIndexSchema.model_validate(animal) for animal in animals]
return self.templates.TemplateResponse(
name=self._complete_template_path("index.html"),
context={"request": self.request, "cards": cards}
Expand Down
2 changes: 2 additions & 0 deletions backend/settings/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class BaseSettings(PydanticBaseSettings):
APP_HOST: str | None = "0.0.0.0"
APP_PORT: int | None = 8080

HTTP_PROTOCOL: str | None = "https"

URL_PREFIX: str | None = "/api/v1/shelterpaws/"

LOGGING_MIDDLEWARE_ENABLED: bool = True
Expand Down
4 changes: 3 additions & 1 deletion backend/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Settings(BaseSettings):
DB_PORT: str | None = "5432"
DB_USER: str | None = "postgres"
DB_PASSWORD: str | None = "postgres"
DB_NAME: str | None = "postgres"
DB_NAME: str | None = "shelterpaws-testing-db"

APP_HOST: str | None = "0.0.0.0"
APP_PORT: int | None = 8080
Expand All @@ -16,6 +16,8 @@ class Settings(BaseSettings):

URL_PHOTO_DOWNLOAD: str = "http://localhost:8080/animal_photo/download"

HTTP_PROTOCOL: str = "HTTP"

@property
def database_url(self):
return (
Expand Down
2 changes: 1 addition & 1 deletion backend/templates/general/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="page-footer__wrapper">
<img
class="page-footer__pic"
src="/upload/shelter.jpg"
src="/static/img/upload/shelter.jpg"
alt="shelter The Territory of Goodness"
/>
<div class="page-footer__content">
Expand Down

0 comments on commit d90eb1f

Please sign in to comment.