Skip to content

Commit

Permalink
Add "capacity dates check" for dog owner search.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
tamirmatok committed May 18, 2022
1 parent 97dd0a4 commit eea8053
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
37 changes: 37 additions & 0 deletions orders/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.db import models
from django.db.models import QuerySet
from django.utils import timezone
from daycare.models import DayCare
from dogowner.models import DogOwner
import datetime


class StatusOptions(models.TextChoices):
Expand Down Expand Up @@ -71,3 +73,38 @@ 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 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)
21 changes: 21 additions & 0 deletions orders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,24 @@ def test_dog_owner_id_is_deleted_when_dog_owner_is_deleted(self, create_order):
def test_daycare_id_is_deleted_when_daycare_is_deleted(self, create_order):
DayCare.objects.get(id=1).delete()
assert Order.objects.get(id=create_order.id).daycare_id is None

@pytest.mark.parametrize('start_date, end_date',
[
("2022-05-02", "2022-08-02"),
])
def test_day_care_available_on_dates_appears_in_available_day_cares_queryset(self, create_daycare_user,
start_date, end_date):
day_care_user = create_daycare_user
assert day_care_user in Order.get_all_day_cares_available_on_dates(start_date, end_date)

@pytest.mark.parametrize('start_date, end_date',
[
("2022-05-02", "2022-08-02"),
])
def test_day_care_not_available_on_dates_not_appears_in_available_day_cares_queryset(self, create_daycare_user,
start_date, end_date):
day_care_user = create_daycare_user
for _ in range(day_care_user.capacity):
Order.create(dog_owner_id=DogOwner.objects.get(id=1), daycare_id=day_care_user,
start_date=start_date, end_date=end_date, price_per_day=500).approve_order()
assert day_care_user not in Order.get_all_day_cares_available_on_dates(start_date, end_date)

0 comments on commit eea8053

Please sign in to comment.