Esempio n. 1
0
        def fn_patch(_id, request_doc=None):
            """Handles PATCH requests to the given resource."""

            if request.method == "OPTIONS":
                return

            request_doc = request_doc or json.loads(request.body.getvalue())
            JsonAPIValidator.validate_patch(request_doc, _id,
                                            self.model._meta.name)

            self.listener.before_patch(request_doc)

            entry = self.model.select().where(
                self.model._meta.primary_key == _id).get()

            if "attributes" in request_doc["data"]:
                # each attribute that is present will be updated
                for key, val in request_doc["data"]["attributes"].iteritems():
                    setattr(entry, key, val)

                entry.save()

            if "relationships" in request_doc["data"]:
                # patch given relationships
                self.__patch_relationships(
                    _id, request_doc["data"]["relationships"])

            if self.listener.after_patch(response):
                # if the listener changed something else then return the object
                return self.__get(_id)

            else:
                # nothing changed, we return a 204 No Content status
                response.status = 204
Esempio n. 2
0
        def fn_patch_relationship(_id):
            """Patches a relationship with the given ID and returns."""

            if request.method == "OPTIONS":
                return

            request_doc = json.loads(request.body.getvalue())
            # JsonAPIValidator.validate_patch(request_doc, _id, None)

            # PATCH /res/<_id>/relationships/other_res is equal to patching the
            # main resource which is what we are doing now
            rewritten_request = {
                u"data": {
                    u"id": unicode(_id),
                    u"type": unicode(self.model._meta.name),
                    u"relationships": {
                        unicode(relationship): {
                            u"data": request_doc["data"]
                        }
                    }
                }
            }

            JsonAPIValidator.validate_patch(rewritten_request, _id,
                                            self.model._meta.name)

            return self.patch()(_id, request_doc=rewritten_request)
Esempio n. 3
0
        def fn_patch_relationship(_id):
            """Patches a relationship with the given ID and returns."""

            if request.method == "OPTIONS":
                return

            request_doc = json.loads(request.body.getvalue())
            # JsonAPIValidator.validate_patch(request_doc, _id, None)

            # PATCH /res/<_id>/relationships/other_res is equal to patching the
            # main resource which is what we are doing now
            rewritten_request = {
                u"data": {
                    u"id": unicode(_id),
                    u"type": unicode(self.model._meta.name),
                    u"relationships": {
                        unicode(relationship): {
                            u"data": request_doc["data"]
                        }
                    }
                }
            }

            JsonAPIValidator.validate_patch(
                rewritten_request, _id, self.model._meta.name
            )

            return self.patch()(_id, request_doc=rewritten_request)
Esempio n. 4
0
        def fn_patch(_id, request_doc=None):
            """Handles PATCH requests to the given resource."""

            if request.method == "OPTIONS":
                return

            request_doc = request_doc or json.loads(request.body.getvalue())
            JsonAPIValidator.validate_patch(
                request_doc,
                _id,
                self.model._meta.name
            )

            self.listener.before_patch(request_doc)

            entry = self.model.select().where(
                self.model._meta.primary_key == _id
            ).get()

            if "attributes" in request_doc["data"]:
                # each attribute that is present will be updated
                for key, val in request_doc["data"]["attributes"].iteritems():
                    setattr(entry, key, val)

                entry.save()

            if "relationships" in request_doc["data"]:
                # patch given relationships
                self.__patch_relationships(
                    _id,
                    request_doc["data"]["relationships"]
                )

            if self.listener.after_patch(response):
                # if the listener changed something else then return the object
                return self.__get(_id)

            else:
                # nothing changed, we return a 204 No Content status
                response.status = 204