Model Reference

The models Module

Database main objects, functions and constants.

chrysalio.models.includeme(configurator)[source]

Initialize the model for a Chrysalio application.

Parameters:

configurator (pyramid.config.Configurator) – Object used to do configuration declaration within the application.

chrysalio.models.get_dbengine(settings, prefix='sqlalchemy.')[source]

Get SQLAlchemy engine.

Parameters:
  • settings (pyramid.registry.Registry.settings) – Application settings.

  • prefix (str) – (default=’sqlalchemy’) Prefix in settings for SQLAlchemy configuration.

Return type:

sqlalchemy.engine.base.Engine

chrysalio.models.get_dbsession_factory(dbengine)[source]

Get SQLAlchemy session factory.

Parameters:

dbengine (sqlalchemy.engine.base.Engine) – Database engine.

Return type:

sqlalchemy.orm.session.sessionmaker()

chrysalio.models.get_tm_dbsession(dbsession_factory, transaction_manager)[source]

Get a sqlalchemy.orm.Session instance backed by a transaction.

Parameters:
  • dbsession_factory (sqlalchemy.orm.session.sessionmaker) – Function to create session.

  • transaction_manager (transaction._manager.ThreadTransactionManager) – Transaction manager.

Return type:

sqlalchemy.orm.session.Session

This function will hook the session to the transaction manager which will take care of committing any changes.

  • When using pyramid_tm it will automatically be committed or aborted depending on whether an exception is raised.

  • When using scripts you should wrap the session in a manager yourself.

    For example:

    import transaction
    
    dbsession_factory = get_dbsession_factory(DB_METADATA.bind)
    with transaction.manager:
        dbsession = get_tm_dbsession(
            dbsession_factory, transaction.manager)
    
chrysalio.models.add_column(table_class, column)[source]

Add a column to a database table.

Parameters:
  • table_class (DBDeclarativeClass) – SQLAlchemy object of the table to proceed.

  • column (sqlalchemy.schema.Column) – Column to create.

The models.dbbase Module

Database main objects, functions and constants.

class chrysalio.models.dbbase.DBBaseClass[source]

Base class for prime SQLAlchemy table.

label(request)[source]

Return a translated label.

Parameters:

request (pyramid.request.Request) – Current request.

Return type:

str

description(request)[source]

Return a translated description.

Parameters:

request (pyramid.request.Request) – Current request.

Return type:

str

attachments2directory(attachments, directory)[source]

Copy from attachments directory the file corresponding to the user.

Parameters:
  • attachments (str) – Absolute path to the attachments directory.

  • directory (str) – The backup directory.

classmethod settings_tabs(request)[source]

Return a tuple of tab labels to view or edit the object.

Parameters:

request (pyramid.request.Request) – Current request.

Return type:

tuple

The models.dbsettings Module

SQLAlchemy-powered model definitions for settings.

class chrysalio.models.dbsettings.DBSettings(**kwargs)[source]

SQLAlchemy-powered settings class.

classmethod exists(dbsession)[source]

Check if table settings exists and has at least one item.

Parameters:

dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session (for testing).

Return type:

bool

classmethod xml2db(dbsession: Session, settings_elt: Element)[source]

Load settings from a XML element.

Parameters:
classmethod db2xml(dbsession: Session) etree.Element | None[source]

Serialize settings to a XML representation.

Parameters:

dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

Return type:

lxml.etree.Element

classmethod db2dict(settings: dict, dbsession: Session, default_email: str) dict[source]

Return a dictionary containing settings.

Parameters:
  • settings (dict) – Pyramid settings.

  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session (for testing).

  • default_email (str) – Default email for the site, usually the administrator email.

Return type:

dict

classmethod tab4view(request: Request, tab_index: int, form: Form) str[source]

Generate the tab content for settings.

Parameters:
Return type:

helpers.literal.Literal

classmethod settings_schema(request: Request) tuple[SchemaNode, dict][source]

Return a Colander schema to edit settings.

Parameters:

request (pyramid.request.Request) – Current request.

Return type:

tuple

Returns:

A tuple such as (schema, defaults).

classmethod tab4edit(request: Request, tab_index: int, form: Form) str[source]

Generate the tab content for edition.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • tab_index (int) – Index of the tab.

  • form (.lib.form.Form) – Current form object.

