Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve-doc #1

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/source/api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# API documentation

## ParlerAdminWagtailMixin (internal use)

**⚠️ This mixin is documented for dev only: you SHOULD NOT use it directly ⚠️**

```{eval-rst}
.. autoclass:: wagtail_parler.handlers.ParlerAdminWagtailMixin
:members:
:undoc-members:
:show-inheritance:
```

## ParlerSnippetAdminMixin

```{eval-rst}
.. autoclass:: wagtail_parler.handlers.ParlerSnippetAdminMixin
:members:
:undoc-members:
:show-inheritance:
```


## ParlerModelAdminMixin

```{eval-rst}
.. autoclass:: wagtail_parler.handlers.ParlerModelAdminMixin
:members:
:undoc-members:
:show-inheritance:

```

## AutoParlerModelForm

**⚠️ This class is documented for dev only: you SHOULD NOT use it directly ⚠️**

```{eval-rst}
.. autoclass:: wagtail_parler.forms.AutoParlerModelForm
:members:
:undoc-members:
:show-inheritance:

```
2 changes: 2 additions & 0 deletions docs/source/index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
```{include} presentation.md
```

```{toctree}
:maxdepth: 2
presentation
configuration
usage
api
```
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
| migrations
| \.tox
| \.venv
| docs/\.*/
)/
'''

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ testing =
types-requests

[flake8]
exclude = build,.git,.tox,./wagtail_parler_tests/.env,./.venv,./wagtail_parler_tests/settings.py
exclude = build,.git,.tox,./wagtail_parler_tests/.env,./.venv,docs/.*/,./wagtail_parler_tests/settings.py
max-line-length = 99

[black]
Expand Down
14 changes: 9 additions & 5 deletions wagtail_parler/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,19 @@ def _save_locale(self, locale: str) -> Tuple:
"""
Depending sent data, will save/create/delete translation for the given language code

param: locale: str: locale code of translation to save/create/delete
return: (None|True|False, int|instance|[instances]:
for deletion, will return None and the number of translations deleted
for creation, will return True and the created of translation
for update, will return False and the updated of translation
Args:
locale (str): locale code of translation to save/create/delete

Returns:
(None|True|False, int|instance|[instances]: for deletion, will return None and the
number of translations deleted
for creation, will return True and the created of translation
for update, will return False and the updated of translation
"""
obj = self.instance # type: ignore
trans_exists = obj.has_translation(locale)
data = self.cleaned_data_for_locales.get(locale)

if not data or all(not d for d in data.values()):
return None, obj.delete_translation(locale) if trans_exists else 0
return (
Expand Down
63 changes: 62 additions & 1 deletion wagtail_parler/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def get_bound_panel(
class ParlerAdminWagtailMixin:
"""
Mixin to manage translations via Parler inside a ModelAdmin or SnippetViewSet

You **SHOULD NOT** use this Mixin directly but ParlerModelAdminMixin or ParlerSnippetAdminMixin
"""

def _set_translations_handlers(
Expand Down Expand Up @@ -162,9 +164,68 @@ def get_edit_handler(self: ModelAdmin) -> TabbedInterface:


class ParlerModelAdminMixin(ParlerAdminWagtailMixin):
pass
"""
Mixin to use with ModelAdmin to manage parler translations.
ex:

.. code-block:: python

class SomeModelAdmin(ParlerModelAdminMixin, ModelAdmin):
model = SomeModel


class FoodModelAdmin(ParlerModelAdminMixin, ModelAdmin):
model = Food
edit_handler = ObjectList(
children=[
FieldPanel("yum_rating"),
TranslationsList(
heading="%(code)s: %(locale)s %(status)s", # type: ignore
# let children empty, it will be auto populated
),
ObjectList(
heading=_("Régime"),
children=[FieldPanel("vegetarian"), FieldPanel("vegan")]
),
],
)
"""


class ParlerSnippetAdminMixin(ParlerAdminWagtailMixin, CompatibleSnippetViewSetMixin):
"""
Mixin to use with SnippetViewSet to manage parler translations.

If no edit_handler are defined, the default behaviour will create an unstranlated data tab
then a tab for each language
You can specify your edit_handler to have more control over how form is built.
TranslationsList will be duplicated for each language and populated with
translations fields if no children are given. Of course, you can also configure
fields manually
ex:

.. code-block:: python

class SomeModelAdminSnippet(ParlerSnippetAdminMixin, SnippetViewSet):
model = SomeModel


class FoodAdminSnippet(ParlerSnippetAdminMixin, SnippetViewSet):
model = Food
edit_handler = ObjectList(
children=[
FieldPanel("yum_rating"),
TranslationsList(
heading="%(code)s: %(locale)s %(status)s",
# let children empty, it will be auto populated
),
ObjectList(
heading=_("Régime"),
children=[FieldPanel("vegetarian"), FieldPanel("vegan")]
),
],
)
"""

def get_edit_handler(self: SnippetViewSet) -> TabbedInterface:
return super().get_edit_handler().bind_to_model(self.model)
Loading