Browse Source

Bugfixes on backreference handling

Yann Weber 7 years ago
parent
commit
740d0c1167
1 changed files with 20 additions and 11 deletions
  1. 20
    11
      plugins/mongodb_datasource/datasource.py

+ 20
- 11
plugins/mongodb_datasource/datasource.py View File

@@ -222,12 +222,7 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
222 222
     #@param upd_datas dict : datas to update (new values)
223 223
     #@return int : Number of updated records
224 224
     def update(self, target, filters, relational_filters, upd_datas):
225
-        for dname in upd_datas:
226
-            if isinstance(upd_datas[dname], set):
227
-                #pymongo raises :
228
-                #bson.errors.InvalidDocument: Cannot encode object: {...}
229
-                #with sets
230
-                upd_datas[dname] = list(upd_datas[dname])
225
+        self._data_cast(upd_datas)
231 226
         res = self.__update_no_backref(target, filters, relational_filters,
232 227
             upd_datas)
233 228
         self.__update_backref_filtered(target, filters, relational_filters,
@@ -257,6 +252,7 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
257 252
     # @param new_datas dict : datas to insert
258 253
     # @return the inserted uid
259 254
     def insert(self, target, new_datas):
255
+        self._data_cast(new_datas)
260 256
         logger.debug("Insert called on %s with datas : %s"% (
261 257
             target, new_datas))
262 258
         uidname = target.uid_fieldname()[0] #MULTIPLE UID BROKEN HERE
@@ -272,6 +268,8 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
272 268
     # @param datas_list list : list of dict
273 269
     # @return list : list of the inserted records' ids
274 270
     def insert_multi(self, target, datas_list):
271
+        for datas in datas_list:
272
+            self._data_cast(datas)
275 273
         res = self.__collection(target).insert_many(datas_list)
276 274
         for new_datas in datas_list:
277 275
             self.__update_backref(target, None, new_datas) 
@@ -525,13 +523,9 @@ value : in %s field %s" % (bref_leo, bref_fname))
525 523
     #subclass (major failure)
526 524
     def __bref_get_check(self, bref_cls, uidv, bref_fname):
527 525
         bref_leo = bref_cls.get_from_uid(uidv)
528
-        if len(bref_leo) == 0:
526
+        if bref_leo is None:
529 527
             raise MongoDbConsistencyError("Unable to get the object we make \
530 528
 reference to : %s with uid = %s" % (bref_cls, repr(uidv)))
531
-        if len(bref_leo) > 1:
532
-            raise MongoDbConsistencyFatalError("Will retrieving data with UID \
533
-as filter we got more than one result !!! Bailout !!!")
534
-        bref_leo = bref_leo[0]
535 529
         bref_dh = bref_leo.data_handler(bref_fname)
536 530
         if not isinstance(bref_dh, Reference):
537 531
             raise LodelFatalError("Found a back reference field that \
@@ -836,3 +830,18 @@ field/operator couple in a query. We will keep only the first one")
836 830
             1 if (a[fname]>b[fname] if cmpdir == 'ASC' else a[fname]<b[fname])\
837 831
             else -1)
838 832
 
833
+    
834
+    ##@brief Correct some datas before giving them to pymongo
835
+    #
836
+    #For example sets has to be casted to lise
837
+    #@param datas
838
+    #@return datas
839
+    @classmethod
840
+    def _data_cast(cls, datas):
841
+        for dname in datas:
842
+            if isinstance(datas[dname], set):
843
+                #pymongo raises :
844
+                #bson.errors.InvalidDocument: Cannot encode object: {...}
845
+                #with sets
846
+                datas[dname] = list(datas[dname])
847
+        return datas

Loading…
Cancel
Save