ソースを参照

fixed: adding event; error: changing an event with 'order_synced' in body

System Administrator 1 年間 前
コミット
5847761b17

+ 3 - 1
apiviews/event.py

@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404
 from rest_framework import viewsets, permissions
 from rest_framework.response import Response
 from webservice_running.models import Event
-from webservice_running.serializers import EventSerializer
+from webservice_running.serializers import EventSerializer, EventCreateSerializer
 
 
 class EventMVS(viewsets.ModelViewSet):
@@ -56,3 +56,5 @@ class EventMVS(viewsets.ModelViewSet):
             instance._prefetched_objects_cache = {}
 
         return Response(serializer.data)
+
+

+ 70 - 0
apiviews/event.py~

@@ -0,0 +1,70 @@
+from django.shortcuts import get_object_or_404
+from rest_framework import viewsets, permissions
+from rest_framework.response import Response
+from webservice_running.models import Event
+from webservice_running.serializers import EventSerializer, EventCreateSerializer
+
+
+class EventMVS(viewsets.ModelViewSet):
+    queryset = Event.objects.all()
+    serializer_class = EventSerializer
+    #permission_classes = [IsOwnerOrReadOnly]
+    permission_classes = [permissions.IsAuthenticated]
+    
+
+    def get_object(self):
+        """
+        Returns the object the view is displaying.
+
+        You may want to override this if you need to provide non-standard
+        queryset lookups.  Eg if objects are referenced using multiple
+        keyword arguments in the url conf.
+        """
+        # queryset = self.filter_queryset(self.get_queryset())
+        queryset = Event.objects.select_related('order_synced__ticket')
+
+        # Perform the lookup filtering.
+        lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field
+
+        assert lookup_url_kwarg in self.kwargs, (
+            'Expected view %s to be called with a URL keyword argument '
+            'named "%s". Fix your URL conf, or set the `.lookup_field` '
+            'attribute on the view correctly.' %
+            (self.__class__.__name__, lookup_url_kwarg)
+        )
+
+        filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]}
+        obj = get_object_or_404(queryset, **filter_kwargs)
+
+        # May raise a permission denied
+        self.check_object_permissions(self.request, obj)
+
+        return obj
+
+
+    def update(self, request, *args, **kwargs):
+        partial = kwargs.pop('partial', False)
+        instance = self.get_object()# Event.objects.select_related('order_synced__ticket').get(id=1)
+        serializer = self.get_serializer(instance, data=request.data, partial=partial)
+        # for serializer in serializers:
+        serializer.is_valid(raise_exception=True)
+        self.perform_update(serializer)
+
+        if getattr(instance, '_prefetched_objects_cache', None):
+            # If 'prefetch_related' has been applied to a queryset, we need to
+            # forcibly invalidate the prefetch cache on the instance.
+            instance._prefetched_objects_cache = {}
+
+        return Response(serializer.data)
+
+
+    def create(self, request, *args, **kwargs):
+        serializer = self.get_serializer(data=request.data) #EventCreateSerializer(data=request.data)
+        serializer.is_valid(raise_exception=True)
+        self.perform_create(serializer)
+        headers = self.get_success_headers(serializer.data)
+        print("="*100)
+        print(serializer.data)
+        print("="*100)
+        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
+

+ 2 - 1
serializers/__init__.py

@@ -5,10 +5,11 @@ from .browse_event import BrowseEventSerializer
 from .browse_sportsman import BrowseSportsmanSerializer
 from .event_gallery import EventGallerySerializer
 from .event import EventSerializer
+from .event_create import EventCreateSerializer
 from .participant import ParticipantSerializer
 from .saved_event import SavedEventSerializer
 from .saved_sportsman import SavedSportsmanSerializer
 from .sport_preferrence import SportPreferrenceSerializer
 from .sport import SportSerializer
 from .sportsman_gallery import SportsmanGallerySerializer
-from .sportsman import SportsmanSerializer
+from .sportsman import SportsmanSerializer

+ 14 - 0
serializers/__init__.py~

