118 lines
4.1 KiB
Python
118 lines
4.1 KiB
Python
from django.db import models
|
|
from django_softdelete.models import SoftDeleteModel
|
|
|
|
from authentication.models import ExtendedUser
|
|
from inventory.models import Event
|
|
from django.db.models.signals import post_save, pre_save
|
|
from django.dispatch import receiver
|
|
|
|
STATE_CHOICES = (
|
|
('pending_new', 'New'),
|
|
('pending_open', 'Open'),
|
|
('pending_shipping', 'Needs to be shipped'),
|
|
('pending_physical_confirmation', 'Needs to be confirmed physically'),
|
|
('pending_return', 'Needs to be returned'),
|
|
('pending_postponed', 'Postponed'),
|
|
('waiting_details', 'Waiting for details'),
|
|
('waiting_pre_shipping', 'Waiting for Address/Shipping Info'),
|
|
('closed_returned', 'Closed: Returned'),
|
|
('closed_shipped', 'Closed: Shipped'),
|
|
('closed_not_found', 'Closed: Not found'),
|
|
('closed_not_our_problem', 'Closed: Not our problem'),
|
|
('closed_duplicate', 'Closed: Duplicate'),
|
|
('closed_timeout', 'Closed: Timeout'),
|
|
('closed_spam', 'Closed: Spam'),
|
|
('closed_nothing_missing', 'Closed: Nothing missing'),
|
|
('closed_wtf', 'Closed: WTF'),
|
|
('found_open', 'Item Found and stored externally'),
|
|
('found_closed', 'Item Found and stored externally and closed'),
|
|
)
|
|
|
|
|
|
class IssueThread(SoftDeleteModel):
|
|
id = models.AutoField(primary_key=True)
|
|
uuid = models.CharField(max_length=255, unique=True, null=False, blank=False)
|
|
name = models.CharField(max_length=255)
|
|
manually_created = models.BooleanField(default=False)
|
|
|
|
def short_uuid(self):
|
|
return self.uuid[:8]
|
|
|
|
@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)
|
|
|
|
@property
|
|
def assigned_to(self):
|
|
try:
|
|
return self.assignments.order_by('-timestamp').first().assigned_to
|
|
except AttributeError:
|
|
return None
|
|
|
|
@assigned_to.setter
|
|
def assigned_to(self, value):
|
|
if self.assigned_to == value:
|
|
return
|
|
self.assignments.create(assigned_to=value)
|
|
|
|
def __str__(self):
|
|
return '[' + str(self.id) + '][' + self.short_uuid() + '] ' + self.name
|
|
|
|
class Meta:
|
|
permissions = [
|
|
('send_mail', 'Can send mail'),
|
|
('add_issuethread_manual', 'Can add issue thread manually'),
|
|
('assign_issuethread', 'Can assign issue thread'),
|
|
]
|
|
|
|
|
|
@receiver(pre_save, sender=IssueThread)
|
|
def set_uuid(sender, instance, **kwargs):
|
|
import uuid
|
|
if instance.uuid is None or instance.uuid == '':
|
|
instance.uuid = str(uuid.uuid4())
|
|
|
|
|
|
@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')
|
|
comment = models.TextField()
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
def __str__(self):
|
|
return str(self.issue_thread) + ' comment #' + str(self.id)
|
|
|
|
|
|
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, choices=STATE_CHOICES, default='pending_new')
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
def __str__(self):
|
|
return str(self.issue_thread) + ' state change to ' + self.state
|
|
|
|
|
|
class Assignment(models.Model):
|
|
id = models.AutoField(primary_key=True)
|
|
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='assignments')
|
|
assigned_to = models.ForeignKey(ExtendedUser, on_delete=models.CASCADE, related_name='assigned_tickets')
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|
|
|
|
def __str__(self):
|
|
return str(self.issue_thread) + ' assigned to ' + self.assigned_to.username
|