This repository was archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_input.py
More file actions
416 lines (316 loc) · 10.8 KB
/
_input.py
File metadata and controls
416 lines (316 loc) · 10.8 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""PytSite Input Widgets
"""
__author__ = 'Oleksandr Shepetko'
__email__ = 'a@shepetko.com'
__license__ = 'MIT'
import htmler
from typing import List
from pytsite import validation, router
from ._base import Abstract
from ._container import MultiRowList
class Input(Abstract):
pass
class Hidden(Input):
"""Hidden Input Widget
"""
def __init__(self, uid: str, **kwargs):
super().__init__(uid, **kwargs)
self._hidden = True
self._form_group = False
self._has_messages = False
def _get_element(self, **kwargs) -> htmler.Input:
inp = htmler.Input(
type='hidden',
id=self.uid,
name=self.name,
value=self.value,
required=self.required
)
for k, v in self._data.items():
inp.set_attr('data_' + k, v)
return inp
class Text(Input):
"""Text Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init.
"""
super().__init__(uid, **kwargs)
self._autocomplete = kwargs.get('autocomplete', 'on')
self._min_length = kwargs.get('min_length')
self._max_length = kwargs.get('max_length')
self._prepend = kwargs.get('prepend')
self._append = kwargs.get('append')
self._inputmask = kwargs.get('inputmask')
self._type = 'text'
@property
def autocomplete(self, ) -> int:
return self._autocomplete
@autocomplete.setter
def autocomplete(self, value: int):
self._autocomplete = value
@property
def min_length(self, ) -> int:
return self._min_length
@min_length.setter
def min_length(self, value: int):
self._min_length = value
@property
def max_length(self, ) -> int:
return self._max_length
@max_length.setter
def max_length(self, value: int):
self._max_length = value
@property
def prepend(self, ) -> int:
return self._prepend
@prepend.setter
def prepend(self, value: int):
self._prepend = value
@property
def append(self, ) -> int:
return self._append
@append.setter
def append(self, value: int):
self._append = value
@property
def inputmask(self, ) -> int:
return self._inputmask
@inputmask.setter
def inputmask(self, value: int):
self._inputmask = value
def _get_element(self, **kwargs) -> htmler.Input:
"""Render the widget
:param **kwargs:
"""
inp = htmler.Input(
type=self._type,
id=self._uid,
name=self._name,
css='form-control',
autocomplete=self._autocomplete,
placeholder=self._placeholder,
required=self._required
)
value = self.get_val()
if value:
inp.set_attr('value', value)
if not self._enabled:
inp.set_attr('disabled', 'true')
if self._min_length:
inp.set_attr('minlength', self._min_length)
if self._max_length:
inp.set_attr('maxlength', self._max_length)
if self._prepend or self._append:
group = htmler.Div(css='input-group')
if self._prepend:
prepend = group.append_child(htmler.Div(css='input-group-addon input-group-prepend'))
prepend.append_child(htmler.Div(self._prepend, css='input-group-text'))
group.append_child(inp)
if self._append:
append = group.append_child(htmler.Div(css='input-group-addon input-group-append'))
append.append_child(htmler.Div(self._append, css='input-group-text'))
inp = group
if self._inputmask:
inp.set_attr('data_inputmask', ','.join(["'{}': '{}'".format(k, v) for k, v in self._inputmask.items()]))
return inp
class Password(Text):
def __init__(self, uid: str, **kwargs):
super().__init__(uid, **kwargs)
self._type = 'password'
class Email(Text):
"""Email Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
super().__init__(uid, **kwargs)
self._type = 'email'
self.add_rule(validation.rule.Email())
class Url(Text):
"""URL Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
super().__init__(uid, **kwargs)
self.add_rule(validation.rule.Url())
class DNSName(Text):
"""DNS Name Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
super().__init__(uid, **kwargs)
self.add_rule(validation.rule.DNSName())
class TextArea(Abstract):
"""Text Area Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init.
"""
super().__init__(uid, **kwargs)
self._rows = kwargs.get('rows', 5)
self._required = kwargs.get('required', False)
self._max_length = kwargs.get('max_length')
def _get_element(self, **kwargs) -> htmler.Element:
"""Hook
"""
html_input = htmler.Textarea(
self.get_val(),
id=self._uid,
name=self._name,
disabled='disabled' if not self._enabled else None,
css=' '.join(('form-control', self._css)),
placeholder=self._placeholder,
rows=self._rows,
required=self._required
)
for k, v in self._data.items():
html_input.set_attr('data_' + k, v)
if self._max_length:
html_input.set_attr('maxlength', self._max_length)
return html_input
class TypeaheadText(Text):
def __init__(self, uid: str, **kwargs):
"""Init.
"""
super().__init__(uid, **kwargs)
source_url = kwargs.get('source_url')
if not source_url:
raise ValueError('AJAX endpoint is not specified.')
source_url_query_arg = kwargs.get('source_url_query_arg', self.uid)
source_url_q = kwargs.get('source_url_args', {})
source_url_q.update({source_url_query_arg: '__QUERY'})
source_url = router.url(source_url, query=source_url_q)
self._data['source_url'] = source_url
self._data['min_length'] = kwargs.get('typeahead_min_length', 1)
class Number(Text):
def __init__(self, uid: str, **kwargs):
"""Init
"""
# This must be set BEFORE calling parent init because it takes part in set_val()
self._convert_type = kwargs.get('convert_type', int)
super().__init__(uid, **kwargs)
self._type = 'tel'
self._allow_minus = kwargs.get('allow_minus', False)
self._right_align = kwargs.get('right_align', False)
self._min = kwargs.get('min')
self._max = kwargs.get('max')
if self._allow_minus or (self._min is not None and self._min < 0):
self._data['allow_minus'] = 'true'
if self._right_align:
self._data['right_align'] = 'true'
# Validation rules
if self._min is not None:
self.add_rule(validation.rule.GreaterOrEqual(than=self._min))
if self._max is not None:
self.add_rule(validation.rule.LessOrEqual(than=self._max))
def set_val(self, value, **kwargs):
"""Set value of the widget
"""
if isinstance(value, str):
value = value.strip()
if not value:
value = None
if value is not None and not isinstance(value, self._convert_type):
value = self._convert_type(value)
return super().set_val(value)
class Integer(Number):
"""Integer Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init.
"""
super().__init__(uid, convert_type=int, **kwargs)
self.add_rule(validation.rule.Integer())
class Decimal(Number):
"""Decimal Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init.
"""
super().__init__(uid, convert_type=float, **kwargs)
self.add_rule(validation.rule.Decimal())
class StringList(MultiRowList):
"""List of Strings Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
kwargs.setdefault('is_header_hidden', True)
super().__init__(uid, **kwargs)
self._autocomplete = kwargs.get('autocomplete', 'on')
self._min_length = kwargs.get('min_length')
self._max_length = kwargs.get('max_length')
self._prepend = kwargs.get('prepend')
self._append = kwargs.get('append')
self._inputmask = kwargs.get('inputmask')
def _get_widgets(self) -> List[Abstract]:
"""Hook
"""
return [Text(
uid='item',
label=self.label,
label_hidden=True,
rules=self.get_rules(),
autocomplete=self._autocomplete,
min_length=self._min_length,
max_length=self._max_length,
prepend=self._prepend,
append=self._append,
inputmask=self._inputmask,
enabled=self._enabled,
)]
class Tokens(Text):
"""Tokens Text Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
super().__init__(uid, **kwargs)
self._local_source = kwargs.get('local_source')
self._remote_source = kwargs.get('remote_source')
self._data = {
'local_source': self._local_source,
'remote_source': self._remote_source,
}
def set_val(self, value, **kwargs):
"""Set value of the widget
"""
if isinstance(value, str):
value = value.split(',')
return super().set_val(value)
def _get_element(self, **kwargs) -> htmler.Element:
html_input = htmler.Input(
type='text',
id=self._uid,
name=self._name,
value=','.join(self.get_val()) if self.get_val() else '',
css=' '.join(('form-control', self._css)),
)
return html_input
class File(Abstract):
"""File Input Widget
"""
def __init__(self, uid: str, **kwargs):
"""Init
"""
super().__init__(uid, **kwargs)
self._max_files = kwargs.get('max_files', 1)
self._multiple = False if self._max_files == 1 else True
self._accept = kwargs.get('accept', '*/*')
self._upload_endpoint = kwargs.get('upload_endpoint')
self._data['max_files'] = self._max_files
if self._upload_endpoint:
self._data['upload-endpoint'] = self._upload_endpoint
def _get_element(self, **kwargs) -> htmler.Element:
html_input = htmler.Input(
type='file',
id=self._uid,
name=self._name,
accept=self._accept,
)
if self._multiple:
html_input.set_attr('multiple', 'true')
return html_input