Return type:

helpers.literal.Literal

The models.dbprofile Module

SQLAlchemy-powered model definitions for profiles.

class chrysalio.models.dbprofile.DBProfile(**kwargs)[source]

SQLAlchemy-powered profile class.

classmethod xml2db(dbsession, profile_elt, error_if_exists=True, kwargs=None)[source]

Load a profile from a XML element.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • profile_elt (lxml.etree.Element) – Profile XML element.

  • error_if_exists (bool) – (default=True) It returns an error if profile already exists.

  • kwargs (dict) – (optional) Dictionary of keyword arguments.

Return type:

pyramid.i18n.TranslationString or None

Returns:

Error message or None.

classmethod record_from_xml(profile_id, profile_elt)[source]

Convert an profile XML element into a dictionary.

Parameters:
  • profile_id (str) – Profile ID.

  • profile_elt (lxml.etree.Element) – Profile XML element.

Return type:

dict

classmethod record_format(record)[source]

Check and possibly correct a record before inserting it in the database.

Parameters:

record (dict) – Dictionary of values to check.

Return type:

None or pyramid.i18n.TranslationString

Returns:

None or error message.

db2xml(dbsession=None)[source]

Serialize an profile to a XML representation.

Parameters:

dbsession (sqlalchemy.orm.session.Session) – (optional) SQLAlchemy session.

Return type:

lxml.etree.Element

tab4view(request, tab_index, form)[source]

Generate the tab content of a profile.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • index (int) – Index of the tab.

  • form (.lib.form.Form) – Current form object.

Return type:

helpers.literal.Literal

classmethod settings_schema(request, dbprofile=None)[source]

Return a Colander schema to edit profile.

Parameters:
Return type:

tuple

Returns:

A tuple such as (schema, defaults).

classmethod tab4edit(request, tab_index, form, dbprofile=None)[source]

Generate the tab content of profile for edition.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • tab_index (int) – Index of the tab.

  • form (.lib.form.Form) – Current form object.

  • dbprofile (DBProfile) – (optional) Current profile SqlAlchemy object.

Return type:

helpers.literal.Literal

class chrysalio.models.dbprofile.DBProfilePrincipal(**kwargs)[source]

SQLAlchemy-powered profile permission class (one-to-many).

The models.dbuser Module

SQLAlchemy-powered model definitions for users.

class chrysalio.models.dbuser.DBUser(**kwargs)[source]

SQLAlchemy-powered user model.

set_password(password)[source]

Set the password, possibly hashing it.

Parameters:

password (str) – Password to set. If it does not begin with $, we use bcrypt algorithm before setting.

check_password(password)[source]

Check the validy of the given password.

Parameters:

password (str) – Clear password to check.

Return type:

bool

classmethod get(request, login=None, password=None)[source]

Retrieve a user using login and password or the content of request.params.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • login (str) – (optional) Login of the user to authenticate. If it is None, we try to find it in request.params.

  • password (str) – (optional) Clear password. If it is None, we try to find it in request.params.

Return type:

tuple

Returns:

A tuple like (dbuser, error) where dbuser is a DBUser object representing the authenticated user or None and error is an error message.

If password is None and not in request.params, password checking is not performed.

If the user is authenticated, it updates last_login field in database.

set_session(request)[source]

Set up user session (session['user']).

Parameters:

request (pyramid.request.Request) – Current request.

It saves in session the following values:

  • lang: user language

  • theme: user theme

  • home: route of home page

  • user: user dictionary

The user dictionary includes:

  • user_id: user ID

  • login: user login

  • email: user e-mail

  • name: user first name and last name

  • attachment: attachment ID

  • principals: list of principals (premission groups) (see “Pyramid Security” for more information)

classmethod load_administrator(dbsession, record)[source]

Load the administrator user from INI configuration file.

Parameters:
Return type:

None or pyramid.i18n.TranslationString

Returns:

None or error message.

classmethod xml2db(dbsession, user_elt, error_if_exists=True, kwargs=None)[source]

Load a user from a XML element.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • user_elt (lxml.etree.Element) – User XML element.

  • error_if_exists (bool) – (default=True) It returns an error if user already exists.

  • kwargs (dict) – (optional) Dictionary of keyword arguments with the key 'profiles'.