@@ -0,0 +1,14 @@
+from .achievement import AchievementSerializer
+from .achievements import AchievementsSerializer
+from .blocked_sportsman import BlockedSportsmanSerializer
+from .browse_event import BrowseEventSerializer
+from .browse_sportsman import BrowseSportsmanSerializer
+from .event_gallery import EventGallerySerializer
+from .event import EventSerializer
+from .participant import ParticipantSerializer
+from .saved_event import SavedEventSerializer
+from .saved_sportsman import SavedSportsmanSerializer
+from .sport_preferrence import SportPreferrenceSerializer
+from .sport import SportSerializer
+from .sportsman_gallery import SportsmanGallerySerializer
+from .sportsman import SportsmanSerializer

+ 36 - 40
serializers/event.py

@@ -1,42 +1,43 @@
+from django.forms.models import model_to_dict
 from rest_framework import serializers
 from webservice_running.models import Event
 from webservice_running.exceptions import OrderNotDefined
 
 
 class EventSerializer(serializers.ModelSerializer):
-    service = serializers.IntegerField(source="order_synced.service.pk", allow_null=True)    
-    service_type = serializers.IntegerField(source="order_synced.service_type.pk", allow_null=True) 
-    state = serializers.CharField(source="order_synced.state", allow_null=True) 
-    id_metaservice = serializers.IntegerField(source="order_synced.id_metaservice", allow_null=True) 
-    
-    provider = serializers.IntegerField(source="order_synced.provider.pk", allow_null=True) 
-    provider_screen_name = serializers.CharField(source="order_synced.provider.user_id.full_name", allow_null=True) 
+    service = serializers.IntegerField(source="order_synced.service.pk", allow_null=True, required=False)
+    service_type = serializers.IntegerField(source="order_synced.service_type.pk", allow_null=True, required=False)
+    state = serializers.CharField(source="order_synced.state", allow_null=True, required=False)
+    id_metaservice = serializers.IntegerField(source="order_synced.id_metaservice", allow_null=True, required=False)
 
-    receiver = serializers.IntegerField(source="order_synced.receiver.pk", allow_null=True)
-    receiver_screen_name = serializers.CharField(source="order_synced.receiver.full_name", allow_null=True) 
-    
-    client_id = serializers.CharField(source="order_synced.client_id.pk", allow_null=True) 
-    client_screen_name = serializers.CharField(source="order_synced.client.user.full_name", allow_null=True) 
+    provider = serializers.IntegerField(source="order_synced.provider.pk", allow_null=True, required=False)
+    provider_screen_name = serializers.CharField(source="order_synced.provider.user_id.full_name", allow_null=True, required=False)
 
