Skip to content

Commit 1d8c93e

Browse files
author
Johan Lübcke
committed
Cleanup for class meta failing with Table(query=Query(...)) provided
1 parent 5e5922c commit 1d8c93e

File tree

3 files changed

+64
-63
lines changed

3 files changed

+64
-63
lines changed

iommi/declarative/util.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,16 @@ def inject_args(args, kwargs, extra_args, pos_arg_names, merge_namespaces):
4444
else:
4545
new_args = args
4646

47-
for k, v in kwargs.items():
48-
new_value = new_kwargs.get(k, None)
49-
if merge_namespaces and isinstance(new_value, Namespace):
50-
if v is not None:
51-
new_kwargs[k] = Namespace(new_value, v)
52-
else:
53-
new_kwargs[k] = None
54-
else:
55-
new_kwargs[k] = v
47+
if merge_namespaces:
48+
new_kwargs = Namespace(new_kwargs, kwargs)
49+
else:
50+
new_kwargs.update(kwargs)
5651

5752
return new_args, new_kwargs
5853

5954

6055
def strip_prefix(s, *, prefix, strict=False):
6156
if s.startswith(prefix):
62-
return s[len(prefix):]
57+
return s[len(prefix) :]
6358
assert strict is False, f"String '{s}' does not start with prefix '{prefix}'"
6459
return s

iommi/table.py

Lines changed: 42 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
datetime,
55
time,
66
)
7-
from enum import (
8-
Enum,
9-
auto,
10-
)
7+
from enum import auto, \
8+
Enum
119
from functools import total_ordering
1210
from io import StringIO
1311
from itertools import groupby
@@ -44,13 +42,13 @@
4442
)
4543

4644
from iommi._web_compat import (
45+
format_html,
4746
HttpResponse,
4847
HttpResponseRedirect,
49-
Template,
50-
format_html,
5148
mark_safe,
5249
render_template,
5350
smart_str,
51+
Template
5452
)
5553
from iommi.action import (
5654
Action,
@@ -63,25 +61,23 @@
6361
render_attrs,
6462
)
6563
from iommi.base import (
66-
MISSING,
67-
NOT_BOUND_MESSAGE,
6864
build_as_view_wrapper,
6965
capitalize,
7066
get_display_name,
7167
items,
7268
keys,
69+
MISSING,
7370
model_and_rows,
74-
values,
71+
NOT_BOUND_MESSAGE,
72+
values
7573
)
7674
from iommi.declarative import declarative
7775
from iommi.declarative.dispatch import dispatch
78-
from iommi.declarative.namespace import (
79-
EMPTY,
80-
Namespace,
81-
flatten,
82-
getattr_path,
83-
setdefaults_path,
84-
)
76+
from iommi.declarative.namespace import EMPTY, \
77+
flatten, \
78+
getattr_path, \
79+
Namespace, \
80+
setdefaults_path
8581
from iommi.declarative.with_meta import with_meta
8682
from iommi.endpoint import (
8783
DISPATCH_PREFIX,
@@ -96,20 +92,18 @@
9692
Field,
9793
Form,
9894
)
99-
from iommi.fragment import (
100-
Fragment,
101-
Header,
102-
Tag,
103-
TransientFragment,
104-
build_and_bind_h_tag,
105-
html,
106-
)
95+
from iommi.fragment import build_and_bind_h_tag, \
96+
Fragment, \
97+
Header, \
98+
html, \
99+
Tag, \
100+
TransientFragment
107101
from iommi.from_model import (
108102
AutoConfig,
109-
NoRegisteredSearchFieldException,
110103
create_members_from_model,
111104
get_search_fields,
112105
member_from_model,
106+
NoRegisteredSearchFieldException
113107
)
114108
from iommi.member import (
115109
bind_member,
@@ -126,14 +120,14 @@
126120
Query,
127121
)
128122
from iommi.refinable import (
123+
evaluated_refinable,
129124
EvaluatedRefinable,
130125
Prio,
131126
Refinable,
127+
refinable,
132128
RefinableMembers,
133129
RefinableObject,
134-
SpecialEvaluatedRefinable,
135-
evaluated_refinable,
136-
refinable,
130+
SpecialEvaluatedRefinable
137131
)
138132
from iommi.shortcut import (
139133
Shortcut,
@@ -143,14 +137,11 @@
143137
LAST,
144138
sort_after,
145139
)
146-
from iommi.struct import (
147-
Struct,
148-
merged,
149-
)
140+
from iommi.struct import merged, \
141+
Struct
150142
from iommi.traversable import (
151143
Traversable,
152144
)
153-
154145
from ._db_compat import base_defaults_factory
155146

