working on ticket js

This commit is contained in:
2024-01-05 00:12:13 +00:00
parent d9ecd692ef
commit ff4c71dc54
8 changed files with 168 additions and 33 deletions

View File

@ -1,5 +1,7 @@
# -*- encoding: utf-8 -*-
import uuid
import bleach
from datetime import timedelta, datetime
from django.db import models
@ -18,6 +20,7 @@ class TicketTag(models.Model):
class Ticket(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
title = models.CharField(max_length=60)
description = models.TextField(max_length=650)
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
@ -27,6 +30,18 @@ class Ticket(models.Model):
def __str__(self):
return f"#{self.id}{self.title}{self.author}"
def clean_description(self):
cleaned_description = bleach.clean(
self.description,
tags=['b', 'i', 'u', 'p', 'br', 'a', 'h3', 'h4', 'h5', 'h6'],
attributes={'a': ['href', 'title']}
)
return cleaned_description
def save(self, *args, **kwargs):
self.description = self.clean_description()
super().save(*args, **kwargs)
@property
def is_edited(self) -> bool:
"""Returns boolean if the ticket is believed to have been edited.
@ -65,3 +80,16 @@ 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,
"is_older_than_day": self.is_older_than_day,
"timestamp": self.timestamp
}