-
Notifications
You must be signed in to change notification settings - Fork 72
SAFRSBase
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
Object initialization:
- set the named attributes and add the object to the database
- create relationships
If an object with given arguments already exists, this object is instantiated
Create a schema for object creation and updates through the HTTP PATCH and POST interfaces The schema is created using the sqlalchemy database schema. So there is a one-to-one mapping between json input data and db columns :return: swagger doc
Check the column permission (read/write) Goal is to extend this in the future :param column_name: column name :permission: :return: Boolean
returning None will cause our jsonapi to perform a count() on the result this can be overridden with a cached value for performance on large tables (>1G)
Apply a filter to this model :param filter_args: filter to apply, passed as a request URL parameter :return: sqla query object
-
Returns
a list of jsonapi_rpc methods for this class
-
Return type
list
-
Returns
list of jsonapi attribute names
At the moment we expect the column name to be equal to the column name Things will go south if this isn’t the case and we should use the cls.mapper._polymorphic_properties instead
-
Returns
Encoded object according to the jsonapi specification:
`
data = {
“attributes”: { … },
“id”: “…”,
“links”: { … },
“relationships”: { … },
“type”: “…”
}\`
What is returned in the “meta” part may be implemented by the app
Try to fetch and parse the (jsonapi attribute) value for a db column from the kwargs :param kwargs: :param column: database column :return parsed value:
update the object attributes :param
**
attributes:
This method is called when a new item is created with a POST to the json api
-
Parameters
attributes – the jsonapi “data” attributes
-
Returns
new cls instance
_s_post performs attribute sanitization and calls cls.init The attributes may contain an “id” if cls.allow_client_generated_ids is True
ClassPropertyDescriptor
ClassPropertyDescriptor
-
Returns
the relationships used for jsonapi (de/)serialization
-
Returns
a sample instance for the API documentation, i.e. the first item in the DB
-
Returns
a sample to be used as an example payload in the swagger example
-
Returns
a sample id for the API documentation, i.e. the first item in the DB
-
Parameters
url_prefix –
-
Returns
endpoint url of this instance
-
Parameters
-
url_prefix – URL prefix used by the app
-
type – endpoint type, e.g. “instance”
-
-
Returns
the API endpoint
-
Return type
str
-
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
-
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.
-
Returns
the object’s id type
-
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)
ClassPropertyDescriptor
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.