c3lf-system-3/core/tickets/models.py

173 lines
6.2 KiB
Python
Raw Normal View History

from itertools import groupby
2024-06-23 00:50:44 +00:00
from django.utils import timezone
from django.db import models
2024-01-07 18:56:10 +00:00
from django_softdelete.models import SoftDeleteModel
2024-01-22 16:21:22 +00:00
from authentication.models import ExtendedUser
from inventory.models import Event, Item
2024-01-12 21:59:57 +00:00
from django.db.models.signals import post_save, pre_save
from django.dispatch import receiver
2024-01-07 18:56:10 +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'),
2024-01-19 20:13:04 +00:00
('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'),
2024-01-19 20:13:04 +00:00
('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'),
)
RELATION_STATUS_CHOICES = (
('possible', 'Possible'),
('confirmed', 'Confirmed'),
('discarded', 'Discarded'),
('provided', 'Provided'),
)
2024-01-07 18:56:10 +00:00
class IssueThread(SoftDeleteModel):
id = models.AutoField(primary_key=True)
2024-01-12 21:59:57 +00:00
uuid = models.CharField(max_length=255, unique=True, null=False, blank=False)
2024-01-07 18:56:10 +00:00
name = models.CharField(max_length=255)
event = models.ForeignKey(Event, null=True, on_delete=models.SET_NULL, related_name='issue_threads')
2024-01-07 18:56:10 +00:00
manually_created = models.BooleanField(default=False)
2024-01-12 21:59:57 +00:00
def short_uuid(self):
return self.uuid[:8]
@property
def state(self):
try:
2024-11-21 20:14:38 +00:00
state_changes = sorted(self.state_changes.all(), key=lambda x: x.timestamp, reverse=True)
if state_changes:
return state_changes[0].state
else:
return None
except AttributeError:
return 'none'
@state.setter
def state(self, value):
if self.state == value:
return
self.state_changes.create(state=value)
if value == 'closed_spam' and self.emails.exists():
self.emails.first().train_spam()
2024-01-22 16:21:22 +00:00
@property
def assigned_to(self):
try:
2024-11-21 20:14:38 +00:00
assignments = sorted(self.assignments.all(), key=lambda x: x.timestamp, reverse=True)
if assignments:
return assignments[0].assigned_to
else:
return None
2024-01-22 16:21:22 +00:00
except AttributeError:
return None
@assigned_to.setter
def assigned_to(self, value):
if self.assigned_to == value:
return
self.assignments.create(assigned_to=value)
@property
def related_items(self):
groups = groupby(self.item_relation_changes.all(), lambda rel: rel.item.id)
return [sorted(v, key=lambda r: r.timestamp)[0].item for k, v in groups]
def __str__(self):
return '[' + str(self.id) + '][' + self.short_uuid() + '] ' + self.name
2024-01-07 18:56:10 +00:00
class Meta:
permissions = [
('send_mail', 'Can send mail'),
('add_issuethread_manual', 'Can add issue thread manually'),
2024-01-22 16:21:22 +00:00
('assign_issuethread', 'Can assign issue thread'),
2024-01-07 18:56:10 +00:00
]
2024-01-12 21:59:57 +00:00
@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')
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)
def __str__(self):
return str(self.issue_thread) + ' comment #' + str(self.id)
2024-01-07 18:56:10 +00:00
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')
2024-01-07 18:56:10 +00:00
timestamp = models.DateTimeField(auto_now_add=True)
2024-01-22 16:21:22 +00:00
def __str__(self):
return str(self.issue_thread) + ' state change to ' + self.state
2024-01-22 16:21:22 +00:00
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
2024-06-23 00:50:44 +00:00
class ItemRelation(models.Model):
id = models.AutoField(primary_key=True)
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='item_relation_changes')
item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name='issue_relation_changes')
timestamp = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=255, choices=RELATION_STATUS_CHOICES, default='possible')
def __str__(self):
return str(self.issue_thread) + ' related to ' + str(self.item)
2024-06-23 00:50:44 +00:00
class ShippingVoucher(models.Model):
id = models.AutoField(primary_key=True)
issue_thread = models.ForeignKey(IssueThread, on_delete=models.CASCADE, related_name='shipping_vouchers', null=True)
voucher = models.CharField(max_length=255)
type = models.CharField(max_length=255)
timestamp = models.DateTimeField(auto_now_add=True)
used_at = models.DateTimeField(null=True)
def __str__(self):
return self.voucher + ' (' + self.type + ')'
def save(self, *args, **kwargs):
if self.used_at is None and self.issue_thread is not None:
self.used_at = timezone.now()
super().save(*args, **kwargs)