0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Refactor image chooser modal to use preserve_url_parameters and preserve the 'multiple' parameter as well as 'select_format'

This commit is contained in:
Matt Westcott 2023-01-12 23:02:56 +00:00
parent c5513d098a
commit 6bc3b0b258
5 changed files with 32 additions and 20 deletions

View File

@ -2,7 +2,7 @@
{% load i18n wagtailadmin_tags %}
{% block filter_form %}
<form data-chooser-modal-search action="{{ results_url }}{% if will_select_format %}?select_format=true{% endif %}" method="GET" autocomplete="off" novalidate>
<form data-chooser-modal-search action="{{ results_url }}" method="GET" autocomplete="off" novalidate>
<ul class="fields">
{% for field in filter_form %}
{% include "wagtailadmin/shared/field_as_li.html" with field=field %}

View File

@ -7,7 +7,7 @@
<ul class="listing horiz images chooser">
{% for image in results %}
<li>
<a data-chooser-modal-choice class="image-choice" title="{% if collections %}{{ image.collection.name }} » {% endif %}{{ image.title }}" href="{% if will_select_format %}{% url 'wagtailimages_chooser:select_format' image.id %}{% else %}{% url 'wagtailimages_chooser:chosen' image.id %}{% endif %}">
<a data-chooser-modal-choice class="image-choice" title="{% if collections %}{{ image.collection.name }} » {% endif %}{{ image.title }}" href="{{ image.chosen_url }}">
<div class="image">{% image image max-165x165 class="show-transparency" %}</div>
<h3>{{ image.title|ellipsistrim:60 }}</h3>
</a>

View File

@ -8,7 +8,7 @@
{% image image max-800x600 class="show-transparency" %}
</div>
<div class="col6">
<form action="{% url 'wagtailimages_chooser:select_format' image.id %}" method="POST" novalidate>
<form action="{{ select_format_action_url }}" method="POST" novalidate>
{% csrf_token %}
{% for field in form %}
{% include "wagtailadmin/shared/field.html" with field=field %}

View File

@ -1768,8 +1768,9 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils):
new_image = Image.objects.get(title="Test duplicate image")
existing_image = Image.objects.get(title="Test image")
choose_new_image_action = reverse(
"wagtailimages_chooser:select_format", args=(new_image.id,)
choose_new_image_action = (
reverse("wagtailimages_chooser:select_format", args=(new_image.id,))
+ "?select_format=true"
)
self.assertEqual(
response.context["confirm_duplicate_upload_action"], choose_new_image_action
@ -1783,6 +1784,7 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils):
"next": reverse(
"wagtailimages_chooser:select_format", args=(existing_image.id,)
)
+ "?select_format=true"
}
)
)

View File

@ -18,6 +18,7 @@ from wagtail.admin.views.generic.chooser import (
ChosenViewMixin,
CreateViewMixin,
CreationFormMixin,
PreserveURLParametersMixin,
)
from wagtail.admin.viewsets.chooser import ChooserViewSet
from wagtail.images import get_image_model
@ -53,12 +54,6 @@ class ImageCreationFormMixin(CreationFormMixin):
def get_creation_form_class(self):
return get_image_form(self.model)
def get_create_url(self):
url = super().get_create_url()
if self.request.GET.get("select_format"):
url += "?select_format=true"
return url
def get_creation_form_kwargs(self):
kwargs = super().get_creation_form_kwargs()
kwargs.update(
@ -116,12 +111,18 @@ class BaseImageChooseView(BaseChooseView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(
{
"will_select_format": self.request.GET.get("select_format"),
"collections": self.collections,
}
chosen_url_name = (
"wagtailimages_chooser:select_format"
if self.request.GET.get("select_format")
else "wagtailimages_chooser:chosen"
)
for image in context["results"]:
image.chosen_url = self.append_preserved_url_parameters(
reverse(chosen_url_name, args=(image.id,))
)
context["collections"] = self.collections
return context
@ -155,13 +156,16 @@ class ImageChosenView(ChosenViewMixin, ImageChosenResponseMixin, View):
return super().get(request, *args, pk, **kwargs)
class SelectFormatResponseMixin:
class SelectFormatResponseMixin(PreserveURLParametersMixin):
def render_select_format_response(self, image, form):
action_url = self.append_preserved_url_parameters(
reverse("wagtailimages_chooser:select_format", args=(image.id,))
)
return render_modal_workflow(
self.request,
"wagtailimages/chooser/select_format.html",
None,
{"image": image, "form": form},
{"image": image, "form": form, "select_format_action_url": action_url},
json_data={"step": "select_format"},
)
@ -208,8 +212,12 @@ class ImageUploadViewMixin(SelectFormatResponseMixin, CreateViewMixin):
if request.GET.get("select_format")
else "wagtailimages_chooser:chosen"
)
choose_new_image_url = reverse(next_step_url, args=(new_image.id,))
choose_existing_image_url = reverse(next_step_url, args=(existing_image.id,))
choose_new_image_url = self.append_preserved_url_parameters(
reverse(next_step_url, args=(new_image.id,))
)
choose_existing_image_url = self.append_preserved_url_parameters(
reverse(next_step_url, args=(existing_image.id,))
)
cancel_duplicate_upload_action = (
f"{reverse('wagtailimages:delete', args=(new_image.id,))}?"
@ -295,6 +303,7 @@ class ImageChooserViewSet(ChooserViewSet):
select_format_view_class = ImageSelectFormatView
permission_policy = permission_policy
register_widget = False
preserve_url_parameters = ChooserViewSet.preserve_url_parameters + ["select_format"]
icon = "image"
choose_one_text = _("Choose an image")
@ -307,6 +316,7 @@ class ImageChooserViewSet(ChooserViewSet):
def select_format_view(self):
return self.select_format_view_class.as_view(
model=self.model,
preserve_url_parameters=self.preserve_url_parameters,
)
def get_urlpatterns(self):