-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcolumn.py
More file actions
345 lines (256 loc) · 11.2 KB
/
column.py
File metadata and controls
345 lines (256 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from datetime import datetime
from .constants import ColumnTypes
# Set the null list to distinguish the pure none and the number 0 or 0.00, which is
# a critical real value in number type column.
NULL_LIST = ['', [], None]
# Column Value related classes handle the compare computation of the table data
class ColumnValue(object):
"""
This is for the computation of the comparison between the input value and cell value from table
such as >, <, =, >=, <=, !=, which is supposed to fit different column types
"""
def __init__(self, column_value, column_type=None):
self.column_value = column_value
self.column_type = column_type
def equal(self, value):
if value == '':
return self.column_value in NULL_LIST
return self.column_value == value
def unequal(self, value):
if value == '':
return self.column_value not in NULL_LIST
return self.column_value != value
def greater_equal_than(self, value):
raise ValueError("%s type column does not support the query method '%s'" % (self.column_type, '>='))
def greater_than(self, value):
raise ValueError("%s type column does not support the query method '%s'" % (self.column_type, '>'))
def less_equal_than(self, value):
raise ValueError("%s type column does not support the query method '%s'" % (self.column_type, '<='))
def less_than(self, value):
raise ValueError("%s type column does not support the query method '%s'" % (self.column_type, '<'))
def like(self, value):
'''fuzzy search'''
raise ValueError("%s type column does not support the query method '%s'" % (self.column_type, 'like'))
class StringColumnValue(ColumnValue):
"""
the return data of string column value is type of string, including column type of
text, creator, single-select, url, email,....., and support the computation of
= ,!=, and like(fuzzy search)
"""
def like(self, value):
if "%" in value:
column_value = self.column_value or ""
# 1. abc% pattern, start with abc
if value[0] != '%' and value[-1] == '%':
start = value[:-1]
return column_value.startswith(start)
# 2. %abc pattern, end with abc
elif value[0] == '%' and value[-1] != '%':
end = value[1:]
return column_value.endswith(end)
# 3. %abc% pattern, contains abc
elif value[0] == '%' and value[-1] == '%':
middle = value[1:-1]
return middle in column_value
# 4. a%b pattern, start with a and end with b
else:
value_split_list = value.split('%')
start = value_split_list[0]
end = value_split_list[-1]
return column_value.startswith(start) and column_value.endswith(end)
else:
raise ValueError('There is no patterns found in "like" phrases')
class NumberDateColumnValue(ColumnValue):
"""
the returned data of number-date-column is digit number, or datetime obj, including the
type of number, ctime, date, mtime, support the computation of =, > ,< ,>=, <=, !=
"""
def greater_equal_than(self, value):
if value == "":
self.raise_error()
return self.column_value >= value if self.column_value not in NULL_LIST else False
def greater_than(self, value):
if value == "":
self.raise_error()
return self.column_value > value if self.column_value not in NULL_LIST else False
def less_equal_than(self, value):
if value == "":
self.raise_error()
return self.column_value <= value if self.column_value not in NULL_LIST else False
def less_than(self, value):
if value == "":
self.raise_error()
return self.column_value < value if self.column_value not in NULL_LIST else False
def raise_error(self):
raise ValueError("""The token ">", ">=", "<", "<=" does not support the null query string "".""")
class ListColumnValue(ColumnValue):
"""
the returned data of list-column value is a list like data structure, including the
type of multiple-select, image, collaborator and so on, support the computation of
=, != which should be decided by in or not in expression
"""
def equal(self, value):
if not value:
return self.column_value in NULL_LIST
column_value = self.column_value or []
return value in column_value
def unequal(self, value):
if not value:
return self.column_value not in NULL_LIST
column_value = self.column_value or []
return value not in column_value
class BoolColumnValue(ColumnValue):
"""
the returned data of bool-column value is should be True or False, such as check-box
type column. If the value from table shows None, treat it as False
"""
def equal(self, value):
return bool(self.column_value) == value
def unequal(self, value):
return bool(self.column_value) != value
# Column related class handle the treatment of both input value inputted by users by using
# the query statements, and the value in table displayed in different data structure in
# varies types of columns
class BaseColumn(object):
def parse_input_value(self, value):
return value
def parse_table_value(self, value):
return ColumnValue(value)
class TextColumn(BaseColumn):
def __init__(self):
self.column_type = ColumnTypes.TEXT.value
def __str__(self):
return "SeaTable Text Column"
def parse_table_value(self, value):
return StringColumnValue(value, self.column_type)
class LongTextColumn(TextColumn):
def __init__(self):
super(LongTextColumn, self).__init__()
self.column_type = ColumnTypes.LONG_TEXT.value
def __str__(self):
return "SeaTable Long Text Column"
def parse_table_value(self, value):
value = value.strip('\n')
return StringColumnValue(value, self.column_type)
class NumberColumn(BaseColumn):
def __init__(self):
self.column_type = ColumnTypes.NUMBER.value
def __str__(self):
return "SeaTable Number Column"
def parse_input_value(self, value):
if value == "":
return value
if '.' in value:
value = float(value)
else:
try:
value = int(value)
except:
self.raise_input_error(value)
return value
def parse_table_value(self, value):
return NumberDateColumnValue(value, self.column_type)
def raise_input_error(self, value):
raise ValueError("""%s type column does not support the query string as "%s",
please use "" or digital numbers
""" % (self.column_type, value))
class DateColumn(BaseColumn):
def __init__(self):
self.column_type = ColumnTypes.DATE.value
def __str__(self):
return "SeaTable Date Column"
def parse_input_value(self, time_str):
if not time_str:
return ""
try:
time_str_list = time_str.split(' ')
datetime_obj = None
if len(time_str_list) == 1:
ymd = time_str_list[0]
datetime_obj = datetime.strptime(ymd, '%Y-%m-%d')
elif len(time_str_list) == 2:
h, m, s = 0, 0, 0
ymd, hms_str = time_str_list
hms_str_list = hms_str.split(':')
if len(hms_str_list) == 1:
h = hms_str_list[0]
elif len(hms_str_list) == 2:
h, m = hms_str_list
elif len(hms_str_list) == 3:
h, m, s = hms_str_list
datetime_obj = datetime.strptime("%s %s" % (
ymd, "%s:%s:%s" % (h, m, s)), '%Y-%m-%d %H:%M:%S')
return datetime_obj
except:
return self.raise_error(time_str)
def parse_table_value(self, time_str):
return NumberDateColumnValue(self.parse_input_value(time_str), self.column_type)
def raise_error(self, value):
raise ValueError(""" %s type column does not support the query string as "%s",
the supported query string pattern like:
"YYYY-MM-DD" or
"YYYY-MM-DD hh" or
"YYYY-MM-DD hh:mm" or
"YYYY-MM-DD hh:mm:ss" or
""" % (self.column_type, value))
class CTimeColumn(DateColumn):
def __init__(self):
super(CTimeColumn, self).__init__()
self.column_type = ColumnTypes.CTIME.value
def __str__(self):
return "SeaTable CTime Column"
def get_local_time(self, time_str):
utc_time = datetime.strptime(time_str, '%Y-%m-%dT%H:%M:%S.%f+00:00')
delta2utc = datetime.now() - datetime.utcnow()
local_time = utc_time + delta2utc
return local_time
def parse_table_value(self, time_str):
return NumberDateColumnValue(self.get_local_time(time_str), self.column_type)
class MTimeColumn(CTimeColumn):
def __init__(self):
super(MTimeColumn, self).__init__()
self.column_type = ColumnTypes.MTIME.value
def __str__(self):
return "SeaTable MTime Column"
def parse_table_value(self, time_str):
return NumberDateColumnValue(super(MTimeColumn, self).get_local_time(time_str), self.column_type)
class CheckBoxColumn(BaseColumn):
def __init__(self):
super(CheckBoxColumn, self).__init__()
self.column_type = ColumnTypes.CHECKBOX.value
def __str__(self):
return "SeaTable Checkbox Column"
def parse_input_value(self, value):
if not value:
return False
elif value.lower() == 'true':
return True
elif value.lower() == 'false':
return False
else:
self.raise_error(value)
def parse_table_value(self, value):
return BoolColumnValue(value, self.column_type)
def raise_error(self, value):
raise ValueError(""" %s type column does not support the query string as "%s",
the supported query string pattern like:
"true" or "false", case insensitive
""" % (self.column_type, value))
class MultiSelectColumn(BaseColumn):
def __init__(self):
super(MultiSelectColumn, self).__init__()
self.column_type = ColumnTypes.MULTIPLE_SELECT.value
def parse_table_value(self, value):
return ListColumnValue(value, self.column_type)
COLUMN_MAP = {
ColumnTypes.NUMBER.value: NumberColumn(), # 1. number type
ColumnTypes.DATE.value: DateColumn(), # 2. date type
ColumnTypes.CTIME.value: CTimeColumn(), # 3. ctime type, create time
ColumnTypes.MTIME.value: MTimeColumn(), # 4. mtime type, modify time
ColumnTypes.CHECKBOX.value: CheckBoxColumn(), # 5. checkbox type
ColumnTypes.TEXT.value: TextColumn(), # 6. text type
ColumnTypes.MULTIPLE_SELECT.value: MultiSelectColumn(), # 7. multi-select type
ColumnTypes.LONG_TEXT.value: LongTextColumn(), # 8. long-text type
}
def get_column_by_type(column_type):
return COLUMN_MAP.get(column_type, TextColumn())