Browse Source

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 years ago
parent
commit
67bfa2152f
2 changed files with 44 additions and 2 deletions
  1. 41
    2
      lodel/leapi/datahandlers/base_classes.py
  2. 3
    0
      lodel/leapi/datahandlers/exceptions.py

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

2
 
2
 
3
 ## @package lodel.leapi.datahandlers.base_classes Define all base/abstract class for data handlers
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
 import copy
7
 import copy
8
 import importlib
8
 import importlib
14
 LodelContext.expose_modules(globals(), {
14
 LodelContext.expose_modules(globals(), {
15
     'lodel.exceptions': ['LodelException', 'LodelExceptions',
15
     'lodel.exceptions': ['LodelException', 'LodelExceptions',
16
         'LodelFatalError', 'DataNoneValid', 'FieldValidationError'],
16
         'LodelFatalError', 'DataNoneValid', 'FieldValidationError'],
17
+    'lodel.leapi.datahandlers.exceptions': ['LodelDataHandlerConsistencyException', 'LodelDataHandlerException'],
17
     'lodel.logger': 'logger'})
18
     'lodel.logger': 'logger'})
18
 
19
 
19
 
20
 
25
     ##@brief Stores the DataHandler childs classes indexed by name
26
     ##@brief Stores the DataHandler childs classes indexed by name
26
     _base_handlers = None
27
     _base_handlers = None
27
     ##@brief Stores custom datahandlers classes indexed by name
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
     __custom_handlers = dict()
30
     __custom_handlers = dict()
30
 
31
 
31
     help_text = 'Generic Field Data Handler'
32
     help_text = 'Generic Field Data Handler'
335
             logger.warning('Object referenced does not exist')
336
             logger.warning('Object referenced does not exist')
336
             return False
337
             return False
337
         return True
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
 ##@brief This class represent a data_handler for single reference to another object
347
 ##@brief This class represent a data_handler for single reference to another object
357
         #    raise FieldValidationError("List or string expected for a set field")
364
         #    raise FieldValidationError("List or string expected for a set field")
358
         return value
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
 ##@brief This class represent a data_handler for multiple references to another object
380
 ##@brief This class represent a data_handler for multiple references to another object
402
             raise FieldValidationError("MultipleRef have for invalid values [%s]  :" % (",".join(error_list)))
420
             raise FieldValidationError("MultipleRef have for invalid values [%s]  :" % (",".join(error_list)))
403
         return new_val
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
 ## @brief Class designed to handle datas access will fieldtypes are constructing datas
445
 ## @brief Class designed to handle datas access will fieldtypes are constructing datas
407
 #@ingroup lodel2_datahandlers
446
 #@ingroup lodel2_datahandlers

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

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

Loading…
Cancel
Save