Browse Source

First implementation of the data_fields type and of the field_data_handler

Roland Haroutiounian 8 years ago
parent
commit
2f85642769

+ 1
- 1
lodel/leapi/datahandlers/data_field.py View File

@@ -15,7 +15,7 @@ class DataField(FieldDataHandler):
15 15
         if self.__class__ == DataField:
16 16
             raise NotImplementedError("Abstract class")
17 17
 
18
-        super().__init__(internal, **kwargs)
18
+        super(self.__class__, self).__init__(internal, **kwargs)
19 19
 
20 20
         self.nullable = nullable
21 21
         self.uniq = uniq

+ 1
- 1
lodel/leapi/datahandlers/data_fields/__init__.py View File

@@ -1 +1 @@
1
-__author__ = 'roland'
1
+

+ 2
- 2
lodel/leapi/datahandlers/data_fields/bool.py View File

@@ -11,9 +11,9 @@ class EmDataField(DataField):
11 11
     def __init__(self, **kwargs):
12 12
         if 'check_data_value' not in kwargs:
13 13
             kwargs['check_data_value'] = self.check_value
14
-        super(Bool, self).__init__(ftype='bool', **kwargs)
14
+        super(self.__class__, self).__init__(ftype='bool', **kwargs)
15 15
 
16
-    def check_value(self, value):
16
+    def _check_data_value(self, value):
17 17
         error = None
18 18
         try:
19 19
             value = bool(value)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/datetime.py View File

@@ -13,4 +13,4 @@ class EmDataField(DataField):
13 13
     def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
14 14
         self.now_on_update = now_on_update
15 15
         self.now_on_create = now_on_create
16
-        super(Datetime, self).__init__(**kwargs)
16
+        super(self.__class__, self).__init__(**kwargs)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/file.py View File

@@ -9,4 +9,4 @@ class EmDataField(DataField):
9 9
     # @param **kwargs
10 10
     def __init__(self, upload_path=None, **kwargs):
11 11
         self.upload_path = upload_path
12
-        super(File, self).__init__(ftype='file', **kwargs)
12
+        super(self.__class__, self).__init__(ftype='file', **kwargs)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/format.py View File

@@ -15,4 +15,4 @@ class EmDataField(VarcharDataField):
15 15
     def __init__(self, format_string, field_list, max_length, **kwargs):
16 16
         self._field_list = field_list
17 17
         self._format_string = format_string
18
-        super().__init__(internal='automatic', max_length=max_length)
18
+        super(self.__class__, self).__init__(internal='automatic', max_length=max_length)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/integer.py View File

@@ -8,7 +8,7 @@ class EmDataField(DataField):
8 8
     ftype = 'int'
9 9
 
10 10
     def __init__(self, **kwargs):
11
-        super(Integer, self).__init__(ftype='int', **kwargs)
11
+        super(self.__class__, self).__init__(ftype='int', **kwargs)
12 12
 
13 13
     def _check_data_value(self, value):
14 14
         error = None

+ 1
- 1
lodel/leapi/datahandlers/data_fields/regexvarchar.py View File

@@ -15,7 +15,7 @@ class EmDataField(VarcharDataField):
15 15
         self.regex = regex
16 16
         self.compiled_re = re.compile(regex)  # trigger an error if invalid regex
17 17
 
18
-        super().__init__(max_length=max_length, **kwargs)
18
+        super(self.__class__, self).__init__(max_length=max_length, **kwargs)
19 19
 
20 20
     def _check_data_value(self, value):
21 21
         error = None

+ 2
- 2
lodel/leapi/datahandlers/data_fields/text.py View File

@@ -6,5 +6,5 @@ class EmDataField(DataField):
6 6
     help = 'A text field (big string)'
7 7
     ftype = 'text'
8 8
 
9
-    def __init__(self):
10
-        super(Text, self).__init__(ftype='text', **kwargs)
9
+    def __init__(self, **kwargs):
10
+        super(self.__class__, self).__init__(ftype='text', **kwargs)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/uid.py View File

