Skip to content

Commit

Permalink
Merge pull request #419 from infoportugal/test_417
Browse files Browse the repository at this point in the history
Test 417
  • Loading branch information
DiogoMarques29 authored Apr 2, 2024
2 parents c8d95c8 + 2390a52 commit b7346d8
Show file tree
Hide file tree
Showing 35 changed files with 1,883 additions and 1,028 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
- name: Install flake8
- name: Install black
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Lint with flake8
pip install black
- name: Lint with black
run: |
flake8 --ignore=E501 wagtail_modeltranslation
black wagtail_modeltranslation
Test:
runs-on: ubuntu-latest
strategy:
Expand Down
4 changes: 2 additions & 2 deletions wagtail_modeltranslation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.14.1'
default_app_config = 'wagtail_modeltranslation.apps.WagtailModeltranslationConfig'
__version__ = "0.14.1"
default_app_config = "wagtail_modeltranslation.apps.WagtailModeltranslationConfig"
25 changes: 17 additions & 8 deletions wagtail_modeltranslation/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,42 @@


class WagtailModeltranslationConfig(AppConfig):
name = 'wagtail_modeltranslation'
verbose_name = 'Wagtail Modeltranslation'
name = "wagtail_modeltranslation"
verbose_name = "Wagtail Modeltranslation"

def ready(self):
from django.conf import settings
from modeltranslation import settings as mt_settings

# Add Wagtail defined fields as modeltranslation custom fields
wagtail_fields = (
'StreamField',
'RichTextField',
"StreamField",
"RichTextField",
)

# update both the standard settings and the modeltranslation settings,
# as we cannot guarantee the load order, and so django_modeltranslation
# may bootstrap itself either before, or after, our ready() gets called.
custom_fields = getattr(settings, 'MODELTRANSLATION_CUSTOM_FIELDS', tuple())
setattr(settings, 'MODELTRANSLATION_CUSTOM_FIELDS', tuple(set(custom_fields + wagtail_fields)))
custom_fields = getattr(settings, "MODELTRANSLATION_CUSTOM_FIELDS", tuple())
setattr(
settings,
"MODELTRANSLATION_CUSTOM_FIELDS",
tuple(set(custom_fields + wagtail_fields)),
)

mt_custom_fields = getattr(mt_settings, 'CUSTOM_FIELDS', tuple())
setattr(mt_settings, 'CUSTOM_FIELDS', tuple(set(mt_custom_fields + wagtail_fields)))
mt_custom_fields = getattr(mt_settings, "CUSTOM_FIELDS", tuple())
setattr(
mt_settings, "CUSTOM_FIELDS", tuple(set(mt_custom_fields + wagtail_fields))
)

from modeltranslation.models import handle_translation_registrations

handle_translation_registrations()

from .patch_wagtailadmin import patch_wagtail_models

patch_wagtail_models()

from wagtail_modeltranslation.signal_handlers import register_signal_handlers

