Kaynağa Gözat

Bugfixes on backref updates on deletion refs #131

Yann Weber 8 yıl önce
ebeveyn
işleme
aa8c5ee56c
1 değiştirilmiş dosya ile 33 ekleme ve 24 silme
  1. 33
    24
      plugins/mongodb_datasource/datasource.py

+ 33
- 24
plugins/mongodb_datasource/datasource.py Dosyayı Görüntüle

@@ -212,8 +212,8 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
212 212
         #Updating backref before deletion
213 213
         self.__update_backref_filtered(target, filters, relational_filters,
214 214
             None)
215
-        res = self.__collection(target).delete_many(mongo_filters)
216
-        return res.deleted_count
215
+        res = self.__collection(target).remove(mongo_filters)
216
+        return res['n']
217 217
 
218 218
     ##@brief updates records according to given filters
219 219
     #@param target Emclass : class of the object to insert
@@ -246,8 +246,6 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
246 246
         res = self.__collection(target).update(mongo_filters, mongo_arg)
247 247
         return res['n']
248 248
 
249
-        
250
-
251 249
     ## @brief Inserts a record in a given collection
252 250
     # @param target Emclass : class of the object to insert
253 251
     # @param new_datas dict : datas to insert
@@ -255,10 +253,12 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
255 253
     def insert(self, target, new_datas):
256 254
         logger.debug("Insert called on %s with datas : %s"% (
257 255
             target, new_datas))
258
-        res = self.__collection(target).insert(new_datas)
259 256
         uidname = target.uid_fieldname()[0] #MULTIPLE UID BROKEN HERE
260
-        leores = list(self.__collection(target).find({'_id':res}))[0]
261
-        self.__update_backref(target, leores[uidname], None, new_datas) 
257
+        if uidname not in new_datas:
258
+            raise MongoDataSourceError("Missing UID data will inserting a new \
259
+%s" % target.__class__)
260
+        res = self.__collection(target).insert(new_datas)
261
+        self.__update_backref(target, new_datas[uidname], None, new_datas) 
262 262
         return str(res)
263 263
 
264 264
     ## @brief Inserts a list of records in a given collection
@@ -276,14 +276,21 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
276 276
     #@param target leObject child class
277 277
     #@param filters
278 278
     #@param relational_filters,
279
-    #@param datas None | dict : optional new datas if None mean we are deleting
279
+    #@param new_datas None | dict : optional new datas if None mean we are deleting
280 280
     #@return nothing (for the moment
281
-    def __update_backref_filtered(self, target, act,
282
-            filters, relational_filters, datas = None):
283
-        #gathering datas
284
-        old_datas_l = target.get(target, None, filters, relational_filters)
281
+    def __update_backref_filtered(self, target,
282
+            filters, relational_filters, new_datas = None):
283
+        #Getting all the UID of the object that will be deleted in order
284
+        #to update back_references
285
+        mongo_filters = self.__process_filters(
286
+            target, filters, relational_filters)
287
+        old_datas_l = self.__collection(target).find(
288
+            mongo_filters)
289
+        old_datas_l = list(old_datas_l)
290
+        uidname = target.uid_fieldname()[0] #MULTIPLE UID BROKEN HERE
285 291
         for old_datas in old_datas_l:
286
-            self.__update_backref(target, old_datas, datas)
292
+            self.__update_backref(
293
+                target, old_datas[uidname], old_datas, new_datas)
287 294
 
288 295
     ##@brief Update back references of an object
289 296
     #@ingroup plugin_mongodb_bref_op
@@ -357,7 +364,7 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
357 364
                                 for val in new_values
358 365
                                 if val not in old_values]
359 366
                 elif oldd and not newd:
360
-                    to_del = [old_datas[fname]]
367
+                    to_del = old_datas[fname]
361 368
                     to_add = []
362 369
                 elif not oldd and newd:
363 370
                     to_del = []
@@ -456,26 +463,26 @@ abstract, preparing reccursiv calls" % (target, filters, relational_filters))
456 463
         if issubclass(bref_dh.__class__, MultipleRef):
457 464
             if oldd and newdd:
458 465
                 if tuid not in bref_val:
459
-                    raise MongodbConsistencyError("The value we want to \
466
+                    raise MongoDbConsistencyError("The value we want to \
460 467
 delete in this back reference update was not found in the back referenced \
461
-object : %s field %s. Value was : '%s'" % (bref_leo, ref_fname, tuid))
468
+object : %s. Value was : '%s'" % (bref_leo, tuid))
462 469
                 return bref_val
463 470
             elif oldd and not newdd:
464 471
                 #deletion
465 472
                 old_value = values['old']
466 473
                 if tuid not in bref_val:
467
-                    raise MongodbConsistencyError("The value we want to \
474
+                    raise MongoDbConsistencyError("The value we want to \
468 475
 delete in this back reference update was not found in the back referenced \
469
-object : %s field %s. Value was : '%s'" % (bref_leo, ref_fname, tuid))
476
+object : %s. Value was : '%s'" % (bref_leo, tuid))
470 477
                 if isinstance(bref_val, set):
471 478
                     bref_val -= set([tuid])
472 479
                 else:
473 480
                     del(bref_val[bref_val.index(tuid)])
474 481
             elif not oldd and newdd:
475 482
                 if tuid in bref_val:
476
-                    raise MongodbConsistencyError("The value we want to \
483
+                    raise MongoDbConsistencyError("The value we want to \
477 484
 add in this back reference update was found in the back referenced \
478
-object : %s field %s. Value was : '%s'" % (bref_leo, ref_fname, tuid))
485
+object : %s. Value was : '%s'" % (bref_leo, tuid))
479 486
                 if isinstance(bref_val, set):
480 487
                     bref_val |= set([tuid])
481 488
                 else:
@@ -484,16 +491,16 @@ object : %s field %s. Value was : '%s'" % (bref_leo, ref_fname, tuid))
484 491
             #Single value backref
485 492
             if oldd and newdd:
486 493
                 if bref_val != tuid:
487
-                    raise MongodbConsistencyError("The backreference doesn't \
488
-have expected value. Expected was %s but found %s in '%s' field of %s" % (
489
-                        tuid, bref_val, bref_fname, bref_leo))
494
+                    raise MongoDbConsistencyError("The backreference doesn't \
495
+have expected value. Expected was %s but found %s in %s" % (
496
+                        tuid, bref_val, bref_leo))
490 497
                 return bref_val
491 498
             elif oldd and not newdd:
492 499
                 #deletion
493 500
                 if not hasattr(bref_dh, "default"): 
494 501
                     raise MongoDbConsistencyError("Unable to delete a \
495 502
 value for a back reference update. The concerned field don't have a default \
496
-value : in %s field %s" % (bref_leo, ref_fname))
503
+value : in %s field %s" % (bref_leo, bref_fname))
497 504
                 bref_val = getattr(bref_dh, "default")
498 505
             elif not oldd and newdd:
499 506
                 bref_val = tuid
@@ -708,6 +715,8 @@ is not a reference : '%s' field '%s'" % (bref_leo, bref_fname))
708 715
         # We are going to regroup relationnal filters by reference field
709 716
         # then by collection
710 717
         rfilters = dict()
718
+        if relational_filters is None:
719
+            relational_filters = []
711 720
         for (fname, rfields), op, value in relational_filters:
712 721
             if fname not in rfilters:
713 722
                 rfilters[fname] = dict()

Loading…
İptal
Kaydet