Skip to content

Commit

Permalink
Block templatetag render test; skeleton for more tests; fixes to last…
Browse files Browse the repository at this point in the history
… commit (missing context from _resolve_expressions)
  • Loading branch information
Nigel2392 committed Apr 9, 2024
1 parent 934c051 commit 57d43c5
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
5 changes: 3 additions & 2 deletions wagtail_fedit/templatetags/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def render(self, context):
field_name = context["wagtail_fedit_field_name"]

block, block_id, field_name, model =\
_resolve_expressions(block, block_id, field_name, model)
_resolve_expressions(context, block, block_id, field_name, model)

if not block_id and "block_id" not in context and not block:
raise ValueError("Block ID is required")
Expand Down Expand Up @@ -291,7 +291,8 @@ def render(self, context):
model = self.model
inline = self.inline

model, inline = _resolve_expressions(model, inline)
model, inline =\
_resolve_expressions(context, model, inline)

obj = model
for i in range(len(getters) - 1):
Expand Down
59 changes: 52 additions & 7 deletions wagtail_fedit/test/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
from django.http import HttpResponse
from django.utils.text import slugify
from wagtail.snippets.models import register_snippet
from django.template import Template, Context
from wagtail import blocks
from wagtail.fields import StreamField
from wagtail.fields import (
StreamField,
StreamValue,
)
from wagtail.models import (
Page,
LockableMixin,
Expand All @@ -22,19 +26,60 @@ class HeadingComponent(blocks.StructBlock):
heading = blocks.CharBlock(max_length=25)
subheading = blocks.CharBlock(max_length=40)

def render(self, value, context=None):
template = Template("""
<h1>{{ heading }}</h1>
<h2>{{ subheading }}</h2>
""")

context = self.get_context(value, parent_context=context)
return template.render(Context(context))

class LinkBlock(blocks.StructBlock):
text = blocks.CharBlock(max_length=25)

def render(self, value, context=None):
template = Template("""
<a href="#">{{ text }}</a>
""")

context = self.get_context(value, parent_context=context)
return template.render(Context(context))

class MenuItemBlock(blocks.StructBlock):
link = LinkBlock()

def render(self, value, context=None):
template = Template("""
{% load wagtailcore_tags %}
<li>{% include_block self.link %}</li>
""")

context = self.get_context(value, parent_context=context)
return template.render(Context(context))

class FlatMenuComponent(blocks.StructBlock):
title = blocks.CharBlock(max_length=25)
subtitle = blocks.RichTextBlock()
items = blocks.ListBlock(
MenuItemBlock()
)

def render(self, value, context=None):
template = Template("""
{% load wagtailcore_tags %}
<h1>{{ title }}</h1>
<p>{{ subtitle }}</p>
<ul>
{% for item in items %}
{% include_block item %}
{% endfor %}
</ul>
""")

context = self.get_context(value, parent_context=context)
return template.render(Context(context))


class BaseEditableMixin:
def get_url(self, request):
Expand All @@ -47,7 +92,7 @@ def serve(self, request, *args, **kwargs):
class BasicModel(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand All @@ -56,7 +101,7 @@ class BasicModel(models.Model):
class EditableFullModel(BaseEditableMixin, FEditableMixin):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand All @@ -66,7 +111,7 @@ class EditableFullModel(BaseEditableMixin, FEditableMixin):
class EditableDraftModel(BaseEditableMixin, DraftStateMixin, RevisionMixin, models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand All @@ -76,7 +121,7 @@ class EditableDraftModel(BaseEditableMixin, DraftStateMixin, RevisionMixin, mode
class EditableRevisionModel(BaseEditableMixin, RevisionMixin, models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand All @@ -86,7 +131,7 @@ class EditableRevisionModel(BaseEditableMixin, RevisionMixin, models.Model):
class EditablePreviewModel(BaseEditableMixin, PreviewableMixin, models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand All @@ -95,7 +140,7 @@ class EditablePreviewModel(BaseEditableMixin, PreviewableMixin, models.Model):
class EditableLockModel(BaseEditableMixin, WorkflowMixin, DraftStateMixin, RevisionMixin, LockableMixin, models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
content = StreamField([
content: StreamValue = StreamField([
("heading_component", HeadingComponent()),
("flat_menu_component", FlatMenuComponent())
], use_json_field=True)
Expand Down
52 changes: 52 additions & 0 deletions wagtail_fedit/test/core/tests/test_block_templatetag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.contrib.sessions.middleware import SessionMiddleware
from wagtail import blocks

from wagtail_fedit.templatetags import (
fedit as templatetags,
)
from wagtail_fedit import (
utils,
)
from .base import (
BaseFEditTest,
)

class TestBlockTemplateTag(BaseFEditTest):

def test_render_regular(self):

block, contentpath = utils.find_block("a98a19c6-2ead-4e69-9ea2-3158c7e82976", self.basic_model.content)
self.assertEqual(block.value["link"]["text"], "Test Item 3")
self.assertEqual(contentpath, ["3e9144fd-5fa5-47f8-917e-8fe87c15da01", "items", "a98a19c6-2ead-4e69-9ea2-3158c7e82976"])

item = self.basic_model.content.stream_block.get_block_by_content_path(self.basic_model.content, contentpath)
self.assertEqual(item.value["link"]["text"], "Test Item 3")

# Setup user attribute for request.
request = self.request_factory.get("/")
request.user = self.admin_user

context = {
"request": request,
"model": self.basic_model,
"block": block,
}

node = templatetags.BlockEditNode(
nodelist=None,
block=block,
block_id="a98a19c6-2ead-4e69-9ea2-3158c7e82976",
field_name="content",
model=self.basic_model,
)

self.assertHTMLEqual(
node.render(context),
block.render(context)
)






Empty file.

0 comments on commit 57d43c5

Please sign in to comment.