From e3415500565c5a766d6226802eb2df571593c61e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 15 Sep 2017 12:09:45 +0200 Subject: [PATCH] misc: change trigger_jumps into a django command (#18730) --- tests/test_ctl.py | 2 +- wcs/ctl/management/__init__.py | 0 wcs/ctl/management/commands/__init__.py | 0 wcs/ctl/{ => management/commands}/trigger_jumps.py | 91 +++++++++------------- wcs/settings.py | 1 + 5 files changed, 37 insertions(+), 57 deletions(-) create mode 100644 wcs/ctl/management/__init__.py create mode 100644 wcs/ctl/management/commands/__init__.py rename wcs/ctl/{ => management/commands}/trigger_jumps.py (63%) diff --git a/tests/test_ctl.py b/tests/test_ctl.py index 04309c50..04a77596 100644 --- a/tests/test_ctl.py +++ b/tests/test_ctl.py @@ -14,7 +14,7 @@ from wcs.qommon.management.commands.collectstatic import Command as CmdCollectSt from wcs.qommon.management.commands.migrate import Command as CmdMigrate from wcs.ctl.process_bounce import CmdProcessBounce from wcs.ctl.wipe_data import CmdWipeData -from wcs.ctl.trigger_jumps import select_and_jump_formdata +from wcs.ctl.management.commands.trigger_jumps import select_and_jump_formdata from wcs.ctl.delete_tenant import CmdDeleteTenant from wcs.sql import get_connection_and_cursor, cleanup_connection diff --git a/wcs/ctl/management/__init__.py b/wcs/ctl/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wcs/ctl/management/commands/__init__.py b/wcs/ctl/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/wcs/ctl/trigger_jumps.py b/wcs/ctl/management/commands/trigger_jumps.py similarity index 63% rename from wcs/ctl/trigger_jumps.py rename to wcs/ctl/management/commands/trigger_jumps.py index 9f4e3d24..e72182c3 100644 --- a/wcs/ctl/trigger_jumps.py +++ b/wcs/ctl/management/commands/trigger_jumps.py @@ -1,5 +1,5 @@ # w.c.s. - web application for online forms -# Copyright (C) 2005-2013 Entr'ouvert +# Copyright (C) 2005-2017 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 @@ -14,20 +14,20 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see . -import os -import sys import json +import os -from quixote import get_publisher +from django.core.management.base import BaseCommand, CommandError -from qommon.ctl import Command, make_option +from quixote import get_publisher +from qommon.publisher import get_publisher_class from wcs.formdef import FormDef from wcs.workflows import Workflow from wcs.wf.jump import JumpWorkflowStatusItem, jump_and_perform as wcs_jump_and_perform -class CmdTriggerJumps(Command): +class Command(BaseCommand): '''Triggers all "jump trigger" for a formdef, given host publisher context source.json file format: @@ -40,57 +40,42 @@ class CmdTriggerJumps(Command): ] ''' - name = 'trigger-jumps' - - def __init__(self): - Command.__init__(self, [ - make_option('--trigger', metavar='TRIGGER', action='store', - dest='trigger', help='(mandatory) trigger name'), - make_option('--vhost', metavar='VHOST', action='store', - dest='vhost'), - make_option('--workflow-id', metavar='WORKFLOW_ID', action='store', - dest='workflow_id'), - make_option('--formdef-id', metavar='FORMDEF_ID', action='store', - dest='formdef_id'), - make_option('--all-formdata', metavar='ALL_FORMDATA', - action='store_true', dest='all_formdata'), - ]) - - def execute(self, base_options, sub_options, args): - - if not sub_options.all_formdata and not args: - exit('missing input json file(s) (or --all-formdata flag)') - if not sub_options.vhost: - exit('you must specify --vhost') - if not sub_options.trigger: - exit('you must specify --trigger') - if sub_options.formdef_id and sub_options.workflow_id: - exit('specify --workflow-id or --formdef-id') - - import publisher - self.config.remove_option('main', 'error_log') - publisher.WcsPublisher.configure(self.config) - publisher = publisher.WcsPublisher.create_publisher( - register_tld_names=False) - publisher.app_dir = os.path.join(publisher.app_dir, sub_options.vhost) - publisher.set_config() - - trigger = sub_options.trigger - if not sub_options.all_formdata: - rows = list(get_rows(args)) + def add_arguments(self, parser): + parser.add_argument('-d', '--domain', metavar='DOMAIN', required=True) + parser.add_argument('--trigger', metavar='TRIGGER', required=True) + parser.add_argument('--workflow-id', metavar='WORKFLOW_ID') + parser.add_argument('--formdef-id', metavar='FORMDEF_ID') + parser.add_argument('--all-formdata', action='store_true') + parser.add_argument('filenames', metavar='FILENAME', nargs='+') + + def init_tenant_publisher(self, domain): + publisher_class = get_publisher_class() + publisher = publisher_class.create_publisher() + if not domain in publisher.get_tenants(): + raise CommandError('unknown tenant') + publisher.app_dir = os.path.join(publisher.app_dir, domain) + return publisher + + def handle(self, filenames, domain, trigger, workflow_id, formdef_id, + all_formdata, **options): + + self.init_tenant_publisher(domain) + + if not all_formdata: + rows = list(get_rows(filenames)) else: rows = '__all__' - if sub_options.formdef_id: - formdef = FormDef.get(id=sub_options.formdef_id, ignore_errors=True) + if formdef_id: + formdef = FormDef.get(id=formdef_id, ignore_errors=True) if not formdef: - exit('formdef-id does not exist') + raise CommandError('formdef-id does not exist') select_and_jump_formdata(formdef, trigger, rows) else: - if sub_options.workflow_id: - workflow = Workflow.get(id=sub_options.workflow_id, ignore_errors=True) + if workflow_id: + workflow = Workflow.get(id=workflow_id, ignore_errors=True) if not workflow: - exit('workflow does not exist') + raise CommandError('workflow does not exist') workflows = [workflow] else: workflows = Workflow.select() @@ -101,12 +86,6 @@ class CmdTriggerJumps(Command): select_and_jump_formdata(formdef, trigger, rows, status_ids) -CmdTriggerJumps.register() - -def exit(message): - print >> sys.stderr, message - sys.exit(1) - def get_rows(args): for arg in args: for row in json.load(file(arg)): diff --git a/wcs/settings.py b/wcs/settings.py index 482daf96..73f3d605 100644 --- a/wcs/settings.py +++ b/wcs/settings.py @@ -130,6 +130,7 @@ INSTALLED_APPS = ( #'django.contrib.messages', #'django.contrib.admin', 'gadjo', + 'wcs.ctl', 'wcs.qommon', 'django.contrib.staticfiles', ) -- 2.14.1