Browse Source

Merge branch 'unstable' of blezz-tech/sharix-open-tickets into master

3 changed files with 42 additions and 3 deletions
  1. 20 0
      api/v2/serializers/tickets.py
  2. 19 0
      api/v2/views/tickets.py
  3. 3 3
      urls.py

+ 20 - 0
api/v2/serializers/tickets.py

@@ -0,0 +1,20 @@
+from rest_framework import serializers
+from tickets.models import Ticket
+
+class TicketSerializer(serializers.ModelSerializer):
+    available_statuses = serializers.SerializerMethodField()
+
+    def get_available_statuses(self, obj):
+        return obj.get_available_statuses()
+
+    def validate_status(self, status):
+        valid_statuses = self.get_available_statuses(self.instance)
+        if not valid_statuses or status not in valid_statuses:
+            raise serializers.ValidationError("Invalid value for the status field")
+        return status
+
+    class Meta():
+        model = Ticket
+        fields = "__all__"
+        ref_name = "TicketSerializer_v2"
+

+ 19 - 0
api/v2/views/tickets.py

@@ -0,0 +1,19 @@
+from rest_framework import generics, permissions
+from rest_framework_api_key.permissions import HasAPIKey
+
+from tickets.api.v2.serializers.tickets import TicketSerializer
+from tickets.models import Ticket
+
+
+class TicketPatchAPIView(generics.UpdateAPIView):
+    """
+    API для частичного обновления тикета
+    """
+
+    serializer_class = TicketSerializer
+    permission_classes = [
+        permissions.IsAuthenticated | HasAPIKey
+    ]
+    queryset = Ticket.objects.all()
+
+

+ 3 - 3
urls.py

@@ -2,6 +2,7 @@ from django.urls import path, include
 
 from tickets.api import views as apiviews
 from tickets.views import *
+from tickets.api.v2.views.tickets import TicketPatchAPIView
 
 app_name = "tickets"
 
@@ -15,7 +16,7 @@ api_v1_patterns = [
 ]
 
 api_v2_patterns = [
-
+    path('ticket/<int:pk>', TicketPatchAPIView.as_view())
 ]
 
 urlpatterns = [
@@ -33,6 +34,5 @@ urlpatterns = [
     path("attachment/remove/<int:attachment_id>/", remove_attachment, name="remove_attachment"),
 
     path("api/", include(api_v1_patterns)),
-    # TODO: Рассмотреть возможность использования второго API, когда будет готово
-    # path("api/v2/", include(api_v2_patterns)),
+    path("api/v2/", include(api_v2_patterns)),
 ]