This repository was archived by the owner on Mar 16, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy path_form.py
More file actions
174 lines (136 loc) · 4.42 KB
/
Copy path_form.py
File metadata and controls
174 lines (136 loc) · 4.42 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
"""PytSite Form2 Plugin
"""
__author__ = 'Oleksandr Shepetko'
__email__ = 'a@shepetko.com'
__license__ = 'MIT'
import htmler
from typing import Dict, Iterator, Any, Tuple, List
from pytsite.validation import Validator, Rule
from pytsite.util import random_password as random_str
from plugins.widget2 import Widget
from ._const import GET_WIDGETS_PATH, SUBMIT_PATH
class Form:
"""Form
"""
@property
def action(self) -> str:
return self._action
@action.setter
def action(self, value: str):
self._action = value
@property
def cid(self) -> str:
return self._cid
@property
def css(self) -> str:
return self._css
@css.setter
def css(self, value: str):
self._css = value
@property
def name(self) -> str:
return self._name
@name.setter
def name(self, value: str):
self._name = value
@property
def steps(self) -> int:
return self._steps
@property
def uid(self) -> str:
return self._uid
@uid.setter
def uid(self, value: str):
self._uid = value
@property
def values(self) -> Dict[str, Any]:
"""Get all widgets' values
"""
return {k: v[1].value for k, v in self._widgets.items()}
@property
def widgets(self) -> List[Widget]:
"""Get all widgets
"""
return [w[1] for w in sorted(self._widgets.values(), key=lambda x: x[1])]
def __init__(self, uid: str = None, **kwargs):
"""Init
"""
self._cid = f'{self.__module__}.{self.__class__.__name__}'
self._uid = uid or random_str(alphanum_only=True)
self._action = kwargs.get('action', '#')
self._css = ''
self._get_widgets_path = kwargs.get('get_widgets_path', GET_WIDGETS_PATH)
self._name = kwargs.get('name', self._uid)
self._steps = 1
self._validator = Validator()
self._submit_path = kwargs.get('submit_path', SUBMIT_PATH)
self._widgets = {} # type: Dict[str, Tuple[int, Widget]]
def add_widget(self, widget: Widget, step: int = 1):
"""Add a widget
"""
if step < 1 or step > (self._steps + 1):
raise ValueError(f'Invalid step number: {step}')
if widget.uid in self._widgets:
raise ValueError(f"The form '{self.uid}' already contains the widget '{widget.uid}'")
if step > self._steps:
self._steps = step
self._widgets[widget.uid] = (step, widget)
return self
def add_rule(self, widget_uid: str, rule: Rule):
"""Add a validation rule
"""
self._validator.add_rule(widget_uid, rule)
return self
def get_widgets(self, step: int = 1) -> Iterator[Widget]:
"""Get widgets for particular step
"""
if self._steps < step < 1:
raise ValueError(f'Invalid step number: {step}')
return (w[1] for w in self._widgets.values() if w[0] == step)
def fill(self, values: Dict, step: int = 1):
"""Fill widgets with values
"""
if self._steps < step < 1:
raise ValueError(f'Invalid step number: {step}')
for w in self.get_widgets(step):
if w.uid in values:
w.value = values[w.uid]
return self
def validate(self, step: int = 1):
"""Validate the form
"""
if self._steps < step < 1:
raise ValueError(f'Invalid step number: {step}')
fields = []
for w in self.get_widgets(step):
if self._validator.has_field(w.uid):
fields.append(w.uid)
self._validator.set_val(w.uid, w.value)
self._validator.validate(fields)
return self
def submit(self):
"""Submit the form
"""
return self
def render(self) -> str:
"""Render the form
"""
from ._api import store
store(self)
return str(htmler.Div(
css=f'pytsite-form2-container {self._css}'.strip(),
data_action_url=self._action,
data_get_widgets_path=self._get_widgets_path,
data_steps=self._steps,
data_submit_path=self._submit_path,
data_name=self._name,
data_uid=self._uid,
))
def __len__(self) -> int:
"""___len__()
"""
return len(self._widgets)
def __str__(self) -> str:
"""__str__()
"""
return self.render()