Skip to content

Commit e02d655

Browse files
authored
Merge pull request #609 from iommirocks/admin-config-expansion
2 parents 74dbe24 + 2ec35f7 commit e02d655

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

docs/test_doc_admin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,21 @@ class Meta:
221221
`parts__all_models__columns__model_name__cell` and `data-iommi-type` is
222222
:doc:`Cell`. In the docs for `Cell` you can find that cells have `attrs`.
223223
"""
224+
225+
226+
def test_change_grouping(small_discography):
227+
# language=rst
228+
"""
229+
Change grouping of models
230+
-------------------------
231+
232+
By default iommi groups models in the admin by the app they belong to. You can override this with the `group` argument:
233+
"""
234+
235+
class MyAdmin(Admin):
236+
class Meta:
237+
apps__docs_album__group = 'The endless search for where you are'
238+
239+
# @test
240+
show_output(MyAdmin.all_models().as_view()(staff_req('get')))
241+
# @end

iommi/admin.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -306,48 +306,50 @@ def own_evaluate_parameters(self):
306306
operation='all_models',
307307
)
308308
def all_models(cls, table=None, **kwargs):
309-
def rows(admin, request, included_filter=False, **_):
309+
def rows_raw(admin, request, included_filter=False, **_):
310310
for app_name, models in items(django_apps.all_models):
311-
has_yielded_header = False
312-
313311
for model_name, model in sorted(items(models), key=lambda x: x[1]._meta.verbose_name_plural):
314312
key = f'{app_name}_{model_name}'
315-
included = admin.apps.get(key, {}).get('include', False)
313+
conf = admin.apps.get(key, {})
314+
included = conf.get('include', False)
316315
if included == included_filter:
317316
continue
318317

319318
if not cls.has_permission(request, instance=None, model=model, operation='view'):
320319
continue
321320

322-
if not has_yielded_header:
323-
yield Struct(
324-
name=app_verbose_name_by_label[app_name],
325-
verbose_app_name=app_verbose_name_by_label[app_name],
326-
app_name=None,
327-
model_name=None,
328-
url=None,
329-
format=lambda row, table, **_: (
330-
html.h1(row.name, _name='invalid_name').bind(parent=table).__html__()
331-
),
332-
key=None,
333-
)
334-
has_yielded_header = True
321+
group = conf.get('group', app_verbose_name_by_label[app_name])
335322

336323
yield Struct(
337-
verbose_app_name=app_verbose_name_by_label[app_name],
324+
name=conf.get('name', model._meta.verbose_name_plural.capitalize()),
325+
group=group,
338326
app_name=app_name,
339-
model_name=app_name,
340-
name=model._meta.verbose_name_plural.capitalize(),
327+
model_name=model_name,
341328
url='{}/{}/'.format(app_name, model_name),
342329
format=lambda row, **_: row.name,
343330
key=key,
344331
)
345332

333+
def rows(admin, request, included_filter=False, **_):
334+
last_group = None
335+
for row in sorted(rows_raw(admin=admin, request=request, included_filter=included_filter), key=lambda row: (row.group, row.name)):
336+
if last_group != row.group:
337+
yield Struct(
338+
name=row.group,
339+
url=None,
340+
format=lambda row, table, **_: (
341+
html.h1(row.name, _name='invalid_name').bind(parent=table).__html__()
342+
),
343+
key=None,
344+
)
345+
last_group = row.group
346+
yield row
347+
346348
table = setdefaults_path(
347349
Namespace(),
348350
table if table is not None else {},
349351
title='',
350-
call_target__cls=Table,
352+
call_target__cls=cls.get_meta().table_class,
351353
call_target__attribute='div',
352354
sortable=False,
353355
rows=rows,
@@ -364,10 +366,11 @@ def rows(admin, request, included_filter=False, **_):
364366
add_models = setdefaults_path(
365367
Namespace(),
366368
include=settings.DEBUG,
367-
call_target__cls=Table,
369+
call_target__cls=cls.get_meta().table_class,
368370
sortable=False,
369-
rows=functools.partial(rows, included_filter=True),
371+
rows=functools.partial(rows_raw, included_filter=True),
370372
page_size=None,
373+
columns__app_name=cls.get_meta().table_class.get_meta().member_class(auto_rowspan=True),
371374
columns__conf=(
372375
cls.get_meta()
373376
.table_class.get_meta()

0 commit comments

Comments
 (0)