diff --git a/techsupport/models.py b/techsupport/models.py index d881541..e25c2ae 100644 --- a/techsupport/models.py +++ b/techsupport/models.py @@ -284,9 +284,8 @@ class Meta: verbose_name_plural = "subcategories" -class SupportTicket(BaseModel): - """Model representing a support ticket submitted by a coach.""" - +class SupportTicket(models.Model): + """Model representing a support ticket.""" class Status(models.TextChoices): OPEN = "Open", _("Open") IN_PROGRESS = "In Progress", _("In Progress") @@ -302,7 +301,7 @@ class Priority(models.TextChoices): verbose_name=_("ticket number"), unique=True, editable=False ) date_submitted = models.DateTimeField( - verbose_name=_("date submitted"), default=now, editable=False + verbose_name=_("date submitted"), default=timezone.now, editable=False ) date_resolved = models.DateTimeField( verbose_name=_("date resolved"), null=True, blank=True @@ -363,6 +362,7 @@ class Priority(models.TextChoices): blank=True, ) resolution_notes = models.TextField(blank=True) + id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) assigned_to = models.ForeignKey( User, @@ -372,14 +372,14 @@ class Priority(models.TextChoices): blank=True, related_name="assigned_tickets", ) - + archived = models.BooleanField(default=False, verbose_name=_("archived")) + date_archived = models.DateTimeField( + null=True, blank=True, verbose_name=_("date archived") + ) + def save(self, *args, **kwargs): """ - Override the save method to set the ticket number and support description. - - Args: - *args: Arguments passed to the superclass method. - **kwargs: Keyword arguments passed to the superclass method. + Override the save method to auto-generate ticket number and title if not provided. """ if not self.ticket_number: max_ticket_number = SupportTicket.objects.aggregate( @@ -393,8 +393,7 @@ def save(self, *args, **kwargs): def ticket_age(self): """ - Method that returns the difference between the current time and the time - the support ticket was submitted. + Calculate the age of the ticket based on submission time. """ now = timezone.now() age = now - self.date_submitted @@ -414,3 +413,38 @@ def ticket_age(self): return f"{hours} hrs ago" else: return f"{minutes} mins ago" + + def archive(self): + """Archive the ticket if it is resolved.""" + if self.status == SupportTicket.Status.RESOLVED: + self.status = SupportTicket.Status.CLOSED + self.archived = True + self.date_archived = timezone.now() + self.save() + return True + return False + + def unarchive(self): + """ + Unarchive the ticket if it is closed or resolved + """ + if self.can_be_unarchived(): + self.status = SupportTicket.Status.OPEN # Set the status to 'OPEN' when unarchiving + self.archived = False + self.date_archived = None + self.save() + return True + return False + + def can_be_archived(self): + """ + Check if the ticket can be archived + """ + return self.status == SupportTicket.Status.RESOLVED + + def can_be_unarchived(self): + """ + Check if the ticket can be unarchived + """ + return self.status in [SupportTicket.Status.CLOSED, SupportTicket.Status.RESOLVED] + diff --git a/techsupport/templates/base.html b/techsupport/templates/base.html index 1f1bfe6..47c4d2f 100644 --- a/techsupport/templates/base.html +++ b/techsupport/templates/base.html @@ -64,6 +64,12 @@
Technical Support
Profile + {% if user.is_superuser or user.is_admin %} + + + Archive + + {% endif %} @@ -75,20 +81,27 @@
Technical Support
- -
-
-
- {% if messages %} - {% for message in messages %} - +