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:
- 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:
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:
- description(request)[source]
Return a translated description.
- Parameters:
request (pyramid.request.Request) – Current request.
- Return type:
- attachments2directory(attachments, directory)[source]
Copy from attachments directory the file corresponding to the user.
- 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:
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:
- classmethod xml2db(dbsession: Session, settings_elt: Element)[source]
Load settings from a XML element.
- Parameters:
dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.
settings_elt (lxml.etree.Element) – Profile XML element.
- 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:
- classmethod tab4view(request: Request, tab_index: int, form: Form) str [source]
Generate the tab content for settings.
- Parameters:
request (pyramid.request.Request) – Current request.
index (int) – Index of the tab.
principals (tuple) – Groups of principals.
- 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:
- 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
orNone
- Returns:
Error message or
None
.
- classmethod record_from_xml(profile_id, profile_elt)[source]
Convert an profile XML element into a dictionary.
- 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
orpyramid.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:
request (pyramid.request.Request) – Current request.
dbprofile (DBProfile) – (optional) Current profile SqlAlchemy object.
- Return type:
- 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
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.
- classmethod get(request, login=None, password=None)[source]
Retrieve a user using
login
andpassword
or the content ofrequest.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 inrequest.params
.password (str) – (optional) Clear password. If it is
None
, we try to find it inrequest.params
.
- Return type:
- Returns:
A tuple like
(dbuser, error)
wheredbuser
is a DBUser object representing the authenticated user orNone
anderror
is an error message.
If
password
isNone
and not inrequest.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 languagetheme
: user themehome
: route of home pageuser
: user dictionary
The user dictionary includes:
user_id
: user IDlogin
: user loginemail
: user e-mailname
: user first name and last nameattachment
: attachment IDprincipals
: 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:
dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.
record (dict) – Dictionary representing the administrator configuration.
- Return type:
None
orpyramid.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
orNone
- 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
orNone
- Returns:
Error message or
None
.
- classmethod record_from_xml(login, user_elt)[source]
Convert an user XML element into a dictionary.
- 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
orpyramid.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:
- 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:
- 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
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
orNone
- 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
orNone
- Returns:
Error message or
None
.
- classmethod record_from_xml(group_id, group_elt)[source]
Convert an user group XML element into a dictionary.
- 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
orpyramid.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:
request (pyramid.request.Request) – Current request.
index (int) – Index of the tab.
form (.lib.form.Form) – Current form object.
user_filter (chrysalio.lib.filter.Filter) – Filter for users.
user_paging (chrysalio.lib.paging.Paging) – Paging for warehouse users.
- 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:
- 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:
request (pyramid.request.Request) – Current request.
tab_index (int) – Index of the tab.
form (.lib.form.Form) – Current form object.
user_filter (chrysalio.lib.filter.Filter) – Filter for users.
user_paging (chrysalio.lib.paging.Paging) – Paging for all users.
profiles (dict) – A dictionary such as
{profile_id: (label, description),...}
.dbgroup (DBGroup) – (optional) Current user group SqlAlchemy object.
- Return type:
helpers.literal.Literal
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 typeonly
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:
- 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 typeonly
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:
- 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 typeonly
are loaded.error_if_exists (bool) – It returns an error if an item already exists.
modules (
collections.OrderedDict
orNone
) – Dictionary of modules to use to complete the loading.
- Return type:
- Returns:
A list of error messages.
- chrysalio.models.populate.db2xml(dbsession, modules=None)[source]
Return a list of XML elements.
- Parameters:
dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.
modules (collections.OrderedDict) – (optional) Dictionary of modules to use to complete the loading.
- Return type:
- chrysalio.models.populate.module_db2xml(dbsession, modules)[source]
Return a list of XML elements from included modules.
- Parameters:
dbsession (sqlalchemy.orm.session.Session) – SQLAlchemy session.
modules (
collections.OrderedDict
orNone
) – Dictionary of modules to use to complete the loading.
- Return type:
- 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 typeonly
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:
- Returns:
An object
Response
orNone
.