Skip to content

Commit 81306a0

Browse files
authored
feat: Autocomplete fields for grouper selection and option for less verbose UI (#433)
* feat: Less verbose UI and autocomplete fields * Fix linting * Retain model choice form field for models with no admin or no search fields * Fix type hinting for Py 3.9 * Update github test action * Update docs * Fix form styling
1 parent 3b3fa2f commit 81306a0

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
lines changed

djangocms_versioning/admin.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ class VersionAdmin(ChangeListActionsMixin, admin.ModelAdmin, metaclass=MediaDefi
618618
actions = ["compare_versions", "delete_selected"]
619619
list_display = (
620620
"number",
621-
"created",
621+
) + (
622+
("created",) if conf.VERBOSE_UI else ()
623+
) + (
622624
"modified",
623625
"content",
624626
"created_by",
@@ -639,6 +641,9 @@ class VersionAdmin(ChangeListActionsMixin, admin.ModelAdmin, metaclass=MediaDefi
639641
class Media:
640642
js = ["djangocms_versioning/js/versioning.js"]
641643

644+
def has_module_permission(self, request):
645+
return conf.VERBOSE_UI
646+
642647
def get_changelist(self, request, **kwargs):
643648
return VersionChangeList
644649

@@ -963,10 +968,12 @@ def grouper_form_view(self, request):
963968
to show versions of.
964969
"""
965970
language = get_language_from_request(request)
971+
versionable = versionables.for_content(self.model._source_model)
966972
context = dict(
967973
self.admin_site.each_context(request),
968974
opts=self.model._meta,
969-
form=grouper_form_factory(self.model._source_model, language)(),
975+
form=grouper_form_factory(self.model._source_model, language, self.admin_site)(),
976+
title=_("Select {} to view its versions").format(versionable.grouper_model._meta.verbose_name),
970977
)
971978
return render(request, "djangocms_versioning/admin/grouper_form.html", context)
972979

djangocms_versioning/conf.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,9 @@
3333
settings, "DJANGOCMS_VERISONING_ON_PUBLISH_REDIRECT", "published"
3434
)
3535
#: Allowed values: "versions", "published", "preview"
36+
37+
VERBOSE_UI = getattr(
38+
settings, "DJANGOCMS_VERSIONING_VERBOSE_UI", True
39+
)
40+
#: If True, the version admin will be offered in the admin index
41+
#: for each registered versionable model.

djangocms_versioning/forms.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1+
from __future__ import annotations
2+
13
from functools import lru_cache
24

35
from django import forms
6+
from django.contrib.admin.widgets import AutocompleteSelect
47

58
from . import versionables
69

710

11+
class VersionAutocompleteSelect(AutocompleteSelect):
12+
def optgroups(self, name: str, value: str, attr: dict | None = None):
13+
default = (None, [], 0)
14+
default[1].append(self.create_option(name, "", "", False, 0))
15+
return [default]
16+
17+
818
class VersionContentChoiceField(forms.ModelChoiceField):
919
"""Form field used to display a list of grouper instances"""
1020

11-
def __init__(self, *args, **kwargs):
21+
def __init__(self, *args, model=None, admin_site=None, **kwargs):
1222
self.language = kwargs.pop("language")
1323
self.predefined_label_method = kwargs.pop("option_label_override")
24+
if getattr(admin_site._registry.get(model), "search_fields", []):
25+
# If the model is registered in the admin, use the autocomplete widget
26+
kwargs.setdefault("widget", VersionAutocompleteSelect(
27+
model._meta.get_field(versionables.for_content(model).grouper_field_name),
28+
admin_site=admin_site,
29+
))
1430
super().__init__(*args, **kwargs)
1531

1632
def label_from_instance(self, obj):
@@ -22,30 +38,30 @@ def label_from_instance(self, obj):
2238

2339

2440
@lru_cache
25-
def grouper_form_factory(content_model, language=None):
41+
def grouper_form_factory(content_model, language=None, admin_site=None):
2642
"""Returns a form class used for selecting a grouper to see versions of.
2743
Form has a single field - grouper - which is a model choice field
2844
with available grouper objects for specified content model.
2945
3046
:param content_model: Content model class
3147
:param language: Language
3248
"""
33-
versionable = versionables.for_content(content_model)
34-
valid_grouper_pk = content_model.admin_manager\
35-
.latest_content()\
36-
.values_list(versionable.grouper_field_name, flat=True)
49+
if admin_site is None:
50+
from django.contrib.admin import site
51+
admin_site = site
3752

53+
versionable = versionables.for_content(content_model)
3854
return type(
3955
content_model.__name__ + "GrouperForm",
4056
(forms.Form,),
4157
{
4258
"_content_model": content_model,
4359
versionable.grouper_field_name: VersionContentChoiceField(
44-
queryset=versionable.grouper_model.objects.filter(
45-
pk__in=valid_grouper_pk,
46-
),
47-
label=versionable.grouper_model._meta.verbose_name,
60+
label=versionable.grouper_model._meta.verbose_name.capitalize(),
61+
queryset=versionable.grouper_model.objects.all(),
4862
option_label_override=versionable.grouper_selector_option_label,
63+
admin_site=admin_site,
64+
model=content_model,
4965
language=language,
5066
),
5167
},

djangocms_versioning/templates/djangocms_versioning/admin/grouper_form.html

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "admin/base_site.html" %}
1+
{% extends "admin/change_form.html" %}
22
{% load i18n admin_urls static admin_list %}
33

44
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}
@@ -13,6 +13,7 @@
1313
{% endblock %}
1414
{% endif %}
1515

16+
{% block extrastyle %}{{ block.super }}{{ form.media }}{% endblock extrastyle %}
1617
{% block coltype %}flex{% endblock %}
1718

1819
{% block content %}
@@ -33,7 +34,13 @@
3334
{% endblock %}
3435
<div class="module" id="changelist">
3536
<form action="{% url opts|admin_urlname:"changelist" %}">
36-
{{ form }}
37+
<fieldset class="module aligned">
38+
<div class="form-row">
39+
<div class="flex-container">
40+
{{ form }}
41+
</div>
42+
</div>
43+
</fieldset>
3744
<button type="submit">{% translate "Submit" %}</button>
3845
</form>
3946
</form>

docs/api/advanced_configuration.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ Must be defined if the :ref:`extra_grouping_fields` option has been set. This wi
121121
grouper_selector_option_label
122122
++++++++++++++++++++++++++++++
123123

124-
If the version table link is specified without a grouper param, a form with a dropdown of grouper objects will display. This setting defines how the labels of those groupers will display on the dropdown.
124+
If the version table link is specified without a grouper param, a form with a dropdown of grouper objects will display. By default, if the grouper object is registered with the
125+
admin and has a ``search_fields`` attribute, the dropdown will be an autocomplete
126+
field which will display the object's ``__str__`` method. This is the recommended
127+
method.
128+
129+
For models not registerd with the admin, or without search fields, this setting defines how the labels of those groupers will display on the dropdown (regular select field).
125130

126131

127132
.. code-block:: python

docs/settings.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ Settings for djangocms Versioning
8989
* ``"preview"``: The user will be redirected to the content object's
9090
preview endpoint.
9191

92+
.. py:attribute:: DJANGOCMS_VERISONING_VERBOSE_UI
93+
94+
Defaults to ``True``
95+
96+
For many users it is sufficient to interact with djangocms-versioning
97+
through a less verbose UI. If set to ``False``, djangocms-versioning will
98+
not display the creation date in the "manage versions" view. Also, it will
99+
remove its entries in the django admin overview page (index).
100+
"manage versions" remains accessible trough the version menu in the CMS
101+
toolbar.

0 commit comments

Comments
 (0)