commands: set locales in tenant commands (#51734)

This commit is contained in:
Thomas NOËL 2021-03-05 23:44:56 +01:00
parent 25147100b9
commit 54e43a6728
4 changed files with 20 additions and 8 deletions

View File

@ -427,6 +427,7 @@ def test_shell():
class TestAfterJob(AfterJob):
def execute(self):
self.foo = WorkflowStatusItem().compute('{{ global_title|default:"FAIL" }}')
self.l10n_month = WorkflowStatusItem().compute('{{ "10/10/2010"|date:"F" }}')
def test_runjob(pub):
@ -448,6 +449,16 @@ def test_runjob(pub):
call_command('runjob', '--domain=example.net', '--job-id=%s' % job.id)
assert AfterJob.get(job.id).status == 'completed'
assert AfterJob.get(job.id).foo == 'HELLO'
assert AfterJob.get(job.id).l10n_month == 'October'
pub.cfg['language'] = {'language': 'fr'}
pub.write_cfg()
job = TestAfterJob(label='test2')
job.store()
assert AfterJob.get(job.id).status == 'registered'
call_command('runjob', '--domain=example.net', '--job-id=%s' % job.id)
assert AfterJob.get(job.id).status == 'completed'
assert AfterJob.get(job.id).l10n_month == 'octobre'
def test_ctl_print_help(capsys):

View File

@ -28,5 +28,6 @@ class TenantCommand(BaseCommand):
raise CommandError('unknown tenant')
publisher.app_dir = os.path.join(publisher.APP_DIR, domain)
publisher.set_config()
publisher.install_lang()
publisher.substitutions.feed(publisher)
return publisher

View File

@ -67,10 +67,7 @@ def cron_worker(publisher, now, job_name=None):
if minutes and not now[4] in minutes:
continue
class FakeRequest(object):
language = publisher.get_site_language()
publisher.install_lang(FakeRequest())
publisher.install_lang()
publisher.substitutions.reset()
publisher.substitutions.feed(publisher)
for extra_source in publisher.extra_sources:

View File

@ -344,15 +344,18 @@ class QommonPublisher(Publisher, object):
content = template.render(content.templates, content.context)
return template.decorate(content, self.get_request().response)
def install_lang(self, request):
lang = request.language
def install_lang(self, request=None):
if request:
lang = request.language
else:
lang = self.get_site_language()
if lang is None or not lang in [x[0] for x in settings.LANGUAGES]:
lang = 'en'
translation.activate(lang)
request.LANGUAGE_CODE = lang
self.gettext = translation.gettext
self.ngettext = translation.ngettext
if request:
request.LANGUAGE_CODE = lang
def load_site_options(self):
self.site_options = configparser.ConfigParser()