Resources

Resource

Resource is the base class for all other resource types and by contains only one route, which returns the schema for the resource.

class flask_potion.Resource

A plain resource with nothing but a schema.

A resource is configured using the Schema and Meta attributes as well as any properties that are of type routes.Route or routes.RouteSet.

Meta class attributes:

Attribute name Default Description
name Name of the resource; defaults to the lower-case of the model’s class name
title None JSON-schema title declaration
description None JSON-schema description declaration
exclude_routes () A list of strings; any routes — including inherited routes — whose Route.relation match one of these string is omitted from the resource.
route_decorators {} A dictionary of decorators to apply to routes in the resource. The keys must match the Route.relation attribute.
exclude_fields () A list of fields that should not be imported from the model.
required_fields () Fields that are automatically imported from the model are automatically required if their columns are not nullable and do not have a default.
read_only_fields () A list of fields that are returned by the resource but are ignored in POST and PATCH requests. Useful for e.g. timestamps.
write_only_fields () A list of fields that can be written to but are not returned. For secret stuff.

Usage example:

class LogResource(Resource):
    class Schema:
        level = fields.String(enum=['info', 'warning', 'error'])
        message = fields.String()

    class Meta:
        name = 'log'

    @Route.POST('',
                rel="create",
                schema=fields.Inline('self'),
                response_schema=fields.Inline('self'))
    def create(self, properties):
        print('{level}: {message}'.format(**properties))
        return properties
api

Back reference to the Api this resource is registered on.

meta

A AttributeDict of configuration attributes collected from the Meta attributes of the base classes.

routes

A dictionary of routes registered with this resource. Keyed by Route.relation.

schema

A FieldSet containing fields collected from the Schema attributes of the base classes.

route_prefix

The prefix URI to any route in this resource; includes the API prefix.

described_by()

A Route at /schema that contains the JSON Hyper-Schema for this resource.

ModelResource

ModelResource is written for create, read, update, delete actions on collections of items matching the resource schema.

A data store connection is maintained by a manager.Manager instance. The manager class can be specified in Meta.manager; if no manager is specified, Api.default_manager is used. Managers are configured through attributes in Meta. Most managers expect a model to be defined under Meta.model.

class flask_potion.ModelResource
create()

A link — part of a Route at the root of the resource — for creating new items.

Parameters:properties
Returns:created item
instances()

A link — part of a Route at the root of the resource — for reading item instances.

Parameters:
  • where
  • sort
  • page (int) –
  • per_page (int) –
Returns:

list of items

read()

A link — part of a Route at /<{Resource.meta.id_converter}:id> — for reading a specific item.

Parameters:id – item id
Returns:item
update()

A link — part of a Route at /<{Resource.meta.id_converter}:id> — for updating a specific item.

Parameters:
  • id – item id
  • properties – changes
Returns:

item

destroy()

A link — part of a Route at /<{Resource.meta.id_converter}:id> — for deleting a specific item.

Parameters:id – item id
Returns:(None, 204)