datahelper module

These classes can be used to create a functioning database instance for your Altinn3 statistic.

It is intended to be a shortcut for quickly getting an editing application up and running, and serve as an example for those making their own setup.

If you’d rather make your own setup, you are free to do so.

It also contains a utility to fill your database with demo-data.

class Base(**kwargs)

Bases: DeclarativeBase

Sets up the Base class for the SQLAlchemy ORM.

Parameters:

kwargs (Any)

metadata: ClassVar[MetaData] = MetaData()

Refers to the _schema.MetaData collection that will be used for new _schema.Table objects.

See also

orm_declarative_metadata

registry: ClassVar[_RegistryType] = <sqlalchemy.orm.decl_api.registry object>

Refers to the _orm.registry in use where new _orm.Mapper objects will be associated.

class DatabaseBuilderAltinnEimerdb(database_name, storage_location, periods)

Bases: object

This class provides help for creating an eimerdb datastorage for Altinn3 surveys.

It provides the recommended tables which makes sure your datastructure is compatible with the framework.

To use this class for building your storage follow these steps: 1. Create an instance of the class.

db_builder = DatabaseBuilderAltinnEimerdb(

database_name = “my-database-name”, storage_location = “path/to/storage”, periods = “year” # can be list for more time identifiers

)

  1. Now that we have our builder ready, check that the schemas are correct.

    print(db_builder.schemas)

  2. Assuming that the schemas are correct, you can now build the eimerdb.

    db_builder.build_storage()

  3. Your database is now done! The only thing that remains is to insert your data into the storage.

Note

You can add your own custom ‘skjemadata’ tables if you so wish, just make sure their names start with ‘skjemadata_’. It is possible to use this functionality to create your data storage even if your data are not from Altinn, but you might need to adapt your data.

Parameters:
  • database_name (str)

  • storage_location (str)

  • periods (str | list[str])

build_storage()

Calling this method builds your eimerdb storage with the supplied name at the specified location and creates partitions for the defined periods.

Return type:

None

class Datatyper(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘datatyper’ table.

aar
datatype
radnr
tabell
variabel
class DemoDataCreator(engine)

Bases: object

Class for creating demo data to test the editing framework.

Assumes your database is set up according to the template with only ‘aar’ as period columns.

Parameters:

engine (Engine) – An SQLAlchemy Engine to connect to an existing database.

build_demo_database()

Builds the demo database by inserting data into each table in order using the engine connection.

Return type:

None

get_data()

Gathers open data to be used for creating the demo database.

Return type:

None

get_enheter()

Builds the content for the ‘enheter’ table.

Return type:

None

get_enhetsinfo()

Builds the content for the ‘enhetsinfo’ table.

Return type:

None

get_kontaktinfo()

Builds the content for the ‘kontaktinfo’ table.

Return type:

None

get_skjemadata_hoved()

Builds the content for the ‘skjemadata_hoved’ table.

Return type:

None

get_skjemamottak()

Builds the content for the ‘skjemamottak’ table.

Return type:

None

insert_to_db(table_name, dataframe)

Uses self.engine to insert data.

Return type:

None

Parameters:
  • table_name (str)

  • dataframe (DataFrame)

random_date()

Assigns a random date.

Return type:

str

class Enheter(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘enheter’ table.

aar
ident
skjema
class Enhetsinfo(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘enhetsinfo’ table.

aar
ident
variabel
verdi
class Kontaktinfo(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘kontaktinfo’ table.

aar
bekreftet_kontaktinfo
epost
ident
kommentar_kontaktinfo
kommentar_krevende
kontaktperson
refnr
skjema
telefon
class Kontroller(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘kontroller’ table.

aar
kontrollid
kontrolltype
kontrollvariabel
skildring
skjema
sorteringsvariabel
class Kontrollutslag(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘kontrollutslag’ table.

aar
ident
kontrollid
refnr
skjema
utslag
verdi
class Skjemadata_hoved(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘skjemadata_hoved’ table.

aar
ident
refnr
skjema
variabel
verdi
class Skjemamottak(**kwargs)

Bases: Base

SQLAlchemy ORM model for the ‘skjemamottak’ table.

aar
aktiv
dato_mottatt
editert
ident
kommentar
refnr
skjema
create_database(engine)

Uses engine to create the database.

This is only a simple wrapper to make it easier to run ‘Base.metadata.create_all(engine)’

Parameters:

engine (Engine) – An SQLAlchemy Engine to be used for creating the database.

Return type:

None

create_database_engine(database_type, *args, **kwargs)

Wrapper for creating an sqlalchemy engine.

For sqlite requires the argument ‘sqlite_path’ to complete the path after ‘sqlite:///’

Return type:

Engine

Parameters:
  • database_type (str)

  • args (Any)

  • kwargs (Any)

For postgres requires the environment variables:
  • DB_USER

  • DB_PASSWORD

  • DB_HOST

  • DB_PORT

  • DB_NAME

period_columns()

Generate Column definitions for period dimensions based on the value of ‘PERIOD_COUMNS’.

Return type:

dict[str, Column[Any]]