flask_rest_jsonapi package

flask_rest_jsonapi.constants module

flask_rest_jsonapi.decorators module

flask_rest_jsonapi.decorators.check_headers(f)

Check headers according to jsonapi reference

Parameters:f (callable) – the function to decorate
Return callable:
 the wrapped function
flask_rest_jsonapi.decorators.check_method_requirements(f)

Check methods requirements

Parameters:f (callable) – the function to decorate
Return callable:
 the wrapped function

flask_rest_jsonapi.errors module

flask_rest_jsonapi.errors.jsonapi_errors(jsonapi_errors)

Construct api error according to jsonapi 1.0

Parameters:jsonapi_errors (iterable) – an iterable of jsonapi error
Return dict:a dict of errors according to jsonapi 1.0

flask_rest_jsonapi.exceptions module

exception flask_rest_jsonapi.exceptions.BadRequest(source, detail, title=None, status=None)

Bases: flask_rest_jsonapi.exceptions.JsonApiException

status = 400
title = 'Bad request'
exception flask_rest_jsonapi.exceptions.InvalidField(detail)

Bases: flask_rest_jsonapi.exceptions.BadRequest

title = 'Invalid fields querystring parameter.'
exception flask_rest_jsonapi.exceptions.InvalidFilters(detail)

Bases: flask_rest_jsonapi.exceptions.BadRequest

title = 'Invalid filters querystring parameter.'
exception flask_rest_jsonapi.exceptions.InvalidInclude(detail)

Bases: flask_rest_jsonapi.exceptions.BadRequest

title = 'Invalid include querystring parameter.'
exception flask_rest_jsonapi.exceptions.InvalidSort(detail)

Bases: flask_rest_jsonapi.exceptions.BadRequest

title = 'Invalid sort querystring parameter.'
exception flask_rest_jsonapi.exceptions.InvalidType(source, detail, title=None, status=None)

Bases: flask_rest_jsonapi.exceptions.JsonApiException

status = 409
title = 'Invalid type'
exception flask_rest_jsonapi.exceptions.JsonApiException(source, detail, title=None, status=None)

Bases: Exception

status = 500
title = 'Unknow error'
to_dict()
exception flask_rest_jsonapi.exceptions.ObjectNotFound(source, detail, title=None, status=None)

Bases: flask_rest_jsonapi.exceptions.JsonApiException

status = 404
title = 'Object not found'
exception flask_rest_jsonapi.exceptions.RelatedObjectNotFound(source, detail, title=None, status=None)

Bases: flask_rest_jsonapi.exceptions.ObjectNotFound

title = 'Related object not found'
exception flask_rest_jsonapi.exceptions.RelationNotFound(source, detail, title=None, status=None)

Bases: flask_rest_jsonapi.exceptions.JsonApiException

title = 'Relation not found'

flask_rest_jsonapi.pagination module

Add pagination links to result

Parameters:
  • data (dict) – the result of the view
  • object_count (int) – number of objects in result
  • querystring (QueryStringManager) – the managed querystring fields and values
  • base_url (str) – the base url for pagination

flask_rest_jsonapi.querystring module

class flask_rest_jsonapi.querystring.QueryStringManager(querystring, schema)

Bases: object

Querystring parser according to jsonapi reference

MANAGED_KEYS = ('filter', 'page', 'fields', 'sort', 'include')
fields

Return fields wanted by client.

Return dict:a dict of sparse fieldsets information

Return value will be a dict containing all fields by resource, for example:

{
    "user": ['name', 'email'],
}
filters

Return filters from query string.

Return list:filter information
include

Return fields to include

Return list:a list of include information
pagination

Return all page parameters as a dict.

Return dict:a dict of pagination information

To allow multiples strategies, all parameters starting with page will be included. e.g:

{
    "number": '25',
    "size": '150',
}

Example with number strategy:

>>> query_string = {'page[number]': '25', 'page[size]': '10'}
>>> parsed_query.pagination
{'number': '25', 'size': '10'}
querystring

Return original querystring but containing only managed keys

Return dict:dict of managed querystring parameter
sorting

