Bloks

Blok anyblok-core

class anyblok.bloks.anyblok_core.AnyBlokCore(registry)

Bases: anyblok.blok.Blok

This blok is required by all anyblok application. This blok define the main fonctionnality to install, update and uninstall blok. And also list the known models, fields, columns and relationships

autoinstall = True
conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
name = 'anyblok-core'
optional_by = []
pre_migration(latest_version)
pre_migration_0_4_1_fields_become_polymorphic(latest_version)
priority = 0
classmethod reload_declaration_module(reload)
required_by = ['anyblok-io']
version = '0.8.1'

This blok is required by all anyblok application. This blok define the main fonctionnality to install, update and uninstall blok. And also list the known models, fields, columns and relationships:

  • Core Model
    • Base: inherited by all the Model
    • SqlBase: Inherited only by the model with table
    • SqlViewBase: Inherited only by the sql view model
  • System Models
    • Blok: List the bloks
    • Model: List the models
    • Field: List of the fields
    • Column: List of the columns
    • Relationship: List of the relation ship
    • Sequence: Define database sequence
    • Parameter: Define application parameter

Sequence

Some behaviours need to have sequence:

sequence = registry.System.Sequence.insert(
    code="string code",
    formater="One prefix {seq} One suffix")

Note

It is a python formater, you can use the variable:

  • seq: numero of the current data base sequence
  • code: code field
  • id: id field

Get the next value of the sequence:

sequence.nextval()

exemple:

seq = Sequence.insert(code='SO', formater="{code}-{seq:06d}")
seq.nextval()
>>> SO-000001
seq.nextval()
>>> SO-000002

Parameter

Parameter is a simple model key / value:

  • key: must be a String
  • value: any type

Add new value in the paramter model:

registry.System.Parameter.set(key, value)

Note

If the key already exist, then the value of the key is updated

Get an existing value:

registry.System.Parameter.get(key)

Warning

If the key does not existing then an ExceptionParameter will raise

Check the key exist:

registry.System.Parameter.is_exist(key)  # return a Boolean

Return and remove the parameter:

registry.System.Parameter.pop(key)

API doc

Authorization

class anyblok.bloks.anyblok_core.authorization.Authorization

Bases: object

Namespace for models supporting authorization policies.

class anyblok.bloks.anyblok_core.authorization.DefaultModelDeclaration

Bases: object

Pseudo model to represent the default value.

