Skip to content

Commit

Permalink
Merge 2251888 into 8553be5
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmbdsm committed Mar 27, 2018
2 parents 8553be5 + 2251888 commit a14e8d9
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/openprocurement/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
WORKING_DAYS
)
from openprocurement.api.traversal import factory
from pkg_resources import get_distribution
from rfc6266 import build_header
from schematics.exceptions import ModelValidationError
from time import sleep, time as ttime
from urllib import quote, unquote, urlencode
from urlparse import urlparse, urljoin, urlunsplit, parse_qsl
from urlparse import urlparse, urlunsplit, parse_qsl
from uuid import uuid4
from webob.multidict import NestedMultiDict
from pyramid.exceptions import URLDecodeError
Expand Down Expand Up @@ -923,25 +922,65 @@ def decrypt(uuid, name, key):


def calculate_business_date(date_obj, timedelta_obj, context=None, working_days=False):
"""This method calculates end of business period from given start and timedelta
The calculation of end of business period is complex, so this method is used project-wide.
Also this method provides support of accelerated calculation, useful while testing.
:param date_obj: the start of period
:param timedelta_obj: duration of the period
:param context: object, that holds data related to particular business process,
usually it's Auction model's instance
:param working_days:
:type date_obj: datetime.datetime
:type timedelta_obj: datetime.timedelta
:type context: openprocurement.api.models.Tender
:type working_days: boolean
:return: the end of period
:rtype: datetime.datetime
"""
# Acceleration mode logic
if context and 'procurementMethodDetails' in context and context['procurementMethodDetails']:
re_obj = ACCELERATOR_RE.search(context['procurementMethodDetails'])
if re_obj and 'accelerator' in re_obj.groupdict():
return date_obj + (timedelta_obj / int(re_obj.groupdict()['accelerator']))
if working_days:
if timedelta_obj > timedelta():
if date_obj.weekday() in [5, 6] and WORKING_DAYS.get(date_obj.date().isoformat(), True) or WORKING_DAYS.get(date_obj.date().isoformat(), False):
if (
date_obj.weekday() in [5, 6] and
WORKING_DAYS.get(date_obj.date().isoformat(), True) or
WORKING_DAYS.get(date_obj.date().isoformat(), False)
):
date_obj = datetime.combine(date_obj.date(), time(0, tzinfo=date_obj.tzinfo)) + timedelta(1)
while date_obj.weekday() in [5, 6] and WORKING_DAYS.get(date_obj.date().isoformat(), True) or WORKING_DAYS.get(date_obj.date().isoformat(), False):
while (
date_obj.weekday() in [5, 6] and
WORKING_DAYS.get(date_obj.date().isoformat(), True) or
WORKING_DAYS.get(date_obj.date().isoformat(), False)
):
date_obj += timedelta(1)
else:
if date_obj.weekday() in [5, 6] and WORKING_DAYS.get(date_obj.date().isoformat(), True) or WORKING_DAYS.get(date_obj.date().isoformat(), False):
if (
date_obj.weekday() in [5, 6] and
WORKING_DAYS.get(date_obj.date().isoformat(), True) or
WORKING_DAYS.get(date_obj.date().isoformat(), False)
):
date_obj = datetime.combine(date_obj.date(), time(0, tzinfo=date_obj.tzinfo))
while date_obj.weekday() in [5, 6] and WORKING_DAYS.get(date_obj.date().isoformat(), True) or WORKING_DAYS.get(date_obj.date().isoformat(), False):
while (
date_obj.weekday() in [5, 6] and
WORKING_DAYS.get(date_obj.date().isoformat(), True) or
WORKING_DAYS.get(date_obj.date().isoformat(), False)
):
date_obj -= timedelta(1)
date_obj += timedelta(1)
for _ in xrange(abs(timedelta_obj.days)):
date_obj += timedelta(1) if timedelta_obj > timedelta() else -timedelta(1)
while date_obj.weekday() in [5, 6] and WORKING_DAYS.get(date_obj.date().isoformat(), True) or WORKING_DAYS.get(date_obj.date().isoformat(), False):
while (
date_obj.weekday() in [5, 6] and
WORKING_DAYS.get(date_obj.date().isoformat(), True) or
WORKING_DAYS.get(date_obj.date().isoformat(), False)
):
date_obj += timedelta(1) if timedelta_obj > timedelta() else -timedelta(1)
return date_obj
return date_obj + timedelta_obj
Expand Down

0 comments on commit a14e8d9

Please sign in to comment.