diff --git a/hobo/agent/combo/management/commands/import_template.py b/hobo/agent/combo/management/commands/import_template.py new file mode 100644 index 0000000..c6dc1e0 --- /dev/null +++ b/hobo/agent/combo/management/commands/import_template.py @@ -0,0 +1,28 @@ +# hobo - portal to configure and deploy applications +# Copyright (C) 2015-2019 Entr'ouvert +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from hobo.agent.common.management.commands import import_template + +class Command(import_template.Command): + def handle(self, *args, **kwargs): + try: + return super(Command, self).handle(*args, **kwargs) + except import_template.UnknownTemplateError: + # ignore errors if template name is portal-user or portal-agent as + # those names do not actually require an existing file to work. + if kwargs.get('template_name') not in ('portal-user', 'portal-agent'): + raise diff --git a/hobo/agent/common/management/commands/import_template.py b/hobo/agent/common/management/commands/import_template.py index d1d3bf2..c30f9f3 100644 --- a/hobo/agent/common/management/commands/import_template.py +++ b/hobo/agent/common/management/commands/import_template.py @@ -21,6 +21,10 @@ from django.core.management.base import BaseCommand, CommandError from django.core.management import call_command, get_commands +class UnknownTemplateError(CommandError): + pass + + class Command(BaseCommand): def add_arguments(self, parser): @@ -34,7 +38,6 @@ class Command(BaseCommand): if 'import_site' in get_commands(): if not os.path.exists(template): - self.stderr.write(self.style.WARNING( - 'Unknown template (%r)' % template)) + raise UnknownTemplateError('unknown template (%r)' % template) else: call_command('import_site', template) diff --git a/tests/test_import_template.py b/tests/test_import_template.py index 5c4f665..b90cb29 100644 --- a/tests/test_import_template.py +++ b/tests/test_import_template.py @@ -5,7 +5,7 @@ from django.conf import settings from django.core.management.base import CommandError from django.test import override_settings -from hobo.agent.common.management.commands.import_template import Command +from hobo.agent.common.management.commands.import_template import Command, UnknownTemplateError @mock.patch('hobo.agent.common.management.commands.import_template.os.path.exists') @@ -63,7 +63,8 @@ def test_import_template_warning(get_commands, call_command, os_path_exists): # $ XXX-manage import-template import_me command = Command() with override_settings(PROJECT_NAME='bobo'): - command.handle(template_name='import_me') + with pytest.raises(UnknownTemplateError): + command.handle(template_name='import_me') # assert 'import_site' command is not run assert call_command.mock_calls == []