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

author = 'Suzanne Jean-Sébastien'
autoinstall = True
conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
logo = '../anyblok-logo_alpha_256.png'
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.11.0'

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

Declaration type:
 Core
Registry name:Core.Base
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_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

classmethod postcommit_hook(method, *args, **kwargs)

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

you can choice if the hook is called in function of call_only_if:

  • commited: Call if the commit is done without exception
  • raised: Call if one exception was raised
  • always: Always call

Warning

Only one instance with same paramters of the hook is called after 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
  • call_only_if – [‘commited’ (default), ‘raised’, ‘always’]
classmethod precommit_hook(method, *args, **kwargs)

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

Warning

Only one instance with same parameters 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
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 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 getFieldType(name)

Return the type of the column

TheModel.getFieldType(nameOfTheColumn)

this method take care if it is a polymorphic model or not

Parameters:name – name of the column
Return type:String, the name of the Type of column used
get_hybrid_property_columns()

Return the hybrid properties columns name from the Model and the inherited model if they come from polymorphisme

Cached classmethod with size=128

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
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

Declaration type:
 Core
Registry name:Core.SqlBase
delete(byquery=False, 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

expunge()

Expunge the instance in the session

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

Cached classmethod with size=128

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
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

Declaration type:
 Core
Registry name:Core.SqlViewBase
class anyblok.bloks.anyblok_core.core.instrumentedlist.InstrumentedList

Bases: object

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

Declaration type:
 Core
Registry name:Core.InstrumentedList
class anyblok.bloks.anyblok_core.core.query.Query(entities, session=None)

Bases: sqlalchemy.orm.query.Query

Overload the SqlAlchemy Query

Declaration type:
 Core
Registry name:Core.Query
all()

Return an instrumented list of the result of the query

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

Declaration type:
 Core
Registry name:Core.Session

system

class anyblok.bloks.anyblok_core.system.System

Bases: object

Declaration type:
 Model
Tablename:system
Registry name:Model.System
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.system.blok.Blok

Bases: object

Declaration type:
 Model
Tablename:system_blok
Registry name:Model.System.Blok
Inherit model or mixin:
 
field name Description
author
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • foreign_key - None
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • nullable - False
  • DB column name - None
state
  • Context:
  • Field type - <class ‘anyblok.column.Selection’>
  • nullable - False
  • default - uninstalled
  • selections:
  • undefined - Undefined
  • uninstalled - Uninstalled
  • installed - Installed
  • toupdate - To update
  • touninstall - To uninstall
  • toinstall - To install
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
installed_version
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
logo
  • Context:
  • Field type - <class ‘anyblok.field.Function’>
  • Label - None
  • fget - get_logo
version
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
order
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • nullable - False
  • default - -1
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
short_description
  • Context:
  • Field type - <class ‘anyblok.field.Function’>
  • Label - None
  • fget - get_short_description
long_description
  • Context:
  • Field type - <class ‘anyblok.field.Function’>
  • Label - None
  • fget - get_long_description
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_logo()

fget of logo return the path in the blok of the logo

Return type:absolute path or None if unexiste logo
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

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

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

class anyblok.bloks.anyblok_core.system.cache.Cache

Bases: object

Declaration type:
 Model
Tablename:system_cache
Registry name:Model.System.Cache
Inherit model or mixin:
 
field name Description
id
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • autoincrement - True
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
registry_name
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
method
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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

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

class anyblok.bloks.anyblok_core.system.field.Field

Bases: object

Declaration type:
 Model
Tablename:system_field
Registry name:Model.System.Field
Inherit model or mixin:
 
field name Description
model
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
ftype
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - True
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - Type
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
entity_type
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - True
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
code
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - True
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
label
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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
class anyblok.bloks.anyblok_core.system.column.Column

Bases: anyblok.model.Field

Declaration type:
 

Model

Tablename:

system_column

Registry name:

Model.System.Column

Inherit model or mixin:
 
  • <class ‘anyblok.model.Field’>
field name Description
model
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
remote_model
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
foreign_key
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
autoincrement
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - Auto increment
  • foreign_key - None
  • DB column name - None
primary_key
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
unique
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
nullable
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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
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

class anyblok.bloks.anyblok_core.system.relationship.RelationShip

Bases: anyblok.model.Field

Declaration type:
 

Model

Tablename:

system_relationship

Registry name:

Model.System.RelationShip

Inherit model or mixin:
 
  • <class ‘anyblok.model.Field’>
field name Description
model
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
remote
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - False
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
remote_model
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
remote_name
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
local_column
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
remote_column
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
nullable
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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
class anyblok.bloks.anyblok_core.system.cron.Cron

Bases: object

Declaration type:
 Model
Tablename:system_cron
Registry name:Model.System.Cron
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.system.cron.Job

Bases: object

Declaration type:
 Model
Tablename:system_cron_job
Registry name:Model.System.Cron.Job
Inherit model or mixin:
 
field name Description
params
  • Context:
  • Field type - <class ‘anyblok.column.Json’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
model
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - Model.System.Model => name
  • DB column name - None
done_at
  • Context:
  • Field type - <class ‘anyblok.column.DateTime’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
error
  • Context:
  • Field type - <class ‘anyblok.column.Text’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
is_a_class_method
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
id
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • autoincrement - True
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
method
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
available_at
  • Context:
  • Field type - <class ‘anyblok.column.DateTime’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
class anyblok.bloks.anyblok_core.system.cron.Worker(job)

Bases: threading.Thread

Declaration type:
 Model
Tablename:system_cron_worker
Registry name:Model.System.Cron.Worker
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.system.model.Model

Bases: object

Models assembled

Declaration type:
 Model
Tablename:system_model
Registry name:Model.System.Model
Inherit model or mixin:
 
field name Description
table
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 256
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
description
  • Context:
  • Field type - <class ‘anyblok.field.Function’>
  • Label - None
  • fget - get_model_doc_string
is_sql_model
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - Is a SQL model
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • size - 256
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
get_model_doc_string()

Return the docstring of the model

classmethod update_list()

Insert and update the table of models

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

Bases: object

System Parameter

Declaration type:
 Model
Tablename:system_parameter
Registry name:Model.System.Parameter
Inherit model or mixin:
 
field name Description
value
  • Context:
  • Field type - <class ‘anyblok.column.Json’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
multi
  • Context:
  • Field type - <class ‘anyblok.column.Boolean’>
  • default - False
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
key
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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
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
class anyblok.bloks.anyblok_core.system.sequence.Sequence

Bases: object

System sequence

Declaration type:
 Model
Tablename:system_sequence
Registry name:Model.System.Sequence
Inherit model or mixin:
 
field name Description
formater
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - {seq}
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
id
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • autoincrement - True
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
number
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
code
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
seq_name
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
classmethod create_sequence(values)

create the sequence for one instance

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

documentation

class anyblok.bloks.anyblok_core.documentation.DocElement

Bases: object

Declaration type:
 Mixin
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.documentation.Documentation

Bases: anyblok.mixin.DocElement

Declaration type:
 

Model

Tablename:

documentation

Registry name:

Model.Documentation

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.DocElement’>
class anyblok.bloks.anyblok_core.documentation.blok.Blok(blok)

Bases: object

Declaration type:
 Model
Tablename:documentation_blok
Registry name:Model.Documentation.Blok
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.documentation.model.Model(model)

Bases: anyblok.mixin.DocElement

Declaration type:
 

Model

Tablename:

documentation_model

Registry name:

Model.Documentation.Model

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.DocElement’>
class anyblok.bloks.anyblok_core.documentation.model.attribute.Attribute(attribute)

Bases: object

Declaration type:
 Model
Tablename:documentation_model_attribute
Registry name:Model.Documentation.Model.Attribute
Inherit model or mixin:
 
class anyblok.bloks.anyblok_core.documentation.model.field.Field(field)

Bases: object

Declaration type:
 Model
Tablename:documentation_model_field
Registry name:Model.Documentation.Model.Field
Inherit model or mixin:
 

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,
author = 'Suzanne Jean-Sébastien'
conditional_by = []
conflicting_by = []
classmethod declare_io()
classmethod import_declaration_module()
logo = '../anyblok-logo_alpha_256.png'
name = 'anyblok-io'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-core']
required_by = ['anyblok-io-csv', 'anyblok-io-xml']
version = '0.11.0'

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

core

class anyblok.bloks.io.core.Query

Bases: object

Declaration type:
 Core
Registry name:Core.Query
delete(*args, **kwargs)

Inherit the Query.delete methods.:

Model.query().delete(remove_mapping=True)
Parameters:remove_mapping – boolean, if check (default) the mapping is removed
class anyblok.bloks.io.core.SqlBase

Bases: object

Declaration type:
 Core
Registry name:Core.SqlBase
delete(*args, **kwargs)

Inherit the Model.delete methods.:

instance.delete(remove_mapping=True)
Parameters:remove_mapping – boolean, if check (default) the mapping is removed

mapping

class anyblok.bloks.io.mapping.Mapping

Bases: object

Declaration type:
 Model
Tablename:io_mapping
Registry name:Model.IO.Mapping
Inherit model or mixin:
 
field name Description
model
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - Model.System.Model => name
  • DB column name - None
primary_key
  • Context:
  • Field type - <class ‘anyblok.column.Json’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
blokname
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - Blok name
  • foreign_key - Model.System.Blok => name
  • DB column name - None
key
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
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 clean(bloknames=None, models=None)

Clean all mapping with removed object linked:

Mapping.clean(bloknames=['My blok'])

Warning

For filter only the no blokname:

Mapping.clean(bloknames=[None])
Params bloknames:
 filter by blok, keep the order to remove the mapping
Params models:filter by model, keep the order to remove the mapping
classmethod delete(model, key, mapping_only=True, byquery=False)

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

classmethod delete_for_blokname(blokname, models=None, byquery=False)

Clean all mapping with removed object linked:

Mapping.clean('My blok')

Warning

For filter only the no blokname:

Mapping.clean(None)
Params blokname:
 filter by blok
Params models:filter by model, keep the order to remove the mapping
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

Hybrid method

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

Hybrid method

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_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

classmethod multi_delete(model, *keys, **kwargs)

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

classmethod set(key, instance, raiseifexist=True, blokname=None)

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
  • blokname – name of the blok where come from the mapping
Exception:

IOMappingSetException

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

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
  • blokname – name of the blok where come from the mapping
Exception:

IOMappingSetException

mixin

class anyblok.bloks.io.mixin.IOMixin

Bases: object

Declaration type:
 Mixin
Inherit model or mixin:
 
field name Description
model
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - Model.System.Model => name
  • DB column name - None
mode
  • Context:
  • Field type - <class ‘anyblok.column.Selection’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • selections - get_mode_choices
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
id
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • autoincrement - True
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None

importer

class anyblok.bloks.io.importer.Importer

Bases: anyblok.mixin.IOMixin

Declaration type:
 

Model

Tablename:

io_importer

Registry name:

Model.IO.Importer

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.IOMixin’>
field name Description
offset
  • Context:
  • Field type - <class
‘anyblok.column.Integer’>
  • default - 0
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
commit_at_each_grouped
  • Context:
  • Field type - <class
‘anyblok.column.Boolean’>
  • default - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
check_import
  • Context:
  • Field type - <class
‘anyblok.column.Boolean’>
  • default - False
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
nb_grouped_lines
  • Context:
  • Field type - <class
‘anyblok.column.Integer’>
  • nullable - False
  • default - 50
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
file_to_import
  • Context:
  • Field type - <class
‘anyblok.column.LargeBinary’>
  • nullable - False
  • default - <class
‘anyblok.column.NoDefaultValue’>
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None

exporter

class anyblok.bloks.io.exporter.Exporter

Bases: anyblok.mixin.IOMixin

Declaration type:
 

Model

Tablename:

io_exporter

Registry name:

Model.IO.Exporter

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.IOMixin’>

formater

class anyblok.bloks.io.formater.Formater

Bases: object

Declaration type:
 Model
Tablename:io_formater
Registry name:Model.IO.Formater
Inherit model or mixin:
 
class anyblok.bloks.io.formater.Float

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_float

Registry name:

Model.IO.Formater.Float

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Decimal

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_decimal

Registry name:

Model.IO.Formater.Decimal

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Json

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_json

Registry name:

Model.IO.Formater.Json

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Interval

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_interval

Registry name:

Model.IO.Formater.Interval

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Integer

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_integer

Registry name:

Model.IO.Formater.Integer

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.SmallInteger

Bases: anyblok.model.Integer

Declaration type:
 

Model

Tablename:

io_formater_smallinteger

Registry name:

Model.IO.Formater.SmallInteger

Inherit model or mixin:
 
  • <class ‘anyblok.model.Integer’>
class anyblok.bloks.io.formater.BigInteger

Bases: anyblok.model.Integer

Declaration type:
 

Model

Tablename:

io_formater_biginteger

Registry name:

Model.IO.Formater.BigInteger

Inherit model or mixin:
 
  • <class ‘anyblok.model.Integer’>
class anyblok.bloks.io.formater.Boolean

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_boolean

Registry name:

Model.IO.Formater.Boolean

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Time

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_time

Registry name:

Model.IO.Formater.Time

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Date

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_date

Registry name:

Model.IO.Formater.Date

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.DateTime

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_datetime

Registry name:

Model.IO.Formater.DateTime

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.Many2One

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_many2one

Registry name:

Model.IO.Formater.Many2One

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.One2One

Bases: anyblok.model.Many2One

Declaration type:
 

Model

Tablename:

io_formater_one2one

Registry name:

Model.IO.Formater.One2One

Inherit model or mixin:
 
  • <class ‘anyblok.model.Many2One’>
class anyblok.bloks.io.formater.Many2Many

Bases: anyblok.model.Formater

Declaration type:
 

Model

Tablename:

io_formater_many2many

Registry name:

Model.IO.Formater.Many2Many

Inherit model or mixin:
 
  • <class ‘anyblok.model.Formater’>
class anyblok.bloks.io.formater.One2Many

Bases: anyblok.model.Many2Many

Declaration type:
 

Model

Tablename:

io_formater_one2many

Registry name:

Model.IO.Formater.One2Many

Inherit model or mixin:
 
  • <class ‘anyblok.model.Many2Many’>

Blok IO CSV

class anyblok.bloks.io_csv.AnyBlokIOCSV(registry)

Bases: anyblok.blok.Blok

CSV Importer / Exporter behaviour

author = 'Suzanne Jean-Sébastien'
conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
logo = '../anyblok-logo_alpha_256.png'
name = 'anyblok-io-csv'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
required_by = []
version = '0.11.0'

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 Importer:

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

Declaration type:
 Mixin
Inherit model or mixin:
 
field name Description
id
  • Context:
  • Field type - <class ‘anyblok.column.Integer’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • autoincrement - True
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
name
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
class anyblok.bloks.io_csv.mixin.IOCSVMixin

Bases: object

Declaration type:
 Mixin
Inherit model or mixin:
 
field name Description
csv_delimiter
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - ,
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
csv_quotechar
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • nullable - False
  • default - “
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None

importer

class anyblok.bloks.io_csv.importer.Importer

Bases: anyblok.mixin.IOCSVMixin

Declaration type:
 

Model

Tablename:

io_importer

Registry name:

Model.IO.Importer

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.IOCSVMixin’>
field name Description
csv_if_does_not_exist
  • Context:
  • Field type - <class
‘anyblok.column.Selection’>
  • default - create
  • selections:
  • (‘pass’, ‘Pass to the next record’)
  • (‘create’, ‘Create the record’)
  • (‘raise’, ‘Raise an exception’)
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
csv_on_error
  • Context:
  • Field type - <class
‘anyblok.column.Selection’>
  • default - raise_at_the_end
  • selections:
  • (‘raise_now’, ‘Raise now’)
  • (‘raise_at_the_end’, ‘Raise at the end’)
  • (‘ignore’, ‘Ignore and continue’)
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
csv_if_exist
  • Context:
  • Field type - <class
‘anyblok.column.Selection’>
  • default - overwrite
  • selections:
  • (‘pass’, ‘Pass to the next record’)
  • (‘overwrite’, ‘Update the record’)
  • (‘create’, ‘Create another record’)
  • (‘raise’, ‘Raise an exception’)
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
class anyblok.bloks.io_csv.importer.CSV(importer, blokname=None)

Bases: object

Declaration type:
 Model
Tablename:io_importer_csv
Registry name:Model.IO.Importer.CSV
Inherit model or mixin:
 

exporter

class anyblok.bloks.io_csv.exporter.Exporter

Bases: anyblok.mixin.IOCSVMixin

Declaration type:
 

Model

Tablename:

io_exporter

Registry name:

Model.IO.Exporter

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.IOCSVMixin’>
class anyblok.bloks.io_csv.exporter.Field

Bases: anyblok.mixin.IOCSVFieldMixin

Declaration type:
 

Model

Tablename:

io_exporter_field

Registry name:

Model.IO.Exporter.Field

Inherit model or mixin:
 
  • <class ‘anyblok.mixin.IOCSVFieldMixin’>
field name Description
mode
  • Context:
  • Field type - <class ‘anyblok.column.Selection’>
  • nullable - False
  • default - any
  • selections - get_selection
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
exporter
  • Context:
  • model - Model.IO.Exporter
  • Field type - <class ‘anyblok.relationship.Many2One’>
  • info:
  • remote_model - Model.IO.Exporter
  • nullable - False
  • remote_name - fields_to_export
  • Label - None
  • unique - False
  • backref - fields_to_export
  • _remote_columns - None
  • _column_names - None
mapping
  • Context:
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • size - 64
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
class anyblok.bloks.io_csv.exporter.CSV(exporter)

Bases: object

Declaration type:
 Model
Tablename:io_exporter_csv
Registry name:Model.IO.Exporter.CSV
Inherit model or mixin:
 

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

author = 'Suzanne Jean-Sébastien'
conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
logo = '../anyblok-logo_alpha_256.png'
name = 'anyblok-io-xml'
optional_by = []
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
required_by = []
version = '0.11.0'

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 Importer:

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>

In the case of polymorphisme you may use the attribute model on record:

<records
    <record
        ...
        <field name="Many2Many or One2Many">
            ...
            <record model="``polymophic model``">
                ...
                <field name="..." />
                ...
            </record>
            <record model="``another polymophic model``">
                ...
                <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

Declaration type:
 Model
Tablename:io_importer
Registry name:Model.IO.Importer
Inherit model or mixin:
 
class anyblok.bloks.io_xml.importer.XML(importer, blokname=None)

Bases: object

Declaration type:
 Model
Tablename:io_importer_xml
Registry name:Model.IO.Importer.XML
Inherit model or mixin:
 

exporter

class anyblok.bloks.io_xml.exporter.Exporter

Bases: object

Declaration type:
 Model
Tablename:io_exporter
Registry name:Model.IO.Exporter
Inherit model or mixin:
 
class anyblok.bloks.io_xml.exporter.XML(exporter)

Bases: object

Declaration type:
 Model
Tablename:io_exporter_xml
Registry name:Model.IO.Exporter.XML
Inherit model or mixin:
 

Blok Model Auth

class anyblok.bloks.model_authz.ModelBasedAuthorizationBlok(registry)

Bases: anyblok.blok.Blok

author = 'Suzanne Jean-Sébastien'
conditional_by = []
conflicting_by = []
classmethod import_declaration_module()
logo = '../anyblok-logo_alpha_256.png'
name = 'model_authz'
optional_by = []
classmethod reload_declaration_module(reload)
required_by = []
version = '0.11.0'

API doc

class anyblok.bloks.model_authz.models.ModelPermissionGrant

Bases: object

Default model for ModelBasedAuthorizationRule

Declaration type:
 Model
Tablename:authorization_modelpermissiongrant
Registry name:Model.Authorization.ModelPermissionGrant
Inherit model or mixin:
 
field name Description
model
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
permission
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None
principal
  • Context:
  • size - 64
  • Field type - <class ‘anyblok.column.String’>
  • default - <class ‘anyblok.column.NoDefaultValue’>
  • primary_key - True
  • is crypted - False
  • Label - None
  • foreign_key - None
  • DB column name - None