-    #time_created = serializers.DateTimeField(source="order_synced.time_created", allow_null=True) 
-    time_placed = serializers.DateTimeField(source="order_synced.time_placed", allow_null=True) 
-    time_start = serializers.DateTimeField(source="order_synced.time_start", allow_null=True) 
-    time_start_real = serializers.DateTimeField(source="order_synced.time_start_real", allow_null=True)
-    time_start_predicted = serializers.DateTimeField(source="order_synced.time_start_predicted", allow_null=True)
-    time_finish_predicted = serializers.DateTimeField(source="order_synced.time_finish_predicted", allow_null=True) 
-    time_finish_real = serializers.DateTimeField(source="order_synced.time_finish_real", allow_null=True) 
-    
-    predicted_price = serializers.FloatField(source="order_synced.predicted_price", allow_null=True) 
-    real_price = serializers.FloatField(source="order_synced.real_price", allow_null=True) 
-    is_global = serializers.BooleanField(source="order_synced.is_global", allow_null=True) 
-    is_visible = serializers.BooleanField(source="order_synced.is_visible", allow_null=True) 
-    ticket = serializers.IntegerField(source="order_synced.ticket.pk", allow_null=True) 
-    status = serializers.IntegerField(source="order_synced.ticket.stauts", allow_null=True)
-    priority = serializers.IntegerField(source="order_synced.ticket.priority", allow_null=True)
-    title = serializers.CharField(source="order_synced.ticket.title", allow_null=True)
-    note = serializers.CharField(source="order_synced.ticket.note", allow_null=True)
-    due_date = serializers.DateField(source="order_synced.ticket.due_date", allow_null=True)
-    status_changed_date = serializers.DateTimeField(source="order_synced.ticket.status_changed_date", allow_null=True)
+    receiver = serializers.IntegerField(source="order_synced.receiver.pk", allow_null=True, required=False)
+    receiver_screen_name = serializers.CharField(source="order_synced.receiver.full_name", allow_null=True, required=False)
+
+    client_id = serializers.CharField(source="order_synced.client_id.pk", allow_null=True, required=False)
+    client_screen_name = serializers.CharField(source="order_synced.client.user.full_name", allow_null=True, required=False)
+
+    #time_created = serializers.DateTimeField(source="order_synced.time_created", allow_null=True)
+    time_placed = serializers.DateTimeField(source="order_synced.time_placed", allow_null=True, required=False)
+    time_start = serializers.DateTimeField(source="order_synced.time_start", allow_null=True, required=False)
+    time_start_real = serializers.DateTimeField(source="order_synced.time_start_real", allow_null=True, required=False)
+    time_start_predicted = serializers.DateTimeField(source="order_synced.time_start_predicted", allow_null=True, required=False)
+    time_finish_predicted = serializers.DateTimeField(source="order_synced.time_finish_predicted", allow_null=True, required=False)
+    time_finish_real = serializers.DateTimeField(source="order_synced.time_finish_real", allow_null=True, required=False)
+
+    predicted_price = serializers.FloatField(source="order_synced.predicted_price", allow_null=True, required=False)
+    real_price = serializers.FloatField(source="order_synced.real_price", allow_null=True, required=False)
+    is_global = serializers.BooleanField(source="order_synced.is_global", allow_null=True, required=False)
+    is_visible = serializers.BooleanField(source="order_synced.is_visible", allow_null=True, required=False)
+    ticket = serializers.IntegerField(source="order_synced.ticket.pk", allow_null=True, required=False)
+    status = serializers.IntegerField(source="order_synced.ticket.stauts", allow_null=True, required=False)
+    priority = serializers.IntegerField(source="order_synced.ticket.priority", allow_null=True, required=False)
+    title = serializers.CharField(source="order_synced.ticket.title", allow_null=True, required=False)
+    note = serializers.CharField(source="order_synced.ticket.note", allow_null=True, required=False)
+    due_date = serializers.DateField(source="order_synced.ticket.due_date", allow_null=True, required=False)
+    status_changed_date = serializers.DateTimeField(source="order_synced.ticket.status_changed_date", allow_null=True, required=False)
     # order_synced = OrdersSerializer(read_only=True)
     
     class Meta:
@@ -54,9 +55,12 @@ class EventSerializer(serializers.ModelSerializer):
         if not order_instance:
             raise OrderNotDefined()
         ticket_instance = instance.order_synced.ticket
-
+        # ОШИБКА ЕСЛИ В validated_data УКАЗАТЬ order_synced
         for attr, value in validated_data.items():
             if attr == "order_synced":
+                print("="*100)
+                #print(model_to_dict(value))
+                print("="*100)
                 for order_synced_attr, order_synced_value in value.items():
                     if order_synced_attr == "ticket":
                         for ticket_attr, ticket_value in order_synced_value.items():
@@ -72,11 +76,3 @@ class EventSerializer(serializers.ModelSerializer):
         order_instance.save()
         return instance
 
-    def create(self, request, *args, **kwargs):
-        serializer = self.get_serializer(data=request.data)
-        serializer.is_valid(raise_exception=True)
-        self.perform_create(serializer)
-        headers = self.get_success_headers(serializer.data)
-        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
-
-

+ 78 - 0
serializers/event.py~

