Skip to content

Commit

Permalink
Finish skeleton for block tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel2392 committed Apr 10, 2024
1 parent ca4ca57 commit 3edc713
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 36 deletions.
85 changes: 49 additions & 36 deletions wagtail_fedit/templatetags/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,44 +131,19 @@ def render(self, context):
if not _can_edit(request, model):
return rendered

admin_edit_url = edit_url(
model,
request,
hash=f"block-{block_id}-section",
if self.has_block:
extra["has_block"] = self.has_block

return render_editable_block(
request=request,
content=rendered,
block_id=block_id,
field_name=field_name,
model=model,
context=context,
**extra,
)

extra["has_block"] = self.has_block

items = [
FeditBlockEditButton(),
]

for hook in hooks.get_hooks(CONSTRUCT_BLOCK_TOOLBAR):
hook(request=request, items=items, model=model, block_id=block_id, field_name=field_name)

items = [item.render(request) for item in items]
items = list(filter(None, items))

return render_to_string(
"wagtail_fedit/content/editable_block.html",
{
"edit_url": self.get_edit_url(
block_id, field_name,
instance=model,
**extra,
),
"admin_edit_url": admin_edit_url,
"block_id": block_id,
"model": model,
"content": rendered,
"field_name": field_name,
"parent_context": context,
"wagtail_fedit_field_name": field_name,
"wagtail_fedit_instance": model,
"toolbar_items": items,
}
)

@staticmethod
def pack(**kwargs) -> dict:

Expand Down Expand Up @@ -419,6 +394,44 @@ def render_editable_field(request, content, field_name, model, context, **kwargs
request=request,
)

def render_editable_block(request, content, block_id, field_name, model, context, **kwargs):
admin_edit_url = edit_url(
model,
request,
hash=f"block-{block_id}-section",
)

items = [
FeditBlockEditButton(),
]

for hook in hooks.get_hooks(CONSTRUCT_BLOCK_TOOLBAR):
hook(request=request, items=items, model=model, block_id=block_id, field_name=field_name)

items = [item.render(request) for item in items]
items = list(filter(None, items))

return render_to_string(
"wagtail_fedit/content/editable_block.html",
{
"edit_url": BlockEditNode.get_edit_url(
block_id, field_name,
instance=model,
**kwargs,
),
"admin_edit_url": admin_edit_url,
"block_id": block_id,
"model": model,
"content": content,
"field_name": field_name,
"parent_context": context,
"wagtail_fedit_field_name": field_name,
"wagtail_fedit_instance": model,
"toolbar_items": items,
}
)


def get_kwargs(parser: Parser, kwarg_list: list[str], tokens: list[str]) -> dict:
had_kwargs = False
kwargs = {}
Expand Down
44 changes: 44 additions & 0 deletions wagtail_fedit/test/core/tests/test_block_templatetag.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,50 @@ def test_render_regular(self):
block.render(context)
)

def test_render_editable(self):

block, contentpath = utils.find_block(self.BLOCK_ID, self.basic_model.content)
self.assertEqual(block.value["link"]["text"], "Test Item 1")
self.assertEqual(contentpath, ["3e9144fd-5fa5-47f8-917e-8fe87c15da01", "items", self.BLOCK_ID])

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 1")

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

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

# Mark as editable.
setattr(
request,
utils.FEDIT_PREVIEW_VAR,
True,
)

node = templatetags.BlockEditNode(
nodelist=None,
block=block,
block_id=self.BLOCK_ID,
field_name="content",
model=self.basic_model,
)

rendered_block = block.render(context)
rendered_block = templatetags.render_editable_block(
request, rendered_block, self.BLOCK_ID, "content",
self.basic_model, context, has_block=True,
)

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



Expand Down

0 comments on commit 3edc713

Please sign in to comment.