Skip to content

Commit

Permalink
cleanup: Remove obsolete code for old django in loaddata command
Browse files Browse the repository at this point in the history
  • Loading branch information
last-partizan committed Jul 13, 2022
1 parent fba75cc commit a8e5d24
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 61 deletions.
14 changes: 0 additions & 14 deletions docs/modeltranslation/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -377,17 +377,3 @@ Default: ``True``
.. versionadded:: 0.6

Control if :ref:`fallback <fallback>` (both language and value) will occur.


.. _settings-modeltranslation_loaddata_retain_locale:

``MODELTRANSLATION_LOADDATA_RETAIN_LOCALE``
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Default: ``True``

.. versionadded:: 0.7

Control if the ``loaddata`` command should leave the settings-defined locale alone. Setting it
to ``False`` will result in previous behaviour of ``loaddata``: inserting fixtures to database
under ``en-us`` locale.
64 changes: 17 additions & 47 deletions modeltranslation/management/commands/loaddata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django import VERSION
import argparse

from django.core.management.commands.loaddata import Command as LoadDataCommand

# Because this command is used (instead of default loaddata), then settings have been imported
# and we can safely import MT modules
from modeltranslation import settings as mt_settings
from modeltranslation.utils import auto_populate


ALLOWED = (None, False, 'all', 'default', 'required')
ALLOWED_FOR_PRINT = ', '.join(str(i) for i in (0,) + ALLOWED[1:]) # For pretty-printing

Expand All @@ -22,55 +22,25 @@ def check_mode(option, opt_str, value, parser, namespace=None):
class Command(LoadDataCommand):
leave_locale_alone = mt_settings.LOADDATA_RETAIN_LOCALE # Django 1.6

help = (
'Using this option will cause fixtures to be loaded under auto-population MODE.'
+ 'Allowed values are: %s' % ALLOWED_FOR_PRINT
)
if VERSION < (1, 8):
from optparse import make_option

option_list = LoadDataCommand.option_list + (
make_option(
'--populate',
action='callback',
callback=check_mode,
type='string',
dest='populate',
metavar='MODE',
help=help,
class CheckAction(argparse.Action):
def __call__(self, parser, namespace, value, option_string=None):
check_mode(self, option_string, value, parser, namespace)

def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--populate',
action=self.CheckAction,
type=str,
dest='populate',
metavar='MODE',
help=(
'Using this option will cause fixtures to be loaded under auto-population MODE. '
+ 'Allowed values are: %s' % ALLOWED_FOR_PRINT
),
)
else:
import argparse

class CheckAction(argparse.Action):
def __call__(self, parser, namespace, value, option_string=None):
check_mode(self, option_string, value, parser, namespace)

def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'--populate',
action=self.CheckAction,
type=str,
dest='populate',
metavar='MODE',
help=self.help,
)

def __init__(self):
super(Command, self).__init__()
if mt_settings.LOADDATA_RETAIN_LOCALE and VERSION < (1, 6):
from django.utils import translation

self.locale = translation.get_language()

def handle(self, *fixture_labels, **options):
if hasattr(self, 'locale'):
from django.utils import translation

translation.activate(self.locale)

mode = options.get('populate')
if mode is not None:
with auto_populate(mode):
Expand Down

0 comments on commit a8e5d24

Please sign in to comment.