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
classmethod import_declaration_module()
priority = 0
classmethod reload_declaration_module(reload)
version = '0.3.4'

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
  • Field Type
    • Function
  • Column Types:
    • DateTime
    • Decimal
    • Float
    • Time
    • BigInteger
    • Boolean
    • Date
    • Integer
    • Interval
    • LargeBinary
    • SmallInteger
    • String
    • Text
    • uString
    • uText
    • Selection
    • Json
  • Relationship types
    • One2One
    • Many2One
    • One2Many
    • Many2Many
  • 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",
    prefix="One prefix",
    suffix="One suffix")

Get the next value of the sequence:

sequence.nextval()

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.Paramter.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 ExceptionParamter will raise

Check the key exist:

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

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,
classmethod declare_io()
classmethod import_declaration_module()
classmethod reload_declaration_module(reload)
required = ['anyblok-core']
version = '0.3.4'

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

Blok IO CSV

class anyblok.bloks.io_csv.AnyBlokIOCSV(registry)

Bases: anyblok.blok.Blok

CSV Importer / Exporter behaviour

classmethod import_declaration_module()
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
uninstall()
update(latest_version)
version = '0.3.4'

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
    • update (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

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

classmethod import_declaration_module()
classmethod reload_declaration_module(reload)
required = ['anyblok-io']
uninstall()
update(latest_version)
version = '0.3.4'

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:
    • update (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:
    • update (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>