Browse Source

Updated lequery.py to use targe_class datasource and not taking it as arguement of execute methods

Yann Weber 8 years ago
parent
commit
3fe63787fb
4 changed files with 60 additions and 22 deletions
  1. BIN
      examples/em_test.pickle
  2. 6
    1
      lodel/editorial_model/components.py
  3. 54
    21
      lodel/leapi/query.py
  4. BIN
      tests/editorial_model.pickle

BIN
examples/em_test.pickle View File


+ 6
- 1
lodel/editorial_model/components.py View File

@@ -58,7 +58,7 @@ class EmClass(EmComponent):
58 58
         super().__init__(uid, display_name, help_text, group)
59 59
         self.abstract = bool(abstract)
60 60
         self.pure_abstract = bool(pure_abstract)
61
-        self.datasource = datasource
61
+        self.__datasource = datasource
62 62
         if self.pure_abstract:
63 63
             self.abtract = True
64 64
         if parents is not None:
@@ -82,6 +82,11 @@ class EmClass(EmComponent):
82 82
             res.update(pfields)
83 83
         res.update(self.__fields)
84 84
         return res
85
+    
86
+    ##@brief RO access to datasource attribute
87
+    @property
88
+    def datasource(self):
89
+        return self.__datasource
85 90
 
86 91
     ##@brief Return the list of all dependencies
87 92
     #

+ 54
- 21
lodel/leapi/query.py View File

@@ -46,15 +46,14 @@ class LeQuery(object):
46 46
         if not issubclass(target_class, LeObject):
47 47
             raise TypeError("target class has to be a child class of LeObject")
48 48
         self._target_class = target_class
49
+        self._datasource = target_class._datasource
49 50
     
50 51
     ##@brief Execute a query and return the result
51 52
     # @param **datas
52 53
     # @return the query result
53 54
     # @see LeQuery.__query()
54 55
     #
55
-    # @note maybe the datasource in not an argument but should be determined
56
-    #elsewhere
57
-    def execute(self, datasource, datas = None):
56
+    def execute(self, datas = None):
58 57
         if len(datas) > 0:
59 58
             self._target_class.check_datas_value(
60 59
                                                     datas,
@@ -65,7 +64,7 @@ class LeQuery(object):
65 64
         LodelHook.call_hook(    self._hook_prefix+'_pre',
66 65
                                 self._target_class,
67 66
                                 datas)
68
-        ret = self.__query(datasource, **datas)
67
+        ret = self.__query(self._datasource, **datas)
69 68
         ret = LodelHook.call_hook(  self._hook_prefix+'_post',
70 69
                                     self._target_class,
71 70
                                     ret)
@@ -88,6 +87,9 @@ class LeQuery(object):
88 87
                             target_class = self._target_class)
89 88
         
90 89
 
90
+##@brief Abstract class handling query with filters
91
+#
92
+#@todo add handling of inter-datasource queries
91 93
 class LeFilteredQuery(LeQuery):
92 94
     
93 95
     ##@brief The available operators used in query definitions
@@ -368,21 +370,22 @@ class LeInsertQuery(LeQuery):
368 370
     
369 371
     ## @brief Implements an insert query operation, with only one insertion
370 372
     # @param **datas : datas to be inserted
371
-    def __query(self, datasource, **datas):
372
-        nb_inserted = datasource.insert(self._target_class,**datas)
373
+    def __query(self, **datas):
374
+        nb_inserted = self._datasource.insert(self._target_class,**datas)
373 375
         if nb_inserted < 0:
374 376
             raise LeQueryError("Insertion error")
375 377
         return nb_inserted
376 378
     ## @brief Implements an insert query operation, with multiple insertions
377 379
     # @param datas : list of **datas to be inserted
378
-    def __query(self, datasource, datas):
379
-        nb_inserted = datasource.insert_multi(self._target_class,datas_list)
380
+    def __query(self, datas):
381
+        nb_inserted = self._datasource.insert_multi(self._target_class,datas_list)
380 382
         if nb_inserted < 0:
381 383
             raise LeQueryError("Multiple insertions error")
382 384
         return nb_inserted
385
+
383 386
     ## @brief Execute the insert query
384
-    def execute(self, datasource, **datas):
385
-        super().execute(datasource, **datas)
387
+    def execute(self, **datas):
388
+        super().execute(self._datasource, **datas)
386 389
         
