Browse Source

Mongodb datasource : added new methods

Roland Haroutiounian 8 years ago
parent
commit
d58168a69f

+ 2
- 8
lodel/datasource/mongodb/datasource.py View File

@@ -38,13 +38,7 @@ class MongoDbDataSource(GenericDataSource):
38 38
     # @return dict
39 39
     # @TODO Change the return value using the Lodel 2 settings module
40 40
     def _get_connection_args(self, connection_name):
41
-        return {
42
-            'host': 'localhost',
43
-            'port': 27017,
44
-            'login': 'login',  # TODO modifier la valeur
45
-            'password': 'password',  # TODO modifier la valeur
46
-            'dbname': 'lodel'
47
-        }
41
+        return {'host': 'localhost', 'port': 28015, 'login': 'lodel_admin', 'password': 'lapwd', 'dbname': 'lodel'}
48 42
 
49 43
     ## @brief checks if the connection args are valid and complete
50 44
     # @param connection_args dict
@@ -59,7 +53,7 @@ class MongoDbDataSource(GenericDataSource):
59 53
         if len(errors) > 0 :
60 54
             raise MongoDbDataSourceError("\r\n-".join(errors))
61 55
 
62
-        return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
56
+        return (connection_args['login'], connection_args['password'], connection_args['host'],
63 57
                 connection_args['port'], connection_args['dbname'])
64 58
 
65 59
     ## @brief returns a selection of documents from the datasource

+ 35
- 3
lodel/datasource/mongodb/migration_handler.py View File

@@ -2,10 +2,14 @@
2 2
 
3 3
 from lodel.datasource.generic.migrationhandler import GenericMigrationHandler
4 4
 from lodel.datasource.mongodb.datasource import MongoDbDataSource
5
+import lodel.datasource.mongodb.utils as utils
5 6
 from lodel.editorial_model.components import EmClass, EmField
7
+
8
+
6 9
 class MigrationHandlerChangeError(Exception):
7 10
     pass
8 11
 
12
+
9 13
 ## @brief Modifies a MongoDb database given editorial model changes
10 14
 class MongoDbMigrationHandler(GenericMigrationHandler):
11 15
 
@@ -19,6 +23,7 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
19 23
             # del conn_args['module']
20 24
 
21 25
         self.db_conn = MongoDbDataSource(self.connection_name)
26
+        self.database = self.db_conn.database
22 27
         # TODO Réimplémenter la partie sur les settings
23 28
         mh_settings = {}
24 29
         self.dryrun = kwargs['dryrun'] if 'dryrun' in kwargs else mh_settings['dryrun']
@@ -34,8 +39,35 @@ class MongoDbMigrationHandler(GenericMigrationHandler):
34 39
     # @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.
35 40
     # @throw MigrationHandlerChangeError if the change was refused
36 41
     def register_change(self, em, uid, initial_state, new_state):
37
-        pass
38
-
42
+        if isinstance(em.classes(uid), EmClass):
43
+            collection_name = utils.object_collection_name(em.classes(uid).display_name)
44
+            if initial_state is None:
45
+                self._create_collection(collection_name)
46
+            elif new_state is None:
47
+                self._delete_collection(collection_name)
39 48
 
40 49
     def _create_default_collections(self, drop_if_exist=False):
41
-        pass
50
+        if_exists = 'drop' if drop_if_exist else 'nothing'
51
+        # Object collection
52
+        collection_name = utils.common_collections['object']
53
+        self._create_collection(collection_name, if_exists=if_exists)
54
+        # TODO triggers ?
55
+        object_collection_name = collection_name
56
+
57
+        # TODO collections de relations
58
+        # TODO clés étrangères
59
+
60
+    def _create_collection(self, collection_name, if_exists='nothing'):
61
+        if if_exists == 'drop':
62
+            if collection_name in self.database.collection_names():
63
+                self.database[collection_name].drop()
64
+        else:
65
+            if collection_name not in self.database.collection_names():
66
+                self.database.create_collection(collection_name)
67
+
68
+        # TODO Relational fields
69
+
70
+    def _delete_collection(self, collection_name):
71
+        # TODO : delete the triggers ?
72
+        if collection_name in self.database.collection_names():
73
+            self.database[collection_name].drop()

+ 5
- 0
lodel/datasource/mongodb/utils.py View File

@@ -2,6 +2,10 @@
2 2
 
3 3
 import pymongo
4 4
 
5
+common_collections = {
6
+    'object': 'object'
7
+}
8
+
5 9
 collection_prefix = {
6 10
     'relation': 'rel_',
7 11
     'collection': 'class_'
@@ -30,6 +34,7 @@ MONGODB_SORT_OPERATORS_MAP = {
30 34
     'DESC': -1
31 35
 }
32 36
 
37
+
33 38
 ## @brief Returns a collection name given a Emclass name
34 39
 # @param class_name str : The class name
35 40
 # @return str

Loading…
Cancel
Save