don't save ticket state in multiple locations

This commit is contained in:
j3d1 2023-12-30 18:34:35 +01:00
parent fd7847993b
commit 7b77c183fb
3 changed files with 62 additions and 43 deletions

View file

@ -2,6 +2,8 @@ from django.db import models
from django_softdelete.models import SoftDeleteModel
from inventory.models import Event
from django.db.models.signals import post_save
from django.dispatch import receiver
STATE_CHOICES = (
('pending_new', 'New'),
@ -24,11 +26,23 @@ STATE_CHOICES = (
class IssueThread(SoftDeleteModel):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=255)
state = models.CharField('state', choices=STATE_CHOICES, max_length=32, default='pending_new')
assigned_to = models.CharField(max_length=255, null=True)
last_activity = models.DateTimeField(auto_now=True)
manually_created = models.BooleanField(default=False)
@property
def state(self):
try:
return self.state_changes.order_by('-timestamp').first().state
except AttributeError:
return 'none'
@state.setter
def state(self, value):
if self.state == value:
return
self.state_changes.create(state=value)
class Meta:
permissions = [
('send_mail', 'Can send mail'),
@ -36,6 +50,12 @@ class IssueThread(SoftDeleteModel):
]
@receiver(post_save, sender=IssueThread)
def create_issue_thread(sender, instance, created, **kwargs):
if created:
StateChange.objects.create(issue_thread=instance, state='pending_new')
class Comment(models.Model):
id = models.AutoField(primary_key=True)
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='comments')
@ -46,5 +66,5 @@ class Comment(models.Model):
class StateChange(models.Model):
id = models.AutoField(primary_key=True)
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='state_changes')
state = models.CharField(max_length=255)
state = models.CharField(max_length=255, choices=STATE_CHOICES, default='pending_new')
timestamp = models.DateTimeField(auto_now_add=True)