ticket.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from django.db import models
  2. from django.urls import reverse
  3. from django.contrib.auth import get_user_model
  4. from django.core.exceptions import ValidationError
  5. from tickets.models.ticket_list import TicketList
  6. class Ticket(models.Model):
  7. TICKET_TYPES = (
  8. (1, "ST_REQUEST", [
  9. [111, 121, 149, 159],
  10. [110, 121, 149, 159],
  11. [121, 131, 149, 159],
  12. [131, 141, 149],
  13. [141, 151, 110],
  14. [149, 151, 110],
  15. [159],
  16. [151]
  17. ]),
  18. (2, "SERVICE_REQUEST", [
  19. [210, 211, 251],
  20. [211, 212, 220, 238, 249],
  21. [212, 221, 229, 238, 249],
  22. [221, 222, 238, 249],
  23. [220, 211, 238, 249],
  24. [229, 211, 251],
  25. [222, 231, 238, 249],
  26. [231, 241, 238, 249],
  27. [238, 231, 239, 211, 212, 221, 220, 222, 249],
  28. [239, 231, 239, 211, 212, 221, 220, 222, 249],
  29. [241, 251],
  30. [249, 251],
  31. [251]
  32. ]),
  33. (3, "ACCESS_REQUEST", [
  34. [320, 321, 359],
  35. [321],
  36. [359]
  37. ]),
  38. (4, "NEG_REQUEST", [
  39. [420, 421, 459],
  40. [421],
  41. [459]
  42. ])
  43. )
  44. TICKET_STATUSES_NAMES = {
  45. 111: "NEW",
  46. 110: "REOPENED",
  47. 121: "ASSIGNED",
  48. 131: "IN PROCESS",
  49. 149: "WONTFIX",
  50. 141: "DONE",
  51. 159: "DUPLICATE",
  52. 151: "CLOSED",
  53. 210: "TEMPLATE",
  54. 211: "BOOKED",
  55. 212: "ASSIGNED",
  56. 221: "ACCEPTED",
  57. 220: "PENDING",
  58. 229: "DECLINED",
  59. 222: "PRE-START",
  60. 231: "PROCESS",
  61. 238: "PRE-FORCEMAJEUER",
  62. 239: "FORCEMAJEUER",
  63. 241: "DONE",
  64. 249: "CANCELLED",
  65. 251: "CLOSED",
  66. 320: "PENDING",
  67. 321: "ACCEPTED",
  68. 359: "DECLINED",
  69. 420: "PENDING",
  70. 421: "ACCEPTED",
  71. 459: "DECLINED"
  72. }
  73. TICKET_TYPES_CHOICES = tuple((item[0], item[1]) for item in TICKET_TYPES)
  74. LIFE_CYCLE_DICT = dict((item[0], item[2]) for item in TICKET_TYPES)
  75. title = models.CharField(max_length=128)
  76. ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE)
  77. ticket_type = models.PositiveSmallIntegerField(choices=TICKET_TYPES_CHOICES, default=1)
  78. status = models.PositiveSmallIntegerField(null=True)
  79. created_at = models.DateTimeField(auto_now_add=True, editable=False)
  80. updated_at = models.DateTimeField(auto_now=True)
  81. due_date = models.DateField()
  82. created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="created_by", editable=False)
  83. assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="assigned_to")
  84. note = models.TextField(blank=True, null=True)
  85. json = models.TextField(blank=True, null=True)
  86. priority = models.PositiveSmallIntegerField(default=0)
  87. def __str__(self):
  88. return self.title
  89. def _get_statuses(self) -> list:
  90. return self.LIFE_CYCLE_DICT.get(self.ticket_type)
  91. def get_available_statuses(self) -> list:
  92. for status_scenario in self._get_statuses():
  93. if status_scenario[0] == int(self.status):
  94. return status_scenario[1:] if len(status_scenario) > 1 else None
  95. def set_first_status(self):
  96. self.status = self._get_statuses()[0][0]
  97. def get_status_display(self):
  98. return self.TICKET_STATUSES_NAMES.get(self.status)
  99. def save(self, *args, **kwargs):
  100. if not self.status:
  101. self.set_first_status()
  102. super(Ticket, self).save(*args, **kwargs)
  103. def get_absolute_url(self):
  104. return reverse("tickets:ticket_detail", kwargs={"ticket_id": self.pk})
  105. class Meta:
  106. ordering = ["-priority", "created_at"]
  107. class TicketArchive(models.Model):
  108. title = models.CharField(max_length=128, editable=False)
  109. ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE, editable=False)
  110. ticket_type = models.PositiveSmallIntegerField(choices=Ticket.TICKET_TYPES_CHOICES, editable=False)
  111. status = models.PositiveSmallIntegerField(editable=False)
  112. created_at = models.DateTimeField(editable=False)
  113. updated_at = models.DateTimeField(editable=False)
  114. due_date = models.DateField(editable=False)
  115. created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="ticket_archive_created_by", editable=False)
  116. assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="ticket_archive_assigned_to", editable=False)
  117. note = models.TextField(blank=True, null=True, editable=False)
  118. def get_status_display(self):
  119. return Ticket.TICKET_STATUSES_NAMES.get(self.status)