Return type:

pyramid.i18n.TranslationString or None

Returns:

Error message or None.

xml2db_extra(dbsession, user_elt, kwargs)[source]

Load extra information on a user from a XML element.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • user_elt (lxml.etree.Element) – User XML element.

  • kawrgs (dict) – Dictionary of keyword arguments with the key 'profiles'.

Return type:

pyramid.i18n.TranslationString or None

Returns:

Error message or None.

classmethod record_from_xml(login, user_elt)[source]

Convert an user XML element into a dictionary.

Parameters:
  • login (str) – User login.

  • user_elt (lxml.etree.Element) – User XML element.

Return type:

dict

classmethod record_format(record)[source]

Check and possibly correct a record before inserting it in the database.

Parameters:

record (dict) – Dictionary of values to check.

Return type:

None or pyramid.i18n.TranslationString

Returns:

None or error message.

classmethod record_convert_dates(record)[source]

Possibly convert dates of a record.

Parameters:

record (dict) – Dictionary of values to check.

db2xml(dbsession=None)[source]

Serialize a user to a XML representation.

Parameters:

dbsession (sqlalchemy.orm.session.Session) – (optional) SQLAlchemy session.

Return type:

lxml.etree.Element

db2xml_extra(user_elt)[source]

Serialize depedencies of a user to a XML representation.

Parameters:

user_elt (lxml.etree.Element) – User XML element.

classmethod paging_filter(request, form, user_filter, user_paging)[source]

Filter for users.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • form (.lib.form.Form) – Current form object.

  • user_filter (.lib.filter.Filter) – Filter for users.

  • user_paging (.lib.paging.Paging) – Paging for users.

Return type:

str

tab4view(request, tab_index, form)[source]

Generate the tab content of user account.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • index (int) – Index of the tab.

  • form (.lib.form.Form) – Current form object.

Return type:

helpers.literal.Literal

classmethod settings_schema(request, profiles, groups, dbuser=None)[source]

Return a Colander schema to edit user account.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • profiles (dict) – A dictionary such as {profile_id: (label, description),...}.

  • groups (dict) – A dictionary such as {group_id: (label, description),...}.

  • dbuser (DBUser) – (optional) Current user SqlAlchemy object.

Return type:

tuple

Returns:

A tuple such as (schema, defaults).

classmethod tab4edit(request, tab_index, form, profiles, groups, dbuser=None)[source]

Generate the tab content of user account for edition.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • tab_index (int) – Index of the tab.

  • form (.lib.form.Form) – Current form object.

  • profiles (dict) – A dictionary such as {profile_id: (label, description),...}.

  • groups (dict) – A dictionary such as {group_id: (label, description),...}.

  • dbuser (DBUser) – (optional) Current user SqlAlchemy object.

Return type:

helpers.literal.Literal

class chrysalio.models.dbuser.DBUserProfile(**kwargs)[source]

Class to link users with their profiles (many-to-many).

The models.dbgroup Module

SQLAlchemy-powered model definitions for user groups.

class chrysalio.models.dbgroup.DBGroup(**kwargs)[source]

SQLAlchemy-powered user group class.

classmethod xml2db(dbsession, group_elt, error_if_exists=True, kwargs=None)[source]

Load a user group from a XML element.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • group_elt (lxml.etree.Element) – User group XML element.

  • error_if_exists (bool) – (default=True) It returns an error if user group already exists.

  • kwargs (dict) – (optional) Dictionary of keyword arguments.

Return type:

pyramid.i18n.TranslationString or None

Returns:

Error message or None.

xml2db_extra(dbsession, group_elt, kwargs)[source]

Load extra information on a group from a XML element.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • group_elt (lxml.etree.Element) – Group XML element.

  • kawrgs (dict) – Dictionary of keyword arguments with the key 'profiles'.

Return type:

pyramid.i18n.TranslationString or None

Returns:

Error message or None.

classmethod record_from_xml(group_id, group_elt)[source]

Convert an user group XML element into a dictionary.

Parameters:
  • group_id (str) – User group ID.

  • group_elt (lxml.etree.Element) – Group XML element.

Return type:

dict

classmethod record_format(record)[source]

Check and possibly correct a record before inserting it in the database.

