wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
226 stars 86 forks source link

ImageChooser/DocumentChooser in StructBlock in ListBlock results in crash #625

Closed th3hamm0r closed 2 years ago

th3hamm0r commented 2 years ago

Currently, if you want to translate a page that contains a ListBlock with a StructBlock, and that StructBlock itself contains an ImageChooserBlock (or a DocumentChooserBlock), and you select something in the original page, the edit-page of the translated page will always fail with the following error:

2022-10-04 11:45:38 ERROR django.request: Internal Server Error: /admin/content/pages/18/edit/ (log.py:224)
Traceback (most recent call last):
  File "/.../lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/.../lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/.../lib/python3.10/site-packages/sentry_sdk/integrations/django/views.py", line 67, in sentry_wrapped_callback
    return callback(request, *args, **kwargs)
  File "/.../lib/python3.10/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/.../lib/python3.10/site-packages/wagtail/admin/urls/__init__.py", line 165, in wrapper
    return view_func(request, *args, **kwargs)
  File "/.../lib/python3.10/site-packages/wagtail/admin/auth.py", line 205, in decorated_view
    return view_func(request, *args, **kwargs)
  File "/.../lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/.../lib/python3.10/site-packages/wagtail/admin/views/pages/edit.py", line 347, in dispatch
    response = self.run_hook("before_edit_page", self.request, self.page)
  File "/.../lib/python3.10/site-packages/wagtail/admin/views/generic/mixins.py", line 27, in run_hook
    result = fn(*args, **kwargs)
  File "/.../lib/python3.10/site-packages/wagtail_localize/wagtail_hooks.py", line 262, in before_edit_page
    return edit_translation.edit_translation(request, translation, page)
  File "/.../lib/python3.10/site-packages/wagtail_localize/views/edit_translation.py", line 750, in edit_translation
    location_info = get_segment_location_info(
  File "/.../lib/python3.10/site-packages/wagtail_localize/views/edit_translation.py", line 523, in get_segment_location_info
    "widget": widget_from_block(block_type, content_components)
  File "/.../lib/python3.10/site-packages/wagtail_localize/views/edit_translation.py", line 500, in widget_from_block
    return widget_from_block(block.child_block, content_components[1:])
TypeError: 'NoneType' object is not subscriptable

The used StreamBlock for the StreamField looks like this:

from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock
class PageStreamBlock(blocks.StreamBlock):
    team = blocks.ListBlock(blocks.StructBlock([
        ('image', ImageChooserBlock(required=False)),
        ('name', blocks.CharBlock()),
    ]))

Only setting a "name" in the above example, won't result in a crash, but as soon as you select an image, it will fail.

Setup:

Related:

525