1
0

ticket.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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 archive(self):
  100. """
  101. Архивирует текущий тикет.
  102. Создает новую запись в TicketArchive с информацией текущего тикета,
  103. а затем удаляет текущий тикет из базы данных.
  104. """
  105. with transaction.atomic():
  106. # Создаем новую запись в TicketArhive, сохраняя значения всех полей
  107. TicketArchive.objects.create(
  108. title=self.title,
  109. ticket_list=self.ticket_list,
  110. ticket_type=self.ticket_type,
  111. status=self.status,
  112. created_at=self.created_at,
  113. due_date=self.due_date,
  114. created_by=self.created_by,
  115. assigned_to=self.assigned_to,
  116. note=self.note
  117. )
  118. # Удаляем текущий тикет
  119. self.delete()
  120. def save(self, *args, **kwargs):
  121. if not self.status:
  122. self.set_first_status()
  123. super(Ticket, self).save(*args, **kwargs)
  124. def get_absolute_url(self):
  125. return reverse("tickets:ticket_detail", kwargs={"ticket_id": self.pk})
  126. class Meta:
  127. ordering = ["-priority", "created_at"]
  128. class TicketArchive(models.Model):
  129. title = models.CharField(max_length=128, editable=False)
  130. ticket_list = models.ForeignKey(TicketList, on_delete=models.CASCADE, editable=False)
  131. ticket_type = models.PositiveSmallIntegerField(choices=Ticket.TICKET_TYPES_CHOICES, editable=False)
  132. status = models.PositiveSmallIntegerField(editable=False)
  133. created_at = models.DateTimeField(editable=False)
  134. updated_at = models.DateTimeField(auto_now_add=True, editable=False)
  135. due_date = models.DateField(editable=False)
  136. created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="ticket_archive_created_by", editable=False)
  137. assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="ticket_archive_assigned_to", editable=False)
  138. note = models.TextField(blank=True, null=True, editable=False)
  139. def get_status_display(self):
  140. return Ticket.TICKET_STATUSES_NAMES.get(self.status)