DBBaseResource

class flask_restful_dbbase.resources.DBBaseResource[source]

Bases: Resource

DBBaseResource Class

This model class implements the base class.

model_class: a dbbase.Model url_prefix: the portion of the path leading up to the resource, for example: /api/v2

url_name: the url_name defaults to a lower case version of the the model_class name if left as None, but can have an explicit name if necessary.

serial_fields: if left as None, it uses the serial list from the model class. Left as None, it will default to the underlying model.

serial_field_relations: can customize how relationship variables are presented in output.

before_commit/after_commit: These variables are designed to extend the capabilities of the methods be enabling a function or class to modify the data just prior to commit, or after. By using these, it is possible to send data to message queues, adjust the data, or otherwise change the data.

There are expectations of about the functions/classes.

Format for a before/after function:

def myfunc(resource_self, item, status_code):

Parameters:
  • resource_self – (obj) : This is the self of the resource. This provides access to the resource itself.

  • item – (obj) : This is the SQLAlchemy object.

  • status_code (int) – If due to the processing that status_code should change, you can change it here. Otherwise, simply return it.

Returns:

item – (obj) : The modified record status_code (int) : The possibly altered response status_code

Example of a Class:

A class can be used to hold additional data. This example shows how a resource can receive a POSTed object, but return the job created as a result instead.

A class requires that a run function be implemented with the input variables as shown below.

class JobCreator(object):
def __init__(self, class_name):

self.Job = class_name

def run(self, resource_self, item, status_code):

# obj_self gives access resource characteristics # but not used in this case data = item.to_dict(to_camel_case=False) job = self.Job() job = self.Job(

owner_id=data[‘owner_id’], model_id=data[‘id’], job_type_id=0, status_id=0

).save() if resource_self.serial_fields is None:

resource_self.serial_fields = {}

resource_self.serial_fields[‘post’] = self.Job.get_serial_fields()

# job submitted here ->

status_code = 202 return job, status_code

meta_doc: This set of attributes is designed to supplement meta information automatically generated. It uses the MetaDoc class found in utils.

Methods

as_view(name, *class_args, **class_kwargs)

Convert the class into a view function that can be registered for a route.

create_url()

Url can come from: * url_name resource variable

dispatch_request(*args, **kwargs)

The actual view function behavior.

format_key(key, key_type)

This function returns the portion of the URL that embodies the key.

get_key_names([formatted])

This function returns column names marked as primary_key.

get_meta([method])

This function returns the settings for the resource.

get_obj_params()

This is a convenience function for getting documentation parameters from the model class.

get_urls()

This function returns something similar to

is_collection()

This function returns True if identified as a collection resource.

screen_data(data, obj_params[, ...])

Assumes data is deserialized

Attributes

after_commit

before_commit

decorators

A list of decorators to apply, in order, to the generated view function.

default_sort

fields

init_every_request

Create a new instance of this view class for every request by default.

meta_doc

method_decorators

methods

The methods this view is registered for.

model_class

model_name

The string version of the Model class name.

provide_automatic_options

Control whether the OPTIONS method is handled automatically.

representations

requires_parameter

serial_field_relations

serial_fields

url_name

url_prefix

use_date_conversions

use_date_conversions can be used if the database, such as SQlite3 does not support the acceptance of string-based dates.

use_date_conversions = False

use_date_conversions can be used if the database, such as SQlite3 does not support the acceptance of string-based dates. Since it takes processing time and adherence to a format, it is an optional feature.

model_name = None

The string version of the Model class name. This is set upon initialization.

classmethod get_key_names(formatted=False)[source]

This function returns column names marked as primary_key.

Parameters:

formatted – (bool) : will return in form [<int:id>]

Returns:

key_names (list) – a list of keys is returned

classmethod get_obj_params()[source]

This is a convenience function for getting documentation parameters from the model class.

Returns:

(dict) – The object properties of the model class

static format_key(key, key_type)[source]

This function returns the portion of the URL that embodies the key.

Parameters:
  • key – (str) : The name of the key field.

  • key_type – (str) : Either ‘integer’ or something else.

Returns:

formatted key – (str) : such as <int:id>

classmethod get_urls()[source]
This function returns something similar to

[ {url_prefix}/{this_url}, {url_prefix}/{this_url}/<int:id> ]

classmethod get_meta(method=None)[source]

This function returns the settings for the resource.

Parameters:

method – (str : None) : choices are get/post/put/patch/delete.

Returns:

meta_data (dict) – A dict with the resource characteristics. If a method is preferred, the focus will be narrowed to that method.

The intent of this function is to show relevant information for someone interacting with an API.

classmethod is_collection()[source]

This function returns True if identified as a collection resource.

classmethod create_url()[source]

Url can come from: * url_name resource variable

but if None * snake_case version of class name

NOTE: figure out flag for use table/classname

screen_data(data, obj_params, skip_missing_data=False)[source]

Assumes data is deserialized

This function screens data from a few parameters, missing data, unneeded fields in data, excessive string lengths, improper numbers.

Note that this function does not exhaustively determine the problem. It ends on first failure.

Parameters:
  • data – (dict) : incoming data

  • obj_params – (dict) : table parameters

  • skip_missing_data – (bool) : Flag to check for all for record

Returns:

status – (bool) : True, Successful screening msg : (None : dict) : description of the problem

Each test for bad data outputs a status of True, None for each successful test. Unsuccessful tests add the problem to an error list to be returned at the end. That way, there is a relatively complete list of the problems encountered.