Esempio n. 1
0
    def put(self, **kwargs):
        """ Respond to PUT requests.

        Replace the resource with the data from the request.

        Returns 200 OK when the resource is found and updated.

        Returns 404 NOT FOUND when the resource does not exist, and it has a
        `create` link specified in the schema

        Returns 400 BAD REQUEST when the request body could not be parsed.

        Return 422 Unprocessable Entity when the request can be parsed, but the
        resource is invalid according to the schema.
        """
        self.check_authorization()

        resource = self.resource(json.loads(request.data))

        if resource.rel('create'):
            upsert = False
        else:
            upsert = True

        self.authorize(resource)

        resource.save(upsert=upsert)

        return response.ResourceResponse(resource)
Esempio n. 2
0
    def patch(self, **kwargs):
        """ Respond to PATCH requests.

        Returns 200 OK when the resource is found and updated.

        Returns 404 NOT FOUND when the resource does not exist

        Returns 400 BAD REQUEST when the request body could not be parsed, or
        the request body is not a valid json patch.

        Return 422 Unprocessable Entity when the request can be parsed, but the
        resource is invalid according to the schema.
        """
        self.check_authorization()

        resource = self.resource(kwargs)
        resource.load()

        patch = Patch(json.loads(request.data))

        self.authorize(resource)
        resource.patch(patch)
        resource.save()

        return response.ResourceResponse(resource)
Esempio n. 3
0
    def get(self, **kwargs):
        """ Respond to GET requests.

        Return 200 OK when the list is returned.
        Returns 404 NOT FOUND is a page is requested that does not exist.

        Returns a list of resources. GET parameters can be used to sort and page
        and filter the response.
        """
        self.check_authorization()

        resource = self.resource(kwargs)
        resource['meta'].update(request.args.to_dict())

        self.authorize(resource)

        return response.ResourceResponse(resource.load())
Esempio n. 4
0
    def get(self, **kwargs):
        """ Respond to get requests.

        Looks up the resource in the db, and creates a response.

        Returns 200 OK when the resource is found.

        Returns 404 NOT FOUND when the resource does not exist
        """
        self.check_authorization()

        resource = self.resource(kwargs)

        resource.load()
        self.authorize(resource)

        return response.ResourceResponse(resource)
Esempio n. 5
0
    def post(self, *args):
        """ Respond to POST requests.

        Returns 201 CREATED when the resource is created.

        Returns 400 BAD REQUEST when the body cannot be parsed

        Returns 422 UNPROCESSABLE ENTITY when the body can be parsed, but the
        resulting resource is not valid according to the schema.


        A `Location` header will be sent with to location of the created user.
        """
        self.check_authorization()

        resource = self.resource(json.loads(request.data))
        self.authorize(resource)

        resource.save(create=True)

        return response.ResourceResponse(resource, status=201)