Refactoring earliest and latest follow-up time selection logic, also allows for time_spent recalculation if unset

This commit is contained in:
Sam Splunks 2024-02-15 09:08:25 +00:00
parent b1759520d8
commit 99b49e3f39

View File

@ -1000,17 +1000,12 @@ class FollowUp(models.Model):
return u"%s#followup%s" % (self.ticket.get_absolute_url(), self.id) return u"%s#followup%s" % (self.ticket.get_absolute_url(), self.id)
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
now = timezone.now() self.ticket.modified = timezone.now()
t = self.ticket self.ticket.save()
if helpdesk_settings.FOLLOWUP_TIME_SPENT_AUTO and not self.time_spent: if helpdesk_settings.FOLLOWUP_TIME_SPENT_AUTO and not self.time_spent:
try: self.time_spent = self.time_spent_calculation()
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()
super(FollowUp, self).save(*args, **kwargs) super(FollowUp, self).save(*args, **kwargs)
def get_markdown(self): def get_markdown(self):
@ -1020,9 +1015,23 @@ class FollowUp(models.Model):
def time_spent_formated(self): def time_spent_formated(self):
return format_time_spent(self.time_spent) 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." "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 time_spent_seconds = 0
open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS open_hours = helpdesk_settings.FOLLOWUP_TIME_SPENT_OPENING_HOURS
holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS holidays = helpdesk_settings.FOLLOWUP_TIME_SPENT_EXCLUDE_HOLIDAYS