apicrud.BasicCRUD

class apicrud.BasicCRUD(resource=None, model=None)

Controller base class

Create/Read/Update/Delete/find controller operations.

This class provides permission-based, paginated access to database models behind your application’s endpoints. Most endpoints need no boilerplate code, and can inherit these functions directly. Some endpoints only need a few lines of code before or after inheriting these functions. You can always write your own custom function for special-case endpoints.

Parameters:
  • resource (str) – a resource name (endpoint prefix)

  • model (obj) – the model corresponding to the resource

__init__(resource=None, model=None)

Methods

__init__([resource, model])

create(body[, id_prefix, limit_related])

Controller for POST endpoints.

db_get(id)

Activate a SQLalchemy query object for the specified ID in the current model

delete(ids[, force])

Controller for DELETE endpoints.

find(**kwargs)

Find records which match query parameters passed from connexion by name, in a dictionary that also includes user and token info

get(id)

Controller for GET endpoints.

update(id, body[, access, limit_related])

Controller for PUT endpoints.

update_contact(id, body)

This is a special-case function for the contact-update resource

static create(body, id_prefix='x-', limit_related={})

Controller for POST endpoints. This method assigns a new object ID, sets the _created_ timestamp, evaluates user’s permissions, adds a default category_id if the model has this attribute, and inserts a row to the back-end database.

Parameters:
  • body (dict) – resource fields as defined by openapi.yaml schema

  • id_prefix (str) – generated objects will be assigned a random 10- to 16-character ID; you can set a unique prefix if desired

  • limit_related (dict) – limits on number of related records, keyed by relationship name

Returns:

first element is a dict with the id, second element is response code (201 on success)

Return type:

tuple

db_get(id)

Activate a SQLalchemy query object for the specified ID in the current model

Parameters:

id (str) – object ID

Returns:

query object

Return type:

obj

static delete(ids, force=False)

Controller for DELETE endpoints. This method looks for existing records, evaluates user’s permissions, and updates or removes rows in the back-end database.

Parameters:
  • ids (list of str) – record IDs to be flagged for removal

  • force (bool) – flag for removal if false; remove data if true

Returns:

first element is a dict with the id, second element is response code (200 on success)

Return type:

tuple

static find(**kwargs)

Find records which match query parameters passed from connexion by name, in a dictionary that also includes user and token info

Parameters:
  • cursor_next (str) – pagination token to fetch subsequent records

  • filter (dict) – field/value pairs to query (simple queries only, with string or list matching; or * for any)

  • limit (int) – max records to fetch

  • offset (int) – old-style pagination starting offset

  • sort (str) – <field>[:{asc|desc}]

  • status (str) – value is added to filter

Returns:

items (list), count(int), cursor_next (str)

Return type:

dict

static get(id)

Controller for GET endpoints. This method evaluates privacy settings against the user’s permissions, looks up category, owner and geocode values, and fetches the object from back-end database.

Parameters:

id (str) – ID of the desired resource

Returns:

first element is a dict with the object or error message, second element is response code (200 on success)

Return type:

tuple

static update(id, body, access='u', limit_related={})

Controller for PUT endpoints. This method looks for an existing record, evaluates user’s permissions, and updates the row in the back-end database.

Parameters:
  • body (dict) – fields to be updated

  • access (str) – access-level required for RBAC evaluation

  • limit_related (dict) – limits on number of related records, indexed by relationship name

Returns:

first element is a dict with the id, second element is response code (200 on success)

Return type:

dict

static update_contact(id, body)

This is a special-case function for the contact-update resource

  • validate sms carrier

  • keep person identity in sync with primary contact

Parameters:
  • id (str) – resource ID

  • body (dict) – as defined in openapi.yaml