pyramid_jsonapi package

Module contents

Tools for constructing a JSON-API from sqlalchemy models in Pyramid.

class pyramid_jsonapi.DebugView(request)[source]

Bases: object

Pyramid view class defining a debug API.

These are available as /debug/{action} if pyramid_jsonapi.debug_endpoints == 'true'.

engine

sqlalchemy engine with connection to the db.

metadata

sqlalchemy model metadata

test_data

module with an add_to_db() method which will populate the database

drop()[source]

Drop all tables from the database!!!

populate()[source]

Create tables and populate with test data.

reset()[source]

The same as ‘drop’ and then ‘populate’.

class pyramid_jsonapi.PyramidJSONAPI(config, models, get_dbsession=None)[source]

Bases: object

Class encapsulating an API.

Parameters
  • config (pyramid.config.Configurator) – pyramid config object from app.

  • models (module or iterable) – a models module or iterable of models.

Keyword Arguments

get_dbsession (callable) – function accepting an instance of CollectionViewBase and returning a sqlalchemy database session.

collection_view_factory(model, collection_name=None, expose_fields=None)[source]

Build a class to handle requests for model.

Parameters

model – a model class derived from DeclarativeMeta.

Keyword Arguments
  • collection_name – string name of collection.

  • expose_fields – set of field names to expose.

config_defaults = {'allow_client_ids': {'desc': 'Allow client to specify resource ids.', 'val': False}, 'api_version': {'desc': 'API version for prefixing endpoints and metadata generation.', 'val': ''}, 'debug_endpoints': {'desc': 'Whether or not to add debugging endpoints.', 'val': False}, 'debug_meta': {'desc': 'Whether or not to add debug information to the meta key in returned JSON.', 'val': False}, 'debug_test_data_module': {'desc': 'Module responsible for populating test data.', 'val': 'test_data'}, 'debug_traceback': {'desc': 'Whether or not to add a stack traceback to errors.', 'val': False}, 'expose_foreign_keys': {'desc': 'Expose foreign key fields in JSON.', 'val': False}, 'inform_of_get_authz_failures': {'desc': "True = return information in meta about authz failures; False = pretend items don't exist", 'val': True}, 'metadata_endpoints': {'desc': 'Should /metadata endpoint be enabled?', 'val': True}, 'metadata_modules': {'desc': 'Modules to load to provide metadata endpoints (defaults are modules provided in the metadata package).', 'val': 'JSONSchema OpenAPI'}, 'openapi_file': {'desc': 'File containing OpenAPI data (YAML or JSON)', 'val': ''}, 'paging_default_limit': {'desc': 'Default pagination limit for collections.', 'val': 10}, 'paging_max_limit': {'desc': 'Default limit on the number of items returned for collections.', 'val': 100}, 'route_name_prefix': {'desc': 'Prefix for pyramid route names for view_classes.', 'val': 'pyramid_jsonapi'}, 'route_name_sep': {'desc': 'Separator for pyramid route names.', 'val': ':'}, 'route_pattern_api_prefix': {'desc': 'Prefix for api endpoints (if metadata_endpoints is enabled).', 'val': 'api'}, 'route_pattern_metadata_prefix': {'desc': 'Prefix for metadata endpoints (if metadata_endpoints is enabled).', 'val': 'metadata'}, 'route_pattern_prefix': {'desc': '"Parent" prefix for all endpoints', 'val': ''}, 'route_pattern_sep': {'desc': 'Separator for pyramid route patterns.', 'val': '/'}, 'schema_file': {'desc': 'File containing jsonschema JSON for validation.', 'val': ''}, 'schema_validation': {'desc': 'jsonschema schema validation enabled?', 'val': True}, 'workflow_collection_get': {'desc': 'Module implementing the collection_get workflow.', 'val': 'pyramid_jsonapi.workflow.loop.collection_get'}, 'workflow_collection_post': {'desc': 'Module implementing the collection_post workflow.', 'val': 'pyramid_jsonapi.workflow.loop.collection_post'}, 'workflow_item_delete': {'desc': 'Module implementing the item_delete workflow.', 'val': 'pyramid_jsonapi.workflow.loop.item_delete'}, 'workflow_item_get': {'desc': 'Module implementing the item_get workflow.', 'val': 'pyramid_jsonapi.workflow.loop.item_get'}, 'workflow_item_patch': {'desc': 'Module implementing the item_patch workflow.', 'val': 'pyramid_jsonapi.workflow.loop.item_patch'}, 'workflow_related_get': {'desc': 'Module implementing the related_get workflow.', 'val': 'pyramid_jsonapi.workflow.loop.related_get'}, 'workflow_relationships_delete': {'desc': 'Module implementing the relationships_delete workflow.', 'val': 'pyramid_jsonapi.workflow.loop.relationships_delete'}, 'workflow_relationships_get': {'desc': 'Module implementing the relationships_get workflow.', 'val': 'pyramid_jsonapi.workflow.loop.relationships_get'}, 'workflow_relationships_patch': {'desc': 'Module implementing the relationships_patch workflow.', 'val': 'pyramid_jsonapi.workflow.loop.relationships_patch'}, 'workflow_relationships_post': {'desc': 'Module implementing the relationships_post workflow.', 'val': 'pyramid_jsonapi.workflow.loop.relationships_post'}}
create_jsonapi(engine=None, test_data=None, api_version='')[source]

Auto-create jsonapi from module or iterable of sqlAlchemy models.

Keyword Arguments
  • engine – a sqlalchemy.engine.Engine instance. Only required if using the debug view.

  • test_data – a module with an add_to_db() method which will populate the database.

  • api_version – An optional version to be used in generating urls, docs etc. defaults to ‘’. Can also be set globally in settings ini file.

create_jsonapi_using_magic_and_pixie_dust(engine=None, test_data=None, api_version='')

Auto-create jsonapi from module or iterable of sqlAlchemy models.

Keyword Arguments
  • engine – a sqlalchemy.engine.Engine instance. Only required if using the debug view.

  • test_data – a module with an add_to_db() method which will populate the database.

  • api_version – An optional version to be used in generating urls, docs etc. defaults to ‘’. Can also be set globally in settings ini file.

create_resource(model, collection_name=None, expose_fields=None)[source]

Produce a set of resource endpoints.

Parameters

model – a model class derived from DeclarativeMeta.

Keyword Arguments
  • collection_name – string name of collection. Passed through to collection_view_factory()

  • expose_fields – set of field names to be exposed. Passed through to collection_view_factory()

enable_permission_handlers(stage_names)[source]

Add permission handlers to all views.

Permission handlers are not added to views by default for performance reasons. Call this function to add permission handlers to all views for the stage names specified.

Parameters

stage_names – an iterable of stage names to enable.

static error(exc, request)[source]

Error method to return jsonapi compliant errors.

view_classes = {}
class pyramid_jsonapi.StdRelationship(name, obj, view_class)[source]

Bases: object

Standardise access to relationship information.

obj

the actual object representing the relationship.

property mirror_relationship
property proxy_direction
property proxy_mirror_relationship
property proxy_tgt_class
property rel_direction
property rel_mirror_relationship
property rel_tgt_class
property to_many
property to_one
pyramid_jsonapi.get_class_by_tablename(tablename, registry)[source]

Return class reference mapped to table.

Parameters
  • tablename – String with name of table.

  • registry – metadata registry

return: Class reference or None.