Return fields to sort by including sort name for SQLAlchemy and row sort parameter for other ORMs

Return list:a list of sorting information

Example of return value:

[
    {'field': 'created_at', 'order': 'desc'},
]

flask_rest_jsonapi.resource module

class flask_rest_jsonapi.resource.Resource

Bases: flask.views.MethodView

dispatch_request(*args, **kwargs)
class flask_rest_jsonapi.resource.ResourceDetail

Bases: flask_rest_jsonapi.resource.Resource

after_delete(result)
after_get(result)
after_patch(result)
before_delete(*args, **kwargs)
before_get(*args, **kwargs)
before_patch(*args, **kwargs)
delete(*args, **kwargs)
get(*args, **kwargs)
methods = ['DELETE', 'GET', 'PATCH']
patch(*args, **kwargs)
class flask_rest_jsonapi.resource.ResourceList

Bases: flask_rest_jsonapi.resource.Resource

after_get(result)
after_post(result)
before_get(*args, **kwargs)
before_post(*args, **kwargs)
get(*args, **kwargs)
methods = ['GET', 'POST']
post(*args, **kwargs)
class flask_rest_jsonapi.resource.ResourceRelationship

Bases: flask_rest_jsonapi.resource.Resource

after_delete(result)
after_get(result)
after_patch(result)
after_post(result)
before_delete(*args, **kwargs)
before_get(*args, **kwargs)
before_patch(*args, **kwargs)
before_post(*args, **kwargs)
delete(*args, **kwargs)
get(*args, **kwargs)
methods = ['DELETE', 'GET', 'PATCH', 'POST']
patch(*args, **kwargs)
post(*args, **kwargs)

flask_rest_jsonapi.data_layers.alchemy module

class flask_rest_jsonapi.data_layers.alchemy.SqlalchemyDataLayer(**kwargs)

Bases: flask_rest_jsonapi.data_layers.base.BaseDataLayer

after_create_object(obj, data, **view_kwargs)

Provide additional data after object creation

Parameters:
  • obj – an object from data layer
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
after_create_relationship(obj, updated, json_data, relationship_field, related_id_field, **view_kwargs)

Make work after to create a relationship

Parameters:
  • obj – an object from data layer
  • updated (bool) – True if object was updated else False
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

after_delete_object(obj, **view_kwargs)

Make work after delete object

Parameters:
  • obj – an object from data layer
  • view_kwargs (dict) – kwargs from the resource view
after_delete_relationship(obj, updated, json_data, relationship_field, related_id_field, **view_kwargs)

Make work after to delete a relationship

Parameters:
  • obj – an object from data layer
  • updated (bool) – True if object was updated else False
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
after_get_collection(collection, qs, **view_kwargs)

Make work after to retrieve a collection of objects

Parameters:
  • collection (iterable) – the collection of objects
  • qs (QueryStringManager) – a querystring manager to retrieve information from url
  • view_kwargs (dict) – kwargs from the resource view
after_get_object(obj, **view_kwargs)

Make work after to retrieve an object

Parameters:
  • obj – an object from data layer
  • view_kwargs (dict) – kwargs from the resource view
after_get_relationship(obj, related_objects, relationship_field, related_type_, related_id_field, **view_kwargs)

Make work after to get information about a relationship

Parameters:
  • obj – an object from data layer
  • related_objects (iterable) – related objects of the object
  • relationship_field (str) – the model attribut used for relationship
  • related_type (str) – the related resource type
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return tuple:

the object and related object(s)

after_update_object(obj, data, **view_kwargs)

Make work after update object

Parameters:
  • obj – an object from data layer
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
after_update_relationship(obj, updated, json_data, relationship_field, related_id_field, **view_kwargs)

Make work after to update a relationship

Parameters:
  • obj – an object from data layer
  • updated (bool) – True if object was updated else False
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

apply_relationships(data, obj)

Apply relationship provided by data to obj

Parameters:
  • data (dict) – data provided by the client
  • obj (DeclarativeMeta) – the sqlalchemy object to plug relationships to
Return boolean:

