Skip to content

Commit

Permalink
add supports of
Browse files Browse the repository at this point in the history
  • Loading branch information
DylannCordel committed Sep 18, 2024
1 parent 2d9fd81 commit 4889cd0
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 178 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.2.0 - 2024-09-18

* add supports of `TitleFieldPanel.targets` to autocomplete unstranslated or translated fields

# 0.2.0 - 2024-09-04

* remove support of wagtail < 5.0
Expand Down
17 changes: 17 additions & 0 deletions docs/source/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,23 @@ PARLER_LANGUAGES = {
...
```

## Autocomplete fields from TitleFieldPanel

If you configure your i18n TitleFieldPanel to target some other fields, `wagtail_parler` will configure it as it:

* if a target is a translated field, the translated field will be auto populated by the source field of the same lang (ex: `translations_title_fr` will populate `translations_slug_fr`)
* if a target is an unstranslated field, only the first translated source field will populate the target. (ex: if `fr` is your first language, `translations_title_fr` will populate `no_i18n_slug`, but `translations_title_en` won't)

If your source TitleFieldPanel is an unstranslated field but you want it autocomplete some translated fields, you'll have to explicitely target all translated fields (a translated field `foo` will be prefixed by `translations_` and suffixed by `_<lang>`: `translation_foo_fr`, `translation_foo_en`…). ex:

```python
...
children = [
TitleFieldPanel("no_i18n_title", targets=["translations_slug_fr", "translations_slug_en", "translations_slug_de"])
]
...
```

![Food ModelAdmin - Specific tabs 2](images/food-model-admin-specific-tabs-2.png)

[wagtail]: https://docs.wagtail.org/en/stable/index.html
Expand Down
326 changes: 165 additions & 161 deletions test_fixtures.json
Original file line number Diff line number Diff line change
@@ -1,164 +1,168 @@
[
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 1,
"fields": {
"language_code": "fr",
"name": "Gelée",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Pouvez-vous emmener une \\u00ab Jelly \\u00bb dans un avion ?\"}, \"id\": \"8c23eadb-60e0-4271-831d-332dd33ce36b\"}]",
"master": 1
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 1,
"fields": {
"language_code": "fr",
"name": "Gelée",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Pouvez-vous emmener une \\u00ab Jelly \\u00bb dans un avion ?\"}, \"id\": \"8c23eadb-60e0-4271-831d-332dd33ce36b\"}]",
"master": 1
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 2,
"fields": {
"language_code": "fr",
"name": "Pudding de Noël",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"De quelle origine est ce plat ?\"}, \"id\": \"6b40015d-0cd9-48b4-8a06-3719cd6bce60\"}]",
"master": 2
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 3,
"fields": {
"language_code": "fr",
"name": "Omelette au fromage",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Dans quelle s\\u00e9rie parle t-on de \\\"Omelette du fromage\\\"\"}, \"id\": \"05aeb9c2-b6e3-42cb-ac6b-195f93885af4\"}]",
"master": 3
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 4,
"fields": {
"language_code": "fr",
"name": "Raclette",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Est-ce di\\u00e9t\\u00e9tique ?\"}, \"id\": \"a414bab0-97e9-4e3d-9ef1-5f2b2d165ece\"}]",
"master": 4
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 5,
"fields": {
"language_code": "en",
"name": "Jelly",
"summary": "Summary EN",
"content": "Content EN",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Can you bring a Jelly in a plane ?\"}, \"id\": \"bbc8985f-f249-4ce2-9cab-cee966ffb4aa\"}]",
"master": 1
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 6,
"fields": {
"language_code": "en",
"name": "Christmas Pudding",
"summary": "Summary EN",
"content": "Content EN",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"What is the origin of this meal ?\"}, \"id\": \"a2a0a74c-7817-4129-a6d5-490c5af3afe9\"}]",
"master": 2
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 1,
"fields": {
"slug": "gely",
"yum_rating": 3,
"vegetarian": true,
"vegan": true
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 2,
"fields": {
"slug": "pudding",
"yum_rating": 5,
"vegetarian": false,
"vegan": false
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 3,
"fields": {
"slug": "omelet",
"yum_rating": 8,
"vegetarian": true,
"vegan": false
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 4,
"fields": {
"slug": "raclette",
"yum_rating": 10,
"vegetarian": true,
"vegan": false
}
},
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "md5$Xibmtaqa1WGE7j0V46OcOQ$8e5148fef28cc57db487f7a8978cb345",
"last_login": "2023-09-12T07:47:24.764Z",
"is_superuser": true,
"username": "admin",
"first_name": "",
"last_name": "",
"email": "[email protected]",
"is_staff": true,
"is_active": true,
"date_joined": "2023-08-02T06:58:26.271Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.group",
"pk": 1,
"fields": {
"name": "Moderators",
"permissions": [
1,
2,
3,
5,
4,
6,
7,
9,
8
]
}
},
{
"model": "auth.group",
"pk": 2,
"fields": {
"name": "Editors",
"permissions": [
1,
2,
3,
5,
4,
6,
7,
9,
8
]
}
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 2,
"fields": {
"language_code": "fr",
"name": "Pudding de Noël",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"De quelle origine est ce plat ?\"}, \"id\": \"6b40015d-0cd9-48b4-8a06-3719cd6bce60\"}]",
"master": 2
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 3,
"fields": {
"language_code": "fr",
"name": "Omelette au fromage",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"De quelle s\\u00e9rie parle t-on de \\\"Omelette du fromage\\\"\"}, \"id\": \"05aeb9c2-b6e3-42cb-ac6b-195f93885af4\"}]",
"master": 3
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 4,
"fields": {
"language_code": "fr",
"name": "Raclette",
"summary": "Summary FR",
"content": "Content FR",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Est-ce di\\u00e9t\\u00e9tique ?\"}, \"id\": \"a414bab0-97e9-4e3d-9ef1-5f2b2d165ece\"}]",
"master": 4
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 5,
"fields": {
"language_code": "en",
"name": "Jelly",
"summary": "Summary EN",
"content": "Content EN",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"Can you bring a Jelly in a plane ?\"}, \"id\": \"bbc8985f-f249-4ce2-9cab-cee966ffb4aa\"}]",
"master": 1
}
},
{
"model": "wagtail_parler_tests.foodtranslation",
"pk": 6,
"fields": {
"language_code": "en",
"name": "Christmas Pudding",
"summary": "Summary EN",
"content": "Content EN",
"qa": "[{\"type\": \"QaBlock\", \"value\": {\"text\": \"What is the origin of this meal ?\"}, \"id\": \"a2a0a74c-7817-4129-a6d5-490c5af3afe9\"}]",
"master": 2
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 1,
"fields": {
"yum_rating": 3,
"vegetarian": true,
"vegan": true
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 2,
"fields": {
"yum_rating": 5,
"vegetarian": false,
"vegan": false
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 3,
"fields": {
"yum_rating": 8,
"vegetarian": true,
"vegan": false
}
},
{
"model": "wagtail_parler_tests.food",
"pk": 4,
"fields": {
"yum_rating": 10,
"vegetarian": true,
"vegan": false
}
},
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "md5$Xibmtaqa1WGE7j0V46OcOQ$8e5148fef28cc57db487f7a8978cb345",
"last_login": "2023-09-12T07:47:24.764Z",
"is_superuser": true,
"username": "admin",
"first_name": "",
"last_name": "",
"email": "[email protected]",
"is_staff": true,
"is_active": true,
"date_joined": "2023-08-02T06:58:26.271Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.group",
"pk": 1,
"fields": {
"name": "Moderators",
"permissions": [
1,
2,
3,
5,
4,
6,
7,
9,
8
]
}
},
{
"model": "auth.group",
"pk": 2,
"fields": {
"name": "Editors",
"permissions": [
1,
2,
3,
5,
4,
6,
7,
9,
8
]
}
}
]
10 changes: 8 additions & 2 deletions wagtail_parler/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from typing import Any, Dict, Generator, Optional, Set, Tuple
from typing import Any
from typing import Dict
from typing import Generator
from typing import Optional
from typing import Set
from typing import Tuple

from django.db.models import Model

Expand All @@ -14,6 +19,7 @@
from django.db import transaction
from django.forms import Form
from django.forms.models import fields_for_model

from wagtail.admin.forms import WagtailAdminModelForm


Expand All @@ -29,7 +35,7 @@ class AutoParlerModelForm(Form):
def __init__(self, *args: Tuple, **kwargs: Dict) -> None:
kwargs.setdefault("initial", {})
self._init_i18n_initials(kwargs.get("instance"), kwargs["initial"])
self.for_user = kwargs.pop("for_user", None)
self.for_user = kwargs.get("for_user", getattr(self, "for_user", None))
super().__init__(*args, **kwargs)
# All fields of others locales than the default one must NOT be required
for conf in settings.PARLER_LANGUAGES[None][1:]:
Expand Down
Loading

0 comments on commit 4889cd0

Please sign in to comment.