Skip to content

Commit

Permalink
Improved error messages; removed unused hooks; added documentation fo…
Browse files Browse the repository at this point in the history
…r adapter toolbar hook
  • Loading branch information
Nigel2392 committed Apr 15, 2024
1 parent f306d7b commit 80bbbf2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 40 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ Getting Started
{# myapp/render_my_field.html #}
{% load fedit %}
{% for block in self.content %}
{# Sub-Blocks wrapped by fedit_block do not require the field_name or model argument. #}
{# This is taken from the parent (also wrapped by `fedit_block`); the model and field name are shared through context. #}
{# Sub-Blocks wrapped by {# fedit block #} do not require the field_name or model argument. #}
{# This can (and probably should be) replaced with the `from_context` argument. #}
{# The variables are then taken from the parent {% fedit %} tag. The model and field name are shared through context. #}
{# This keeps everything extensible; but in this case it does not matter since what we are doing is instance- specific. #}
{% fedit block self.content block=block block_id=block.id %}
{% endfor %}
Expand All @@ -101,8 +103,8 @@ Getting Started
class MyPage(...): # Can be any type of model.
content = StreamField(...)

def render_fedit_content(self, request):
return render_to_string("myapp/render_my_field.html", self.get_context(request))
def render_fedit_content(self, request, context):
return render_to_string("myapp/render_my_field.html", self.get_context(request) | context)
```

Your content will then automatically be rendered with that method when need be by using
Expand Down Expand Up @@ -200,16 +202,19 @@ Our new loop would then be:

## Hooks

### wagtail_fedit.construct_block_toolbar
### wagtail_fedit.construct_adapter_toolbar

Construct the toolbar for the given block.
This is used to display the edit icon in the block.
Construct the toolbar for the given adapter.
This is used to display the edit icon for the given adapter.

How it is called:

```python
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 = [
FeditAdapterEditButton(),
]
for hook in hooks.get_hooks(CONSTRUCT_ADAPTER_TOOLBAR):
hook(items=items, adapter=adapter)
```

### wagtail_fedit.construct_field_toolbar
Expand Down
3 changes: 2 additions & 1 deletion wagtail_fedit/adapters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def form_valid(self, form: "forms.Form"):

def form_invalid(self, form: "forms.Form"):
pass


@classmethod
def render_content(self, parent_context: dict = None) -> str:
"""
Render the content for the field.
Expand Down
29 changes: 0 additions & 29 deletions wagtail_fedit/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,6 @@ def prefix(name):
return f"wagtail_fedit.{name}"


CONSTRUCT_BLOCK_TOOLBAR = prefix("construct_block_toolbar")
"""
### wagtail_fedit.construct_block_toolbar
Construct the toolbar for the given block.
This is used to display the edit icon in the block.
How it is called:
```python
for hook in hooks.get_hooks(CONSTRUCT_BLOCK_TOOLBAR):
hook(request=request, items=items, model=model, block_id=block_id, field_name=field_name)
```
"""


CONSTRUCT_FIELD_TOOLBAR = prefix("construct_field_toolbar")
"""
### wagtail_fedit.construct_field_toolbar
Construct the toolbar for the given field.
This is used to display the edit icon in the field.
How it is called:
```python
for hook in hooks.get_hooks(CONSTRUCT_FIELD_TOOLBAR):
hook(request=request, items=items, model=model, field_name=field_name)
```
"""

CONSTRUCT_ADAPTER_TOOLBAR = prefix("construct_adapter_toolbar")
"""
### wagtail_fedit.construct_adapter_toolbar
Expand Down
5 changes: 4 additions & 1 deletion wagtail_fedit/templatetags/fedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ def render(self, context):
else:

if not model:
raise TemplateSyntaxError("Model is required")
raise TemplateSyntaxError(
"`instance.field` is required; use `from_context` if "
"this is wrapped by a fedit templatetag or adress a model field."
)

field_name = getters[len(getters)-1]
obj = model
Expand Down

0 comments on commit 80bbbf2

Please sign in to comment.