Browse Source

MultipleRef datahandlers bugfixes

In construct and check data
Yann Weber 8 years ago
parent
commit
78e3dca30b
2 changed files with 16 additions and 35 deletions
  1. 6
    30
      lodel/leapi/datahandlers/base_classes.py
  2. 10
    5
      lodel/leapi/datahandlers/references.py

+ 6
- 30
lodel/leapi/datahandlers/base_classes.py View File

@@ -399,41 +399,17 @@ class MultipleRef(Reference):
399 399
                 new_val.append(v)
400 400
             except (FieldValidationError) as f:
401 401
                 error_list.append(repr(v))
402
-        logger.warning("Provisoire pour faire les tests, à fixer")
403
-        if (len(error_list) > 0):
402
+        if len(error_list) > 0:
404 403
             raise FieldValidationError("MultipleRef have for invalid values [%s]  :" % (",".join(error_list)))
405 404
         return new_val
406
-
405
+    
406
+    ##@brief Construct a multiple ref data
407 407
     def construct_data(self, emcomponent, fname, datas, cur_value):
408 408
         cur_value = super().construct_data(emcomponent, fname, datas, cur_value)
409
-        if cur_value == 'None' or cur_value is None or cur_value == '':
410
-            return None
411
-        emcomponent_fields = emcomponent.fields()
412
-        data_handler = None
413
-        if fname in emcomponent_fields:
414
-            data_handler = emcomponent_fields[fname]
415
-        u_fname = emcomponent.uid_fieldname()
416
-        uidtype = emcomponent.field(u_fname[0]) if isinstance(u_fname, list) else emcomponent.field(u_fname)
417
-
418
-        if isinstance(cur_value, str):
419
-            value = cur_value.split(',')
420
-            l_value = [uidtype.cast_type(uid) for uid in value]
421
-        elif isinstance(cur_value, list):
422
-            l_value = list()
423
-            for value in cur_value:
424
-                if isinstance(value,str):
425
-                    l_value.append(uidtype.cast_type(value))
426
-                elif isinstance(value,uidtype.cast_type):
427
-                    l_value.append(value)
428
-                else:
429
-                    raise ValueError("The items must be of the same type, string or %s" % (emcomponent.__name__))
430
-        else:
431
-            l_value = None
432
-
433
-        if l_value is not None:
409
+        if cur_value is not None:
434 410
             if self.back_reference is not None:
435 411
                 br_class = self.back_reference[0]
436
-                for br_id in l_value:
412
+                for br_id in cur_value:
437 413
                     query_filters = list()
438 414
                     query_filters.append((br_class.uid_fieldname()[0], '=', br_id))
439 415
                     br_obj = br_class.get(query_filters)
@@ -443,7 +419,7 @@ class MultipleRef(Reference):
443 419
                             br_list = list()
444 420
                         if br_id not in br_list:
445 421
                             br_list.append(br_id)
446
-        return l_value
422
+        return cur_value
447 423
     
448 424
     ## @brief Checks the backreference, updates it if it is not complete
449 425
     # @param emcomponent EmComponent : An EmComponent child class instance

+ 10
- 5
lodel/leapi/datahandlers/references.py View File

@@ -27,8 +27,11 @@ class List(MultipleRef):
27 27
     #@return value
28 28
     def _check_data_value(self, value):
29 29
         value = super()._check_data_value(value)
30
-        if not(isinstance(value, list) or isinstance(value, str)):
31
-            raise FieldValidationError("List or string expected for a set field")
30
+        try:
31
+            return list(value)
32
+        except Exception as e:
33
+            raise FieldValidationError("Given iterable is not castable in \
34
+a list : %s" % e)
32 35
         return value
33 36
 
34 37
 ##@brief Child class of MultipleRef where references are represented in the form of a python set
@@ -51,9 +54,11 @@ class Set(MultipleRef):
51 54
     #@return value
52 55
     def _check_data_value(self, value):
53 56
         value = super()._check_data_value(value)
54
-        if not (isinstance(value, set) or isinstance(value, str)):
55
-            raise FieldValidationError("Set or string expected for a set field")
56
-        return value
57
+        try:
58
+            return set(value)
59
+        except Exception as e:
60
+            raise FieldValidationError("Given iterable is not castable in \
61
+a set : %s" % e)
57 62
     
58 63
 ##@brief Child class of MultipleRef where references are represented in the form of a python dict
59 64
 class Map(MultipleRef):

Loading…
Cancel
Save