register_signal_handlers()
1 change: 1 addition & 0 deletions wagtail_modeltranslation/contextlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class use_language:
with use_language('en'):
en_url = obj.get_absolute_url()
"""

def __init__(self, lang):
self.language = lang
self.current_language = get_language()
Expand Down
2 changes: 1 addition & 1 deletion wagtail_modeltranslation/makemigrations/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'wagtail_modeltranslation.makemigrations.apps.MakemigrationsConfig'
default_app_config = "wagtail_modeltranslation.makemigrations.apps.MakemigrationsConfig"
4 changes: 2 additions & 2 deletions wagtail_modeltranslation/makemigrations/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


class MakemigrationsConfig(AppConfig):
name = 'wagtail_modeltranslation.makemigrations'
label = 'wagtail_modeltranslation_makemigrations'
name = "wagtail_modeltranslation.makemigrations"
label = "wagtail_modeltranslation_makemigrations"
verbose_name = "Wagtail Modeltranslation makemigrations"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from wagtail_modeltranslation.management.commands.makemigrations_translation import \
Command as MakeMigrationsCommand
from wagtail_modeltranslation.management.commands.makemigrations_translation import (
Command as MakeMigrationsCommand,
)


class Command(MakeMigrationsCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.core.management.commands.makemigrations import \
Command as MakeMigrationsCommand
from django.core.management.commands.makemigrations import (
Command as MakeMigrationsCommand,
)


class Command(MakeMigrationsCommand):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import copy

from django.core.management.commands.makemigrations import \
Command as MakeMigrationsCommand
from django.core.management.commands.makemigrations import (
Command as MakeMigrationsCommand,
)
from django.db.migrations.autodetector import MigrationAutodetector


def autodetector_decorator(func):
def wrapper(self, from_state, to_state, questioner=None):
# Replace to_state.app_configs.models and to_state.models' version of page with the old one
# so no changes are detected by MigrationAutodetector
from_state_page = from_state.apps.get_model('wagtailcore', 'page')
from_state_page = from_state.apps.get_model("wagtailcore", "page")
new_to_state = copy.deepcopy(to_state)
new_to_state.apps.app_configs['wagtailcore'].models['page'] = from_state_page
new_to_state.models['wagtailcore', 'page'] = from_state.models['wagtailcore', 'page']
new_to_state.apps.app_configs["wagtailcore"].models["page"] = from_state_page
new_to_state.models["wagtailcore", "page"] = from_state.models[
"wagtailcore", "page"
]

return func(self, from_state, new_to_state, questioner)

return wrapper


Expand All @@ -23,7 +27,9 @@ class Command(MakeMigrationsCommand):

def handle(self, *args, **options):
old_autodetector_init = MigrationAutodetector.__init__
MigrationAutodetector.__init__ = autodetector_decorator(MigrationAutodetector.__init__)
MigrationAutodetector.__init__ = autodetector_decorator(
MigrationAutodetector.__init__
)

try:
super(Command, self).handle(*args, **options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
from django.core.management.commands.migrate import Command as MigrateCommand
from django.db.migrations.autodetector import MigrationAutodetector

from .sync_page_translation_fields import \
Command as SyncPageTranslationFieldsCommand
from .sync_page_translation_fields import Command as SyncPageTranslationFieldsCommand


# decorate MigrationAutodetector.changes so we can silence any wagtailcore migrations missing warnings
def changes_decorator(func):
def wrapper(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
changes = func(self, graph, trim_to_apps, convert_apps, migration_name)
if 'wagtailcore' in changes:
del changes['wagtailcore']
if "wagtailcore" in changes:
del changes["wagtailcore"]
return changes

return wrapper


class Command(MigrateCommand):
help = "Updates database schema. Manages both apps with migrations and those without. " \
"Updates Wagtail Page translation fields"
help = (
"Updates database schema. Manages both apps with migrations and those without. "
"Updates Wagtail Page translation fields"
)

def handle(self, *args, **options):
old_autodetector_changes = MigrationAutodetector.changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
class Command(BaseCommand):
def __init__(self):
super(Command, self).__init__()
update_fields = ['url_path']
update_fields = ["url_path"]
for language in mt_settings.AVAILABLE_LANGUAGES:
localized_url_path = build_localized_fieldname('url_path', language)
localized_url_path = build_localized_fieldname("url_path", language)
update_fields.append(localized_url_path)
self.update_fields = update_fields

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from modeltranslation.management.commands.sync_translation_fields import \
Command as SyncTranslationsFieldsCommand
from modeltranslation.management.commands.sync_translation_fields import (
Command as SyncTranslationsFieldsCommand,
)
from modeltranslation.translator import translator
from wagtail.models import Page

Expand All @@ -13,9 +14,11 @@ def get_page_model(self, abstract=True):


class Command(SyncTranslationsFieldsCommand):
help = ("Detect new translatable fields or new available languages and"
" sync Wagtail's Page database structure. Does not remove "
" columns of removed languages or undeclared fields.")
help = (
"Detect new translatable fields or new available languages and"
" sync Wagtail's Page database structure. Does not remove "
" columns of removed languages or undeclared fields."
)

def handle(self, *args, **options):
translator.get_registered_models = get_page_model.__get__(translator)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from modeltranslation.management.commands.update_translation_fields import \
Command as UpdateTranslationFieldsCommand
from modeltranslation.management.commands.update_translation_fields import (
Command as UpdateTranslationFieldsCommand,
)


class Command(UpdateTranslationFieldsCommand):
Expand Down
2 changes: 1 addition & 1 deletion wagtail_modeltranslation/migrate/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default_app_config = 'wagtail_modeltranslation.migrate.apps.MigrateConfig'
default_app_config = "wagtail_modeltranslation.migrate.apps.MigrateConfig"
4 changes: 2 additions & 2 deletions wagtail_modeltranslation/migrate/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@


class MigrateConfig(AppConfig):
name = 'wagtail_modeltranslation.migrate'
label = 'wagtail_modeltranslation_migrate'
name = "wagtail_modeltranslation.migrate"
label = "wagtail_modeltranslation_migrate"
verbose_name = "Wagtail Modeltranslation migrate"
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from wagtail_modeltranslation.management.commands.migrate_translation import \
Command as MigrateCommand
from wagtail_modeltranslation.management.commands.migrate_translation import (
Command as MigrateCommand,
)


class Command(MigrateCommand):
Expand Down
Loading

0 comments on commit b7346d8

Please sign in to comment.