@@ -11,7 +11,7 @@ class EmDataField(IntegerDataField):
11 11
     def __init__(self, is_id_class, **kwargs):
12 12
         self._is_id_class = is_id_class
13 13
         kwargs['internal'] = 'automatic'
14
-        super().__init__(is_id_class=is_id_class, **kwargs)
14
+        super(self.__class__, self).__init__(is_id_class=is_id_class, **kwargs)
15 15
 
16 16
     def _check_data_value(self, value):
17 17
         return (value, None)

+ 1
- 1
lodel/leapi/datahandlers/data_fields/varchar.py View File

@@ -9,4 +9,4 @@ class EmDataField(DataField):
9 9
     # @brief max_length int: The maximum length of this field
10 10
     def __init__(self, max_length=64, **kwargs):
11 11
         self.max_length = int(max_length)
12
-        super().__init__(**kwargs)
12
+        super(self.__class__, self).__init__(**kwargs)

+ 35
- 1
lodel/leapi/datahandlers/field_data_handler.py View File

@@ -1,6 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2 2
 import importlib
3 3
 
4
+
4 5
 class FieldDataHandler(object):
5 6
 
6 7
     help_text = 'Generic Field Data Handler'
@@ -12,7 +13,11 @@ class FieldDataHandler(object):
12 13
     # @param internal False | str : define whether or not a field is internal
13 14
     # @param immutable bool : indicates if the fieldtype has to be defined in child classes of LeObject or if it is designed globally and immutable
14 15
     # @param **args
16
+    # @throw NotImplementedError if it is instanciated directly
15 17
     def __init__(self, internal=False, immutable=False, **args):
18
+        if self.__class__ == GenericFieldType:
19
+            raise NotImplementedError("Abstract class")
20
+
16 21
         self.internal = internal  # Check this value ?
17 22
         self.immutable = bool(immutable)
18 23
 
@@ -28,7 +33,7 @@ class FieldDataHandler(object):
28 33
     ## @brief checks if a fieldtype is internal
29 34
     # @return bool
30 35
     def is_internal(self):
31
-        return self.internal != False
36
+        return self.internal is not False
32 37
 
33 38
     ## @brief calls the data_field defined _check_data_value() method
34 39
     # @return tuple (value, error|None)
@@ -38,6 +43,35 @@ class FieldDataHandler(object):
38 43
     def _check_data_value(self, value):
39 44
         return (value, None)
40 45
 
46
+    ## @brief Build field value
47
+    # @param emcomponent EmComponent : An EmComponent child class instance
48
+    # @param fname str : The field name
49
+    # @param datas dict : dict storing fields values (from the component)
50
+    # @param cur_value : the value from the current field (identified by fieldname)
51
+    def construct_data(self, emcomponent, fname, datas, cur_value):
52
+        emcomponent_fields = emcomponent.fields()
53
+        fname_data_handler = None
54
+        if fname in emcomponent_fields:
55
+            fname_data_handler = FieldDataHandler.from_name(emcomponent_fields[fname])
56
+
57
+        if fname in datas.keys():
58
+            return cur_value
59
+        elif fname_data_handler is not None and hasattr(fname_data_handler, 'default'):
60
+                return fname_data_handler.default
61
+        elif fname_data_handler is not None and fname_data_handler.nullable:
62
+                return None
63
+
64
+        raise RuntimeError("Unable to construct data for field %s", fname)
65
+
66
+    ## @brief Check datas consistency
67
+    # @param emcomponent EmComponent : An EmComponent child class instance
68
+    # @param fname : the field name
69
+    # @param datas dict : dict storing fields values
70
+    # @return an Exception instance if fails else True
71
+    # @todo A implémenter
72
+    def check_data_consistency(self, emcomponent, fname, datas):
73
+        return True
74
+
41 75
     ## @brief given a field type name, returns the associated python class
42 76
     # @param fieldtype_name str : A field type name
43 77
     # @return DataField child class

Loading…
Cancel
Save