|
@@ -8,32 +8,33 @@ from lodel.context import LodelContext
|
8
|
8
|
LodelContext.expose_modules(globals(), {
|
9
|
9
|
'lodel.leapi.datahandlers.base_classes': ['DataField'],
|
10
|
10
|
'lodel.exceptions': ['LodelException', 'LodelExceptions',
|
11
|
|
- 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
|
|
11
|
+ 'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
|
12
|
12
|
|
13
|
13
|
|
14
|
|
-##@brief Data field designed to handle boolean values
|
|
14
|
+## @brief Data field designed to handle boolean values
|
15
|
15
|
class Boolean(DataField):
|
16
|
16
|
|
17
|
17
|
help = 'A basic boolean field'
|
18
|
18
|
base_type = 'bool'
|
19
|
19
|
|
20
|
|
- ##@brief A boolean field
|
|
20
|
+ ## @brief A boolean field
|
21
|
21
|
def __init__(self, **kwargs):
|
22
|
22
|
#if 'check_data_value' not in kwargs:
|
23
|
23
|
# kwargs['check_data_value'] = self._check_data_value
|
24
|
24
|
super().__init__(ftype='bool', **kwargs)
|
25
|
25
|
|
26
|
|
- ##@brief Check and cast value in appropriate type
|
27
|
|
- #@param value *
|
28
|
|
- #@throw FieldValidationError if value is unappropriate or can not be cast
|
29
|
|
- #@return value
|
|
26
|
+ ## @brief Check and cast value in appropriate type
|
|
27
|
+ # @param value *
|
|
28
|
+ # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
29
|
+ # @return value
|
30
|
30
|
def _check_data_value(self, value):
|
31
|
31
|
value = super()._check_data_value(value)
|
32
|
32
|
if not isinstance(value, bool):
|
33
|
33
|
raise FieldValidationError("The value '%s' is not, and will never, be a boolean" % value)
|
34
|
34
|
return value
|
35
|
35
|
|
36
|
|
-##@brief Data field designed to handle integer values
|
|
36
|
+
|
|
37
|
+## @brief Data field designed to handle integer values
|
37
|
38
|
class Integer(DataField):
|
38
|
39
|
|
39
|
40
|
help = 'Basic integer field'
|
|
@@ -41,14 +42,14 @@ class Integer(DataField):
|
41
|
42
|
cast_type = int
|
42
|
43
|
|
43
|
44
|
def __init__(self, **kwargs):
|
44
|
|
- super().__init__( **kwargs)
|
|
45
|
+ super().__init__(**kwargs)
|
45
|
46
|
|
46
|
|
- ##@brief Check and cast value in appropriate type
|
|
47
|
+ ## @brief Check and cast value in appropriate type
|
47
|
48
|
# @param value *
|
48
|
49
|
# @param strict bool : tells if the value must be an integer or a value that can be converted into an integer
|
49
|
50
|
# @throw FieldValidationError if value is unappropriate or can not be cast
|
50
|
51
|
# @return value
|
51
|
|
- def _check_data_value(self, value, strict = False):
|
|
52
|
+ def _check_data_value(self, value, strict=False):
|
52
|
53
|
value = super()._check_data_value(value)
|
53
|
54
|
if (strict and not isinstance(value, int)):
|
54
|
55
|
raise FieldValidationError("The value '%s' is not a python type integer" % value)
|
|
@@ -61,19 +62,20 @@ class Integer(DataField):
|
61
|
62
|
raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
|
62
|
63
|
return value
|
63
|
64
|
|
64
|
|
-##@brief Data field designed to handle string
|
|
65
|
+
|
|
66
|
+## @brief Data field designed to handle string
|
65
|
67
|
class Varchar(DataField):
|
66
|
68
|
|
67
|
69
|
help = 'Basic string (varchar) field. Default size is 64 characters'
|
68
|
70
|
base_type = 'char'
|
69
|
71
|
|
70
|
|
- ##@brief A string field
|
|
72
|
+ ## @brief A string field
|
71
|
73
|
# @brief max_length int: The maximum length of this field
|
72
|
74
|
def __init__(self, max_length=64, **kwargs):
|
73
|
75
|
self.max_length = int(max_length)
|
74
|
76
|
super().__init__(**kwargs)
|
75
|
77
|
|
76
|
|
- ##@brief checks if this class can override the given data handler
|
|
78
|
+ ## @brief checks if this class can override the given data handler
|
77
|
79
|
# @param data_handler DataHandler
|
78
|
80
|
# @return bool
|
79
|
81
|
def can_override(self, data_handler):
|
|
@@ -83,25 +85,26 @@ class Varchar(DataField):
|
83
|
85
|
return False
|
84
|
86
|
return True
|
85
|
87
|
|
86
|
|
- ##@brief Check and cast value in appropriate type
|
87
|
|
- #@param value *
|
88
|
|
- #@throw FieldValidationError if value is unappropriate or can not be cast
|
89
|
|
- #@return value
|
|
88
|
+ ## @brief Check and cast value in appropriate type
|
|
89
|
+ # @param value *
|
|
90
|
+ # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
91
|
+ # @return value
|
90
|
92
|
def _check_data_value(self, value):
|
91
|
93
|
value = super()._check_data_value(value)
|
92
|
94
|
if not isinstance(value, str):
|
93
|
95
|
raise FieldValidationError("The value '%s' can't be a str" % value)
|
94
|
96
|
if len(value) > self.max_length:
|
95
|
|
- raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
|
|
97
|
+ raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
|
96
|
98
|
return value
|
97
|
99
|
|
98
|
|
-##@brief Data field designed to handle date & time
|
|
100
|
+
|
|
101
|
+## @brief Data field designed to handle date & time
|
99
|
102
|
class DateTime(DataField):
|
100
|
103
|
|
101
|
104
|
help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
|
102
|
105
|
base_type = 'datetime'
|
103
|
106
|
|
104
|
|
- ##@brief A datetime field
|
|
107
|
+ ## @brief A datetime field
|
105
|
108
|
# @param now_on_update bool : If true, the date is set to NOW on update
|
106
|
109
|
# @param now_on_create bool : If true, the date is set to NEW on creation
|
107
|
110
|
# @param **kwargs
|
|
@@ -111,13 +114,13 @@ class DateTime(DataField):
|
111
|
114
|
self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
|
112
|
115
|
super().__init__(**kwargs)
|
113
|
116
|
|
114
|
|
- ##@brief Check and cast value in appropriate type
|
115
|
|
- #@param value *
|
116
|
|
- #@throw FieldValidationError if value is unappropriate or can not be cast
|
117
|
|
- #@return value
|
|
117
|
+ ## @brief Check and cast value in appropriate type
|
|
118
|
+ # @param value *
|
|
119
|
+ # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
120
|
+ # @return value
|
118
|
121
|
def _check_data_value(self, value):
|
119
|
122
|
value = super()._check_data_value(value)
|
120
|
|
- if isinstance(value,str):
|
|
123
|
+ if isinstance(value, str):
|
121
|
124
|
try:
|
122
|
125
|
value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
|
123
|
126
|
except ValueError:
|
|
@@ -131,7 +134,8 @@ class DateTime(DataField):
|
131
|
134
|
return datetime.datetime.now()
|
132
|
135
|
return cur_value
|
133
|
136
|
|
134
|
|
-##@brief Data field designed to handle long string
|
|
137
|
+
|
|
138
|
+## @brief Data field designed to handle long string
|
135
|
139
|
class Text(DataField):
|
136
|
140
|
help = 'A text field (big string)'
|
137
|
141
|
base_type = 'text'
|
|
@@ -139,22 +143,23 @@ class Text(DataField):
|
139
|
143
|
def __init__(self, **kwargs):
|
140
|
144
|
super(self.__class__, self).__init__(ftype='text', **kwargs)
|
141
|
145
|
|
142
|
|
- ##@brief Check and cast value in appropriate type
|
143
|
|
- #@param value *
|
144
|
|
- #@throw FieldValidationError if value is unappropriate or can not be cast
|
145
|
|
- #@return value
|
|
146
|
+ ## @brief Check and cast value in appropriate type
|
|
147
|
+ # @param value *
|
|
148
|
+ # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
149
|
+ # @return value
|
146
|
150
|
def _check_data_value(self, value):
|
147
|
151
|
value = super()._check_data_value(value)
|
148
|
152
|
if not isinstance(value, str):
|
149
|
153
|
raise FieldValidationError("The content passed to this Text field is not a convertible to a string")
|
150
|
154
|
return value
|
151
|
155
|
|
152
|
|
-##@brief Data field designed to handle Files
|
|
156
|
+
|
|
157
|
+## @brief Data field designed to handle Files
|
153
|
158
|
class File(DataField):
|
154
|
159
|
|
155
|
160
|
base_type = 'file'
|
156
|
161
|
|
157
|
|
- ##@brief a file field
|
|
162
|
+ ## @brief a file field
|
158
|
163
|
# @param upload_path str : None by default
|
159
|
164
|
# @param **kwargs
|
160
|
165
|
def __init__(self, upload_path=None, **kwargs):
|
|
@@ -164,4 +169,3 @@ class File(DataField):
|
164
|
169
|
# @todo Add here a check for the validity of the given value (should have a correct path syntax)
|
165
|
170
|
def _check_data_value(self, value):
|
166
|
171
|
return super()._check_data_value(value)
|
167
|
|
-
|