implemented rest into home app

This commit is contained in:
2024-01-12 19:25:45 +00:00
parent e0e16efb71
commit 362c8f0a16
8 changed files with 219 additions and 101 deletions

View File

@ -1,7 +1,7 @@
# -*- encoding: utf-8 -*-
import uuid
import bleach
from uuid import uuid4
from datetime import timedelta, datetime
from django.db import models
@ -11,7 +11,7 @@ from django.utils.translation import gettext_lazy as _
class TicketPriority(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
title = models.CharField(max_length=32)
colour = models.CharField(max_length=7)
@ -20,17 +20,9 @@ class TicketPriority(models.Model):
def __str__(self):
return self.title
def serialize(self) -> dict:
return {
"id": self.id,
"title": self.title,
"colour": self.colour,
"backgroundcolour": self.backgroundcolour
}
class TicketTag(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
title = models.CharField(max_length=32)
colour = models.CharField(max_length=7)
@ -39,66 +31,68 @@ class TicketTag(models.Model):
def __str__(self):
return self.title
def serialize(self) -> dict:
return {
"id": self.id,
"title": self.title,
"colour": self.colour,
"backgroundcolour": self.backgroundcolour
}
class Ticket(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
"""Represents a Ticket used to communicate issues or questions."""
uuid = models.UUIDField(primary_key=True, default=uuid4, editable=False)
# Main Attributes
title = models.CharField(
_("title"),
verbose_name=_("title"),
help_text=_("An extremely short summary of the ticket subject."),
max_length=100,
help_text=_("An extremely short summary of the ticket subject.")
)
description = models.TextField(
_("description"),
verbose_name=_("description"),
help_text=_("Detailed description of the ticket subject."),
max_length=650,
help_text=_("Detailed description of the ticket subject.")
)
# Dirty Foreigers
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_("author"),
help_text=_("The creator of the ticket."),
on_delete=models.CASCADE,
help_text=_("The creator of the ticket.")
)
priority = models.ForeignKey(
TicketPriority,
verbose_name=_("priority"),
help_text=_("The importance level of this ticket."),
on_delete=models.CASCADE,
help_text=_("The importance level of this ticket.")
)
tags = models.ManyToManyField(
TicketTag,
verbose_name=_("tags"),
help_text=_("Categories of the ticket."),
blank=True,
help_text=_("Categories of the ticket.")
)
# Timestamps
create_timestamp = models.DateTimeField(
_("Creation Date"),
verbose_name=_("Creation Date"),
help_text=_("When the user was created."),
editable=True,
default=timezone.now,
help_text=_("When the user was created.")
)
edit_timestamp = models.DateTimeField(
_("Last Edited"),
verbose_name=_("Last Edited"),
help_text=_("When the user was last edited."),
editable=True,
default=timezone.now,
help_text=_("When the user was last edited.")
)
def __str__(self):
return f"#{self.id}{self.title}{f' {self.author.department.title}' if self.author.department else ''} {self.author.formal_fullname}"
return f"#{self.uuid}{self.title}{f' {self.author.department.title}' if self.author.department else ''} {self.author.formal_fullname}"
def clean_description(self):
"""Sanitise the description as it may contain some allowed HTML tags."""
cleaned_description = bleach.clean(
self.description,
tags=[
@ -110,13 +104,14 @@ class Ticket(models.Model):
return cleaned_description
def save(self, *args, **kwargs):
"""Override the save method to clean the description and apply timestamps."""
self.description = self.clean_description()
# we must use the same datetime object, otherwise they wont match
now = timezone.now()
if self._state.adding: self.create_timestamp = now
self.edit_timestamp = now
if self._state.adding:
self.create_timestamp = now
super().save(*args, **kwargs)
@ -174,19 +169,3 @@ class Ticket(models.Model):
"""
return self.edit_timestamp if self.is_edited else self.create_timestamp
def serialize(self) -> dict:
return {
"id": self.id,
"title": self.title,
"description": self.description,
"author": self.author.serialize(),
"create_timestamp": self.create_timestamp,
"edit_timestamp": self.edit_timestamp,
"is_edited": self.is_edited,
"was_yesterday": self.was_yesterday,
"is_older_than_day": self.is_older_than_day,
"timestamp": self.timestamp,
"priority": self.priority.serialize(),
"tags": [tag.serialize() for tag in self.tags.all()]
}