Skip to content

Commit

Permalink
Conditionally try wagtail_modeladmin
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Nov 7, 2023
1 parent 0e9f9ed commit a2d916f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Wagtail Localize requires the following:

- Python (3.8, 3.9, 3.10, 3.11)
- Django (3.2, 4.1, 4.2)
- Wagtail (4.1, 4.2, 5.0) with [internationalisation enabled](https://docs.wagtail.org/en/stable/advanced_topics/i18n.html#configuration)
- Wagtail (4.1, 5.1, 5.2) with [internationalisation enabled](https://docs.wagtail.org/en/stable/advanced_topics/i18n.html#configuration)
- [wagtail-modeladmin](https://pypi.org/project/wagtail-modeladmin/) if `using wagtail_localize.modeladmin` and Wagtail >= 5.0

## Installation

Expand Down
10 changes: 8 additions & 2 deletions wagtail_localize/modeladmin/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
from django.contrib.admin.utils import quote
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from wagtail.contrib.modeladmin.helpers import ButtonHelper, PageButtonHelper
from wagtail.contrib.modeladmin.views import InspectView
from wagtail.models import Locale, Page, TranslatableMixin

from wagtail_localize.models import TranslationSource


try:
from wagtail_modeladmin.helpers import ButtonHelper, PageButtonHelper
from wagtail_modeladmin.views import InspectView
except ImportError:
from wagtail.contrib.modeladmin.helpers import ButtonHelper, PageButtonHelper
from wagtail.contrib.modeladmin.views import InspectView


class TranslatableButtonHelper(ButtonHelper):
def get_buttons_for_obj(self, obj, **kwargs):
btns = super().get_buttons_for_obj(obj, **kwargs)
Expand Down
7 changes: 6 additions & 1 deletion wagtail_localize/modeladmin/options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from wagtail.contrib.modeladmin.options import ModelAdmin
from wagtail.models import TranslatableMixin

from .helpers import TranslatableButtonHelper, TranslatablePageButtonHelper
Expand All @@ -15,6 +14,12 @@
)


try:
from wagtail_modeladmin.options import ModelAdmin
except ImportError:
from wagtail.contrib.modeladmin.options import ModelAdmin


class TranslatableModelAdmin(ModelAdmin):
index_view_class = TranslatableIndexView
create_view_class = TranslatableCreateView
Expand Down
31 changes: 22 additions & 9 deletions wagtail_localize/modeladmin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
from django.urls import NoReverseMatch, reverse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext as _
from wagtail.contrib.modeladmin.views import (
ChooseParentView,
CreateView,
DeleteView,
EditView,
HistoryView,
IndexView,
InspectView,
)
from wagtail.models import Locale, TranslatableMixin
from wagtail.utils.version import get_main_version

Expand All @@ -25,6 +16,28 @@
from wagtail_localize.views.submit_translations import SubmitTranslationView


try:
from wagtail_modeladmin.views import (
ChooseParentView,
CreateView,
DeleteView,
EditView,
HistoryView,
IndexView,
InspectView,
)
except ImportError:
from wagtail.contrib.modeladmin.views import (
ChooseParentView,
CreateView,
DeleteView,
EditView,
HistoryView,
IndexView,
InspectView,
)


class TranslatableViewMixin:
def __init__(self, *args, **kwargs):
self.locale = None
Expand Down
10 changes: 9 additions & 1 deletion wagtail_localize/test/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
import dj_database_url


try:
import wagtail_modeladmin # noqa: F401
except ImportError:
HAS_MODELADMIN_PACKAGE = False
else:
HAS_MODELADMIN_PACKAGE = True


# Build paths inside the project like this: os.path.join(PROJECT_DIR, ...)
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
Expand Down Expand Up @@ -49,7 +57,7 @@
"wagtail.search",
"wagtail.admin",
"wagtail.api.v2",
"wagtail.contrib.modeladmin",
"wagtail_modeladmin" if HAS_MODELADMIN_PACKAGE else "wagtail.contrib.modeladmin",
"wagtail.contrib.routable_page",
"wagtail.sites",
"wagtail",
Expand Down

0 comments on commit a2d916f

Please sign in to comment.