commands: add import_site management command (#34503)

This commit is contained in:
Nicolas Roche 2019-07-04 19:22:24 +02:00
parent 686aea9d54
commit c1f0acb94c
3 changed files with 76 additions and 0 deletions

BIN
tests/site.zip Normal file

Binary file not shown.

View File

@ -381,3 +381,27 @@ open(os.path.join(get_publisher().app_dir, 'runscript.test'), 'w').close()
os.unlink(os.path.join(pub.app_dir, 'runscript.test'))
call_command('runscript', '--all-tenants', os.path.join(pub.app_dir, 'test2.py'))
assert os.path.exists(os.path.join(pub.app_dir, 'runscript.test'))
def test_import_site():
with pytest.raises(CommandError):
call_command('import_site')
pub = create_temporary_pub()
FormDef.wipe()
assert FormDef.count() == 0
assert 'workflows' not in os.listdir(pub.app_dir)
site_zip_path = os.path.join(os.path.dirname(__file__), 'site.zip')
call_command('import_site', '--domain=example.net', site_zip_path)
assert FormDef.count() == 1
assert Workflow.count() == 1
assert 'workflows' in os.listdir(pub.app_dir)
FormDef.wipe()
assert FormDef.count() == 0
assert Workflow.count() == 1
call_command('import_site', '--domain=example.net', '--if-empty', site_zip_path)
assert FormDef.count() == 0
assert Workflow.count() == 1
site_zip_path = os.path.join(os.path.dirname(__file__), 'missing_file.zip')
with pytest.raises(CommandError, match='missing file:'):
call_command('import_site', '--domain=example.net', site_zip_path)

View File

@ -0,0 +1,52 @@
# w.c.s. - web application for online forms
# Copyright (C) 2019 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import os
from django.core.management.base import CommandError
from wcs.formdef import FormDef
from wcs.categories import Category
from wcs.workflows import Workflow
from . import TenantCommand
class Command(TenantCommand):
help = 'Import an exported site'
def add_arguments(self, parser):
parser.add_argument('-d', '--domain', '--vhost', metavar='DOMAIN', required=True)
parser.add_argument('filename', metavar='FILENAME', type=str,
help='name of file to import')
parser.add_argument('--if-empty', action='store_true', default=False,
help='Import only if site is empty')
def handle(self, filename, domain, if_empty, **options):
publisher = self.init_tenant_publisher(domain)
if not os.path.exists(filename):
raise CommandError('missing file: %s' % filename)
# do not reconfigure if the 'if_empty' option is provided
is_empty = (FormDef.count() == 0
and Category.count() == 0
and Workflow.count() == 0)
if if_empty and not is_empty:
return
publisher.import_zip(open(filename, 'r'))
publisher.cleanup()