Browse Source

Rewriting CRUD methods of datasources

- Now delete & update methods use filters & rel_filters
Yann Weber 8 years ago
parent
commit
5708194875
2 changed files with 26 additions and 37 deletions
  1. 1
    0
      lodel/leapi/query.py
  2. 25
    37
      plugins/mongodb_datasource/main.py

+ 1
- 0
lodel/leapi/query.py View File

@@ -33,6 +33,7 @@ class LeQueryError(Exception):
33 33
             )
34 34
         return msg
35 35
 
36
+##@todo check datas when running query
36 37
 class LeQuery(object):
37 38
     
38 39
     ##@brief Hookname prefix

+ 25
- 37
plugins/mongodb_datasource/main.py View File

@@ -16,7 +16,6 @@ from .utils import mongodbconnect, object_collection_name, MONGODB_SORT_OPERATOR
16 16
 class MongoDbDataSourceError(Exception):
17 17
     pass
18 18
 
19
-
20 19
 class MongoDbDatasource(object):
21 20
     
22 21
     ##@brief Mapping from lodel2 operators to mongodb operator
@@ -95,53 +94,42 @@ class MongoDbDatasource(object):
95 94
 
96 95
     ##@brief Deletes one record defined by its uid
97 96
     #@param target Emclass : class of the record to delete
98
-    #@param uid dict|list : a dictionary of fields and values composing the 
99
-    # unique identifier of the record or a list of several dictionaries
97
+    #@param filters list : List of filters
98
+    #@param relational_filters list : List of relational filters
100 99
     #@return int : number of deleted records
101
-    #@TODO Implement the error management
102
-    def delete(self, target, uid):
103
-        if isinstance(uid, dict):
104
-            uid = [uid]
105
-        collection_name = object_collection_name(target)
106
-        collection = self.database[collection_name]
107
-        result = collection.delete_many(uid)
108
-        return result.deleted_count
100
+    def delete(self, target, filters, relational_filters):
101
+        mongo_filters = self.__process_filters(
102
+            target, filters, relational_filters)
103
+        res = self.__collection(target).delete_many(mongo_filters)
104
+        return res.deleted_count
109 105
 
110 106
     ## @brief updates one or a list of records
111
-    # @param target Emclass : class of the object to insert
112
-    # @param uids list : list of uids to update
113
-    # @param datas dict : datas to update (new values)
114
-    # @return int : Number of updated records
115
-    # @todo check if the values need to be parsed
116
-    def update(self, target, uids, **datas):
117
-        if not isinstance(uids, list):
118
-            uids = [uids]
119
-        collection_name = object_collection_name(target)
120
-        collection = self.database[collection_name]
121
-        results = collection.update_many({'uid': {'$in': uids}}, datas)
122
-        return results.modified_count()
107
+    #@param target Emclass : class of the object to insert
108
+    #@param filters list : List of filters
109
+    #@param rel_filters list : List of relational filters
110
+    #@param upd_datas dict : datas to update (new values)
111
+    #@return int : Number of updated records
112
+    def update(self, target, filters, relational_filters, upd_datas):
113
+        mongo_filters = self.__process_filters(
114
+            target, filters, relational_filters)
115
+        res = self.__collection(target).update_many(mongo_filters, upd_datas)
116
+        return res.modified_count()
123 117
 
124 118
     ## @brief Inserts a record in a given collection
125 119
     # @param target Emclass : class of the object to insert
126
-    # @param datas dict : datas to insert
127
-    # @return bool
128
-    # @TODO Implement the error management
129
-    def insert(self, target, **datas):
130
-        collection_name = object_collection_name(target)
131
-        collection = self.database[collection_name]
132
-        result = collection.insert_one(datas)
133
-        return len(result.inserted_id)
120
+    # @param new_datas dict : datas to insert
121
+    # @return the inserted uid
122
+    def insert(self, target, new_datas):
123
+        res = self.__collection(target).insert_one(new_datas)
124
+        return res.inserted_id
134 125
 
135 126
     ## @brief Inserts a list of records in a given collection
136 127
     # @param target Emclass : class of the objects inserted
137
-    # @param datas_list
128
+    # @param datas_list list : list of dict
138 129
     # @return list : list of the inserted records' ids
139
-    # @TODO Implement the error management
140 130
     def insert_multi(self, target, datas_list):
141
-        collection_name = object_collection_name(target)
142
-        collection = self.database[collection_name]
143
-        result = collection.insert_many(datas_list)
144
-        return len(result.inserted_ids)
131
+        res = self.__collection.insert_many(datas_list)
132
+        return list(result.inserted_ids)
145 133
     
146 134
     ##@brief Return a pymongo collection given a LeObject child class
147 135
     #@param leobject LeObject child class (no instance)

Loading…
Cancel
Save