2024-01-07 18:56:10 +00:00
|
|
|
from django.db import models
|
|
|
|
from django_softdelete.models import SoftDeleteModel
|
|
|
|
|
|
|
|
from inventory.models import Event
|
2023-12-30 17:34:35 +00:00
|
|
|
from django.db.models.signals import post_save
|
|
|
|
from django.dispatch import receiver
|
2024-01-07 18:56:10 +00:00
|
|
|
|
2023-12-28 20:08:26 +00:00
|
|
|
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'),
|
|
|
|
('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'),
|
|
|
|
)
|
|
|
|
|
2024-01-07 18:56:10 +00:00
|
|
|
|
|
|
|
class IssueThread(SoftDeleteModel):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
assigned_to = models.CharField(max_length=255, null=True)
|
|
|
|
last_activity = models.DateTimeField(auto_now=True)
|
|
|
|
manually_created = models.BooleanField(default=False)
|
|
|
|
|
2023-12-30 17:34:35 +00:00
|
|
|
@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)
|
|
|
|
|
2024-01-07 18:56:10 +00:00
|
|
|
class Meta:
|
|
|
|
permissions = [
|
|
|
|
('send_mail', 'Can send mail'),
|
|
|
|
('add_issuethread_manual', 'Can add issue thread manually'),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-12-30 17:34:35 +00:00
|
|
|
@receiver(post_save, sender=IssueThread)
|
|
|
|
def create_issue_thread(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
StateChange.objects.create(issue_thread=instance, state='pending_new')
|
|
|
|
|
|
|
|
|
2024-01-07 18:56:10 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
class StateChange(models.Model):
|
|
|
|
id = models.AutoField(primary_key=True)
|
|
|
|
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='state_changes')
|
2023-12-30 17:34:35 +00:00
|
|
|
state = models.CharField(max_length=255, choices=STATE_CHOICES, default='pending_new')
|
2024-01-07 18:56:10 +00:00
|
|
|
timestamp = models.DateTimeField(auto_now_add=True)
|