mirror of
https://github.com/yweber/lodel2.git
synced 2026-10-31 16:51:32 +01:00
Mongodb datasource : added new methods
This commit is contained in:
parent
c439b6d929
commit
d58168a69f
3 changed files with 42 additions and 11 deletions
|
|
@ -38,13 +38,7 @@ class MongoDbDataSource(GenericDataSource):
|
||||||
# @return dict
|
# @return dict
|
||||||
# @TODO Change the return value using the Lodel 2 settings module
|
# @TODO Change the return value using the Lodel 2 settings module
|
||||||
def _get_connection_args(self, connection_name):
|
def _get_connection_args(self, connection_name):
|
||||||
return {
|
return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
|
||||||
'host': 'localhost',
|
|
||||||
'port': 27017,
|
|
||||||
'login': 'login', # TODO modifier la valeur
|
|
||||||
'password': 'password', # TODO modifier la valeur
|
|
||||||
'dbname': 'lodel'
|
|
||||||
}
|
|
||||||
|
|
||||||
## @brief checks if the connection args are valid and complete
|
## @brief checks if the connection args are valid and complete
|
||||||
# @param connection_args dict
|
# @param connection_args dict
|
||||||
|
|
@ -59,7 +53,7 @@ class MongoDbDataSource(GenericDataSource):
|
||||||
if len(errors) > 0 :
|
if len(errors) > 0 :
|
||||||
raise MongoDbDataSourceError("\r\n-".join(errors))
|
raise MongoDbDataSourceError("\r\n-".join(errors))
|
||||||
|
|
||||||
return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
|
return (connection_args['login'], connection_args['password'], connection_args['host'],
|
||||||
connection_args['port'], connection_args['dbname'])
|
connection_args['port'], connection_args['dbname'])
|
||||||
|
|
||||||
## @brief returns a selection of documents from the datasource
|
## @brief returns a selection of documents from the datasource
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,14 @@
|
||||||
|
|
||||||
from lodel.datasource.generic.migrationhandler import GenericMigrationHandler
|
from lodel.datasource.generic.migrationhandler import GenericMigrationHandler
|
||||||
from lodel.datasource.mongodb.datasource import MongoDbDataSource
|
from lodel.datasource.mongodb.datasource import MongoDbDataSource
|
||||||
|
import lodel.datasource.mongodb.utils as utils
|
||||||
from lodel.editorial_model.components import EmClass, EmField
|
from lodel.editorial_model.components import EmClass, EmField
|
||||||
|
|
||||||
|
|
||||||
class MigrationHandlerChangeError(Exception):
|
class MigrationHandlerChangeError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
## @brief Modifies a MongoDb database given editorial model changes
|
## @brief Modifies a MongoDb database given editorial model changes
|
||||||
class MongoDbMigrationHandler(GenericMigrationHandler):
|
class MongoDbMigrationHandler(GenericMigrationHandler):
|
||||||
|
|
||||||
|
|
@ -19,6 +23,7 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
|
||||||
# del conn_args['module']
|
# del conn_args['module']
|
||||||
|
|
||||||
self.db_conn = MongoDbDataSource(self.connection_name)
|
self.db_conn = MongoDbDataSource(self.connection_name)
|
||||||
|
self.database = self.db_conn.database
|
||||||
# TODO Réimplémenter la partie sur les settings
|
# TODO Réimplémenter la partie sur les settings
|
||||||
mh_settings = {}
|
mh_settings = {}
|
||||||
self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
|
self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
|
||||||
|
|
@ -34,8 +39,35 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
|
||||||
# @param new_state dict|None : dict with field name as key and field value as value. Represents the new state. None means it's a component deletion.
|
# @param new_state dict|None : dict with field name as key and field value as value. Represents the new state. None means it's a component deletion.
|
||||||
# @throw MigrationHandlerChangeError if the change was refused
|
# @throw MigrationHandlerChangeError if the change was refused
|
||||||
def register_change(self, em, uid, initial_state, new_state):
|
def register_change(self, em, uid, initial_state, new_state):
|
||||||
pass
|
if isinstance(em.classes(uid), EmClass):
|
||||||
|
collection_name = utils.object_collection_name(em.classes(uid).display_name)
|
||||||
|
if initial_state is None:
|
||||||
|
self._create_collection(collection_name)
|
||||||
|
elif new_state is None:
|
||||||
|
self._delete_collection(collection_name)
|
||||||
|
|
||||||
def _create_default_collections(self, drop_if_exist=False):
|
def _create_default_collections(self, drop_if_exist=False):
|
||||||
pass
|
if_exists = 'drop' if drop_if_exist else 'nothing'
|
||||||
|
# Object collection
|
||||||
|
collection_name = utils.common_collections['object']
|
||||||
|
self._create_collection(collection_name, if_exists=if_exists)
|
||||||
|
# TODO triggers ?
|
||||||
|
object_collection_name = collection_name
|
||||||
|
|
||||||
|
# TODO collections de relations
|
||||||
|
# TODO clés étrangères
|
||||||
|
|
||||||
|
def _create_collection(self, collection_name, if_exists='nothing'):
|
||||||
|
if if_exists == 'drop':
|
||||||
|
if collection_name in self.database.collection_names():
|
||||||
|
self.database[collection_name].drop()
|
||||||
|
else:
|
||||||
|
if collection_name not in self.database.collection_names():
|
||||||
|
self.database.create_collection(collection_name)
|
||||||
|
|
||||||
|
# TODO Relational fields
|
||||||
|
|
||||||
|
def _delete_collection(self, collection_name):
|
||||||
|
# TODO : delete the triggers ?
|
||||||
|
if collection_name in self.database.collection_names():
|
||||||
|
self.database[collection_name].drop()
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
|
|
||||||
|
common_collections = {
|
||||||
|
'object': 'object'
|
||||||
|
}
|
||||||
|
|
||||||
collection_prefix = {
|
collection_prefix = {
|
||||||
'relation': 'rel_',
|
'relation': 'rel_',
|
||||||
'collection': 'class_'
|
'collection': 'class_'
|
||||||
|
|
@ -30,6 +34,7 @@ MONGODB_SORT_OPERATORS_MAP = {
|
||||||
'DESC': -1
|
'DESC': -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
## @brief Returns a collection name given a Emclass name
|
## @brief Returns a collection name given a Emclass name
|
||||||
# @param class_name str : The class name
|
# @param class_name str : The class name
|
||||||
# @return str
|
# @return str
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue