Skip to content

Commit

Permalink
Fixes to context processors when re-rendering; added tests for contex…
Browse files Browse the repository at this point in the history
…t processors
  • Loading branch information
Nigel2392 committed Apr 15, 2024
1 parent 7b5a9a0 commit b33636d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 19 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = wagtail_fedit
version = 1.5.0a2
version = 1.5.0a3
description = Frontend editing for your Wagtail site
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div id="{{ adapter.get_element_id }}" class="wagtail-fedit-adapter-wrapper{%if shared_context.inline%} wagtail-fedit-inline{%endif%} wagtail-fedit-{{ identifier }}"{% if shared %} data-shared-context="{{ shared }}"{%endif%} data-edit-url="{{ edit_url }}">
{% load fedit %}<div id="{{ adapter.get_element_id }}" class="wagtail-fedit-adapter-wrapper{%if shared_context.inline%} wagtail-fedit-inline{%endif%} wagtail-fedit-{{ identifier }}"{% if shared %} data-shared-context="{{ shared }}"{%endif%} data-edit-url="{{ edit_url }}">
<div class="wagtail-fedit-buttons">
{% for button in buttons %}
{{ button }}
{% endfor %}
</div>{{ content|safe }}
</div>{% render_adapter adapter %}
</div>
35 changes: 25 additions & 10 deletions wagtail_fedit/templatetags/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
library, Node, TemplateSyntaxError,
)
from django.template.loader import render_to_string
from django.template.context import (
make_context,
Context,
)
from django.template.base import (
Parser, Token,
FilterExpression,
Expand Down Expand Up @@ -156,17 +160,10 @@ def do_render_fedit(parser: Parser, token: Token):
)


def wrap_adapter(request: HttpRequest, adapter: BaseAdapter, context: dict) -> str:
def wrap_adapter(request: HttpRequest, adapter: BaseAdapter, context: dict, run_context_processors: bool = False) -> str:
if not context:
context = {}

context["wagtail_fedit_field"] = adapter.field_name
context["wagtail_fedit_instance"] = adapter.object
context["request"] = request

content = adapter.render_content(context)
shared = adapter.encode_shared_context()

items = [
FeditAdapterEditButton(),
]
Expand All @@ -185,23 +182,41 @@ def wrap_adapter(request: HttpRequest, adapter: BaseAdapter, context: dict) -> s
"model_id": adapter.object.pk,
}

shared = adapter.encode_shared_context()

return render_to_string(
"wagtail_fedit/content/editable_adapter.html",
{
"identifier": adapter.identifier,
"content": content,
"adapter": adapter,
"buttons": items,
"shared": shared,
"shared_context": adapter.kwargs,
"parent_context": context,
"edit_url": reverse(
"wagtail_fedit:edit",
kwargs=reverse_kwargs,
),
},
request=request,
request=request if run_context_processors else None,
)

@register.simple_tag(takes_context=True)
def render_adapter(context: dict, adapter: BaseAdapter) -> str:
context = context.flatten()

parent_context = context.pop("parent_context", {})
if isinstance(parent_context, Context):
parent_context = parent_context.flatten()

context.update(parent_context)

context["wagtail_fedit_field"] = adapter.field_name
context["wagtail_fedit_instance"] = adapter.object
context["request"] = adapter.request

return adapter.render_content(context)


def get_kwargs(parser: Parser, tokens: list[str], kwarg_list: list[str] = None) -> dict:
had_kwargs = False
Expand Down
5 changes: 5 additions & 0 deletions wagtail_fedit/test/core/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def test_context_processor(request):
return {
"fedit": True,
"testing": "testing context processor",
}
39 changes: 39 additions & 0 deletions wagtail_fedit/test/core/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,16 @@ class TestBlockAdapter(BlockAdapter, TestAdapter):
class TestFieldAdapter(FieldAdapter, TestAdapter):
identifier = "test_field"

class TestContextAdapter(TestAdapter):
identifier = "test_context"

def render_content(self, parent_context: dict = None) -> str:
return parent_context["testing"]

adapter_registry.register(TestAdapter)
adapter_registry.register(TestBlockAdapter)
adapter_registry.register(TestFieldAdapter)
adapter_registry.register(TestContextAdapter)


class TestBaseAdapter(BaseFEditTest):
Expand Down Expand Up @@ -167,6 +174,38 @@ def test_adapter_editable(self):
wrap_adapter(request, adapters[4], {})
)

def test_context_processors_run(self):
tpl = Template(
"{% load fedit %}"
"{% fedit test_context object.title test='test' id=5 %}"
)

request = self.request_factory.get(
self.get_editable_url(
self.basic_model.pk, self.basic_model._meta.app_label, self.basic_model._meta.model_name,
)
)
request.user = self.admin_user

setattr(
request,
FEDIT_PREVIEW_VAR,
True,
)

tpl = tpl.render(
Context({
"request": request,
"object": self.basic_model,
"testing": "testing context processor",
}),
)

self.assertHTMLEqual(
tpl,
wrap_adapter(request, adapters[5], {}, run_context_processors=True)
)


class TestBlockAdapter(BaseFEditTest):

Expand Down
1 change: 1 addition & 0 deletions wagtail_fedit/test/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'wagtail_fedit.test.core.context_processors.test_context_processor'
],
},
},
Expand Down
7 changes: 1 addition & 6 deletions wagtail_fedit/views/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.utils.decorators import method_decorator
from django.template.loader import render_to_string
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.template.context import make_context
from django.views.generic import View
from django.http import (
HttpRequest,
Expand Down Expand Up @@ -183,16 +182,12 @@ def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
# Wagtail uses this internally; for example in `{% wagtailpagecache %}`
context[PAGE_TEMPLATE_VAR] = self.instance

context = make_context(
context,
self.request,
).flatten()

# Render the frame HTML
html = wrap_adapter(
request=self.request,
adapter=self.adapter,
context=context,
run_context_processors=True,
)

return JsonResponse({
Expand Down

0 comments on commit b33636d

Please sign in to comment.