Parameters:

record (dict) – Dictionary of values to check.

Return type:

None or pyramid.i18n.TranslationString

Returns:

None or error message.

db2xml(dbsession)[source]

Serialize an user group to a XML representation.

Parameters:

dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

Return type:

lxml.etree.Element

tab4view(request, tab_index, form, user_filter, user_paging)[source]

Generate the tab content of a user group.

Parameters:
Return type:

helpers.literal.Literal

classmethod settings_schema(request, defaults, profiles, dbgroup=None)[source]

Return a Colander schema to edit user group.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • defaults (dict) – Default values for the form set by the user paging object.

  • profiles (dict) – A dictionary such as {profile_id: (label, description),...}.

  • dbgroup (DBGroup) – (optional) Current user group SqlAlchemy object.

Return type:

tuple

Returns:

A tuple such as (schema, defaults).

classmethod tab4edit(request, tab_index, form, user_filter, user_paging, profiles, dbgroup=None)[source]

Generate the tab content of user group for edition.

Parameters:
Return type:

helpers.literal.Literal

class chrysalio.models.dbgroup.DBGroupUser(**kwargs)[source]

Class to link groups with their authorized users (many-to-many).

class chrysalio.models.dbgroup.DBGroupProfile(**kwargs)[source]

Class to link groups with their profiles (many-to-many).

The models.populate Module

Function to import and export database from and into XML files.

chrysalio.models.populate.xml2db(dbsession, tree, only=None, error_if_exists=True, modules=None)[source]

Load an XML configuration file into the database.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • tree (lxml.etree.ElementTree) – Content of the XML document.

  • only (str) – (optional) If not None, only the items of type only are loaded.

  • error_if_exists (bool) – (default=True) It returns an error if an item already exists.

  • modules (collections.OrderedDict) – (optional) Dictionary of modules to use to complete the loading.

Return type:

list

Returns:

A list of error messages.

chrysalio.models.populate.element2db(dbsession, tree, only, error_if_exists, element)[source]

Load XML elements into the database according to an XPath.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • tree (lxml.etree.ElementTree) – Content of the configuration file.

  • only (str) – If not None, only the items of type only are loaded.

  • error_if_exists (bool) – It returns an error if an item already exists.

  • element (dict) – A dictionary with keys tag, class and, possibly, grouptag, kwargs, relaxng, representing the element to insert into the database.

Return type:

list

Returns:

A list of error messages.

chrysalio.models.populate.module_xml2db(dbsession, tree, only, error_if_exists, modules)[source]

Load elements from included modules.

Parameters:
  • dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.

  • tree (lxml.etree.ElementTree) – Content of the configuration file.

  • only (str) – If not None, only the items of type only are loaded.

  • error_if_exists (bool) – It returns an error if an item already exists.

  • modules (collections.OrderedDict or None) – Dictionary of modules to use to complete the loading.

Return type:

list

Returns:

A list of error messages.

chrysalio.models.populate.db2xml(dbsession, modules=None)[source]

Return a list of XML elements.

Parameters:
Return type:

list

chrysalio.models.populate.module_db2xml(dbsession, modules)[source]

Return a list of XML elements from included modules.

Parameters:
Return type:

list

chrysalio.models.populate.web2db(request, _xml2db, only=None, relaxngs=None, error_if_exists=True)[source]

Read XML or ZIP files from Web and load them into the database.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • _xml2db – Function to load an XML configuration file.

  • only (str) – (optional) If not None, only the items of type only are loaded.

  • relaxngs (dict) – (optional) Dictionary of extra Relax NG files.

  • error_if_exists (bool) – (default=True) It returns an error if an item already exists.

chrysalio.models.populate.db2web(request, dbitems, filename, relaxng=None)[source]

Convert SqlAlchemy items into an XML file and its attachments embedded in a Pyramid response.

Parameters:
  • request (pyramid.request.Request) – Current request.

  • dbitems (list) – A list of SqlAlchemy objects with a method db2xml() and a method attachments2directory().

  • filename (str) – The name of the downloaded file.

  • relaxng (dict) – (optional) A dictionary describing the Relax NG used by func:.lib.xml.create_entire_xml.

Return type:

pyramid.response.Response

Returns:

An object Response or None.