1
0

ticket.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from django.db import models, transaction
  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, 359],
  36. [359]
  37. ]),
  38. (4, "NEG_REQUEST", [
  39. [420, 421, 459],
  40. [421, 459],
  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. # DATA
  76. title = models.CharField(max_length=128)
  77. status = models.PositiveSmallIntegerField(null=True)
  78. created_at = models.DateTimeField(auto_now_add=True, editable=False)
  79. updated_at = models.DateTimeField(auto_now=True)
  80. due_date = models.DateField()
  81. note = models.TextField(blank=True, null=True)
  82. json = models.TextField(blank=True, null=True)
  83. priority = models.PositiveSmallIntegerField(default=0)
  84. # FK
  85. created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="created_by", editable=False)
  86. assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="assigned_to")
  87. ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE)
  88. ticket_type = models.PositiveSmallIntegerField(choices=TICKET_TYPES_CHOICES, default=1)
  89. def __str__(self):
  90. return self.title
  91. def _get_statuses(self) -> list:
  92. return self.LIFE_CYCLE_DICT.get(self.ticket_type)
  93. def get_available_statuses(self) -> list:
  94. for status_scenario in self._get_statuses():
  95. if status_scenario[0] == int(self.status):
  96. return status_scenario[1:] if len(status_scenario) > 1 else None
  97. def set_first_status(self):
  98. self.status = self._get_statuses()[0][0]
  99. def get_status_display(self):
  100. return self.TICKET_STATUSES_NAMES.get(self.status)
  101. def archive(self):
  102. """
  103. Архивирует текущий тикет.
  104. Создает новую запись в TicketArchive с информацией текущего тикета,
  105. а затем удаляет текущий тикет из базы данных.
  106. """
  107. with transaction.atomic():
  108. # Создаем новую запись в TicketArhive, сохраняя значения всех полей
  109. TicketArchive.objects.create(
  110. title=self.title,
  111. ticket_list=self.ticket_list,
  112. ticket_type=self.ticket_type,
  113. status=self.status,
  114. created_at=self.created_at,
  115. due_date=self.due_date,
  116. created_by=self.created_by,
  117. assigned_to=self.assigned_to,
  118. note=self.note
  119. )
  120. # Удаляем текущий тикет
  121. self.delete()
  122. def save(self, *args, **kwargs):
  123. if not self.status:
  124. self.set_first_status()
  125. super(Ticket, self).save(*args, **kwargs)
  126. def get_absolute_url(self):
  127. return reverse("tickets:ticket_detail", kwargs={"ticket_id": self.pk})
  128. class Meta:
  129. ordering = ["-priority", "created_at"]
  130. class TicketArchive(models.Model):
  131. title = models.CharField(max_length=128, editable=False)
  132. ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE, editable=False)
  133. ticket_type = models.PositiveSmallIntegerField(choices=Ticket.TICKET_TYPES_CHOICES, editable=False)
  134. status = models.PositiveSmallIntegerField(editable=False)
  135. created_at = models.DateTimeField(editable=False)
  136. updated_at = models.DateTimeField(auto_now_add=True, editable=False)
  137. due_date = models.DateField(editable=False)
  138. created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="ticket_archive_created_by", editable=False)
  139. assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="ticket_archive_assigned_to", editable=False)
  140. note = models.TextField(blank=True, null=True, editable=False)
  141. def get_status_display(self):
  142. return Ticket.TICKET_STATUSES_NAMES.get(self.status)