From b361ca39e89f5159fa0bbacc3a250b8b099d7f26 Mon Sep 17 00:00:00 2001 From: tamirmatok Date: Mon, 2 May 2022 16:41:32 +0300 Subject: [PATCH] Add "capacity dates check" for dog owner search. Capacity dates check for show only available daycares in dates range. "get_capacity_of_daycare_in_dates_range" function took from #95 . waiting for approval. Signed-off-by: tamirmatok --- orders/models.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/orders/models.py b/orders/models.py index 06209eb..1e37cdc 100644 --- a/orders/models.py +++ b/orders/models.py @@ -121,3 +121,38 @@ def is_the_order_cancelable(self): def is_the_order_approvable(self): return self.status == StatusOptions.Pending and self.are_order_dates_available() + + @staticmethod + def get_capacity_of_daycare_in_dates_range(daycare_id, start_date, end_date): + relevant_orders = Order.objects.filter(daycare_id=daycare_id, status__in=['A', 'O']) + start_date = datetime.date(year=start_date.year, month=start_date.month, day=start_date.day) + end_date = datetime.date(year=end_date.year, month=end_date.month, day=end_date.day) + capacity_per_day_list = [0] * ((end_date - start_date).days + 2) + + for order in relevant_orders: + if end_date < order.start_date or order.end_date < start_date: + continue + + number_of_days = (order.end_date - order.start_date).days + + for day in range(number_of_days): + current_date = order.start_date + datetime.timedelta(days=day) + if current_date < start_date: + continue + elif current_date > end_date: + break + else: + capacity_per_day_list[day] = capacity_per_day_list[day] + 1 + + return capacity_per_day_list + + @staticmethod + def get_all_day_cares_available_on_dates(start_date: str, end_date: str) -> QuerySet: + start_date = datetime.date.fromisoformat(start_date) + end_date = datetime.date.fromisoformat(end_date) + id_list = [] + for day_care in DayCare.objects.all(): + capacity_per_day_list = Order.get_capacity_of_daycare_in_dates_range(day_care.id, start_date, end_date) + if all(current_capacity < day_care.capacity for current_capacity in capacity_per_day_list): + id_list.append(day_care.id) + return DayCare.objects.filter(id__in=id_list)