From 362af52cdda0efe33874b9c007831eab396be7c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Fri, 19 Apr 2019 14:52:12 +0200
Subject: [PATCH] combo: add custom import_template to ignore errors with some
 names (#32495)

---
 .../management/commands/import_template.py    | 28 +++++++++++++++++++
 .../management/commands/import_template.py    |  7 +++--
 tests/test_import_template.py                 |  5 ++--
 3 files changed, 36 insertions(+), 4 deletions(-)
 create mode 100644 hobo/agent/combo/management/commands/import_template.py

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 <http://www.gnu.org/licenses/>.
+
+
+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 == []
-- 
2.20.1

