コード例 #1
0
    def patch(self):
        """
        Handles a PATCH request.

        http://jsonapi.org/format/#crud-updating-relationships
        """
        # Make sure the request contains a valid JSONapi relationship object.
        relationship_object = self.request.json
        validators.assert_relationship_object(relationship_object)

        # Patch the relationship.
        unserializer = self.api.get_unserializer(self.real_typename)
        yield from unserializer.update_relationship(
            self.db, self.resource, self.relname, relationship_object
        )

        # Save thte changes.
        self.db.save([self.resource])
        yield from self.db.commit()

        # Build the response
        self.response.headers["content-type"] = "application/vnd.api+json"
        self.response.status_code = 200
        self.response.body = self.build_body()
        return None
コード例 #2
0
    def post(self):
        """
        Handles a POST request.

        This method is only allowed for to-many relationships.

        http://jsonapi.org/format/#crud-updating-relationships
        """
        # This method is only allowed for *to-many* relationships.
        if not self.relationship.to_many:
            raise errors.MethodNotAllowed()

        # Get the relationship document from the request.
        relationship_object = self.request.json
        validators.assert_relationship_object(relationship_object)

        # Extend the relationship.
        unserializer = self.api.get_unserializer(self.real_typename)
        yield from unserializer.extend_relationship(
            self.db, self.resource, self.relname, relationship_object
        )

        # Save the resource.
        self.db.save([self.resource])
        yield from self.db.commit()

        # Build the response
        self.response.headers["content-type"] = "application/vnd.api+json"
        self.response.status_code = 200
        self.response.body = self.build_body()
        return None