Browse Source

Create ticket when creating order; edited .gitignore to ignore migrations and __pycache__

yobafromstarvvars 1 year ago
parent
commit
82a62ae039
2 changed files with 50 additions and 1 deletions
  1. 2 0
      .gitignore
  2. 48 1
      serializer/orders.py

+ 2 - 0
.gitignore

@@ -1,6 +1,8 @@
 # Python
 __pycache__/
+*/__pycache__
 migrations/
+!migrations/__init__.py
 !__init___.py
 *.py[cod]
 *$py.class

+ 48 - 1
serializer/orders.py

@@ -1,7 +1,54 @@
 from rest_framework import serializers
 from metaservicesynced.models import Orders
+from tickets.models import Task, TaskList
+import traceback
 
 class OrdersSerializer(serializers.ModelSerializer):
     class Meta:
         model = Orders
-        fields = '__all__'
+        fields = '__all__'
+
+    def create(self, validated_data):
+        ModelClass = self.Meta.model
+
+        if "ticket" not in validated_data:
+            # Create ticket with minimal required data
+            task_data = {
+                "task_list": TaskList.objects.get(pk=9), # MS-SUP входящие обычные заявки ST_REQUEST
+                "status": None,
+                "created_date": "2000-01-01", # mandatory
+                "status_changed_date": None,
+                "created_by": None,
+                "priority": 0, # default: 0
+                "type": None,
+                "title": "New ticket title", # mandatory
+                "note": None,
+                "due_date": None,
+                "assigned_to": None,
+            }
+            ticket_instance = Task._default_manager.create(**task_data)
+            validated_data["ticket"] = ticket_instance
+
+        try:
+            instance = ModelClass._default_manager.create(**validated_data)
+        except TypeError:
+            tb = traceback.format_exc()
+            msg = (
+                'Got a `TypeError` when calling `%s.%s.create()`. '
+                'This may be because you have a writable field on the '
+                'serializer class that is not a valid argument to '
+                '`%s.%s.create()`. You may need to make the field '
+                'read-only, or override the %s.create() method to handle '
+                'this correctly.\nOriginal exception was:\n %s' %
+                (
+                    ModelClass.__name__,
+                    ModelClass._default_manager.name,
+                    ModelClass.__name__,
+                    ModelClass._default_manager.name,
+                    self.__class__.__name__,
+                    tb
+                )
+            )
+            raise TypeError(msg)
+        
+        return instance