Skip to content

Instantly share code, notes, and snippets.

@begimai
Created May 8, 2020 06:48
Show Gist options
  • Save begimai/7488501ebcc59b4d051458ddb0e97c39 to your computer and use it in GitHub Desktop.
Save begimai/7488501ebcc59b4d051458ddb0e97c39 to your computer and use it in GitHub Desktop.
import datetime
from typing import Dict, List
from apps.orders.models import OrderQuerySet
def split_by_order_dates(available_dates: List[Dict[str, datetime.datetime]],
ordered_dates: 'OrderQuerySet') -> List[Dict[str, datetime.datetime]]:
"""
It removes dates allocated by accepted Orders from available schedule dates.
:param available_dates: list of advert's available Schedule,
:param ordered_dates: list of start/end dates of accepted orders by this particular advert,
:return: list available schedule subtract accepted orders dates.
"""
return_dates = list(available_dates)
if len(ordered_dates) == 0:
return return_dates
for out in ordered_dates:
for inner in return_dates:
if inner['start_date'] < out['end_date'] <= inner['end_date'] and inner['end_date'] > out['start_date']:
return_dates += [{'start_date': inner['start_date'], 'end_date': out['start_date']},
{'start_date': out['end_date'], 'end_date': inner['end_date']}]
return_dates.remove(inner)
return return_dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment