0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Make chooser_select_format into a class-based view

This commit is contained in:
Matt Westcott 2022-07-01 19:56:08 +01:00 committed by Matt Westcott
parent 21d172200c
commit e173ca9f22
2 changed files with 24 additions and 18 deletions

View File

@ -46,7 +46,7 @@ urlpatterns = [
path("chooser/upload/", chooser.ImageUploadView.as_view(), name="chooser_upload"),
path(
"chooser/<int:image_id>/select_format/",
chooser.chooser_select_format,
chooser.ImageSelectFormatView.as_view(),
name="chooser_select_format",
),
]

View File

@ -278,10 +278,20 @@ class ImageUploadView(
pass
def chooser_select_format(request, image_id):
image = get_object_or_404(get_image_model(), id=image_id)
class ImageSelectFormatView(View):
def get(self, request, image_id):
image = get_object_or_404(get_image_model(), id=image_id)
initial = {"alt_text": image.default_alt_text}
initial.update(request.GET.dict())
# If you edit an existing image, and there is no alt text, ensure that
# "image is decorative" is ticked when you open the form
initial["image_is_decorative"] = initial["alt_text"] == ""
form = ImageInsertionForm(initial=initial, prefix="image-chooser-insertion")
return self.render_select_format_response(image, form)
def post(self, request, image_id):
image = get_object_or_404(get_image_model(), id=image_id)
if request.method == "POST":
form = ImageInsertionForm(
request.POST,
initial={"alt_text": image.default_alt_text},
@ -315,18 +325,14 @@ def chooser_select_format(request, image_id):
None,
json_data={"step": "chosen", "result": image_data},
)
else:
initial = {"alt_text": image.default_alt_text}
initial.update(request.GET.dict())
# If you edit an existing image, and there is no alt text, ensure that
# "image is decorative" is ticked when you open the form
initial["image_is_decorative"] = initial["alt_text"] == ""
form = ImageInsertionForm(initial=initial, prefix="image-chooser-insertion")
else:
return self.render_select_format_response(image, form)
return render_modal_workflow(
request,
"wagtailimages/chooser/select_format.html",
None,
{"image": image, "form": form},
json_data={"step": "select_format"},
)
def render_select_format_response(self, image, form):
return render_modal_workflow(
self.request,
"wagtailimages/chooser/select_format.html",
None,
{"image": image, "form": form},
json_data={"step": "select_format"},
)