neon-jungle / wagtail-birdsong

Create, send, preview, edit and test email campaigns from within Wagtail
BSD 3-Clause "New" or "Revised" License
103 stars 27 forks source link

Reload Preview not working #14

Closed kfields closed 2 years ago

kfields commented 2 years ago

My pyproject.toml:

wagtail = "^2.15.2"
wagtail-birdsong = "^0.2.0"

I'm getting:

[27/Jan/2022 12:13:32] "POST /admin/newsletter/newsletter/edit/2/ HTTP/1.1" 403 2519
Forbidden (CSRF token missing or incorrect.): /admin/newsletter/newsletter/edit/2/

Also the green wagtail header is messed up:

2022-01-27

I'll try to look into it further later. For now I'm just going to overload the birdsong templates and these:

    form_view_extra_js = ['birdsong/js/preview_campaign.js']
    form_view_extra_css = ['birdsong/css/campaign-editor.css']

Let me know if I can be of assistance. Thank you!!!

kfields commented 2 years ago

I fixed the CSRF issue, but now I'm getting this:

Traceback (most recent call last):
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/admin/urls/__init__.py", line 125, in wrapper
    return view_func(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/admin/auth.py", line 174, in decorated_view
    response = view_func(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/contrib/modeladmin/options.py", line 413, in edit_view
    return view_class.as_view(**kwargs)(request)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
    return self.dispatch(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/contrib/modeladmin/views.py", line 707, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/contrib/modeladmin/views.py", line 81, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/views/generic/base.py", line 98, in dispatch
    return handler(request, *args, **kwargs)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/birdsong/views/editor.py", line 89, in post
    return ajax_preview(request, self)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/birdsong/views/editor.py", line 66, in ajax_preview
    if form.is_valid():
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/modelcluster/forms.py", line 318, in is_valid
    form_is_valid = super(ClusterForm, self).is_valid()
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/forms/forms.py", line 175, in is_valid
    return self.is_bound and not self.errors
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/forms/forms.py", line 170, in errors
    self.full_clean()
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/forms/forms.py", line 372, in full_clean
    self._clean_fields()
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/forms/forms.py", line 384, in _clean_fields
    value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/core/blocks/base.py", line 535, in value_from_datadict
    return self.block_def.value_from_datadict(data, files, name)
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/wagtail/core/blocks/stream_block.py", line 97, in value_from_datadict
    count = int(data['%s-count' % prefix])
  File "/home/kurt/.cache/pypoetry/virtualenvs/polrev-root-FWL1_uR--py3.10/lib/python3.10/site-packages/django/utils/datastructures.py", line 78, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'body-count'
[27/Jan/2022 13:32:19] "POST /admin/newsletter/newsletter/edit/2/ HTTP/1.1" 500 186371
kfields commented 2 years ago

I got tired of messing with it so I created a preview tab and a custom EditHandler. I'll put together a PR if anyone likes this solution

2022-01-27 (1)

tjwalch commented 2 years ago

Yes, please make a PR, that would be appreciated.

danhayden commented 2 years ago

I've been trying to get this working on a project for a few days now and still unable to find the root cause of why wagtail-birdsong preview is broken in wagtail 2.15.0

After digging around I have found that in wagtail 2.14.2 request.POST in https://github.com/neon-jungle/wagtail-birdsong/blob/master/birdsong/views/editor.py#L65 returns something like:

<QueryDict: {
  "csrfmiddlewaretoken": ["xxxxxxxxxx"],
  "name": ["name"],
  "subject": ["subject"],
  "body-count": ["1"],
  "body-0-deleted": [""],
  "body-0-order": ["0"],
  "body-0-type": ["rich_text"],
  "body-0-id": ["xxxxxxxxxx"],
  "body-0-value": [
    {
      "blocks": [...],
      "entityMap": {}
    }
  ]
}>

however in wagtail 2.15.0 request.POST returns only <QueryDict: {}> which produces the error django.utils.datastructures.MultiValueDictKeyError: 'body-count'

I'm yet to find what has changed in wagtail 2.15.0 that would cause this.. Does anyone have an ideas as I'm not sure where to look next.

seb-b commented 2 years ago

@danhayden I think the structure of the templates has changed between 2.13 -> 2.15, that's why the UI looks a little messed up when you upgrade (random bars down the bottom).

If the post is empty I'd check the markup and make sure that those fields are in the form, at that the form tag encloses them properly could be that the browser is auto-closing the tag so it ends up with something like -

<form> 
   ... empty ...
 </form>

 <input />
<input />
etc