Пример #1
0
    def _create_many(self, data):
        items = []
        errors = []
        result = {}
        serializers = []

        for entry in data:
            serializer = self.get_serializer(data=entry)
            try:
                serializer.is_valid(raise_exception=True)
            except exceptions.ValidationError as e:
                errors.append({'detail': str(e), 'source': entry})
            else:
                if self.ENABLE_BULK_PARTIAL_CREATION:
                    self.perform_create(serializer)
                    items.append(
                        serializer.to_representation(serializer.instance))
                else:
                    serializers.append(serializer)
        if not self.ENABLE_BULK_PARTIAL_CREATION and not errors:
            for serializer in serializers:
                self.perform_create(serializer)
                items.append(serializer.to_representation(serializer.instance))

        # Populate serialized data to the result.
        result = SideloadingProcessor(self.get_serializer(), items).data

        # Include errors if any.
        if errors:
            result['errors'] = errors

        code = (status.HTTP_201_CREATED
                if not errors else status.HTTP_400_BAD_REQUEST)

        return Response(result, status=code)
Пример #2
0
 def data(self):
     if getattr(self, '_processed_data', None) is None:
         data = super(WithDynamicSerializerMixin, self).data
         data = SideloadingProcessor(self,
                                     data).data if self.envelope else data
         self._processed_data = ReturnDict(data, serializer=self)
     return self._processed_data
Пример #3
0
 def data(self):
     if not hasattr(self, '_sideloaded_data'):
         data = super(WithDynamicSerializerMixin, self).data
         self._sideloaded_data = ReturnDict(SideloadingProcessor(
             self, data).data if self.sideload else data,
                                            serializer=self)
     return self._sideloaded_data
Пример #4
0
 def data(self):
     if not hasattr(self, '_processed_data'):
         data = super(WithDynamicSerializerMixin, self).data
         data = SideloadingProcessor(self,
                                     data).data if self.envelope else data
         processed_data = ReturnDict(data, serializer=self)
         self._processed_data = post_process(processed_data)
     return self._processed_data
Пример #5
0
 def data(self):
     """Get the data, after performing post-processing if necessary."""
     data = super(DynamicListSerializer, self).data
     processed_data = ReturnDict(
         SideloadingProcessor(self, data).data,
         serializer=self) if self.child.envelope else ReturnList(
             data, serializer=self)
     return processed_data
Пример #6
0
 def data(self):
     """Get the data, after performing post-processing if necessary."""
     if not hasattr(self, '_sideloaded_data'):
         data = super(DynamicListSerializer, self).data
         if self.child.sideload:
             self._sideloaded_data = ReturnDict(SideloadingProcessor(
                 self, data).data,
                                                serializer=self)
         else:
             self._sideloaded_data = ReturnList(data, serializer=self)
     return self._sideloaded_data