|
@@ -3,14 +3,16 @@ import warnings
|
3
|
3
|
import datetime
|
4
|
4
|
import time
|
5
|
5
|
import os
|
6
|
|
-
|
7
|
6
|
from lodel.context import LodelContext
|
|
7
|
+
|
|
8
|
+
|
8
|
9
|
LodelContext.expose_modules(globals(), {
|
9
|
10
|
'lodel.leapi.datahandlers.base_classes': ['DataField'],
|
10
|
11
|
'lodel.exceptions': ['LodelException', 'LodelExceptions',
|
11
|
12
|
'LodelFatalError', 'DataNoneValid', 'FieldValidationError']})
|
12
|
13
|
|
13
|
14
|
|
|
15
|
+##
|
14
|
16
|
## @brief Data field designed to handle boolean values
|
15
|
17
|
class Boolean(DataField):
|
16
|
18
|
|
|
@@ -19,13 +21,17 @@ class Boolean(DataField):
|
19
|
21
|
|
20
|
22
|
## @brief A boolean field
|
21
|
23
|
def __init__(self, **kwargs):
|
|
24
|
+ ##
|
|
25
|
+ # @remarks Commented out code left in the code base. Consider deletion.
|
22
|
26
|
#if 'check_data_value' not in kwargs:
|
23
|
27
|
# kwargs['check_data_value'] = self._check_data_value
|
24
|
28
|
super().__init__(ftype='bool', **kwargs)
|
25
|
29
|
|
26
|
|
- ## @brief Check and cast value in appropriate type
|
27
|
|
- # @param value *
|
28
|
|
- # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
30
|
+ ##
|
|
31
|
+ # @brief Checks value.
|
|
32
|
+ #
|
|
33
|
+ # @param value mixed:
|
|
34
|
+ # @throw FieldValidationError: if value is not valid
|
29
|
35
|
# @return value
|
30
|
36
|
def _check_data_value(self, value):
|
31
|
37
|
value = super()._check_data_value(value)
|
|
@@ -34,7 +40,8 @@ class Boolean(DataField):
|
34
|
40
|
return value
|
35
|
41
|
|
36
|
42
|
|
37
|
|
-## @brief Data field designed to handle integer values
|
|
43
|
+##
|
|
44
|
+# @brief Data field designed to handle integer values.
|
38
|
45
|
class Integer(DataField):
|
39
|
46
|
|
40
|
47
|
help = 'Basic integer field'
|
|
@@ -44,10 +51,12 @@ class Integer(DataField):
|
44
|
51
|
def __init__(self, **kwargs):
|
45
|
52
|
super().__init__(**kwargs)
|
46
|
53
|
|
47
|
|
- ## @brief Check and cast value in appropriate type
|
48
|
|
- # @param value *
|
49
|
|
- # @param strict bool : tells if the value must be an integer or a value that can be converted into an integer
|
50
|
|
- # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
54
|
+ ##
|
|
55
|
+ # @brief Checks and casts value in appropriate type
|
|
56
|
+ #
|
|
57
|
+ # @param value *:
|
|
58
|
+ # @param strict bool: Whether the value type should be treated strictly or cast.
|
|
59
|
+ # @throw FieldValidationError: if value is inappropriate or can not be cast
|
51
|
60
|
# @return value
|
52
|
61
|
def _check_data_value(self, value, strict=False):
|
53
|
62
|
value = super()._check_data_value(value)
|
|
@@ -59,23 +68,28 @@ class Integer(DataField):
|
59
|
68
|
else:
|
60
|
69
|
value = int(float(value))
|
61
|
70
|
except(ValueError, TypeError):
|
62
|
|
- raise FieldValidationError("The value '%s' is not, and will never, be an integer" % value)
|
|
71
|
+ raise FieldValidationError("The value '%s' is not an integer nor could be cast to." % value)
|
63
|
72
|
return value
|
64
|
73
|
|
65
|
74
|
|
66
|
|
-## @brief Data field designed to handle string
|
|
75
|
+##
|
|
76
|
+# @brief Data field designed to handle string
|
67
|
77
|
class Varchar(DataField):
|
68
|
78
|
|
69
|
79
|
help = 'Basic string (varchar) field. Default size is 64 characters'
|
70
|
80
|
base_type = 'char'
|
71
|
81
|
|
72
|
|
- ## @brief A string field
|
|
82
|
+ ##
|
|
83
|
+ # @brief A string field
|
|
84
|
+ #
|
73
|
85
|
# @brief max_length int: The maximum length of this field
|
74
|
86
|
def __init__(self, max_length=64, **kwargs):
|
75
|
87
|
self.max_length = int(max_length)
|
76
|
88
|
super().__init__(**kwargs)
|
77
|
89
|
|
78
|
|
- ## @brief checks if this class can override the given data handler
|
|
90
|
+ ##
|
|
91
|
+ # @brief checks if this class can override the given data handler
|
|
92
|
+ #
|
79
|
93
|
# @param data_handler DataHandler
|
80
|
94
|
# @return bool
|
81
|
95
|
def can_override(self, data_handler):
|
|
@@ -84,10 +98,12 @@ class Varchar(DataField):
|
84
|
98
|
if data_handler.max_length != self.max_length:
|
85
|
99
|
return False
|
86
|
100
|
return True
|
87
|
|
-
|
88
|
|
- ## @brief Check and cast value in appropriate type
|
89
|
|
- # @param value *
|
90
|
|
- # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
101
|
+
|
|
102
|
+ ##
|
|
103
|
+ # @brief Check and cast value in appropriate type
|
|
104
|
+ #
|
|
105
|
+ # @param value *:
|
|
106
|
+ # @throw FieldValidationError if value is inappropriate or can not be cast
|
91
|
107
|
# @return value
|
92
|
108
|
def _check_data_value(self, value):
|
93
|
109
|
value = super()._check_data_value(value)
|
|
@@ -98,25 +114,33 @@ class Varchar(DataField):
|
98
|
114
|
return value
|
99
|
115
|
|
100
|
116
|
|
101
|
|
-## @brief Data field designed to handle date & time
|
|
117
|
+##
|
|
118
|
+# @brief Data field designed to handle date & time
|
102
|
119
|
class DateTime(DataField):
|
103
|
120
|
|
104
|
121
|
help = 'A datetime field. Take two boolean options now_on_update and now_on_create'
|
105
|
122
|
base_type = 'datetime'
|
106
|
123
|
|
107
|
|
- ## @brief A datetime field
|
|
124
|
+ ##
|
|
125
|
+ # @brief A datetime field
|
|
126
|
+ #
|
108
|
127
|
# @param now_on_update bool : If true, the date is set to NOW on update
|
109
|
128
|
# @param now_on_create bool : If true, the date is set to NEW on creation
|
110
|
129
|
# @param **kwargs
|
|
130
|
+ #
|
|
131
|
+ # @remarks Is it intended that DateTime objects can not be instantiated with
|
|
132
|
+ # a provided arbitrary string value or something else?
|
111
|
133
|
def __init__(self, now_on_update=False, now_on_create=False, **kwargs):
|
112
|
134
|
self.now_on_update = now_on_update
|
113
|
135
|
self.now_on_create = now_on_create
|
114
|
136
|
self.datetime_format = '%Y-%m-%d' if 'format' not in kwargs else kwargs['format']
|
115
|
137
|
super().__init__(**kwargs)
|
116
|
138
|
|
117
|
|
- ## @brief Check and cast value in appropriate type
|
118
|
|
- # @param value *
|
119
|
|
- # @throw FieldValidationError if value is unappropriate or can not be cast
|
|
139
|
+ ##
|
|
140
|
+ # @brief Check and cast value in appropriate type
|
|
141
|
+ #
|
|
142
|
+ # @param value mixed:
|
|
143
|
+ # @throw FieldValidationError: if value is inappropriate or can not be cast
|
120
|
144
|
# @return value
|
121
|
145
|
def _check_data_value(self, value):
|
122
|
146
|
value = super()._check_data_value(value)
|
|
@@ -135,15 +159,19 @@ class DateTime(DataField):
|
135
|
159
|
return cur_value
|
136
|
160
|
|
137
|
161
|
|
138
|
|
-## @brief Data field designed to handle long string
|
|
162
|
+##
|
|
163
|
+# @brief Data field designed to handle long string
|
139
|
164
|
class Text(DataField):
|
|
165
|
+
|
140
|
166
|
help = 'A text field (big string)'
|
141
|
167
|
base_type = 'text'
|
142
|
168
|
|
143
|
169
|
def __init__(self, **kwargs):
|
144
|
170
|
super(self.__class__, self).__init__(ftype='text', **kwargs)
|
145
|
|
-
|
146
|
|
- ## @brief Check and cast value in appropriate type
|
|
171
|
+
|
|
172
|
+ ##
|
|
173
|
+ # @brief Check and cast value in appropriate type
|
|
174
|
+ #
|
147
|
175
|
# @param value *
|
148
|
176
|
# @throw FieldValidationError if value is unappropriate or can not be cast
|
149
|
177
|
# @return value
|
|
@@ -154,18 +182,22 @@ class Text(DataField):
|
154
|
182
|
return value
|
155
|
183
|
|
156
|
184
|
|
157
|
|
-## @brief Data field designed to handle Files
|
|
185
|
+##
|
|
186
|
+# @brief Data field designed to handle Files
|
158
|
187
|
class File(DataField):
|
159
|
188
|
|
160
|
189
|
base_type = 'file'
|
161
|
190
|
|
162
|
|
- ## @brief a file field
|
|
191
|
+ ##
|
|
192
|
+ # @brief a file field
|
|
193
|
+ #
|
163
|
194
|
# @param upload_path str : None by default
|
164
|
195
|
# @param **kwargs
|
165
|
196
|
def __init__(self, upload_path=None, **kwargs):
|
166
|
197
|
self.upload_path = upload_path
|
167
|
198
|
super().__init__(**kwargs)
|
168
|
199
|
|
169
|
|
- # @todo Add here a check for the validity of the given value (should have a correct path syntax)
|
|
200
|
+ ##
|
|
201
|
+ # @todo Add a check for the validity of the given value (should have a correct path syntax)
|
170
|
202
|
def _check_data_value(self, value):
|
171
|
203
|
return super()._check_data_value(value)
|