From 220f6d56a89e7c51d72ade36e1fbe62a2d521420 Mon Sep 17 00:00:00 2001 From: Sam Splunks <72095718+samsplunks@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:03:58 +0000 Subject: [PATCH] Adding HELPDESK_FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS setting --- docs/settings.rst | 13 +++++++++++-- helpdesk/models.py | 29 ++++++++++++++++------------- helpdesk/settings.py | 5 +++++ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/docs/settings.rst b/docs/settings.rst index a78fd2af..8d7001dd 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -299,11 +299,11 @@ Time Tracking Options **Default:** ``HELPDESK_FOLLOWUP_TIME_SPENT_AUTO = False`` - **HELPDESK_FOLLOWUP_TIME_SPENT_OPENING_HOURS** If defined, calculates follow-up 'time_spent' according to open hours. - + **Default:** ``HELPDESK_FOLLOWUP_TIME_SPENT_OPENING_HOURS = {}`` If HELPDESK_FOLLOWUP_TIME_SPENT_AUTO is ``True``, you may set open hours to remove off hours from 'time_spent':: - + HELPDESK_FOLLOWUP_TIME_SPENT_OPENING_HOURS = { "monday": (8.5, 19), "tuesday": (8.5, 19), @@ -316,7 +316,16 @@ Time Tracking Options Valid hour values must be set between 0 and 23.9999. In this example 8.5 is interpreted as 8:30AM, saturdays and sundays don't count. + +- **HELPDESK_FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS** List of days in format "%m-%d" to exclude from automatic follow-up 'time_spent' calculation. + **Default:** ``HELPDESK_FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS = ()`` + + This example removes the first and last days of the year together with Christmas:: + + HELPDESK_FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS = ("01-01", + "12-25", + "12-31",) Staff Ticket Creation Settings ------------------------------ diff --git a/helpdesk/models.py b/helpdesk/models.py index d14239fa..413b5baa 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -1025,25 +1025,28 @@ class FollowUp(models.Model): time_spent_seconds = 0 open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS + holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS # split time interval by days days = latest.toordinal() - earliest.toordinal() - if days: - for day in range(days + 1): - if day == 0: - start_day_time = earliest - end_day_time = earliest.replace(hour=23, minute=59, second=59) - elif day == days: - start_day_time = latest.replace(hour=0, minute=0, second=0) + for day in range(days + 1): + if day == 0: + start_day_time = earliest + if days == 0: + # close single day case end_day_time = latest else: - middle_day_time = earliest + datetime.timedelta(days=day) - start_day_time = middle_day_time.replace(hour=0, minute=0, second=0) - end_day_time = middle_day_time.replace(hour=23, minute=59, second=59) + end_day_time = earliest.replace(hour=23, minute=59, second=59) + elif day == days: + start_day_time = latest.replace(hour=0, minute=0, second=0) + end_day_time = latest + else: + middle_day_time = earliest + datetime.timedelta(days=day) + start_day_time = middle_day_time.replace(hour=0, minute=0, second=0) + end_day_time = middle_day_time.replace(hour=23, minute=59, second=59) + + if start_day_time.strftime("%m-%d") not in holidays: time_spent_seconds += daily_time_spent_calculation(start_day_time, end_day_time, open_hours) - # handle same day - else: - time_spent_seconds += daily_time_spent_calculation(earliest, latest, open_hours) return datetime.timedelta(seconds=time_spent_seconds) diff --git a/helpdesk/settings.py b/helpdesk/settings.py index 96eac1ee..ab610697 100644 --- a/helpdesk/settings.py +++ b/helpdesk/settings.py @@ -166,6 +166,11 @@ FOLLOWUP_TIME_SPENT_OPENING_HOURS = getattr(settings, 'HELPDESK_FOLLOWUP_TIME_SPENT_OPENING_HOURS', {}) +# Holidays don't count for time_spent calculation +FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS= getattr(settings, + 'HELPDESK_FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS', + ()) + ############################ # options for public pages # ############################