Browse Source

New version of EmFieldtype

Now fieldtypes are not derivated from EmField anymore
Yann Weber 9 years ago
parent
commit
5cd8e140bd

+ 3
- 6
EditorialModel/fieldtypes/bool.py View File

@@ -1,16 +1,13 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.fields import EmField
3
+from EditorialModel.fieldtypes.generic import GenericFieldType
4 4
 
5
+class EmFieldBool(GenericFieldType):
5 6
 
6
-class EmFieldBool(EmField):
7
-
8
-    ftype = 'bool'
9 7
     help = 'A basic boolean field'
10 8
 
11 9
     ## @brief A char field
12 10
     # @brief max_length int : The maximum length of this field
13 11
     def __init__(self, **kwargs):
14
-        super(EmFieldBool, self).__init__(**kwargs)
12
+        super(EmFieldBool, self).__init__(ftype='bool',**kwargs)
15 13
 
16
-fclass = EmFieldBool

+ 3
- 6
EditorialModel/fieldtypes/char.py View File

@@ -1,17 +1,14 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.fields import EmField
3
+from EditorialModel.fieldtypes.generic import GenericFieldType
4 4
 
5
+class EmFieldChar(GenericFieldType):
5 6
 
6
-class EmFieldChar(EmField):
7
-
8
-    ftype = 'char'
9 7
     help = 'Basic string (varchar) field. Take max_length=64 as option'
10 8
 
11 9
     ## @brief A char field
12 10
     # @brief max_length int : The maximum length of this field
13 11
     def __init__(self, max_length=64, **kwargs):
14 12
         self.max_length = max_length
15
-        super(EmFieldChar, self).__init__(**kwargs)
13
+        super(EmFieldChar, self).__init__(ftype = 'char', **kwargs)
16 14
 
17
-fclass = EmFieldChar

+ 1
- 4
EditorialModel/fieldtypes/datetime.py View File

@@ -5,8 +5,6 @@ from EditorialModel.fields import EmField
5 5
 
6 6
 class EmFieldDatetime(EmField):
7 7
 
8
-    ftype = 'datetime'
9
-
10 8
     help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
11 9
 
12 10
     ## @brief A datetime field
@@ -15,6 +13,5 @@ class EmFieldDatetime(EmField):
15 13
     def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
16 14
         self.now_on_update = now_on_update
17 15
         self.now_on_create = now_on_create
18
-        super(EmFieldDatetime, self).__init__(**kwargs)
16
+        super(EmFieldDatetime, self).__init__(ftype='datetime',**kwargs)
19 17
 
20
-fclass = EmFieldDatetime

+ 1
- 3
EditorialModel/fieldtypes/file.py View File

@@ -5,13 +5,11 @@ from EditorialModel.fields import EmField
5 5
 
6 6
 class EmFieldFile(EmField):
7 7
 
8
-    ftype = 'file'
9 8
     help = 'A file field. With one options upload_path'
10 9
 
11 10
     ## @brief A char field
12 11
     # @brief max_length int : The maximum length of this field
13 12
     def __init__(self, upload_path=None, **kwargs):
14 13
         self.upload_path = upload_path
15
-        super(EmFieldFile, self).__init__(**kwargs)
14
+        super(EmFieldFile, self).__init__(ftype='char',**kwargs)
16 15
 
17
-fclass = EmFieldFile

+ 82
- 0
EditorialModel/fieldtypes/generic.py View File

@@ -0,0 +1,82 @@
1
+#-*- coding: utf-8 -*-
2
+
3
+import types
4
+
5
+## @brief Abstract class representing a fieldtype
6
+class GenericFieldType(object):
7
+    
8
+    ## @brief Text describing the fieldtype
9
+    help = 'Generic field type : abstract class for every fieldtype'
10
+    ## @brief Allowed type for handled datas
11
+    _allowed_ftype = ['char', 'str', 'int', 'bool', 'datetime', 'text', 'rel2type']
12
+    
13
+    ## @brief Instanciate a new fieldtype
14
+    # @param ftype str : The type of datas handled by this fieldtype
15
+    # @param default ? : The default value
16
+    # @param nullable bool : is None allowed as value ?
17
+    # @param check_function function : A callback check function that takes 1 argument and raise a TypeError if the validation fails
18
+    # @param **kwargs dict : Other arguments
19
+    # @throw NotImplementedError if called directly
20
+    # @throw AttributeError if bad ftype
21
+    # @throw AttributeError if bad check_function
22
+    def __init__(self, ftype, default = None, nullable = False, check_function = None, **kwargs):
23
+        if self.__class__ == GenericFieldType:
24
+            raise NotImplementedError("Abstract class")
25
+        
26
+        if ftype not in self._allowed_ftype:
27
+            raise AttributeError("Ftype '%s' not known"%ftype)
28
+        
29
+        if check_function is None:
30
+            check_function = self.dummy_check
31
+        elif not isinstance(check_function, types.FunctionType):
32
+            raise AttributeError("check_function argument has to be a function")
33
+
34
+        self.ftype = ftype
35
+        self.check_function = check_function
36
+        self.nullalble = bool(nullable)
37
+
38
+        self.check_or_raise(default)
39
+        self.default = default
40
+
41
+        for argname,argvalue in kwargs.items():
42
+            setattr(self, argname, argvalue)
43
+    
44
+    ## @brief Check if a value is correct
45
+    # @param value * : The value
46
+    # @throw TypeError if not valid
47
+    @staticmethod
48
+    def dummy_check(value):
49
+        pass
50
+    
51
+    ## @brief Transform a value into a valid python representation according to the fieldtype
52
+    # @param value ? : The value to cast
53
+    # @param kwargs dict : optionnal cast arguments
54
+    # @return Something (depending on the fieldtype
55
+    # @throw AttributeError if error in argument given to the method
56
+    # @throw TypeError if the cast is not possible
57
+    def cast(self, value, **kwargs):
58
+        if len(kwargs) > 0:
59
+            raise AttributeError("No optionnal argument allowed for %s cast method"%self.__class__.__name__)
60
+        return value
61
+    
62
+    ## @brief Check if a value is correct
63
+    # @param value * : The value to check
64
+    # @return True if valid else False
65
+    def check(self, value):
66
+        try:
67
+            self.check_or_raise(value)
68
+        except TypeError as e:
69
+            return False
70
+        return True
71
+
72
+    ## @brief Check if a value is correct
73
+    # @param value * : The value
74
+    # @throw TypeError if not valid
75
+    def check_or_raise(self, value):
76
+        if value is None and not self.nullable:
77
+            raise TypeError("Not nullable field")
78
+        self.check_function(value)
79
+
80
+class FieldTypeError(Exception):
81
+    pass
82
+

