From 99b49e3f39e9dea8d49c0f5ef16d9bf55b004dc2 Mon Sep 17 00:00:00 2001 From: Sam Splunks <72095718+samsplunks@users.noreply.github.com> Date: Thu, 15 Feb 2024 09:08:25 +0000 Subject: [PATCH] Refactoring earliest and latest follow-up time selection logic, also allows for time_spent recalculation if unset --- helpdesk/models.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/helpdesk/models.py b/helpdesk/models.py index 71ab2631..d965e0b0 100644 --- a/helpdesk/models.py +++ b/helpdesk/models.py @@ -1000,17 +1000,12 @@ class FollowUp(models.Model): return u"%s#followup%s" % (self.ticket.get_absolute_url(), self.id) def save(self, *args, **kwargs): - now = timezone.now() - t = self.ticket + self.ticket.modified = timezone.now() + self.ticket.save() + if helpdesk_settings.FOLLOWUP_TIME_SPENT_AUTO and not self.time_spent: - try: - latest_fup = t.followup_set.exclude(id=self.id).latest("date") - latest_time = latest_fup.date - except ObjectDoesNotExist: - latest_time = t.created - self.time_spent = self.time_spent_calculation(latest_time, now) - t.modified = now - t.save() + self.time_spent = self.time_spent_calculation() + super(FollowUp, self).save(*args, **kwargs) def get_markdown(self): @@ -1020,9 +1015,23 @@ class FollowUp(models.Model): def time_spent_formated(self): return format_time_spent(self.time_spent) - def time_spent_calculation(self, earliest, latest): + def time_spent_calculation(self): "Returns timedelta according to rules settings." - + + # extract earliest time from previous follow-up + # or ticket creation time + try: + prev_fup_qs = self.ticket.followup_set.all() + if self.id: + prev_fup_qs = prev_fup_qs.filter(id__lt=self.id) + prev_fup = prev_fup_qs.latest("date") + earliest = prev_fup.date + except ObjectDoesNotExist: + earliest = self.ticket.created + + # latest time is current follow-up date + latest = self.date + time_spent_seconds = 0 open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS