Skip to content

SAFRSBase

Thomas Pollet edited this page Apr 5, 2020 · 4 revisions

class safrs.base.SAFRSBase(*args, **kwargs)

Bases: flask_sqlalchemy.model.Model

This SQLAlchemy mixin implements Json Serialization for SAFRS SQLalchemy Persistent Objects Serialization itself is performed by the to_dict method Initialization and instantiation are quite complex because we rely on the DB schema

The jsonapi id is generated from the primary keys of the columns

This class is mostly used as a sqla model mixin therefore the object attributes should not match column names or sqla attribute names, this is why most of the methods & properties have (or should have, hindsight is great :/) the distinguishing s prefix

property Type()

jsonapi spec doesn’t allow “type” as an attribute nmae, but this is a pretty common column name we rename type to Type so we can support it. A bit hacky but better than not supporting “type” at all This may cause other errors too, for ex when sorting :return: renamed type

_init_(*args, **kwargs)

Object initialization:

  • set the named attributes and add the object to the database
  • create relationships

allow_client_generated_ids( = False)

db_commit( = True)

exclude_attrs( = [])

exclude_rels( = [])

classmethod get_endpoint(url_prefix=None, type=None)

  • Parameters

    • url_prefix – URL prefix used by the app

    • type – endpoint type, e.g. “instance”

  • Returns

    the API endpoint

  • Return type

    str

classmethod get_instance(item=None, failsafe=False)

  • Parameters

    • item – instance id or dict { “id” : .. “type” : ..}

    • failsafe – indicates whether we want an exception to be raised in case the id is not found

  • Returns

    Instance or None. An error is raised if an invalid id is used

classmethod get_swagger_doc(http_method)

  • Parameters

    http_method – the http method for which to retrieve the documentation

  • Returns

    swagger body and response dictionaries

  • Return type

    tuple

Create a swagger api model based on the sqlalchemy schema.

http_methods( = {'DELETE', 'GET', 'PATCH', 'POST', 'PUT'})

id_type()

  • Returns

    the object’s id type

property jsonapi_id()

  • Returns

    json:api id

  • Return type

    str

if the table/object has a single primary key “id”, it will return this id. In the other cases, the jsonapi “id” will be generated by the cls.id_type (typically by combining the PKs)

object_id( = 'SAFRSBaseId')

query()

ClassPropertyDescriptor

query_limit( = 50)

swagger_models( = {'collection': None, 'instance': None})

to_dict(fields=None)

Create a dictionary with all the instance “attributes” this method will be called by SAFRSJSONEncoder to serialize objects

  • Parameters

    fields – if set, fields to include in the result

  • Returns

    dictionary object

The optional fields attribute is used to implement jsonapi “Sparse Fieldsets” https://jsonapi.org/format/#fetching-sparse-fieldsets:

client MAY request that an endpoint return only specific fields in the response on a per-type basis by including a fields[TYPE] parameter. The value of the fields parameter MUST be a comma-separated (U+002C COMMA, “,”) list that refers to the name(s) of the fields to be returned. If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.

url_prefix( = '')

class safrs.base.SAFRSDummy()

Bases: object

Debug class