+ 3
- 5
EditorialModel/fieldtypes/int.py View File

@@ -1,15 +1,13 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.fields import EmField
3
+from EditorialModel.fieldtypes import GenericFieldType
4 4
 
5 5
 
6
-class EmFieldInt(EmField):
7
-
8
-    ftype = 'int'
6
+class EmFieldInt(GenericFieldType):
9 7
 
10 8
     help = 'Basic integer field'
11 9
 
12 10
     def __init__(self, **kwargs):
13
-        super(EmFieldInt, self).__init__(**kwargs)
11
+        super(EmFieldInt, self).__init__(ftype='int',**kwargs)
14 12
 
15 13
 fclass = EmFieldInt

+ 6
- 19
EditorialModel/fieldtypes/regexchar.py View File

@@ -1,7 +1,7 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3 3
 import re
4
-from EditorialModel.fieldtypes.char import EmFieldChar
4
+from EditorialModel.fieldtypes.generic import GenericFieldType
5 5
 
6 6
 
7 7
 class EmFieldCharRegex(EmFieldChar):
@@ -14,22 +14,9 @@ class EmFieldCharRegex(EmFieldChar):
14 14
     def __init__(self, regex='', **kwargs):
15 15
         self.regex = regex
16 16
         v_re = re.compile(regex)  # trigger an error if invalid regex
17
+        
18
+        def re_match(value):
19
+            if not v_re.match(regex, value):
20
+                raise TypeError('"%s" don\'t match the regex "%s"'%(value, regex))
21
+        super(EmFieldCharRegex, self).__init__(check_function=re_match,**kwargs)
17 22
 
18
-        super(EmFieldCharRegex, self).__init__(**kwargs)
19
-
20
-    def validation_function(self, raise_e=None, ret_valid=None, ret_invalid=None):
21
-        super(EmFieldChar, self).validation_function(raise_e, ret_valid, ret_invalid)
22
-
23
-        if not raise_e is None:
24
-            def v_fun(value):
25
-                if not re.match(self.regex):
26
-                    raise raise_e
27
-        else:
28
-            def v_fun(value):
29
-                if not re.match(self.regex):
30
-                    return ret_invalid
31
-                else:
32
-                    return ret_valid
33
-        return v_fun
34
-
35
-fclass = EmFieldCharRegex

+ 3
- 8
EditorialModel/fieldtypes/rel2type.py View File

@@ -1,17 +1,14 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.fields import EmField
3
+from EditorialModel.fieldtypes.generic import GenericFieldType
4 4
 
5
-
6
-class EmFieldRel2Type(EmField):
7
-
8
-    ftype = 'rel2type'
5
+class EmFieldRel2Type(GenericFieldType):
9 6
 
10 7
     help = 'Relationnal field (relation2type). Take rel_to_type_id as option (an EmType uid)'
11 8
 
12 9
     def __init__(self, rel_to_type_id, **kwargs):
13 10
         self.rel_to_type_id = rel_to_type_id
14
-        super(EmFieldRel2Type, self).__init__(**kwargs)
11
+        super(EmFieldRel2Type, self).__init__(ftype='rel2type',**kwargs)
15 12
 
16 13
     def get_related_type(self):
17 14
         return self.model.component(self.rel_to_type_id)
@@ -19,5 +16,3 @@ class EmFieldRel2Type(EmField):
19 16
     def get_related_fields(self):
20 17
         return [f for f in self.model.components(EmField) if f.rel_field_id == self.uid]
21 18
 
22
-
23
-fclass = EmFieldRel2Type

+ 3
- 6
EditorialModel/fieldtypes/text.py View File

@@ -1,14 +1,11 @@
1 1
 #-*- coding: utf-8 -*-
2 2
 
3
-from EditorialModel.fields import EmField
3
+from EditorialModel.fieldtypes.generic import GenericFieldType
4 4
 
5
+class EmFieldText(GenericFieldType):
5 6
 
6
-class EmFieldText(EmField):
7
-
8
-    ftype = 'text'
9 7
     help = 'A text field (big string)'
10 8
 
11 9
     def __init__(self, **kwargs):
12
-        super(EmFieldText, self).__init__(**kwargs)
10
+        super(EmFieldText, self).__init__(ftype='text',**kwargs)
13 11
 
14
-fclass = EmFieldText

Loading…
Cancel
Save