Crudcast

Crudcast API reference

Crudcast App

class crudcast.app.CrudcastApp(import_name, config_file, **kwargs)

The main Crudcast app object, which extends the Flask app

Parameters:
  • import_name – The import_name parameter for Flask
  • config_file – Local path to a valid config.yml
  • kwargs – additional arguments to be passed to Flask
client = None
crudcast_config = {'db_name': 'database', 'mongo_url': 'mongodb://localhost:27017/', 'swagger': {'basePath': '/api', 'info': {'description': 'This is an API automatically generated by crudcast', 'title': 'My Crudcast app', 'version': '1.0.0'}, 'url': '/api/docs', 'swagger': '2.0'}}
db = None
get_definition(model)

Returns the model definition based on the model’s fields

Return type:dict
get_instance_path(model)

Returns the path entries for instance level calls such as PUT, DELETE, RETRIEVE

Return type:dict
get_model_path(model)

Generates a path entry for the swagger view for each model

Return type:dict
get_security_definitions()
get_swagger_ui_view()

Creates a swagger view using flask_swagger_ui

Returns:a swagger view
get_tag(model)

Returns the tag name/description to be used in the swagger view for each model

Return type:dict
methods = {}
models = {}
set_crudcast_config(config_file)

Overwrites self.crudcast_config with the content of the config file

Parameters:config_file – a local file path to a valid config file
swagger_config

Retrieves the complete swagger configuration

Return type:dict
user_config = {'fields': {}, 'options': {'auth_type': 'basic'}}
user_manager = None

Manual Fields

BaseField

class crudcast.fields.BaseField(name, model, **options)

Base class for field objects. All fields must extend this class

Parameters:
  • name – Field name.
  • model (models.Model) – the Model object to which the field belongs
  • options – field options. The available options vary based on field type. Only the required and unique options are implemented at this level
auto = False
type = ''
validate(data, _id=None)

When creating or updating an object. This method validates the inputs

Parameters:
  • data (dict) – data to validate
  • _id (str or None) – ID as a string, or None in case of new objects
Return type:

dict

StringField

class crudcast.fields.StringField(name, **options)

String input field

Parameters:
  • name – field name
  • unique (Boolean) – if True, no new documents can be created if there is another document in the collection has the same value for this field as the one being provided
type = 'string'
validate(data, _id=None)

Validates that the input value is a string

DateTimeField

class crudcast.fields.DateTimeField(name, **options)

A field for storing datetime objects

Parameters:
  • name – field name
  • format_string – format string to be used for validating input. Defaults to ‘%Y-%m-%d %H:%M:%S.%f’ (ISO format)
validate(data, _id=None)

Validates the input string by asserting that the time format is valid

NumberField

class crudcast.fields.NumberField(name, model, **options)

Numeric input field.

type = 'number'
validate(data, _id=None)

Asserts that the input is numeric

BooleanField

class crudcast.fields.BooleanField(name, model, **options)

A simple true/false field

type = 'boolean'
validate(data, _id=None)

Checks that the input value is a Boolean

ForeignKeyField

class crudcast.fields.ForeignKeyField(name, model, **options)

A field that points at another collection in the database

Parameters:
  • name – field name
  • model – parent model
  • to – the related model’s name

Returns the related model

Parameters:related_model_name – the name of another model in the app
Return type:crudcast.models.Model
type = 'object'
validate(data, _id=None)

Checks to see if a document in the related model’s collection matches the ID provided as data

ManyToManyField

class crudcast.fields.ManyToManyField(name, model, **options)
type = 'array'
validate(data, _id=None)

Checks to see if a document in the related model’s collection matches the ID provided as data

Auto fields

AutoBaseField

class crudcast.fields.AutoBaseField(name, model, **options)

An automatically generated field. When Model.create() is called, the fields will be set automatically - the .set() method must be implemented in order for this to happen. Any attempt to set the value of this field manually will generate a ValidationError

auto = True
get_original(_id)

Helper function that returns the original value of the object, before it is updated. If you want to leave a value unchanged on update, the .set() method should call and return this function (otherwise the field value will be set to None)

Parameters:_id – the object id as a string
set(_id=None)

This method must be extended by all child classes. If it returns None, the value will not be changed

Parameters:_id – If an object is being updated, the _id will be provided
validate(data, _id=None)

Any attempt to set the value of an auto field will raise a validation error

AutoField

class crudcast.fields.AutoField(name, model, **options)

An auto field, e.g. one that creates a sequential number, e.g. 1, 2, 3, etc. Cannot be set manually - fields that extend this must implement the .set() method

set(_id=None)

Returns the collection length + 1 when the object is created

AutoDateTime

class crudcast.fields.AutoDateTimeField(name, model, **options)
set(_id=None)

Sets the field value to the current date/time when the object is created. If the object is being updated, and the value of self.create_only is True, then the date/time will be updated to the current time

Exceptions

ValidationError

class crudcast.exceptions.ValidationError(message, field=None, status_code=None)

When invalid data is sent to via POST or PUT, this exception gets raised

Parameters:
  • message – The error message
  • field – The name of the field that raised the exception, if applicable
  • status_code – allows overwriting of the default bad request status code, 400
status_code = 400
to_dict()

Returns a REST-friendly API response

ConfigException

class crudcast.exceptions.ConfigException

This exception gets raised if the config file is invalid

Resources

Resource

class crudcast.resources.Resource(api=None, *args, **kwargs)

Extension to the default flask_restplus.BaseResource which sets the app paramter, which is used for model creation

app = None
check_auth()
model = None
classmethod set_app(app)

Sets the value of __class__.app

ModelResource

class crudcast.resources.ModelResource(api=None, *args, **kwargs)

Model level resources - implements all REST methods for paths with no ID

get(model_name)

Lists all instances of a given model

Parameters:model_name – the name of the model, as it appears in the config file
Returns:a list of instances of the model object
methods = {'GET', 'POST'}
post(model_name)

Create an instance of a model

Parameters:model_name – the name of the model, as it appears in the config file
Returns:details of the created instance

InstanceResource

class crudcast.resources.InstanceResource(api=None, *args, **kwargs)

Instance level resources - implements all REST methods for paths with an ID

delete(model_name, _id)

Delete a single instance by its ID

Parameters:
  • model_name – the name of the model, as it appears in the config file
  • _id – MongoDB _id string
get(model_name, _id)

Retrieve a single instance by its ID

Parameters:
  • model_name – the name of the model, as it appears in the config file
  • _id – MongoDB _id string
Returns:

the MongoDB document

methods = {'PUT', 'DELETE', 'GET'}
put(model_name, _id)

Update a single instance by its ID

Parameters:
  • model_name – the name of the model, as it appears in the config file
  • _id – MongoDB _id string
Returns:

the MongoDB document