156147
LAST = LAST
@@ -1636,6 +1627,13 @@ class TableAutoConfig(AutoConfig):
16361627

16371628
rows = Refinable()
16381629

1630+
def endpoint__tbody(table, **_):
1631+
return {
1632+
'html': table.container.__html__(
1633+
render=lambda fragment, context: fragment.render_text_or_children(context=context)
1634+
)
1635+
}
1636+
16391637

16401638
def endpoint__csv(table, **_):
16411639
from datetime import timezone
@@ -1780,8 +1778,6 @@ class Meta:
17801778
columns: Dict[str, Column] = RefinableMembers()
17811779

17821780
class Meta:
1783-
query__advanced__assets__query_form_toggle_script__template = "iommi/query/form_toggle_script.html"
1784-
assets__table_js_select_all__template = "iommi/table/js_select_all.html"
17851781
member_class = Column
17861782
form_class = Form
17871783
query_class = Query
@@ -1790,24 +1786,6 @@ class Meta:
17901786
cells_class = Cells
17911787
row_group_class = RowGroup
17921788

1793-
@staticmethod
1794-
def endpoints__tbody__func(table, **_):
1795-
return {
1796-
'html': table.container.__html__(
1797-
render=lambda fragment, context: fragment.render_text_or_children(context=context)
1798-
)
1799-
}
1800-
1801-
endpoints__csv__func = endpoint__csv
1802-
1803-
container__attrs = Namespace(
1804-
{
1805-
'data-endpoint': lambda table, **_: DISPATCH_PREFIX + table.endpoints.tbody.iommi_path,
1806-
'data-iommi-id': lambda table, **_: table.iommi_path,
1807-
}
1808-
)
1809-
1810-
query__form__attrs = {'data-iommi-id-of-table': lambda table, **_: table.iommi_path}
18111789
columns = EMPTY
18121790
parts = EMPTY
18131791
row__attrs__class = EMPTY
@@ -1875,6 +1853,17 @@ def post_bulk_edit(table, queryset, updates, **_):
18751853
# style
18761854
query__form__actions__submit__call_target=Action.button,
18771855
title=MISSING,
1856+
endpoints__tbody__func=endpoint__tbody,
1857+
endpoints__csv__func=endpoint__csv,
1858+
query__advanced__assets__query_form_toggle_script__template = "iommi/query/form_toggle_script.html",
1859+
query__form__attrs = {
1860+
'data-iommi-id-of-table': lambda table, **_: table.iommi_path,
1861+
},
1862+
assets__table_js_select_all__template = "iommi/table/js_select_all.html",
1863+
container__attrs ={
1864+
'data-endpoint': lambda table, **_: DISPATCH_PREFIX + table.endpoints.tbody.iommi_path,
1865+
'data-iommi-id': lambda table, **_: table.iommi_path,
1866+
},
18781867
)
18791868
def __init__(
18801869
self,

iommi/table__tests.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4561,3 +4561,20 @@ class TestTable(NoSortTable):
45614561
t = t.bind(request=req('get'))
45624562
verify_table_html(table=t, expected_html=expected_html)
45634563
verify_table_html(table=t, expected_html=expected_html)
4564+
4565+
4566+
def test_table_explicit_query():
4567+
Table(
4568+
rows=[],
4569+
query=Query(
4570+
filter=lambda rows, **_: rows,
4571+
),
4572+
).bind(request=req('get'))
4573+
4574+
4575+
def test_table_list_custom_query():
4576+
Table(
4577+
rows=[],
4578+
query__filter=lambda rows, **_: rows,
4579+
query__filters__foo=Filter(),
4580+
).bind(request=req('get'))

0 commit comments

Comments
 (0)