Core`

class anyblok.bloks.anyblok_core.core.base.Base

Bases: object

Inherited by all the models

classmethod fire(event, *args, **kwargs)

Call a specific event on the model

Parameters:event – Name of the event
classmethod from_primary_keys(**pks)

No SQL Model has not primary key

classmethod get_model(model)
classmethod get_primary_keys(**pks)

No SQL Model has not primary key

classmethod has_model_perm(principals, permission)

Check that one of principals has permission on given model.

Since this is a classmethod, even if called on a record, only its model class will be considered for the permission check.

has_perm(principals, permission)

Check that one of principals has permission on given record.

Since this is an ordinary instance method, it can’t be used on the model class itself. For this use case, see has_model_perm()

classmethod initialize_model()

This method is called to initialize a model during the creation of the registry

is_sql = False
to_primary_keys()

No SQL Model has not primary key

class anyblok.bloks.anyblok_core.core.sqlbase.SqlMixin

Bases: object

classmethod aliased(*args, **kwargs)

Facility to Apply an aliased on the model:

MyModelAliased = MyModel.aliased()

is equal at:

from sqlalchemy.orm import aliased
MyModelAliased = aliased(MyModel)
Return type:SqlAlchemy aliased of the model
classmethod define_mapper_args()
classmethod define_table_args()
classmethod fields_description(fields=None)
classmethod from_multi_primary_keys(*pks)

return the instances of the model from the primary keys

Parameters:*pks – list of dict [{primary_key: value, ...}]
Return type:instances of the model
classmethod from_primary_keys(**pks)

return the instance of the model from the primary keys

Parameters:**pks – dict {primary_key: value, ...}
Return type:instance of the model
classmethod get_primary_keys()

return the name of the primary keys of the model

Type:list of the primary keys name
classmethod get_where_clause_from_primary_keys(**pks)

return the where clause to find object from pks

Parameters:**pks – dict {primary_key: value, ...}
Return type:where clause
Exception:SqlBaseException
is_sql = True
classmethod query(*elements)

Facility to do a SqlAlchemy query:

query = MyModel.query()

is equal at:

query = self.registry.session.query(MyModel)
Parameters:elements – pass at the SqlAlchemy query, if the element is a string then thet are see as field of the model
Return type:SqlAlchemy Query
to_dict(*fields)

Transform a record to the dict of value

Parameters:fields

list of fields to put in dict; if not selected, fields then take them all. A field is either one of these:

  • a string (which is the name of the field)
  • a 2-tuple if the field is a relationship (name of the field, tuple of foreign model fields)
Return type:dict

Here are some examples:

=>> instance.to_dict()  # get all fields
{"id": 1,
 "column1": "value 1",
 "column2": "value 2",
 "column3": "value 3",
 "relation1": {"relation_pk_1": 42, "relation_pk_2": "also 42"}
                                # m2o or o2o : this is a dictionary
 "relation2": [{"id": 28}, {"id": 1}, {"id": 34}]
                    # o2m or m2m : this is a list of dictionaries
 }

=>> instance.to_dict("column1", "column2", "relation1")
            # get selected fields only (without any constraints)
{"column1": "value 1",
 "column2": "value 2",
 "relation1": {"relation_pk_1": 42, "relation_pk_2": "also 42"}
 }

=>> instance.to_dict("column1", "column2", (
            # select fields to use in the relation related model
    "relation1", ("relation_pk1", "name", "value")
                # there is no constraints in the choice of fields
    ))
{"column1": "value",
 "column2": "value",
 "relation1": {"relation_pk_1": 42, "name": "H2G2", "value": "42"}
 }

=>> instance.to_dict("column1", "column2", ("relation1", ))
# or
=>> instance.to_dict("column1", "column2", ("relation1", None))
# or
=>> instance.to_dict("column1", "column2", ("relation1", ()))
                      # select all the fields of the relation ship
{"column1": "value",
 "column2": "value",
 "relation1": {"relation_pk_1": 42, "name": "H2G2", "value": "42"}
 }

=>> instance.to_dict("column1", "column2", (
                            # select relation fields recursively
    "relation1", ("name", "value", (
        "relation", ("a", "b", "c")
        ))
    ))
{"column1": "value",
 "column2": "value",
 "relation1": {"name": "H2G2", "value": "42", "relation": [
     {"a": 10, "b": 20, "c": 30},
     {"a": 11, "b": 22, "c": 33},
     ]}
 }
to_primary_keys()

return the primary keys and values for this instance

Return type:dict {primary key: value, ...}
class anyblok.bloks.anyblok_core.core.sqlbase.SqlBase

Bases: anyblok.bloks.anyblok_core.core.sqlbase.SqlMixin

this class is inherited by all the SQL model

delete(flush=True)

Call the SqlAlchemy Query.delete method on the instance of the model:

self.delete()

is equal at:

flush the session
remove the instance of the session
and expire all the session, to reload the relation ship
expire(*fields)

Expire the attribute of the instance, theses attributes will be load at the next call of the instance

see: http://docs.sqlalchemy.org/en/latest/orm/session_api.html #sqlalchemy.orm.session.Session.expire

expire_relationship_mapped(mappers)

Expire the objects linked with this object, in function of the mappers definition

find_relationship(*fields)

Find column and relation ship link with the column or relationship passed in fields.

Parameters:*fields – lists of the attribute name
Return type:list of the attribute name of the attribute and relation ship
find_remote_attribute_to_expire(*fields)
classmethod insert(**kwargs)

Insert in the table of the model:

MyModel.insert(...)

is equal at:

mymodel = MyModel(...)
MyModel.registry.session.add(mymodel)
MyModel.registry.flush()
classmethod multi_insert(*args)

Insert in the table one or more entry of the model:

MyModel.multi_insert([{...}, ...])

the flush will be done only one time at the end of the insert

Exception:SqlBaseException
classmethod precommit_hook(method, *args, **kwargs)

Same in the registry a hook to call just before the commit

Warning

Only one instance of the hook is called before the commit

Parameters:
  • method – the method to call on this model
  • put_at_the_end_if_exist – If True the hook is move at the end
refresh(*fields)

Expire and reload all the attribute of the instance

See: http://docs.sqlalchemy.org/en/latest/orm/session_api.html #sqlalchemy.orm.session.Session.refresh

update(**values)

Hight livel method to update the session for the instance

self.update(val1=.., val2= ...)

..warning:

the columns and values is passed as named arguments to show
a difference with Query.update meth
class anyblok.bloks.anyblok_core.core.sqlviewbase.SqlViewBase

Bases: anyblok.bloks.anyblok_core.core.sqlbase.SqlMixin

this class is inherited by all the SQL view

delete(*args, **kwargs)
update(*args, **kwargs)
class anyblok.bloks.anyblok_core.core.instrumentedlist.InstrumentedList

Bases: object

class of the return of the query.all() or the relationship list

class anyblok.bloks.anyblok_core.core.query.Query(entities, session=None)

Bases: sqlalchemy.orm.query.Query

Overload the SqlAlchemy Query

all()

Return an instrumented list of the result of the query

dictall()
dictfirst()
dictone()
get_field_nams_in_column_description()
with_perm(principals, permission)

Add authorization pre- and post-filtering to query.

This must be last in the construction chain of the query. Queries too complicated for the authorization system to infer safely will be refused.

Parameters:
  • principals – list, set or tuple of strings
  • permission (str) – the permission to filter for
Returns:

a query-like object, with only the returning methods, such as all(), count() etc. available.

class anyblok.bloks.anyblok_core.core.session.Session(*args, **kwargs)

Bases: sqlalchemy.orm.session.Session

Overload of the SqlAlchemy session

system

class anyblok.bloks.anyblok_core.system.System

Bases: object

class anyblok.bloks.anyblok_core.system.blok.Blok

Bases: object

STATES = {'touninstall': 'To uninstall', 'toinstall': 'To install', 'installed': 'Installed', 'uninstalled': 'Uninstalled', 'undefined': 'Undefined', 'toupdate': 'To update'}
classmethod apply_state(*bloks)

Call the rigth method is the blok state change

Warning

for the uninstallation the method called is uninstall_all

Parameters:bloks – list of the blok name load by the registry
classmethod check_if_the_conditional_are_installed(blok)

Return True if all the conditions to install the blok are satisfied

Parameters:blok – blok name
Return type:boolean
get_long_description()

fget of the long_description Column.Selection

Return type:the readme file of the blok
get_short_description()

fget of the short_description Column.Selection

Return type:the docstring of the blok
install()

Method to install the blok

installed_version = <anyblok.column.String object>
classmethod list_by_state(*states)

Return the blok name in function of the wanted states

Parameters:states – list of the state
Return type:list if state is a state, dict if the states is a list
load()

Method to load the blok when the registry is completly loaded

classmethod load_all()

Load all the installed bloks

long_description = <anyblok.field.Function object>
name = <anyblok.column.String object>
order = <anyblok.column.Integer object>
short_description = <anyblok.field.Function object>
state = <anyblok.column.Selection object>
uninstall()

Method to uninstall the blok

classmethod uninstall_all(*bloksname)

Search and call the uninstall method for all the uninstalled bloks

Warning

Use the desc order to uninstall because we can’t uninstall a dependancies before

Parameters:bloksname – list of the blok name to uninstall
classmethod update_list()

Populate the bloks list and update the state of existing bloks

upgrade()

Method to update the blok

version = <anyblok.column.String object>
class anyblok.bloks.anyblok_core.system.cache.Cache

Bases: object

classmethod clear_invalidate_cache()

Invalidate the cache that needs to be invalidated

classmethod detect_invalidation()

Return True if a new invalidation is found in the table

Return type:Boolean
classmethod get_invalidation()

Return the pointer of the method to invalidate

classmethod get_last_id()

Return the last primary key id value

id = <anyblok.column.Integer object>
classmethod initialize_model()

Initialize the last_cache_id known

classmethod invalidate(registry_name, method)

Call the invalidation for a specific method cached on a model

Parameters:
  • registry_name – namespace of the model
  • method – name of the method on the model
Exception:

CacheException

classmethod invalidate_all()
last_cache_id = None
lrus = {}
method = <anyblok.column.String object>
registry_name = <anyblok.column.String object>
class anyblok.bloks.anyblok_core.system.field.Field

Bases: object

classmethod add_field(rname, label, model, table, ftype)

Insert a field definition

Parameters:
  • rname – name of the field
  • label – label of the field
  • model – namespace of the model
  • table – name of the table of the model
  • ftype – type of the AnyBlok Field
classmethod alter_field(field, label, ftype)

Update an existing field

Parameters:
  • field – instance of the Field model to update
  • label – label of the field
  • ftype – type of the AnyBlok Field
code = <anyblok.column.String object>
classmethod define_mapper_args()
classmethod define_table_args()
entity_type = <anyblok.column.String object>
ftype = <anyblok.column.String object>
classmethod get_cname(field, cname)
label = <anyblok.column.String object>
model = <anyblok.column.String object>
name = <anyblok.column.String object>
class anyblok.bloks.anyblok_core.system.column.Column

Bases: anyblok.model.Field

classmethod add_field(cname, column, model, table, ftype)

Insert a column definition

Parameters:
  • cname – name of the column
  • column – instance of the column
  • model – namespace of the model
  • table – name of the table of the model
  • ftype – type of the AnyBlok Field
classmethod alter_field(column, meta_column, ftype)

Update an existing column

Parameters:
  • column – instance of the Column model to update
  • meta_column – instance of the SqlAlchemy column
  • ftype – type of the AnyBlok Field
autoincrement = <anyblok.column.Boolean object>
foreign_key = <anyblok.column.String object>
classmethod get_cname(field, cname)

Return the real name of the column

Parameters:
  • field – the instance of the column
  • cname – Not use here
Return type:

string of the real column name

model = <anyblok.column.String object>
name = <anyblok.column.String object>
nullable = <anyblok.column.Boolean object>
primary_key = <anyblok.column.Boolean object>
remote_model = <anyblok.column.String object>
unique = <anyblok.column.Boolean object>
class anyblok.bloks.anyblok_core.system.relationship.RelationShip

Bases: anyblok.model.Field

classmethod add_field(rname, relation, model, table, ftype)

Insert a relationship definition

Parameters:
  • rname – name of the relationship
  • relation – instance of the relationship
  • model – namespace of the model
  • table – name of the table of the model
  • ftype – type of the AnyBlok Field
classmethod alter_field(field, label, ftype)
local_column = <anyblok.column.String object>
model = <anyblok.column.String object>
name = <anyblok.column.String object>
nullable = <anyblok.column.Boolean object>
remote = <anyblok.column.Boolean object>
remote_column = <anyblok.column.String object>
remote_model = <anyblok.column.String object>
remote_name = <anyblok.column.String object>
class anyblok.bloks.anyblok_core.system.cron.Cron

Bases: object

classmethod add_worker_for(record)
classmethod close_worker_on_error(worker, error)
classmethod close_worker_with_success(worker)
classmethod lock_one_job()
classmethod run(sleep_time=60, timeout=None)
started = True
class anyblok.bloks.anyblok_core.system.cron.Job

Bases: object

available_at = <anyblok.column.DateTime object>
done_at = <anyblok.column.DateTime object>
error = <anyblok.column.Text object>
id = <anyblok.column.Integer object>
is_a_class_method = <anyblok.column.Boolean object>
method = <anyblok.column.String object>
model = <anyblok.column.String object>
params = <anyblok.column.Json object>
class anyblok.bloks.anyblok_core.system.cron.Worker(job)

Bases: threading.Thread

call_method()
get_args_and_kwargs()
get_error()
get_model_record()
run()
class anyblok.bloks.anyblok_core.system.model.Model

Bases: object

Models assembled

classmethod add_fields(model, table)
description = <anyblok.field.Function object>
classmethod get_field(model, cname)
classmethod get_field_model(field)
get_model_doc_string()

Return the docstring of the model

is_sql_model = <anyblok.column.Boolean object>
classmethod listener_update_model(model)
name = <anyblok.column.String object>
table = <anyblok.column.String object>
classmethod update_fields(model, table)
classmethod update_list()

Insert and update the table of models

Exception:Exception
class anyblok.bloks.anyblok_core.system.parameter.Parameter

Bases: object

System Parameter

classmethod get(key)

Return the value of the key

Parameters:key – key to check
Return type:return value
Exception:ExceptionParameter
classmethod is_exist(key)

Check if one parameter exist for the key

Parameters:key – key to check
Return type:Boolean, True if exist
key = <anyblok.column.String object>
multi = <anyblok.column.Boolean object>
classmethod pop(key)

Remove return the value of the key

Parameters:key – key to check
Return type:return value
Exception:ExceptionParameter
classmethod set(key, value)

Insert or Update parameter for the key

Parameters:
  • key – key to save
  • value – value to save
value = <anyblok.column.Json object>
class anyblok.bloks.anyblok_core.system.sequence.Sequence

Bases: object

System sequence

code = <anyblok.column.String object>
classmethod create_sequence(values)

create the sequence for one instance

formater = <anyblok.column.String object>
id = <anyblok.column.Integer object>
classmethod initialize_model()

Create the sequence to determine name

classmethod insert(**kwargs)

Overwrite insert

classmethod multi_insert(*args)

Overwrite multi_insert

nextval()

return the next value of the sequence

classmethod nextvalBy(**kwargs)

Get the first sequence filtering by entries and return the next value

number = <anyblok.column.Integer object>
seq_name = <anyblok.column.String object>

documentation

class anyblok.bloks.anyblok_core.documentation.DocElement

Bases: object

class anyblok.bloks.anyblok_core.documentation.Documentation

Bases: anyblok.mixin.DocElement

auto_doc()
auto_doc_blok()
auto_doc_model()
chapter2RST(doc)
footer2RST(doc)
header2RST(doc)
toRST(doc)
toRST_blok(doc)
toRST_model(doc)
toSQL(dot)
toUML(dot)
class anyblok.bloks.anyblok_core.documentation.blok.Blok(blok)

Bases: object

exist()
classmethod filterBloks(query)
classmethod footer2RST(doc)
classmethod getelements()
classmethod header2RST(doc)
toRST(doc)
toRST_get_field()
toRST_write_params(doc)
class anyblok.bloks.anyblok_core.documentation.model.Model(model)

Bases: anyblok.mixin.DocElement

exist()
classmethod filterModel(query)
classmethod footer2RST(doc)
classmethod getelements()
classmethod header2RST(doc)
toRST(doc)
toRST_docstring(doc)
toRST_field(doc)
toRST_method(doc)
toRST_properties(doc)
toRST_properties_get()
toSQL_add_fields(dot)
toSQL_add_table(dot)
toUML_add_attributes(dot)
toUML_add_model(dot)
class anyblok.bloks.anyblok_core.documentation.model.attribute.Attribute(attribute)

Bases: object

exist(model)
classmethod filterAttribute(model, name)
classmethod footer2RST(doc)
classmethod getelements(model)
classmethod header2RST(doc)
toRST(doc)
toRST_docstring(doc)
toUML(dot, modelname)
class anyblok.bloks.anyblok_core.documentation.model.field.Field(field)

Bases: object

exist(model)
classmethod filterField(query)
classmethod footer2RST(doc)
classmethod getelements(model)
classmethod header2RST(doc)
mappers = {('Many2One', True): ('m2o', 'o2m'), ('Many2Many', True): ('m2m', 'm2m'), ('Many2One', False): ('m2o', None), ('Many2Many', False): ('m2m', None), ('One2Many', False): ('o2m', None), ('One2One', True): ('o2o', 'o2o'), ('One2Many', True): ('o2m', 'm2o'), ('One2One', False): ('o2o', 'o2o')}
toRST(doc)
toRST_docstring(doc)
toRST_properties(doc)
toRST_properties_get()
toSQL(dot)
toSQL_column(dot)
toSQL_field(dot)
toSQL_relationship(dot)
toUML(dot)
toUML_column(dot)
toUML_field(dot)
toUML_relationship(dot)

exception

exception anyblok.bloks.anyblok_core.exceptions.CoreBaseException

Bases: TypeError

Exception for Core.Base

exception anyblok.bloks.anyblok_core.exceptions.SqlBaseException

Bases: Exception

Simple Exception for sql base

exception anyblok.bloks.anyblok_core.exceptions.QueryException

Bases: Exception

Simple Exception for query

exception anyblok.bloks.anyblok_core.exceptions.CacheException

Bases: Exception

Simple Exception for the cache Model

exception anyblok.bloks.anyblok_core.exceptions.ParameterException

Bases: Exception

Simple exception for System.Parameter

exception anyblok.bloks.anyblok_core.exceptions.CronWorkerException

Bases: Exception

Simple exception for System.Parameter

Blok IO

class anyblok.bloks.io.AnyBlokIO(registry)

Bases: anyblok.blok.Blok

In / Out tool’s:

  • Formater: convert value 2 str or str 2 value in function of the field,
  • Importer: main model to define an import,
  • Exporter: main model to define an export,
conditional_by = []
conflicting_by = []
classmethod declare_io()
classmethod import_declaration_module()
name = 'anyblok-io'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-core']
required_by = ['anyblok-io-csv', 'anyblok-io-xml']
version = '0.8.1'

Note

Require the anyblok-io blok

Mapping

Model.IO.Mapping allows to link a Model instance by a Model namesapce and str key. this key is an external ID

Save an instance with a key:

Blok = self.registry.System.Blok
blok = Blok.query().filter(Blok.name == 'anyblok-core').first()
self.registry.IO.Mapping.set('External ID', blok)

Warning

By default if you save another instance with the same key and the same model, an IOMapingSetException will be raised. Il really you want this mapping you must call the set method with the named argument raiseifexist=False:

self.registry.IO.Mapping.set('External ID', blok, raiseifexist=False)

Get an entry in the mapping:

blok2 = self.registry.IO.Mapping.get('Model.System.Blok', 'External ID')
assert blok2 is blok

Formater

The goal of the formater is to get:

  • value from string
  • value from mapping key
  • string from value
  • mapping key from value

The value is the value of the field.

Warning

The relation ships are particulare cases. The value is the json of the primary keys. The Many2Many and the One2Many are the json of the list of the primary keys

Exporter

The Model.IO.Exporter export some entries in fonction of configuration. anyblok-io blok doesn’t give complete exporter, just the base Model to standardize all the possible export:

exporter = registry.IO.Exporter.insert(...)  # create a exporter
entries = ...  # entries are instance of model
fp = exporter.run(entries)
# fp is un handler on the opened file (StringIO)

Importer

The Model.IO.Importer import some entries in fonction of configuration. anyblok-io blok doesn’t give complete importer, just the base Model to standardize all the possible import:

importer = registry.IO.Importer.insert(...)  # create an importer
# the file to import are filled in the parameter
entries = importer.run()

API doc

exceptions

exception anyblok.bloks.io.exceptions.IOException

Bases: Exception

IO exception

exception anyblok.bloks.io.exceptions.IOMappingCheckException

Bases: anyblok.bloks.io.exceptions.IOException

IO Exception for setter

exception anyblok.bloks.io.exceptions.IOMappingSetException

Bases: anyblok.bloks.io.exceptions.IOException

IO Exception for setter

exception anyblok.bloks.io.exceptions.ImporterException

Bases: Exception

Simple Exception for importer

exception anyblok.bloks.io.exceptions.ExporterException

Bases: Exception

Simple Exception for exporter

exception anyblok.bloks.io.exceptions.FormaterException

Bases: Exception

Simple Exception for importer

mapping

class anyblok.bloks.io.mapping.Mapping

Bases: object

classmethod check_primary_keys(model, *pks)

check if the all the primary keys match with primary keys of the model

Parameters:
  • model – model to check
  • pks – list of the primary keys to check
Exception:

IOMappingCheckException

classmethod delete(model, key)

Delete the key for this model

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

Boolean True if the mapping is removed

filter_by_model_and_key(model, key)

SQLAlechemy hybrid method to filter by model and key

Parameters:
  • model – model of the mapping
  • key – external key of the mapping
filter_by_model_and_keys(model, *keys)

SQLAlechemy hybrid method to filter by model and key

Parameters:
  • model – model of the mapping
  • key – external key of the mapping
classmethod get(model, key)

return instance of the model with this external key

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

instance of the model

classmethod get_from_entry(entry)
classmethod get_from_model_and_primary_keys(model, pks)
classmethod get_mapping_primary_keys(model, key)

return primary key for a model and an external key

Parameters:
  • model – model of the mapping
  • key – string of the key
Return type:

dict primary key: value or None

key = <anyblok.column.String object>
model = <anyblok.column.String object>
classmethod multi_delete(model, *keys)

Delete all the keys for this model

Parameters:
  • model – model of the mapping
  • *keys – list of the key
Return type:

Boolean True if the mappings are removed

primary_key = <anyblok.column.Json object>
classmethod set(key, instance, raiseifexist=True)

Add or update a mmping with a model and a external key

Parameters:
  • model – model to check
  • key – string of the key
  • instance – instance of the model to save
  • raiseifexist – boolean (True by default), if True and the entry exist then an exception is raised
Exception:

IOMappingSetException

classmethod set_primary_keys(model, key, pks, raiseifexist=True)

Add or update a mmping with a model and a external key

Parameters:
  • model – model to check
  • key – string of the key
  • pks – dict of the primary key to save
  • raiseifexist – boolean (True by default), if True and the entry exist then an exception is raised
Exception:

IOMappingSetException

mixin

class anyblok.bloks.io.mixin.IOMixin

Bases: object

get_formater(ctype)
classmethod get_mode_choices()
id = <anyblok.column.Integer object>
mode = <anyblok.column.Selection object>
model = <anyblok.column.String object>

importer

class anyblok.bloks.io.importer.Importer

Bases: anyblok.mixin.IOMixin

check_import = <anyblok.column.Boolean object>
commit()
commit_at_each_grouped = <anyblok.column.Boolean object>
file_to_import = <anyblok.column.LargeBinary object>
get_key_mapping(key)
nb_grouped_lines = <anyblok.column.Integer object>
offset = <anyblok.column.Integer object>
run()
str2value(value, ctype, external_id=False, model=None)

exporter

class anyblok.bloks.io.exporter.Exporter

Bases: anyblok.mixin.IOMixin

classmethod get_external_id(model)
classmethod get_key_mapping(entry)
run(entries)
value2str(value, ctype, external_id=False, model=None)

formater

class anyblok.bloks.io.formater.Formater

Bases: object

externalIdStr2value(value, model)
externalIdValue2str(value, model)
str2value(value, model)
value2str(value, model)
class anyblok.bloks.io.formater.Float

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.Decimal

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.Json

Bases: anyblok.model.Formater

str2value(value, model)
value2str(value, model)
class anyblok.bloks.io.formater.Interval

Bases: anyblok.model.Formater

str2value(value, model)
value2str(value, model)
class anyblok.bloks.io.formater.Integer

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.SmallInteger

Bases: anyblok.model.Integer

class anyblok.bloks.io.formater.BigInteger

Bases: anyblok.model.Integer

class anyblok.bloks.io.formater.Boolean

Bases: anyblok.model.Formater

str2value(value, model)
value2str(value, model)
class anyblok.bloks.io.formater.Time

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.Date

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.DateTime

Bases: anyblok.model.Formater

str2value(value, model)
class anyblok.bloks.io.formater.Many2One

Bases: anyblok.model.Formater

externalIdStr2value(value, model)
externalIdValue2str(value, model)
str2value(value, model)
value2str(value, model)
class anyblok.bloks.io.formater.One2One

Bases: anyblok.model.Many2One

class anyblok.bloks.io.formater.Many2Many

Bases: anyblok.model.Formater

externalIdStr2value(values, model)
externalIdValue2str(values, model)
str2value(value, model)
value2str(values, model)
class anyblok.bloks.io.formater.One2Many

Bases: anyblok.model.Many2Many

Blok IO CSV

class anyblok.bloks.io_csv.AnyBlokIOCSV(registry)

Bases: anyblok.blok.Blok

CSV Importer / Exporter behaviour

conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
name = 'anyblok-io-csv'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
required_by = []
version = '0.8.1'

Note

Require the anyblok-io-csv blok

Exporter

Add an exporter mode (CSV) in AnyBlok:

Exporter = registry.IO.Exporter.CSV

Define what export:

csv_delimiter = ','
csv_quotechar = '"'
model = ``Existing model name``
fields = [
    {'name': 'field name'},
    {'name': 'fieldname1.fieldname2. ... .fieldnamen}  #  fieldname1, fieldname 2 must be Many2One or have foreign keys
    {'name': 'field', model="external_id"}  # field must be Many2One or foreign_keys or primary keys
    ...
]

Create the Exporter:

exporter = Exporter.insert(csv_delimiter=csv_delimiter,
                           csv_quotechar=csv_quotechar,
                           model=model,
                           fields=fields)

Warning

You can also make insert with registry.IO.Exporter directly

Run the export:

fp = exporter.run(entries)  # entries are instance of the ``model``

Importer

Add an importer mode (CSV) in AnyBlok:

Importer = registry.IO.Importer.CSV

Define what import:

csv_delimiter = ','
csv_quotechar = '"'
model = ``Existing model name``
with open(..., 'rb') as fp:
    file_to_import = fp.read()

Create the Exporter:

importer = Importer.insert(csv_delimiter=csv_delimiter,
                           csv_quotechar=csv_quotechar,
                           model=model,
                           file_to_import=file_to_import)

Warning

You can also make insert with registry.IO.Importer directly

Run the import:

res = importer.run()

The result is a dict with:

  • error_found: List the error, durring the import
  • created_entries: Entries created by the import
  • updated_entries: Entries updated by the import

List of the options for the import:

  • csv_on_error:
    • raise_now: Raise now
    • raise_at_the_end (default): Raise at the end
    • ignore: Ignore and continue
  • csv_if_exist:
    • pass: Pass to the next record
    • overwrite (default): Update the record
    • create: Create another record
    • raise: Raise an exception
  • csv_if_does_not_exist:
    • pass: Pass to the next record
    • create (default): Create another record
    • raise: Raise an exception

API doc

exceptions

exception anyblok.bloks.io_csv.exceptions.CSVImporterException

Bases: anyblok.bloks.io.exceptions.ImporterException

Simple exception for CSV importer

exception anyblok.bloks.io_csv.exceptions.CSVExporterException

Bases: anyblok.bloks.io.exceptions.ExporterException

Simple exception for CSV exporter

mixin

class anyblok.bloks.io_csv.mixin.IOCSVFieldMixin

Bases: object

id = <anyblok.column.Integer object>
name = <anyblok.column.String object>
class anyblok.bloks.io_csv.mixin.IOCSVMixin

Bases: object

csv_delimiter = <anyblok.column.String object>
csv_quotechar = <anyblok.column.String object>

importer

class anyblok.bloks.io_csv.importer.Importer

Bases: anyblok.mixin.IOCSVMixin

csv_if_does_not_exist = <anyblok.column.Selection object>
csv_if_exist = <anyblok.column.Selection object>
csv_on_error = <anyblok.column.Selection object>
classmethod get_mode_choices()
class anyblok.bloks.io_csv.importer.CSV(importer)

Bases: object

commit()
consume_nb_grouped_lines()
consume_offset()
get_header()
get_reader()
classmethod insert(delimiter=None, quotechar=None, **kwargs)
parse_row(row)
run()

exporter

class anyblok.bloks.io_csv.exporter.Exporter

Bases: anyblok.mixin.IOCSVMixin

classmethod get_mode_choices()
class anyblok.bloks.io_csv.exporter.Field

Bases: anyblok.mixin.IOCSVFieldMixin

exporter = <anyblok.relationship.Many2One object>
format_header()
classmethod get_selection()
mapping = <anyblok.column.String object>
mode = <anyblok.column.Selection object>
value2str(exporter, entry)
class anyblok.bloks.io_csv.exporter.CSV(exporter)

Bases: object

get_header()
classmethod insert(delimiter=None, quotechar=None, fields=None, **kwargs)
run(entries)

Blok IO XML

class anyblok.bloks.io_xml.AnyBlokIOXML(registry)

Bases: anyblok.blok.Blok

XML Importer / Exporter behaviour

Warning

Importer and Exporter are not implemented yet

conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
name = 'anyblok-io-xml'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
required_by = []
version = '0.8.1'

Note

Require the anyblok-io-xml blok

Exporter

TODO

Importer

Add an importer mode (XML) in AnyBlok:

Importer = registry.IO.Importer.XML

Define what import:

model = ``Existing model name``
with open(..., 'rb') as fp:
    file_to_import = fp.read()

Create the Exporter:

importer = Importer.insert(model=model,
                           file_to_import=file_to_import)

Warning

You can also make insert with registry.IO.Importer directly

Run the import:

res = importer.run()

The result is a dict with:

  • error_found: List the error, durring the import
  • created_entries: Entries created by the import
  • updated_entries: Entries updated by the import

Root structure of the XML file:

<records on_error="...">
    ...
</records>

raise can have the value:

  • raise (dafault)
  • ignore

records node can have:

  • commit: commit the import, only if no error found
  • record: import one record

Add a record:

<records>
    <record>
        ...
        <field name="..." />
        ...
    </record>
</records>

Record attribute:

  • model: if not filled, then the importer will indicate the model

  • external_id: Mapping key

  • param: Mapping key only for the import (not save)

  • on_error:
    • raise
    • ignore (default)
  • if_exist:
    • overwrite (default)
    • create
    • pass: continue to the next record
    • continue: continue on the sub record without take this record
    • raise
  • id_does_not_exist:
    • create (default)
    • pass
    • raise

The field node represente a Field, a Column or RelationShip, the attribute are:

  • name (required): name of the field, column or relation ship

Case of the relation ship, they have some more attribute:

  • external_id:

  • param:

  • on_error:
    • raise
    • ignore (default)
  • if_exist:
    • overwrite (default)
    • create
    • pass: continue to the next record
    • continue: continue on the sub record without take this record
    • raise
  • id_does_not_exist:
    • create (default)
    • pass
    • raise

Many2One and One2One declaration is directly in the field node:

<records
    <record
        ...
        <field name="Many2One or One2One">
            ...
            <field name="..." />
            ...
        </field>
        ...
    </record>
</records>

One2Many and Many2Many declarations is also in the field but with a record node:

<records
    <record
        ...
        <field name="Many2Many or One2Many">
            ...
            <record>
                ...
                <field name="..." />
                ...
            </record>
            ...
        </field>
        ...
    </record>
</records>

API doc

exceptions

exception anyblok.bloks.io_xml.exceptions.XMLImporterException

Bases: anyblok.bloks.io.exceptions.ImporterException

Simple exception for XML importer

exception anyblok.bloks.io_xml.exceptions.XMLExporterException

Bases: anyblok.bloks.io.exceptions.ExporterException

Simple exception for XML exporter

importer

class anyblok.bloks.io_xml.importer.Importer

Bases: object

classmethod get_mode_choices()
class anyblok.bloks.io_xml.importer.XML(importer)

Bases: object

check_entry_before_import(record, entry, if_exist='overwrite', if_does_not_exist='create', **kwargs)
commit()
create_entry(Model, values, two_way, **kwargs)
find_entry(model=None, external_id=None, param=None, **kwargs)
get_from_param(param, model)
import_entry(entry, values, model=None, external_id=None, param=None, if_exist='overwrite', if_does_not_exist='create', two_way=False, **kwargs)
import_field(field, ctype, model=None, on_error='ignore')
import_multi_values(records, model)
import_record(record, two_way=False, **kwargs)
import_records(records)
import_value(record, ctype, model, on_error='ignore')
classmethod insert(delimiter=None, quotechar=None, **kwargs)
map_imported_entry(model, param, external_id, two_way, entry, if_exist='overwrite', **kwargs)
run()
two_ways_to_external_id()
update_x2M(entry, inValues, notInValues)
validate_field(field, Model, fields_description, **_kw)

exporter

class anyblok.bloks.io_xml.exporter.Exporter

Bases: object

classmethod get_mode_choices()
class anyblok.bloks.io_xml.exporter.XML(exporter)

Bases: object

classmethod insert(delimiter=None, quotechar=None, fields=None, **kwargs)
run(entries)

Blok Model Auth

class anyblok.bloks.model_authz.ModelBasedAuthorizationBlok(registry)

Bases: anyblok.blok.Blok

conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
name = 'model_authz'
optional_by = []
classmethod reload_declaration_module(reload)
required_by = []
version = '0.8.1'

API doc

class anyblok.bloks.model_authz.models.ModelPermissionGrant

Bases: object

Default model for ModelBasedAuthorizationRule

model = <anyblok.column.String object>
permission = <anyblok.column.String object>
principal = <anyblok.column.String object>