|
@@ -4,7 +4,7 @@ import datetime
|
4
|
4
|
import time
|
5
|
5
|
import os
|
6
|
6
|
|
7
|
|
-from lodel.leapi.datahandlers.base_classes import DataField
|
|
7
|
+from lodel.leapi.datahandlers.base_classes import DataField, FieldValidationError
|
8
|
8
|
|
9
|
9
|
##@brief Data field designed to handle boolean values
|
10
|
10
|
class Boolean(DataField):
|
|
@@ -14,18 +14,15 @@ class Boolean(DataField):
|
14
|
14
|
|
15
|
15
|
##@brief A boolean field
|
16
|
16
|
def __init__(self, **kwargs):
|
17
|
|
- if 'check_data_value' not in kwargs:
|
18
|
|
- kwargs['check_data_value'] = self.check_data_value
|
|
17
|
+ #if 'check_data_value' not in kwargs:
|
|
18
|
+ # kwargs['check_data_value'] = self._check_data_value
|
19
|
19
|
super().__init__(ftype='bool', **kwargs)
|
20
|
20
|
|
21
|
21
|
def _check_data_value(self, value):
|
22
|
|
- error = None
|
23
|
|
- try:
|
24
|
|
- if type(value) != bool:
|
25
|
|
- raise TypeError()
|
26
|
|
- except(ValueError, TypeError):
|
27
|
|
- error = TypeError("The value '%s' is not, and will never, be a boolean" % value)
|
28
|
|
- return value, error
|
|
22
|
+ value = super()._check_data_value(value)
|
|
23
|
+ if not isinstance(value, bool):
|
|
24
|
+ raise FieldValidationError("The value '%s' is not, and will never, be a boolean" % value)
|
|
25
|
+ return value
|
29
|
26
|
|
30
|
27
|
##@brief Data field designed to handle integer values
|
31
|
28
|
class Integer(DataField):
|
|
@@ -37,17 +34,17 @@ class Integer(DataField):
|
37
|
34
|
def __init__(self, **kwargs):
|
38
|
35
|
super().__init__( **kwargs)
|
39
|
36
|
|
40
|
|
- def _check_data_value(self, value):
|
41
|
|
- error = None
|
|
37
|
+ def _check_data_value(self, value, strict = False):
|
|
38
|
+ value = super()._check_data_value(value)
|
|
39
|
+ if (strict and isinstance(value, int)):
|
|
40
|
+ raise FieldValidationError("The value '%s' is not a python type integer" % value)
|
42
|
41
|
try:
|
43
|
42
|
value = float(value)
|
44
|
|
- if value % 1 == 0:
|
45
|
|
- value = int(value)
|
46
|
|
- else:
|
|
43
|
+ if value != int(value):
|
47
|
44
|
raise TypeError()
|
48
|
45
|
except(ValueError, TypeError):
|
49
|
|
- error = TypeError("The value '%s' is not, and will never, be an integer" % value)
|
50
|
|
- return value, error
|
|
46
|
+ raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
|
|
47
|
+ return value
|
51
|
48
|
|
52
|
49
|
##@brief Data field designed to handle string
|
53
|
50
|
class Varchar(DataField):
|
|
@@ -72,17 +69,12 @@ class Varchar(DataField):
|
72
|
69
|
return True
|
73
|
70
|
|
74
|
71
|
def _check_data_value(self, value):
|
75
|
|
- error = None
|
76
|
|
- try:
|
77
|
|
- value = str(value)
|
78
|
|
- if len(value) > self.max_length:
|
79
|
|
- raise ValueError
|
80
|
|
- except TypeError:
|
81
|
|
- error = TypeError("The value '%s' can't be a str" % value)
|
82
|
|
- except ValueError:
|
83
|
|
- error = ValueError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
|
84
|
|
- return value, error
|
85
|
|
-
|
|
72
|
+ value = super()._check_data_value(value)
|
|
73
|
+ if not isinstance(value, str):
|
|
74
|
+ raise FieldValidationError("The value '%s' can't be a str" % value)
|
|
75
|
+ if len(value) > self.max_length:
|
|
76
|
+ raise FieldValidationError("The value '%s' is longer than the maximum length of this field (%s)" % (value, self.max_length))
|
|
77
|
+ return value
|
86
|
78
|
##@brief Data field designed to handle date & time
|
87
|
79
|
class DateTime(DataField):
|
88
|
80
|
|
|
@@ -100,15 +92,16 @@ class DateTime(DataField):
|
100
|
92
|
super().__init__(**kwargs)
|
101
|
93
|
|
102
|
94
|
def _check_data_value(self, value):
|
103
|
|
- error = None
|
|
95
|
+ value = super()._check_data_value(value)
|
104
|
96
|
if isinstance(value,str):
|
105
|
97
|
try:
|
106
|
98
|
datetime_value = datetime.datetime.fromtimestamp(time.mktime(time.strptime(value, self.datetime_format)))
|
107
|
|
- except ValueError:
|
108
|
|
- error = ValueError("The value '%s' cannot be converted as a datetime" % value)
|
|
99
|
+ except ValueError:
|
|
100
|
+ raise FieldValidationError("The value '%s' cannot be converted as a datetime" % value)
|
109
|
101
|
elif not isinstance(value, datetime.datetime):
|
110
|
|
- error = ValueError("Tue value has to be a string or a datetime")
|
111
|
|
- return value, error
|
|
102
|
+ raise FieldValidationError("Tue value has to be a string or a datetime")
|
|
103
|
+ else:
|
|
104
|
+ return value
|
112
|
105
|
|
113
|
106
|
def _construct_data(self, emcomponent, fname, datas, cur_value):
|
114
|
107
|
if (self.now_on_create and cur_value is None) or self.now_on_update:
|
|
@@ -124,12 +117,10 @@ class Text(DataField):
|
124
|
117
|
super(self.__class__, self).__init__(ftype='text', **kwargs)
|
125
|
118
|
|
126
|
119
|
def _check_data_value(self, value):
|
127
|
|
- error = None
|
128
|
|
- try:
|
129
|
|
- value = str(value)
|
130
|
|
- except (ValueError, TypeError):
|
131
|
|
- error = ValueError("The content passed to this Text field is not a convertible to a string")
|
132
|
|
- return value, error
|
|
120
|
+ value = super()._check_data_value(value)
|
|
121
|
+ if not isinstance(value, str):
|
|
122
|
+ raise FieldValidationError("The content passed to this Text field is not a convertible to a string")
|
|
123
|
+ return value
|
133
|
124
|
|
134
|
125
|
##@brief Data field designed to handle Files
|
135
|
126
|
class File(DataField):
|
|
@@ -145,6 +136,5 @@ class File(DataField):
|
145
|
136
|
|
146
|
137
|
# @todo Add here a check for the validity of the given value (should have a correct path syntax)
|
147
|
138
|
def _check_data_value(self, value):
|
148
|
|
- error = None
|
149
|
|
- return value, error
|
|
139
|
+ return super()._check_data_value(value)
|
150
|
140
|
|