Browse Source

[DataSource] added the utils.object_collection_name method to utils module. Added also the insert and insert_multi methods to the MongoDb datasource class

Roland Haroutiounian 9 years ago
parent
commit
688e15f8e0
2 changed files with 59 additions and 17 deletions
  1. 46
    16
      lodel/datasource/mongodb/datasource.py
  2. 13
    1
      lodel/datasource/mongodb/utils.py

+ 46
- 16
lodel/datasource/mongodb/datasource.py View File

@@ -2,18 +2,10 @@
2 2
 import pymongo
3 3
 from pymongo import MongoClient
4 4
 from pymongo.errors import BulkWriteError
5
-
6 5
 import urllib
7 6
 
8
-# TODO Positionner cette variable dans les settings
9
-DEFAULT_CONNECTION = {
10
-    'host': 'localhost',
11
-    'port': 27017,
12
-    'login': 'login',  # TODO modifier la valeur
13
-    'password': 'password',  # TODO modifier la valeur
14
-    'dbname': 'lodel'
15
-}
16
-
7
+import lodel.datasource.mongodb.utils as utils
8
+from lodel.settings.settings import Settings
17 9
 
18 10
 class MongoDbDataSourceError(Exception):
19 11
     pass
@@ -21,9 +13,12 @@ class MongoDbDataSourceError(Exception):
21 13
 
22 14
 class MongoDbDataSource(object):
23 15
 
24
-    MANDATORY_CONNECTION_ARGS = ('host', 'post', 'login', 'password', 'dbname')
16
+    MANDATORY_CONNECTION_ARGS = ('host', 'port', 'login', 'password', 'dbname')
25 17
 
26
-    def __init__(self, module=pymongo, connection_args=DEFAULT_CONNECTION):
18
+    ## @brief Instanciates a Database object given a connection name
19
+    # @param connection_name str
20
+    def __init__(self, connection_name='default'):
21
+        connection_args = self._get_connection_args(connection_name)
27 22
         login, password, host, port, dbname = MongoDbDataSource._check_connection_args(connection_args)
28 23
 
29 24
         # Creating of the connection
@@ -32,6 +27,19 @@ class MongoDbDataSource(object):
32 27
         # Getting the database
33 28
         self.database = self.connection[dbname]
34 29
 
30
+    ## @brief Gets the settings given a connection name
31
+    # @param connection_name str
32
+    # @return dict
33
+    # @TODO Change the return value using the Lodel 2 settings module
34
+    def _get_connection_args(self, connection_name):
35
+        return {
36
+            'host': 'localhost',
37
+            'port': 27017,
38
+            'login': 'login',  # TODO modifier la valeur
39
+            'password': 'password',  # TODO modifier la valeur
40
+            'dbname': 'lodel'
41
+        }
42
+
35 43
     ## @brief checks if the connection args are valid and complete
36 44
     # @param connection_args dict
37 45
     # @return bool
@@ -48,7 +56,8 @@ class MongoDbDataSource(object):
48 56
         return (connection_args['login'], urllib.quote_plus(connection_args['password']), connection_args['host'],
49 57
                 connection_args['port'], connection_args['dbname'])
50 58
 
51
-    def select(self, target_cls, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0, instanciate=True):
59
+    def select(self, target_cls, field_list, filters, rel_filters=None, order=None, group=None, limit=None, offset=0,
60
+               instanciate=True):
52 61
         pass
53 62
 
54 63
     def delete(self, target_cls, uid):
@@ -57,8 +66,29 @@ class MongoDbDataSource(object):
57 66
     def update(self, target_cls, uid, **datas):
58 67
         pass
59 68
 
69
+    ## @brief Inserts a record in a given collection
70
+    # @param target_cls Emclass : class of the object to insert
71
+    # @param datas dict : datas to insert
72
+    # @return ObjectId : the uid of the inserted record
60 73
     def insert(self, target_cls, **datas):
61
-        pass
62
-
74
+        collection_name = utils.object_collection_name(target_cls.__class__)
75
+        collection = self.database[collection_name]
76
+        result = collection.insert_one(datas)
77
+        return result.inserted_id
78
+
79
+    ## @brief Inserts a list of records in a given collection
80
+    # @param target_cls Emclass : class of the objects inserted
81
+    # @param datas_list
82
+    # @return int : Number of inserted records in the collection
63 83
     def insert_multi(self, target_cls, datas_list):
64
-        pass
84
+        collection_name = utils.object_collection_name(target_cls.__class__)
85
+        collection = self.database[collection_name]
86
+        bulk = collection.initialize_ordered_bulk_op()
87
+        for datas_list_item in datas_list:
88
+            bulk.insert(datas_list_item)
89
+        try:
90
+            result = bulk.execute()
91
+        except BulkWriteError as bwe:
92
+            pass  # TODO add the behavior in case of an exception => bwe.details['writeErrors'], is a list of error info dicts
93
+
94
+        return result['nInserted']

+ 13
- 1
lodel/datasource/mongodb/utils.py View File

@@ -1 +1,13 @@
1
-# -*- coding: utf-8 -*-
1
+# -*- coding: utf-8 -*-
2
+
3
+collection_prefix = {
4
+    'relation': 'rel_',
5
+    'collection': 'class_'
6
+}
7
+
8
+
9
+## @brief Returns a collection name given a Emclass name
10
+# @param class_name str : The class name
11
+# @return str
12
+def object_collection_name(class_name):
13
+    return ("%s%s" % (collection_prefix['object'], class_name)).lower()

Loading…
Cancel
Save