def update(self, request, filters, format):
        """
        Replaces a collection of resources with another collection.

        Calls ``delete_list`` to clear out the collection then ``obj_create``
        with the provided the data to create the new collection.

        Return ``HttpAccepted`` (204 No Content).
        """
        deserialized = self.deserialize(
            request, request.raw_post_data, format=request.META.get("CONTENT_TYPE", "application/json")
        )

        if not "objects" in deserialized:
            raise {"error": "Invalid data sent."}, 400

        self.obj_delete_list(request, filters)
        bundles_seen = []

        for object_data in deserialized["objects"]:
            bundle = self.build_bundle(data=utils.dict_strip_unicode_keys(object_data))

            # Attempt to be transactional, deleting any previously created
            # objects if validation fails.
            try:
                self.is_valid(bundle, request)
            except ImmediateHttpResponse:
                self.rollback(bundles_seen)
                raise

            self.obj_create(bundle, request)
            bundles_seen.append(bundle)

        return HttpAccepted()
Esempio n. 2
0
    def update(self, request, filters, format):
        """
        Either updates an existing resource or creates a new one with the
        provided data.
        
        Calls ``obj_update`` with the provided data first, but falls back to
        ``obj_create`` if the object does not already exist.
        
        If a new resource is created, return ``HttpCreated`` (201 Created).
        If an existing resource is modified, return ``HttpAccepted`` (204 No Content).
        """
        deserialized = self.deserialize(request,
                                        request.raw_post_data,
                                        format=format)
        bundle = self.build_bundle(
            data=utils.dict_strip_unicode_keys(deserialized))
        self.is_valid(bundle, request)

        try:
            updated_bundle = self.obj_update(bundle,
                                             request,
                                             pk=filters.get('pk'))
            return HttpAccepted()
        except:
            updated_bundle = self.obj_create(bundle,
                                             request,
                                             pk=filters.get('pk'))
            return HttpCreated(location=self.get_resource_uri(updated_bundle))
Esempio n. 3
0
 def create(self, request, filters, format):
     """
     Creates a new resource/object with the provided data.
     
     Calls ``obj_create`` with the provided data and returns a response
     with the new resource's location.
     
     If a new resource is created, return ``HttpCreated`` (201 Created).
     """
     deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
     bundle = self.build_bundle(data=utils.dict_strip_unicode_keys(deserialized))
     self.is_valid(bundle, request)
     updated_bundle = self.obj_create(bundle, request)
     return HttpCreated(location=self.get_resource_uri(updated_bundle, format))
Esempio n. 4
0
 def create(self, request, filters, format):
     """
     Creates a new resource/object with the provided data.
     
     Calls ``obj_create`` with the provided data and returns a response
     with the new resource's location.
     
     If a new resource is created, return ``HttpCreated`` (201 Created).
     """
     deserialized = self.deserialize(request,
                                     request.raw_post_data,
                                     format=request.META.get(
                                         'CONTENT_TYPE',
                                         'application/json'))
     bundle = self.build_bundle(
         data=utils.dict_strip_unicode_keys(deserialized))
     self.is_valid(bundle, request)
     updated_bundle = self.obj_create(bundle, request)
     return HttpCreated(
         location=self.get_resource_uri(updated_bundle, format))
    def update(self, request, filters, format):
        """
        Either updates an existing resource or creates a new one with the
        provided data.

        Calls ``obj_update`` with the provided data first, but falls back to
        ``obj_create`` if the object does not already exist.

        If a new resource is created, return ``HttpCreated`` (201 Created).
        If an existing resource is modified, return ``HttpAccepted`` (204 No Content).
        """
        deserialized = self.deserialize(request, request.raw_post_data, format=format)
        bundle = self.build_bundle(data=utils.dict_strip_unicode_keys(deserialized))
        self.is_valid(bundle, request)

        try:
            updated_bundle = self.obj_update(bundle, request, pk=filters.get("pk"))
            return HttpAccepted()
        except:
            updated_bundle = self.obj_create(bundle, request, pk=filters.get("pk"))
            return HttpCreated(location=self.get_resource_uri(updated_bundle))
Esempio n. 6
0
    def update(self, request, filters, format):
        """
        Replaces a collection of resources with another collection.
        
        Calls ``delete_list`` to clear out the collection then ``obj_create``
        with the provided the data to create the new collection.
        
        Return ``HttpAccepted`` (204 No Content).
        """
        deserialized = self.deserialize(request,
                                        request.raw_post_data,
                                        format=request.META.get(
                                            'CONTENT_TYPE',
                                            'application/json'))

        if not 'objects' in deserialized:
            raise {"error": "Invalid data sent."}, 400

        self.obj_delete_list(request, filters)
        bundles_seen = []

        for object_data in deserialized['objects']:
            bundle = self.build_bundle(
                data=utils.dict_strip_unicode_keys(object_data))

            # Attempt to be transactional, deleting any previously created
            # objects if validation fails.
            try:
                self.is_valid(bundle, request)
            except ImmediateHttpResponse:
                self.rollback(bundles_seen)
                raise

            self.obj_create(bundle, request)
            bundles_seen.append(bundle)

        return HttpAccepted()