flask_rest_jsonapi_next package¶
flask_rest_jsonapi_next.data_layers.filtering.alchemy module¶
Helper to create sqlalchemy filters according to filter querystring parameter
- class flask_rest_jsonapi_next.data_layers.filtering.alchemy.Node(model, filter_, resource, schema)¶
Bases:
objectHelper to recursively create filters with sqlalchemy according to filter querystring parameter
- property column¶
Get the column object
- Parameters:
model (DeclarativeMeta) – the model
field (str) – the field
- Return InstrumentedAttribute:
the column to filter on
- property name¶
Return the name of the node or raise a BadRequest exception
- Return str:
the name of the field to filter on
- property op¶
Return the operator of the node
- Return str:
the operator to use in the filter
- property operator¶
Get the function operator from his name
- Return callable:
a callable to make operation on a column
Get the related model of a related (relationship or nested) field
- Return DeclarativeMeta:
the related model
Get the related schema of a related (relationship or nested) field
- Return Schema:
the related schema
- resolve()¶
Create filter for a particular node of the filter tree
- property value¶
Get the value to filter on
- Returns:
the value to filter on
- flask_rest_jsonapi_next.data_layers.filtering.alchemy.create_filters(model, filter_info, resource)¶
Apply filters from filters information to base query
flask_rest_jsonapi_next.data_layers.base module¶
Helper to create sqlalchemy filters according to filter querystring parameter
- class flask_rest_jsonapi_next.data_layers.filtering.alchemy.Node(model, filter_, resource, schema)¶
Bases:
objectHelper to recursively create filters with sqlalchemy according to filter querystring parameter
- property column¶
Get the column object
- Parameters:
model (DeclarativeMeta) – the model
field (str) – the field
- Return InstrumentedAttribute:
the column to filter on
- property name¶
Return the name of the node or raise a BadRequest exception
- Return str:
the name of the field to filter on
- property op¶
Return the operator of the node
- Return str:
the operator to use in the filter
- property operator¶
Get the function operator from his name
- Return callable:
a callable to make operation on a column
- property related_model¶
Get the related model of a related (relationship or nested) field
- Return DeclarativeMeta:
the related model
- property related_schema¶
Get the related schema of a related (relationship or nested) field
- Return Schema:
the related schema
- resolve()¶
Create filter for a particular node of the filter tree
- property value¶
Get the value to filter on
- Returns:
the value to filter on
- flask_rest_jsonapi_next.data_layers.filtering.alchemy.create_filters(model, filter_info, resource)¶
Apply filters from filters information to base query
flask_rest_jsonapi_next.data_layers.alchemy module¶
This module is a CRUD interface between resource managers and the sqlalchemy ORM
- exception flask_rest_jsonapi_next.data_layers.alchemy.FlaskRestJsonApiNextWarning¶
Bases:
UserWarning
- class flask_rest_jsonapi_next.data_layers.alchemy.SqlalchemyDataLayer(kwargs)¶
Bases:
BaseDataLayerSqlalchemy data layer
- after_create_object(obj, data, view_kwargs)¶
Provide additional data after object creation
- 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 attribute 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 attribute 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 attribute 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
- 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 attribute 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_nested_fields(data, obj)¶
- 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_commit(obj)¶
- before_create_object(data, view_kwargs)¶
Provide additional data before object creation
- before_create_relationship(json_data, relationship_field, related_id_field, view_kwargs)¶
Make work before to create a relationship
- 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
- 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
- before_update_object(obj, data, view_kwargs)¶
Make checks or provide additional data before update object
- before_update_relationship(json_data, relationship_field, related_id_field, view_kwargs)¶
Make work before to update a relationship
- create_object(data, view_kwargs)¶
Create an object through sqlalchemy
- create_relationship(json_data, relationship_field, related_id_field, view_kwargs)¶
Create a relationship
- 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
- eagerload_includes(query, qs)¶
Use eagerload feature of sqlalchemy to optimize data retrieval for include querystring parameter
- Parameters:
query (Query) – sqlalchemy queryset
qs (QueryStringManager) – a querystring manager to retrieve information from url
- Return Query:
the query with includes eagerloaded
- 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, filters=None, as_query=True)¶
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
filters (dict) – A dictionary of key/value filters to apply to the eventual query
as_query (bool) – If True, and if possible by concrete implementation, then return value will be tuple of count and query object instead of tuple of count and list of objects. May be more performant in some cases.
- Return tuple:
the number of object and the list of objects
- get_object(view_kwargs, qs=None)¶
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
- 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
- retrieve_object_query(view_kwargs, filter_field, filter_value)¶
Build query to retrieve object
- Parameters:
view_kwargs (dict) – kwargs from the resource view
- Params sqlalchemy_field filter_field:
the field to filter on
- Params filter_value:
the value to filter with
- Return sqlalchemy query:
a query from sqlalchemy
- rollback()¶
Rollbacks data layer to previous state if it supports that kind of behavior or does nothing.
- 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
- update_relationship(json_data, relationship_field, related_id_field, view_kwargs)¶
Update a relationship
flask_rest_jsonapi_next.api module¶
This module contains the main class of the Api to initialize the Api, plug default decorators for each resources methods, specify which blueprint to use, define the Api routes and plug additional oauth manager and permission manager
- class flask_rest_jsonapi_next.api.Api(app=None, blueprint=None, decorators=None, register_at='/')¶
Bases:
objectThe main class of the Api
- static build_scope(resource, method)¶
Compute the name of the scope for oauth
- static check_permissions(view, view_args, view_kwargs, *args, **kwargs)¶
The function use to check permissions
- has_permission(*args, **kwargs)¶
Decorator used to check permissions before to call resource manager method
- init_app(app=None, blueprint=None, additional_blueprints=None)¶
Update flask application with our api
- Parameters:
app (Application) – a flask application
- oauth_manager(oauth_manager)¶
Use the oauth manager to enable oauth for API
- Parameters:
oauth_manager – the oauth manager
- permission_manager(permission_manager, with_decorators=True)¶
Use permission manager to enable permission for API
- Parameters:
permission_manager (callable) – the permission manager
flask_rest_jsonapi_next.decorators module¶
Decorators to check headers and method requirements for each Api calls
- flask_rest_jsonapi_next.decorators.check_headers(func)¶
Check headers according to jsonapi reference
- Parameters:
func (callable) – the function to decorate
- Return callable:
the wrapped function
- flask_rest_jsonapi_next.decorators.check_method_requirements(func)¶
Check methods requirements
- Parameters:
func (callable) – the function to decorate
- Return callable:
the wrapped function
flask_rest_jsonapi_next.errors module¶
Helper to format Api errors according to jsonapi specification
- flask_rest_jsonapi_next.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_next.exceptions module¶
Collection of useful http error for the Api
- exception flask_rest_jsonapi_next.exceptions.AccessDenied(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
JsonApiExceptionThrow this error when requested resource owner doesn’t match the user of the ticket
- status = '403'¶
- title = 'Access denied'¶
- exception flask_rest_jsonapi_next.exceptions.BadRequest(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
JsonApiExceptionBadRequest error
- status = '400'¶
- title = 'Bad request'¶
- exception flask_rest_jsonapi_next.exceptions.InvalidField(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
BadRequestError to warn that a field specified in fields querystring is not in the requested resource schema
- source = {'parameter': 'fields'}¶
- title = 'Invalid fields querystring parameter.'¶
- exception flask_rest_jsonapi_next.exceptions.InvalidFilters(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
BadRequestError to warn that a specified filters in querystring parameter contains errors
- source = {'parameter': 'filter'}¶
- title = 'Invalid filters querystring parameter.'¶
- exception flask_rest_jsonapi_next.exceptions.InvalidInclude(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
BadRequestError to warn that a field specified in include querystring parameter is not a relationship of the requested resource schema
- source = {'parameter': 'include'}¶
- title = 'Invalid include querystring parameter.'¶
- exception flask_rest_jsonapi_next.exceptions.InvalidSort(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
BadRequestError to warn that a field specified in sort querystring parameter is not in the requested resource schema
- source = {'parameter': 'sort'}¶
- title = 'Invalid sort querystring parameter.'¶
- exception flask_rest_jsonapi_next.exceptions.InvalidType(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
JsonApiExceptionError to warn that there is a conflit between resource types
- status = '409'¶
- title = 'Invalid type'¶
- exception flask_rest_jsonapi_next.exceptions.JsonApiException(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
ExceptionBase exception class for unknown errors
- source = None¶
- status = '500'¶
- title = 'Unknown error'¶
- to_dict()¶
Return values of each fields of an jsonapi error
- exception flask_rest_jsonapi_next.exceptions.ObjectNotFound(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
JsonApiExceptionError to warn that an object is not found in a database
- status = '404'¶
- title = 'Object not found'¶
- exception flask_rest_jsonapi_next.exceptions.RelatedObjectNotFound(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
ObjectNotFoundError to warn that a related object is not found
- title = 'Related object not found'¶
- exception flask_rest_jsonapi_next.exceptions.RelationNotFound(detail='', source=None, title=None, status=None, code=None, id_=None, links=None, meta=None)¶
Bases:
JsonApiExceptionError to warn that a relationship is not found on a model
- title = 'Relation not found'¶
flask_rest_jsonapi_next.pagination module¶
Helper to create pagination links according to jsonapi specification
- flask_rest_jsonapi_next.pagination.add_pagination_links(data, object_count, querystring, base_url)¶
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_next.querystring module¶
Helper to deal with querystring parameters according to jsonapi specification
- class flask_rest_jsonapi_next.querystring.QueryStringManager(querystring, schema, allow_disable_pagination=None, max_page_size=None)¶
Bases:
objectQuerystring parser according to jsonapi reference
- MANAGED_KEYS = ('filter', 'page', 'fields', 'sort', 'include', 'q')¶
- property 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'], }
- property filters¶
Return filters from query string.
- Return list:
filter information
- property include¶
Return fields to include
- Return list:
a list of include information
- property 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'}
- property querystring¶
Return original querystring but containing only managed keys
- Return dict:
dict of managed querystring parameter
- property sorting¶
Return fields to sort by, resolving nested relationships properly.
- Return list:
a list of sorting information
- Example return value::
- [
{‘field’: ‘created_at’, ‘order’: ‘desc’},
]
flask_rest_jsonapi_next.resource module¶
This module contains the logic of resource management
- class flask_rest_jsonapi_next.resource.Resource¶
Bases:
MethodViewBase resource class
- dispatch_request(*args, **kwargs)¶
Logic of how to handle a request
- class flask_rest_jsonapi_next.resource.ResourceDetail¶
Bases:
ResourceBase class of a resource detail manager
- after_delete(result)¶
Hook to make custom work after delete method
- after_get(result)¶
Hook to make custom work after get method
- after_patch(result)¶
Hook to make custom work after patch method
- before_delete(args, kwargs)¶
Hook to make custom work before delete method
- before_get(args, kwargs)¶
Hook to make custom work before get method
- before_marshmallow(args, kwargs)¶
- before_patch(args, kwargs, data=None)¶
Hook to make custom work before patch method
- decorators: ClassVar[list[Callable[[...], Any]]] = (<function check_headers>,)¶
A list of decorators to apply, in order, to the generated view function. Remember that
@decoratorsyntax is applied bottom to top, so the first decorator in the list would be the bottom decorator.Added in version 0.8.
- delete(*args, **kwargs)¶
Delete an object
- delete_object(kwargs)¶
- get(*args, **kwargs)¶
Get object details
- get_object(kwargs, qs)¶
- methods: ClassVar[Collection[str] | None] = {'DELETE', 'GET', 'PATCH'}¶
The methods this view is registered for. Uses the same default (
["GET", "HEAD", "OPTIONS"]) asrouteandadd_url_ruleby default.
- patch(*args, **kwargs)¶
Update an object
- update_object(data, qs, kwargs)¶
- class flask_rest_jsonapi_next.resource.ResourceList¶
Bases:
ResourceBase class of a resource list manager
- after_get(result)¶
Hook to make custom work after get method
- after_post(result)¶
Hook to make custom work after post method
- before_get(args, kwargs)¶
Hook to make custom work before get method
- before_marshmallow(args, kwargs)¶
- before_post(args, kwargs, data=None)¶
Hook to make custom work before post method
- create_object(data, kwargs)¶
- decorators: ClassVar[list[Callable[[...], Any]]] = (<function check_headers>,)¶
A list of decorators to apply, in order, to the generated view function. Remember that
@decoratorsyntax is applied bottom to top, so the first decorator in the list would be the bottom decorator.Added in version 0.8.
- get(*args, **kwargs)¶
Retrieve a collection of objects
- get_collection(qs, kwargs, filters=None, as_query=True)¶
Implements override for ResourceList that allows lists as values for simple filters, ie. following query strings are supported: ?filter[foobar_id]=1,2,3
- methods: ClassVar[Collection[str] | None] = {'GET', 'POST'}¶
The methods this view is registered for. Uses the same default (
["GET", "HEAD", "OPTIONS"]) asrouteandadd_url_ruleby default.
- post(*args, **kwargs)¶
Create an object
- class flask_rest_jsonapi_next.resource.ResourceRelationship¶
Bases:
ResourceBase class of a resource relationship manager
- after_delete(result, status_code)¶
Hook to make custom work after delete method
- after_get(result)¶
Hook to make custom work after get method
- after_patch(result, status_code)¶
Hook to make custom work after patch method
- after_post(result, status_code)¶
Hook to make custom work after post method
- before_delete(args, kwargs, json_data=None)¶
Hook to make custom work before delete method
- before_get(args, kwargs)¶
Hook to make custom work before get method
- before_patch(args, kwargs, json_data=None)¶
Hook to make custom work before patch method
- before_post(args, kwargs, json_data=None)¶
Hook to make custom work before post method
- decorators: ClassVar[list[Callable[[...], Any]]] = (<function check_headers>,)¶
A list of decorators to apply, in order, to the generated view function. Remember that
@decoratorsyntax is applied bottom to top, so the first decorator in the list would be the bottom decorator.Added in version 0.8.
- delete(*args, **kwargs)¶
Delete relationship(s)
- get(*args, **kwargs)¶
Get a relationship details
- methods: ClassVar[Collection[str] | None] = {'DELETE', 'GET', 'PATCH', 'POST'}¶
The methods this view is registered for. Uses the same default (
["GET", "HEAD", "OPTIONS"]) asrouteandadd_url_ruleby default.
- patch(*args, **kwargs)¶
Update a relationship
- post(*args, **kwargs)¶
Add / create relationship(s)
flask_rest_jsonapi_next.schema module¶
Helpers to deal with marshmallow schemas
- flask_rest_jsonapi_next.schema.compute_schema(schema_cls, default_kwargs, qs, include)¶
Compute a schema around compound documents and sparse fieldsets
- Parameters:
schema_cls (Schema) – the schema class
default_kwargs (dict) – the schema default kwargs
qs (QueryStringManager) – qs
include (list) – the relation field to include data from
- Return Schema schema:
the schema computed
- flask_rest_jsonapi_next.schema.get_model_field(schema, field)¶
Get the model field of a schema field
- Parameters:
schema (Schema) – a marshmallow schema
field (str) – the name of the schema field
- Return str:
the name of the field in the model
- flask_rest_jsonapi_next.schema.get_nested_fields(schema, model_field=False)¶
Return nested fields of a schema to support a join
- Parameters:
schema (Schema) – a marshmallow schema
model_field (boolean) – whether to extract the model field for the nested fields
- Return list:
list of nested fields of the schema
Retrieve the related schema of a relationship field
- Parameters:
schema (Schema) – the schema to retrieve le relationship field from
field – the relationship field
- Return Schema:
the related schema
- flask_rest_jsonapi_next.schema.get_relationships(schema, model_field=False)¶
Return relationship fields of a schema
- Parameters:
schema (Schema) – a marshmallow schema
list – list of relationship fields of a schema