Skip to content

Commit

Permalink
Merge pull request #105 from ministryofjustice/feature/LGA-1719-timez…
Browse files Browse the repository at this point in the history
…one-aware-assigned-out-of-hours

LGA-1719 - Timezone-aware assigned out of hours
  • Loading branch information
exonian committed Jun 9, 2021
2 parents 9c32f63 + a7ce396 commit 50737f9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cla_common/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.3.14"
__version__ = "0.3.15"
39 changes: 36 additions & 3 deletions cla_common/call_centre_availability/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
from itertools import ifilter, islice, takewhile
import pytz
import requests

from cla_common.services import CacheAdapter
Expand All @@ -10,6 +11,30 @@

BOXING_DAY = datetime.date(year=2020, month=12, day=26)

TIMEZONE_NAME = None


def get_timezone():
"""
Uses the timezone name to set up a pytz timezone instance
Gets the timezone name from either Django settings or Flask conf.
Does it within a function so that we don't try to access Django settings
before they're ready.
Stores it as a constant so that we're not calculating it every time
"""
global TIMEZONE_NAME
if not TIMEZONE_NAME:
try:
from django.conf import settings

TIMEZONE_NAME = settings.TIME_ZONE
except Exception as e:
print(e)
from flask import current_app

TIMEZONE_NAME = current_app.config["TIMEZONE"]
return pytz.timezone(TIMEZONE_NAME)


def current_datetime():
# this function is to make unit testing simpler
Expand Down Expand Up @@ -181,9 +206,17 @@ def __bool__(self):
return not self.is_empty()

def __contains__(self, dt):
return self.contains(dt)

def contains(self, dt, tz_aware=False):
if self.is_empty():
return False
return self.start <= dt.time() < self.end
if tz_aware:
tz_start = get_timezone().localize(dt.combine(dt, self.start))
tz_end = get_timezone().localize(dt.combine(dt, self.end))
return tz_start <= dt < tz_end
else:
return self.start <= dt.time() < self.end

def __repr__(self):
if self.is_empty():
Expand Down Expand Up @@ -253,14 +286,14 @@ def add_rule(self, func, hours):
hours = Hours(*hours)
self.day_hours.append((func, hours))

def available(self, dt, ignore_time=False):
def available(self, dt, ignore_time=False, tz_aware=False):
for (on_day, hours) in self.day_hours:
if on_day(dt):
if hours is None:
continue
if hours and ignore_time:
return True
return dt in hours
return hours.contains(dt, tz_aware=tz_aware)
return False

def can_schedule_callback(self, dt, ignore_time=False):
Expand Down
45 changes: 23 additions & 22 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,45 @@

version = cla_common.__version__

if sys.argv[-1] == 'publish':
os.system('python setup.py sdist upload')
if sys.argv[-1] == "publish":
os.system("python setup.py sdist upload")
print("You probably want to also tag the version now:")
print(" git tag -a %s -m 'version %s'" % (version, version))
print(" git push --tags")
sys.exit()

readme = open('README.rst').read()
readme = open("README.rst").read()

setup(
name='cla_common',
name="cla_common",
version=version,
description="""common code for CLA""",
long_description=readme,
author='MOJ',
author_email='kotecha.ravi@gmail.com',
url='https://github.com/ministryofjustice/cla_common',
author="MOJ",
author_email="kotecha.ravi@gmail.com",
url="https://github.com/ministryofjustice/cla_common",
packages=find_packages(),
include_package_data=True,
install_requires=[
'python-dateutil',
'django-extended-choices==0.3.0',
'requests',
'speaklater==1.3',
"python-dateutil",
"django-extended-choices==0.3.0",
"requests",
"speaklater==1.3",
"pytz==2021.1",
],
license="MIT",
zip_safe=False,
keywords='cla_common',
keywords="cla_common",
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Natural Language :: English',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
"Development Status :: 2 - Pre-Alpha",
"Framework :: Django",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.3",
],
)

0 comments on commit 50737f9

Please sign in to comment.