387 390
 ##@brief A query to update datas for a given object
388 391
 class LeUpdateQuery(LeFilteredQuery):
@@ -397,18 +400,30 @@ class LeUpdateQuery(LeFilteredQuery):
397 400
     # @param **datas : datas to update
398 401
     # @returns the number of updated items
399 402
     # @exception when the number of updated items is not as expected
400
-    def __query(self, datasource, **datas):
403
+    def __query(self, **datas):
401 404
         # select _uid corresponding to query_filter
402
-        l_uids=datasource.select(self._target_class,list(self._target_class.getuid()),query_filter,None, None, None, None, 0, False)
405
+        l_uids=self._datasource.select( self._target_class,
406
+                                        list(self._target_class.getuid()),
407
+                                        query_filter,
408
+                                        None,
409
+                                        None,
410
+                                        None,
411
+                                        None,
412
+                                        0,
413
+                                        False)
403 414
         # list of dict l_uids : _uid(s) of the objects to be updated, corresponding datas
404
-        nb_updated = datasource.update(self._target_class,l_uids, **datas)
415
+        nb_updated = self._datasource.update(   self._target_class,
416
+                                                l_uids,
417
+                                                **datas)
405 418
         if nb_updated != len(l_uids):
406
-            raise LeQueryError("Number of updated items: %d is not as expected: %d " % (nb_updated, len(l_uids)))
419
+            msg = "Number of updated items: %d is not as expected: %d "
420
+            msg %= (nb_updated, len(l_uids))
421
+            raise LeQueryError(msg)
407 422
         return nb_updated
408 423
     
409 424
     ## @brief Execute the update query
410
-    def execute(self, datasource, **datas):
411
-        super().execute(datasource, **datas)
425
+    def execute(self, **datas):
426
+        super().execute(self._datasource, **datas)
412 427
 
413 428
 ##@brief A query to delete an object
414 429
 class LeDeleteQuery(LeFilteredQuery):
@@ -419,19 +434,29 @@ class LeDeleteQuery(LeFilteredQuery):
419 434
         super().__init__(target_class, query_filter)
420 435
 
421 436
     ## @brief Execute the delete query
422
-    def execute(self, datasource):
437
+    def execute(self):
423 438
         super().execute()
424 439
     
425 440
     ##@brief Implements delete query operations
426 441
     # @returns the number of deleted items
427 442
     # @exception when the number of deleted items is not as expected
428
-    def __query(self, datasource):
443
+    def __query(self):
429 444
         # select _uid corresponding to query_filter
430
-        l_uids=datasource.select(self._target_class,list(self._target_class.getuid()),query_filter,None, None, None, None, 0, False)
445
+        l_uids = self._datasource.select(   self._target_class,
446
+                                            list(self._target_class.getuid()),
447
+                                            query_filter,
448
+                                            None,
449
+                                            None,
450
+                                            None,
451
+                                            None,
452
+                                            0,
453
+                                            False)
431 454
         # list of dict l_uids : _uid(s) of the objects to be deleted
432 455
         nb_deleted = datasource.update(self._target_class,l_uids, **datas)
433 456
         if nb_deleted != len(l_uids):
434
-            raise LeQueryError("Number of deleted items %d is not as expected %d " % (nb_deleted, len(l_uids)))
457
+            msg = "Number of deleted items %d is not as expected %d "
458
+            msg %= (nb_deleted, len(l_uids))
459
+            raise LeQueryError(msg)
435 460
         return nb_deleted
436 461
 
437 462
 class LeGetQuery(LeFilteredQuery):
@@ -517,7 +542,15 @@ class LeGetQuery(LeFilteredQuery):
517 542
     # @returns a list containing the item(s)
518 543
     def __query(self, datasource):
519 544
         # select datas corresponding to query_filter
520
-        l_datas=datasource.select(self._target_class,list(self.field_list),self.query_filter,None, self.__order, self.__group, self.__limit, self.offset, False)
545
+        l_datas=datasource.select(  self._target_class,
546
+                                    list(self.field_list),
547
+                                    self.query_filter,
548
+                                    None,
549
+                                    self.__order,
550
+                                    self.__group,
551
+                                    self.__limit,
552
+                                    self.offset,
553
+                                    False)
521 554
         return l_datas
522 555
     
523 556
     ##@return a dict with query infos

BIN
tests/editorial_model.pickle View File


Loading…
Cancel
Save