True if relationship have changed else False

before_create_object(data, **view_kwargs)

Provide additional data before object creation

Parameters:
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
before_create_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Make work before to create a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

before_delete_object(obj, **view_kwargs)

Make checks before delete object

Parameters:
  • obj – an object from data layer
  • view_kwargs (dict) – kwargs from the resource view
before_delete_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Make work before to delete a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
before_get_collection(qs, **view_kwargs)

Make work before to retrieve a collection of objects

Parameters:
  • qs (QueryStringManager) – a querystring manager to retrieve information from url
  • view_kwargs (dict) – kwargs from the resource view
before_get_object(**view_kwargs)

Make work before to retrieve an object

Parameters:view_kwargs (dict) – kwargs from the resource view
before_get_relationship(relationship_field, related_type_, related_id_field, **view_kwargs)

Make work before to get information about a relationship

Parameters:
  • relationship_field (str) – the model attribut used for relationship
  • related_type (str) – the related resource type
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return tuple:

the object and related object(s)

before_update_object(obj, data, **view_kwargs)

Make checks or provide additional data before update object

Parameters:
  • obj – an object from data layer
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
before_update_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Make work before to update a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

create_object(data, **view_kwargs)

Create an object through sqlalchemy

Parameters:
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
Return DeclarativeMeta:
 

an object from sqlalchemy

create_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Create a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

delete_object(obj, **view_kwargs)

Delete an object through sqlalchemy

Parameters:
  • item (DeclarativeMeta) – an item from sqlalchemy
  • view_kwargs (dict) – kwargs from the resource view
delete_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Delete a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
filter_query(query, filter_info, model)

Filter query according to jsonapi 1.0

Parameters:
  • query (Query) – sqlalchemy query to sort
  • filter_info (dict or None) – filter information
  • model (DeclarativeMeta) – an sqlalchemy model
Return Query:

the sorted query

get_collection(qs, **view_kwargs)

Retrieve a collection of objects through sqlalchemy

Parameters:
  • qs (QueryStringManager) – a querystring manager to retrieve information from url
  • view_kwargs (dict) – kwargs from the resource view
Return tuple:

the number of object and the list of objects

get_object(**view_kwargs)

Retrieve an object through sqlalchemy

Params dict view_kwargs:
 kwargs from the resource view
Return DeclarativeMeta:
 an object from sqlalchemy

Get a related object

Parameters:
  • related_model (Model) – an sqlalchemy model
  • related_id_field (str) – the identifier field of the related model
  • obj (DeclarativeMeta) – the sqlalchemy object to retrieve related objects from
Return DeclarativeMeta:
 

a related object

get_relationship(relationship_field, related_type_, related_id_field, **view_kwargs)

Get a relationship

Parameters:
  • relationship_field (str) – the model attribut used for relationship
  • related_type (str) – the related resource type
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return tuple:

the object and related object(s)

paginate_query(query, paginate_info)

Paginate query according to jsonapi 1.0

Parameters:
  • query (Query) – sqlalchemy queryset
  • paginate_info (dict) – pagination information
Return Query:

the paginated query

query(**view_kwargs)

Construct the base query to retrieve wanted data

Parameters:view_kwargs (dict) – kwargs from the resource view
sort_query(query, sort_info)

Sort query according to jsonapi 1.0

Parameters:
  • query (Query) – sqlalchemy query to sort
  • sort_info (list) – sort information
Return Query:

the sorted query

update_object(obj, data, **view_kwargs)

Update an object through sqlalchemy

Parameters:
  • obj (DeclarativeMeta) – an object from sqlalchemy
  • data (dict) – the data validated by marshmallow
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if object have changed else False

update_relationship(json_data, relationship_field, related_id_field, **view_kwargs)

Update a relationship

Parameters:
  • json_data (dict) – the request params
  • relationship_field (str) – the model attribut used for relationship
  • related_id_field (str) – the identifier field of the related model
  • view_kwargs (dict) – kwargs from the resource view
Return boolean:

True if relationship have changed else False

flask_rest_jsonapi.data_layers.mongo module