aiida.backends.djsite package¶
Module with implementation of the database backend using Django.
-
aiida.backends.djsite.
get_scoped_session
()[source]¶ Return a scoped session for the given profile that is exclusively to be used for the QueryBuilder.
Since the QueryBuilder implementation uses SqlAlchemy to map the query onto the models in order to generate the SQL to be sent to the database, it requires a session, which is an
sqlalchemy.orm.session.Session
instance. The only purpose is for SqlAlchemy to be able to connect to the database perform the query and retrieve the results. Even the Django backend implementation will use SqlAlchemy for its QueryBuilder and so also needs an SqlA session. It is important that we do not reuse the scoped session factory in the SqlAlchemy implementation, because that runs the risk of cross-talk once profiles can be switched dynamically in a single python interpreter. Therefore the Django implementation of the QueryBuilder should keep its own SqlAlchemy engine and scoped session factory instances that are used to provide the query builder with a session.- Parameters
profile –
aiida.manage.configuration.profile.Profile
for which to configure the engine.- Returns
sqlalchemy.orm.session.Session
instance with engine configured for the given profile.
-
aiida.backends.djsite.
reset_session
()[source]¶ Reset the session which means setting the global engine and session factory instances to None.
Subpackages¶
Submodules¶
Utilities and configuration of the Django database schema.
-
class
aiida.backends.djsite.manager.
DjangoBackendManager
[source]¶ Bases:
aiida.backends.manager.BackendManager
Class to manage the database schema.
-
__module__
= 'aiida.backends.djsite.manager'¶
-
_migrate_database_generation
()[source]¶ Reset the database schema generation.
For Django we also have to clear the django_migrations table that contains a history of all applied migrations. After clearing it, we reinsert the name of the new initial schema .
-
get_schema_generation_database
()[source]¶ Return the database schema version.
- Returns
distutils.version.StrictVersion with schema version of the database
-
get_schema_version_database
()[source]¶ Return the database schema version.
- Returns
distutils.version.StrictVersion with schema version of the database
-
get_schema_version_reset
(schema_generation_code)[source]¶ Return schema version the database should have to be able to automatically reset to code schema generation.
- Parameters
schema_generation_code – the schema generation of the code.
- Returns
schema version
-
-
class
aiida.backends.djsite.manager.
DjangoSettingsManager
[source]¶ Bases:
aiida.backends.manager.SettingsManager
Class to get, set and delete settings from the DbSettings table.
-
__module__
= 'aiida.backends.djsite.manager'¶
-
delete
(key)[source]¶ Delete the setting with the given key.
- Parameters
key – the key identifying the setting
- Raises
~aiida.common.exceptions.NotExistent if the settings does not exist
-
get
(key)[source]¶ Return the setting with the given key.
- Parameters
key – the key identifying the setting
- Returns
Setting
- Raises
~aiida.common.exceptions.NotExistent if the settings does not exist
-
set
(key, value, description=None)[source]¶ Return the settings with the given key.
- Parameters
key – the key identifying the setting
value – the value for the setting
description – optional setting description
-
table_name
= 'db_dbsetting'¶
-
Django query backend.
-
class
aiida.backends.djsite.queries.
DjangoQueryManager
(backend)[source]¶ Bases:
aiida.backends.general.abstractqueries.AbstractQueryManager
Object that mananges the Django queries.
-
__abstractmethods__
= frozenset({})¶
-
__module__
= 'aiida.backends.djsite.queries'¶
-
_abc_impl
= <_abc_data object>¶
-
get_creation_statistics
(user_pk=None)[source]¶ Return a dictionary with the statistics of node creation, summarized by day, optimized for the Django backend.
- Note
Days when no nodes were created are not present in the returned ctime_by_day dictionary.
- Parameters
user_pk – If None (default), return statistics for all users. If user pk is specified, return only the statistics for the given user.
- Returns
a dictionary as follows:
{ "total": TOTAL_NUM_OF_NODES, "types": {TYPESTRING1: count, TYPESTRING2: count, ...}, "ctime_by_day": {'YYYY-MMM-DD': count, ...}
where in ctime_by_day the key is a string in the format ‘YYYY-MM-DD’ and the value is an integer with the number of nodes created that day.
-
-
aiida.backends.djsite.queries.
get_closest_parents
(pks, *args, **kwargs)[source]¶ Get the closest parents dbnodes of a set of nodes.
- Parameters
pks – one pk or an iterable of pks of nodes
chunk_size – we chunk the pks into groups of this size, to optimize the speed (default=50)
print_progress – print the the progression if True (default=False).
args – additional query parameters
kwargs – additional query parameters
- Returns
a dictionary of the form pk1: pk of closest parent of node with pk1, pk2: pk of closest parent of node with pk2
Note
It works also if pks is a list of nodes rather than their pks
Todo
find a way to always get a parent (when there is one) from each pk. Now, when the same parent has several children in pks, only one of them is kept. This is a BUG, related to the use of a dictionary (children_dict, see below…). For now a work around is to use chunk_size=1.
Django settings for the AiiDA project.
Utility functions specific to the Django backend.