Răsfoiți Sursa

Tickets API v1.2

- Changed display of some fields
- Added possibility to work with new ticket json field
- Deleted tickets are now placed in the archive
TonyKurts 6 luni în urmă
părinte
comite
9f553ce260

+ 21 - 6
admin.py

@@ -1,14 +1,28 @@
 from django.contrib import admin
+from django.utils.safestring import mark_safe
 
-from tickets.models import Attachment, Comment, TicketList, Ticket
+from tickets.models import Attachment, Comment, TicketList, Ticket, TicketArchive
+
+
+class TicketListAdmin(admin.ModelAdmin):
+    list_filter = ("group",)
 
 
 class TicketAdmin(admin.ModelAdmin):
-    list_display = ("title", "ticket_list", "ticket_type",  "status", "priority", "created_at", "due_date")
-    list_filter = ("ticket_list", "ticket_type",)
+    list_display = ("title", "ticket_list",  "status", "created_by", "assigned_to", "created_at", "updated_at", "due_date", "priority")
+    list_filter =("ticket_list", "status", "created_by", "assigned_to", "created_at", "updated_at")
     ordering = ("priority",)
     search_fields = ("title", "note")
 
+class TicketArchiveAdmin(admin.ModelAdmin):
+    list_display = ("title", "ticket_list",  "status", "created_by", "assigned_to", "created_at", "updated_at", "due_date")
+    list_filter = ("ticket_list", "status", "created_by", "assigned_to", "created_at", "updated_at")
+    search_fields = ("title", "note")
+    readonly_fields = ("title", "ticket_list", "ticket_type", "status", "created_at", "updated_at", "due_date", "created_by", "assigned_to", "display_note")
+
+    def display_note(self, obj):
+        return mark_safe(obj.note)
+
 
 class CommentAdmin(admin.ModelAdmin):
     list_display = ("author", "date", "snippet")
@@ -19,7 +33,8 @@ class AttachmentAdmin(admin.ModelAdmin):
     autocomplete_fields = ["added_by", "ticket"]
 
 
-admin.site.register(TicketList)
-admin.site.register(Comment, CommentAdmin)
+admin.site.register(TicketList, TicketListAdmin)
 admin.site.register(Ticket, TicketAdmin)
-admin.site.register(Attachment, AttachmentAdmin)
+admin.site.register(TicketArchive, TicketArchiveAdmin)
+admin.site.register(Comment, CommentAdmin)
+admin.site.register(Attachment, AttachmentAdmin)

+ 4 - 2
api/serializers/ticket.py

@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404
 
 from rest_framework import serializers
 
-from tickets.models import Ticket
+from tickets.models import Ticket, TicketList
 
 
 class BaseTicketSerializer(serializers.ModelSerializer):
@@ -20,7 +20,9 @@ class BaseTicketSerializer(serializers.ModelSerializer):
 
 
 class TicketSerializer(BaseTicketSerializer):
-    note = serializers.CharField(write_only=True, required=False)    
+    note = serializers.CharField(write_only=True, required=False)
+    json = serializers.CharField(write_only=True, required=False)
+    ticket_list = serializers.PrimaryKeyRelatedField(queryset=TicketList.objects.all(), write_only=True)
 
     class Meta():
         model = Ticket

+ 0 - 2
api/serializers/ticket_list.py

@@ -4,8 +4,6 @@ from tickets.models import TicketList
 
 
 class TicketListSerializer(serializers.ModelSerializer):
-    group_name = serializers.CharField(source='group.name', read_only=True)
-    
     class Meta:
         model = TicketList
         fields = "__all__"

+ 2 - 2
api/views/__init__.py

@@ -1,2 +1,2 @@
-from tickets.api.views.ticket_list import TicketListDetailAPIView, TicketListListAPIView
-from tickets.api.views.ticket import TicketDetailAPIView, TicketCreateAPIView, TicketStatusAPIView
+from tickets.api.views.ticket_list import *
+from tickets.api.views.ticket import *

+ 8 - 2
api/views/ticket.py

@@ -1,5 +1,6 @@
-from rest_framework import generics, permissions
-from rest_framework.exceptions import NotFound
+from rest_framework import generics, permissions, status
+from rest_framework.views import APIView
+from rest_framework.response import Response
 
 from tickets.models import Ticket
 from tickets.api.serializers import TicketDetailSerializer, TicketSerializer, TicketStatusSerializer
@@ -11,6 +12,11 @@ class TicketDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
     serializer_class = TicketDetailSerializer
     permission_classes = [permissions.IsAuthenticated & UserTicketAccessPermission]
 
+    def destroy(self, request, *args, **kwargs):
+        instance = self.get_object()
+        instance.archive()
+        return Response(status=status.HTTP_204_NO_CONTENT)
+
 
 class TicketCreateAPIView(generics.CreateAPIView):
     queryset = Ticket.objects.all()

+ 4 - 4
models/__init__.py

@@ -1,4 +1,4 @@
-from tickets.models.attachment import Attachment
-from tickets.models.comment import Comment
-from tickets.models.ticket_list import TicketList
-from tickets.models.ticket import Ticket
+from tickets.models.attachment import *
+from tickets.models.comment import *
+from tickets.models.ticket_list import *
+from tickets.models.ticket import *

+ 27 - 4
models/ticket.py

@@ -1,4 +1,4 @@
-from django.db import models
+from django.db import models, transaction
 from django.urls import reverse
 from django.contrib.auth import get_user_model
 from django.core.exceptions import ValidationError
@@ -35,12 +35,12 @@ class Ticket(models.Model):
         ]),
         (3, "ACCESS_REQUEST", [
             [320, 321, 359],
-            [321],
+            [321, 359],
             [359]
         ]),
         (4, "NEG_REQUEST", [
             [420, 421, 459],
-            [421],
+            [421, 459],
             [459]
         ])
     )
@@ -108,6 +108,29 @@ class Ticket(models.Model):
     def get_status_display(self):
         return self.TICKET_STATUSES_NAMES.get(self.status)
 
+    def archive(self):
+        """
+        Архивирует текущий тикет.
+        
+        Создает новую запись в TicketArchive с информацией текущего тикета,
+        а затем удаляет текущий тикет из базы данных.
+        """
+        with transaction.atomic():
+            # Создаем новую запись в TicketArhive, сохраняя значения всех полей
+            TicketArchive.objects.create(
+                title=self.title,
+                ticket_list=self.ticket_list,
+                ticket_type=self.ticket_type,
+                status=self.status,
+                created_at=self.created_at,
+                due_date=self.due_date,
+                created_by=self.created_by,
+                assigned_to=self.assigned_to,
+                note=self.note
+            )
+            # Удаляем текущий тикет
+            self.delete()
+
     def save(self, *args, **kwargs):
         if not self.status:
             self.set_first_status()
@@ -127,7 +150,7 @@ class TicketArchive(models.Model):
     ticket_type = models.PositiveSmallIntegerField(choices=Ticket.TICKET_TYPES_CHOICES, editable=False)
     status = models.PositiveSmallIntegerField(editable=False)
     created_at = models.DateTimeField(editable=False)
-    updated_at = models.DateTimeField(editable=False)
+    updated_at = models.DateTimeField(auto_now_add=True, editable=False)
     due_date = models.DateField(editable=False)
     created_by = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, related_name="ticket_archive_created_by", editable=False)
     assigned_to = models.ForeignKey(get_user_model(), on_delete=models.SET_NULL, null=True, blank=True, related_name="ticket_archive_assigned_to", editable=False)