2021-09-12 11:05:09 -08:00
|
|
|
from datetime import date
|
2022-07-02 12:44:01 -05:00
|
|
|
from math import ceil
|
2021-12-04 14:18:46 -09:00
|
|
|
from uuid import UUID
|
2021-09-12 11:05:09 -08:00
|
|
|
|
2022-07-02 12:44:01 -05:00
|
|
|
from sqlalchemy import func
|
|
|
|
|
from sqlalchemy.sql import sqltypes
|
|
|
|
|
|
2021-09-12 11:05:09 -08:00
|
|
|
from mealie.db.models.group import GroupMealPlan
|
2022-07-02 12:44:01 -05:00
|
|
|
from mealie.schema.meal_plan.new_meal import PlanEntryPagination, ReadPlanEntry
|
|
|
|
|
from mealie.schema.response.pagination import OrderDirection, PaginationQuery
|
2021-09-12 11:05:09 -08:00
|
|
|
|
2021-12-18 20:52:36 -09:00
|
|
|
from .repository_generic import RepositoryGeneric
|
2021-09-12 11:05:09 -08:00
|
|
|
|
|
|
|
|
|
2021-12-18 20:52:36 -09:00
|
|
|
class RepositoryMeals(RepositoryGeneric[ReadPlanEntry, GroupMealPlan]):
|
2022-06-17 13:25:47 -08:00
|
|
|
def by_group(self, group_id: UUID) -> "RepositoryMeals":
|
|
|
|
|
return super().by_group(group_id) # type: ignore
|
|
|
|
|
|
2022-07-02 12:44:01 -05:00
|
|
|
def get_slice(
|
|
|
|
|
self, pagination: PaginationQuery, start_date: date, end_date: date, group_id: UUID
|
|
|
|
|
) -> PlanEntryPagination:
|
|
|
|
|
start_str = start_date.strftime("%Y-%m-%d")
|
|
|
|
|
end_str = end_date.strftime("%Y-%m-%d")
|
|
|
|
|
|
|
|
|
|
# get the total number of documents
|
|
|
|
|
q = self.session.query(GroupMealPlan).filter(
|
2022-03-15 15:01:56 -08:00
|
|
|
GroupMealPlan.date.between(start_str, end_str),
|
2021-09-12 11:05:09 -08:00
|
|
|
GroupMealPlan.group_id == group_id,
|
|
|
|
|
)
|
|
|
|
|
|
2022-07-02 12:44:01 -05:00
|
|
|
count = q.count()
|
|
|
|
|
|
|
|
|
|
# interpret -1 as "get_all"
|
|
|
|
|
if pagination.per_page == -1:
|
|
|
|
|
pagination.per_page = count
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
total_pages = ceil(count / pagination.per_page)
|
|
|
|
|
|
|
|
|
|
except ZeroDivisionError:
|
|
|
|
|
total_pages = 0
|
|
|
|
|
|
|
|
|
|
# interpret -1 as "last page"
|
|
|
|
|
if pagination.page == -1:
|
|
|
|
|
pagination.page = total_pages
|
|
|
|
|
|
|
|
|
|
# failsafe for user input error
|
|
|
|
|
if pagination.page < 1:
|
|
|
|
|
pagination.page = 1
|
|
|
|
|
|
|
|
|
|
if pagination.order_by:
|
|
|
|
|
if order_attr := getattr(self.model, pagination.order_by, None):
|
|
|
|
|
# queries handle uppercase and lowercase differently, which is undesirable
|
|
|
|
|
if isinstance(order_attr.type, sqltypes.String):
|
|
|
|
|
order_attr = func.lower(order_attr)
|
|
|
|
|
|
|
|
|
|
if pagination.order_direction == OrderDirection.asc:
|
|
|
|
|
order_attr = order_attr.asc()
|
|
|
|
|
elif pagination.order_direction == OrderDirection.desc:
|
|
|
|
|
order_attr = order_attr.desc()
|
|
|
|
|
|
|
|
|
|
q = q.order_by(order_attr)
|
|
|
|
|
|
|
|
|
|
q = q.limit(pagination.per_page).offset((pagination.page - 1) * pagination.per_page)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
data = [self.schema.from_orm(x) for x in q.all()]
|
|
|
|
|
except Exception as e:
|
|
|
|
|
self._log_exception(e)
|
|
|
|
|
self.session.rollback()
|
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
return PlanEntryPagination(
|
|
|
|
|
page=pagination.page,
|
|
|
|
|
per_page=pagination.per_page,
|
|
|
|
|
total=count,
|
|
|
|
|
total_pages=total_pages,
|
|
|
|
|
items=data,
|
|
|
|
|
)
|
2021-09-12 11:05:09 -08:00
|
|
|
|
2021-12-04 14:18:46 -09:00
|
|
|
def get_today(self, group_id: UUID) -> list[ReadPlanEntry]:
|
2021-09-12 11:05:09 -08:00
|
|
|
today = date.today()
|
2021-09-19 15:31:34 -08:00
|
|
|
qry = self.session.query(GroupMealPlan).filter(GroupMealPlan.date == today, GroupMealPlan.group_id == group_id)
|
2021-09-12 11:05:09 -08:00
|
|
|
|
|
|
|
|
return [self.schema.from_orm(x) for x in qry.all()]
|