Browse Source

First implementation of data_field and field_data_handler modules

Roland Haroutiounian 9 years ago
parent
commit
51425bc319

+ 30
- 2
lodel/leapi/datahandlers/data_field.py View File

@@ -4,5 +4,33 @@ from .field_data_handler import FieldDataHandler
4 4
 
5 5
 class DataField(FieldDataHandler):
6 6
 
7
-    def __init__(self):
8
-        pass
7
+    ## @brief Instanciates a new fieldtype
8
+    # @param nullable bool : is None allowed as value ?
9
+    # @param uniq bool : Indicates if a field should handle a uniq value
10
+    # @param primary bool : If true the field is a primary key
11
+    # @param internal str|False: if False, that field is not internal. Other values cans be "autosql" or "internal"
12
+    # @param **kwargs : Other arguments
13
+    # @throw NotImplementedError if called from bad class
14
+    def __init__(self, internal=False, nullable=True, uniq=False, primary=False, **kwargs):
15
+        if self.__class__ == DataField:
16
+            raise NotImplementedError("Abstract class")
17
+
18
+        super().__init__(internal, **kwargs)
19
+
20
+        self.nullable = nullable
21
+        self.uniq = uniq
22
+        self.primary = primary
23
+        if 'defaults' in args:
24
+            self.default, error = self.check_data_value(args['default'])
25
+            if error:
26
+                raise error
27
+            del(args['default'])
28
+
29
+    def check_data_value(self, value):
30
+        if value is None:
31
+            if not self.nullable:
32
+                return (None, TypeError("'None' value but field is not nullable"))
33
+
34
+            return (None, None)
35
+        return super().check_data_value(value)
36
+

+ 30
- 2
lodel/leapi/datahandlers/field_data_handler.py View File

@@ -5,9 +5,30 @@ class FieldDataHandler(object):
5 5
 
6 6
     help_text = 'Generic Field Data Handler'
7 7
 
8
+    ## @brief List fields that will be exposed to the construct_data_method
9
+    _construct_datas_deps = []
10
+
8 11
     ## @brief constructor
9
-    def __init__(self):
10
-        pass
12
+    # @param internal False | str : define whether or not a field is internal
13
+    # @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
+    # @param **args
15
+    def __init__(self, internal=False, immutable=False, **args):
16
+        self.internal = internal  # Check this value ?
17
+        self.immutable = bool(immutable)
18
+
19
+        for argname, argval in args.items():
20
+            setattr(self, argname, argval)
21
+
22
+
23
+    ## Fieldtype name
24
+    @staticmethod
25
+    def name(cls):
26
+        return cls.__module__.split('.')[-1]
27
+
28
+    ## @brief checks if a fieldtype is internal
29
+    # @return bool
30
+    def is_internal(self):
31
+        return self.internal != False
11 32
 
12 33
     ## @brief calls the data_field defined _check_data_value() method
13 34
     # @return tuple (value, error|None)
@@ -31,3 +52,10 @@ class FieldDataHandler(object):
31 52
     @staticmethod
32 53
     def module_name(self, fieldtype_name):
33 54
         return 'leapi.datahandlers.data_fields.%s' % fieldtype_name
55
+
56
+    ## @brief __hash__ implementation for fieldtypes
57
+    def __hash__(self):
58
+        hash_dats = [self.__class__.__module__]
59
+        for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
60
+            hash_dats.append((kdic, getattr(self, kdic)))
61
+        return hash(tuple(hash_dats))

Loading…
Cancel
Save