From b5ebfcdc25d1c7ac16dfc631ff8c8e80a98f38d2 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 and tests. Signed-off-by: tamirmatok --- orders/models.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/orders/models.py b/orders/models.py index 105e1f3..fc328b4 100644 --- a/orders/models.py +++ b/orders/models.py @@ -2,6 +2,7 @@ from django.utils import timezone from daycare.models import DayCare from dogowner.models import DogOwner +import datetime class StatusOptions(models.TextChoices): @@ -71,3 +72,33 @@ def cancel_order(self): def get_order_total_price(self): return self.price_per_day * (self.end_date - self.start_date).days + + @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 day_care_is_available_on_dates(daycare, start_date, end_date): + capacity_per_day_list = Order.get_capacity_of_daycare_in_dates_range(daycare.id, start_date, end_date) + return all(current_capacity < daycare.capacity for current_capacity in capacity_per_day_list) +