Sfoglia il codice sorgente

Implements new methods for reference datahandlers

get_referenced method were implemented for SingleReference and MultipleReference class
They are able to return LeObject instances refereced by them
Yann Weber 8 anni fa
parent
commit
67bfa2152f

+ 41
- 2
lodel/leapi/datahandlers/base_classes.py Vedi File

@@ -2,7 +2,7 @@
2 2
 
3 3
 ## @package lodel.leapi.datahandlers.base_classes Define all base/abstract class for data handlers
4 4
 #
5
-# Contains custom exceptions too
5
+# Contains custom exceptions too
6 6
 
7 7
 import copy
8 8
 import importlib
@@ -14,6 +14,7 @@ from lodel.context import LodelContext
14 14
 LodelContext.expose_modules(globals(), {
15 15
     'lodel.exceptions': ['LodelException', 'LodelExceptions',
16 16
         'LodelFatalError', 'DataNoneValid', 'FieldValidationError'],
17
+    'lodel.leapi.datahandlers.exceptions': ['LodelDataHandlerConsistencyException', 'LodelDataHandlerException'],
17 18
     'lodel.logger': 'logger'})
18 19
 
19 20
 
@@ -25,7 +26,7 @@ class DataHandler(object):
25 26
     ##@brief Stores the DataHandler childs classes indexed by name
26 27
     _base_handlers = None
27 28
     ##@brief Stores custom datahandlers classes indexed by name
28
-    # @todo do it ! (like plugins, register handlers... blablabla)
29
+    # @todo do it ! (like plugins, register handlers... blablabla)
29 30
     __custom_handlers = dict()
30 31
 
31 32
     help_text = 'Generic Field Data Handler'
@@ -335,6 +336,12 @@ class Reference(DataHandler):
335 336
             logger.warning('Object referenced does not exist')
336 337
             return False
337 338
         return True
339
+    
340
+    ##@brief Utility method designed to fetch referenced objects
341
+    #@param value mixed : the field value
342
+    #@throw NotImplementedError
343
+    def get_referenced(self, value):
344
+        raise NotImplementedError
338 345
 
339 346
 
340 347
 ##@brief This class represent a data_handler for single reference to another object
@@ -357,6 +364,17 @@ class SingleRef(Reference):
357 364
         #    raise FieldValidationError("List or string expected for a set field")
358 365
         return value
359 366
 
367
+    ##@brief Utility method designed to fetch referenced objects
368
+    #@param value mixed : the field value
369
+    #@return A LeObject child class instance
370
+    #@throw LodelDataHandlerConsistencyException if no referenced object found
371
+    def get_referenced(self, value):
372
+        for leo_cls in self.linked_classes:
373
+            res = leo_cls.get_from_uid(value)
374
+            if res is not None:
375
+                return res
376
+        raise LodelDataHandlerConsistencyException("Unable to find \
377
+referenced object with uid %s" % value)
360 378
 
361 379
 
362 380
 ##@brief This class represent a data_handler for multiple references to another object
@@ -402,6 +420,27 @@ class MultipleRef(Reference):
402 420
             raise FieldValidationError("MultipleRef have for invalid values [%s]  :" % (",".join(error_list)))
403 421
         return new_val
404 422
 
423
+    ##@brief Utility method designed to fetch referenced objects
424
+    #@param value mixed : the field value
425
+    #@return A list of LeObject child class instance
426
+    #@throw LodelDataHandlerConsistencyException if some referenced objects
427
+    #were not found
428
+    def get_referenced(self, values):
429
+        if values is None or len(values) == 0:
430
+            return list()
431
+        left = set(values)
432
+        values = set(values)
433
+        res = list()
434
+        for leo_cls in self.linked_classes:
435
+            uidname = leo_cls.uid_fieldname()[0] #MULTIPLE UID BROKEN HERE
436
+            tmp_res = leo_cls.get(('%s in (%s)' % (uidname, ','.join(
437
+                [str(l) for l in left]))))
438
+            left ^= set(( leo.uid() for leo in tmp_res))
439
+            res += tmp_res
440
+            if len(left) == 0:
441
+                return res
442
+        raise LodelDataHandlerConsistencyException("Unable to find \
443
+some referenced objects. Followinf uid were not found : %s" % ','.join(left))
405 444
 
406 445
 ## @brief Class designed to handle datas access will fieldtypes are constructing datas
407 446
 #@ingroup lodel2_datahandlers

+ 3
- 0
lodel/leapi/datahandlers/exceptions.py Vedi File

@@ -1,2 +1,5 @@
1 1
 def LodelDataHandlerException(Exception):
2 2
     pass
3
+
4
+def LodelDataHandlerConsistencyException(LodelDataHandlerException):
5
+    pass

Loading…
Annulla
Salva