@@ -0,0 +1,78 @@
+from django.forms.models import model_to_dict
+from rest_framework import serializers
+from webservice_running.models import Event
+from webservice_running.exceptions import OrderNotDefined
+
+
+class EventSerializer(serializers.ModelSerializer):
+    service = serializers.IntegerField(source="order_synced.service.pk", allow_null=True, required=False)
+    service_type = serializers.IntegerField(source="order_synced.service_type.pk", allow_null=True, required=False)
+    state = serializers.CharField(source="order_synced.state", allow_null=True, required=False)
+    id_metaservice = serializers.IntegerField(source="order_synced.id_metaservice", allow_null=True, required=False)
+
+    provider = serializers.IntegerField(source="order_synced.provider.pk", allow_null=True, required=False)
+    provider_screen_name = serializers.CharField(source="order_synced.provider.user_id.full_name", allow_null=True, required=False)
+
+    receiver = serializers.IntegerField(source="order_synced.receiver.pk", allow_null=True, required=False)
+    receiver_screen_name = serializers.CharField(source="order_synced.receiver.full_name", allow_null=True, required=False)
+
+    client_id = serializers.CharField(source="order_synced.client_id.pk", allow_null=True, required=False)
+    client_screen_name = serializers.CharField(source="order_synced.client.user.full_name", allow_null=True, required=False)
+
+    #time_created = serializers.DateTimeField(source="order_synced.time_created", allow_null=True)
+    time_placed = serializers.DateTimeField(source="order_synced.time_placed", allow_null=True, required=False)
+    time_start = serializers.DateTimeField(source="order_synced.time_start", allow_null=True, required=False)
+    time_start_real = serializers.DateTimeField(source="order_synced.time_start_real", allow_null=True, required=False)
+    time_start_predicted = serializers.DateTimeField(source="order_synced.time_start_predicted", allow_null=True, required=False)
+    time_finish_predicted = serializers.DateTimeField(source="order_synced.time_finish_predicted", allow_null=True, required=False)
+    time_finish_real = serializers.DateTimeField(source="order_synced.time_finish_real", allow_null=True, required=False)
+
+    predicted_price = serializers.FloatField(source="order_synced.predicted_price", allow_null=True, required=False)
+    real_price = serializers.FloatField(source="order_synced.real_price", allow_null=True, required=False)
+    is_global = serializers.BooleanField(source="order_synced.is_global", allow_null=True, required=False)
+    is_visible = serializers.BooleanField(source="order_synced.is_visible", allow_null=True, required=False)
+    ticket = serializers.IntegerField(source="order_synced.ticket.pk", allow_null=True, required=False)
+    status = serializers.IntegerField(source="order_synced.ticket.stauts", allow_null=True, required=False)
+    priority = serializers.IntegerField(source="order_synced.ticket.priority", allow_null=True, required=False)
+    title = serializers.CharField(source="order_synced.ticket.title", allow_null=True, required=False)
+    note = serializers.CharField(source="order_synced.ticket.note", allow_null=True, required=False)
+    due_date = serializers.DateField(source="order_synced.ticket.due_date", allow_null=True, required=False)
+    status_changed_date = serializers.DateTimeField(source="order_synced.ticket.status_changed_date", allow_null=True, required=False)
+    # order_synced = OrdersSerializer(read_only=True)
+    
+    class Meta:
+        model = Event
+        fields = "__all__"
+        # fields = ["order", "order_synced"]
+        # depth = 2
+
+    def update(self, instance, validated_data):
+        """
+        Normally order_synced is not null, but in testing it is null=True, so it can produce exceptions
+        """
+
+        order_instance = instance.order_synced
+        if not order_instance:
+            raise OrderNotDefined()
+        ticket_instance = instance.order_synced.ticket
+        # ОШИБКА ЕСЛИ В validated_data УКАЗАТЬ order_synced
+        for attr, value in validated_data.items():
+            if attr == "order_synced":
+                print("="*100)
+                #print(model_to_dict(value))
+                print("="*100)
+                for order_synced_attr, order_synced_value in value.items():
+                    if order_synced_attr == "ticket":
+                        for ticket_attr, ticket_value in order_synced_value.items():
+                            setattr(ticket_instance, ticket_attr, ticket_value)
+                            continue
+                    else:
+                        setattr(order_instance, order_synced_attr, order_synced_value)
+            else:
+                setattr(instance, attr, value)
+
+        instance.save()
+        ticket_instance.save()
+        order_instance.save()
+        return instance
+

+ 10 - 0
serializers/event_create.py

@@ -0,0 +1,10 @@
+from rest_framework import serializers
+from webservice_running.models import Event
+
+
+class EventCreateSerializer(serializers.ModelSerializer):
+    class Meta:
+        model = Event
+        fields = "__all__"
+
+    

+ 11 - 0
serializers/event_create.py~

@@ -0,0 +1,11 @@
+from rest_framework import serializers
+from webservice_running.models import Event
+
+
+class EventCreateSerializer(serializers.ModelSerializer):
+    print("="*100)
+    class Meta:
+        model = Event
+        fields = "__all__"
+
+