-
Notifications
You must be signed in to change notification settings - Fork 4
/
grid_helpers.py
227 lines (194 loc) · 7.69 KB
/
grid_helpers.py
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
import base64
from functools import reduce
from urllib.parse import unquote_plus
from py4web import request, Field, response
from py4web.utils.form import Form, FormStyleBulma
from py4web.utils.grid import AttributesPluginHtmx
from yatl.helpers import (
TAG,
)
BUTTON = TAG.button
class GridSearchQuery:
def __init__(self, name, query, requires=None, datatype="str", default=None):
self.name = name
self.query = query
self.requires = requires
self.datatype = datatype
self.default = default
self.field_name = name.replace(" ", "_").lower()
class GridSearch:
def __init__(
self, search_queries, queries=None, target_element=None, formname="search_form"
):
self.search_queries = search_queries
self.queries = queries
field_names = []
field_requires = dict()
field_datatype = dict()
field_default = dict()
for field in self.search_queries:
field_name = "sq_" + field.name.replace(" ", "_").replace("/", "_").lower()
field_names.append(field_name)
if field.requires and field.requires != "":
field_requires[field_name] = field.requires
if field.datatype and field.datatype.lower() == "boolean":
field_datatype[field_name] = "boolean"
elif field.datatype and field.datatype.lower() == "date":
field_datatype[field_name] = "date"
elif field.datatype and field.datatype.lower() == "datetime":
field_datatype[field_name] = "datetime"
if field.default:
field_default[field_name] = field.default
field_values = dict()
for field in field_names:
if field in request.forms:
field_values[field] = unquote_plus(request.forms.get(field))
elif field in request.query:
field_values[field] = unquote_plus(request.query[field])
form_fields = []
for field in field_names:
label = field.replace("sq_", "").replace("_", " ").title()
placeholder = field.replace("sq_", "").replace("_", " ").capitalize()
if field in field_datatype:
datatype = field_datatype[field]
else:
datatype = "str"
if datatype == "boolean":
if field_values.get(field):
default = field_values.get(field)
else:
default = field_default.get(field)
if default:
form_fields.append(
Field(
field,
type=field_datatype[field],
label=label,
_title=placeholder,
default=True,
)
)
else:
form_fields.append(
Field(
field,
type=field_datatype[field],
label=label,
_title=placeholder,
)
)
else:
form_fields.append(
Field(
field,
type=field_datatype.get(field, "str"),
length=50,
_placeholder=placeholder,
label=label,
requires=field_requires.get(field),
default=field_values.get(field, field_default.get(field)),
_title=placeholder,
_class=field_datatype.get(field, "input"),
)
)
if target_element:
attrs = {
"_hx-post": request.url,
"_hx-target": target_element,
"_hx-swap": "innerHTML",
"_method": "GET",
}
else:
attrs = {"_method": "GET"}
self.search_form = Form(
form_fields,
keep_values=True,
formstyle=FormStyleBulma,
form_name=formname,
**attrs,
)
if self.search_form.accepted:
for field in field_names:
if (
field in field_datatype
and field_datatype[field].lower() == "boolean"
):
if field in self.search_form.vars:
field_values[field] = self.search_form.vars[field]
else:
field_values[field] = False
else:
field_values[field] = self.search_form.vars[field]
if not self.queries:
self.queries = []
for sq in self.search_queries:
field_name = "sq_" + sq.name.replace(" ", "_").replace("/", "_").lower()
if field_name in field_values and field_values[field_name]:
self.queries.append(sq.query(field_values[field_name]))
elif field_name in field_default and field_default[field_name]:
self.queries.append(sq.query(field_default[field_name]))
self.query = reduce(lambda a, b: (a & b), self.queries)
def apply_htmx_attrs(grid, target):
myattrs = {"_hx-post": request.url, "_hx-target": target, "_hx-swap": "innerHTML"}
grid.attributes_plugin["form"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["link"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["search_form"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["button_sort_up"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["button_sort_down"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["button_delete"] = lambda attrs: attrs.update(myattrs)
grid.attributes_plugin["button_page_number"] = lambda attrs: attrs.update(myattrs)
class GridActionButton:
def __init__(
self,
url,
text=None,
icon=None,
onclick=None,
additional_classes="",
message="",
append_id=False,
ignore_attribute_plugin=False,
):
self.url = url
self.text = text
self.icon = icon
self.onclick = onclick
self.additional_classes = additional_classes
self.message = message
self.append_id = append_id
self.ignore_attribute_plugin = ignore_attribute_plugin
def get_referrer(r, default):
referrer = r.query.get("_referrer")
url = default
if referrer:
url = base64.b16decode(referrer.encode("utf8")).decode("utf8")
return url
def enable_htmx_grid(htmx_grid, target, default_referrer, after_swap=None):
"""
Turn this grid into an htmx-capable grid.
Add the Cancel button
Call process on the grid (grid must be instantiated with auto_process=False)
Parameters
----------
htmx_grid: the grid to update
target: htmx target element
default_referrer: default URL for the Cancel button
after_swap: set the javascript function to call after the dom swap is complete - this is to trigger other actions
after the swap
Returns
-------
"""
htmx_grid.attributes_plugin = AttributesPluginHtmx(target)
attrs = {
"_hx-get": get_referrer(
request,
default=default_referrer,
),
"_class": "button is-default",
}
htmx_grid.param.new_sidecar = BUTTON("Cancel", **attrs)
htmx_grid.param.edit_sidecar = BUTTON("Cancel", **attrs)
if after_swap:
if htmx_grid.action.lower() == "select":
response.headers["HX-Trigger-After-Swap"] = after_swap
htmx_grid.process()