1 |
1 |
# w.c.s. - web application for online forms
|
2 |
|
# Copyright (C) 2005-2013 Entr'ouvert
|
|
2 |
# Copyright (C) 2005-2017 Entr'ouvert
|
3 |
3 |
#
|
4 |
4 |
# This program is free software; you can redistribute it and/or modify
|
5 |
5 |
# it under the terms of the GNU General Public License as published by
|
... | ... | |
14 |
14 |
# You should have received a copy of the GNU General Public License
|
15 |
15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
16 |
16 |
|
17 |
|
import os
|
18 |
|
import sys
|
19 |
17 |
import json
|
|
18 |
import os
|
20 |
19 |
|
21 |
|
from quixote import get_publisher
|
|
20 |
from django.core.management.base import BaseCommand, CommandError
|
22 |
21 |
|
23 |
|
from qommon.ctl import Command, make_option
|
|
22 |
from quixote import get_publisher
|
|
23 |
from qommon.publisher import get_publisher_class
|
24 |
24 |
|
25 |
25 |
from wcs.formdef import FormDef
|
26 |
26 |
from wcs.workflows import Workflow
|
27 |
27 |
from wcs.wf.jump import JumpWorkflowStatusItem, jump_and_perform as wcs_jump_and_perform
|
28 |
28 |
|
29 |
29 |
|
30 |
|
class CmdTriggerJumps(Command):
|
|
30 |
class Command(BaseCommand):
|
31 |
31 |
'''Triggers all "jump trigger" for a formdef, given host publisher context
|
32 |
32 |
|
33 |
33 |
source.json file format:
|
... | ... | |
40 |
40 |
]
|
41 |
41 |
'''
|
42 |
42 |
|
43 |
|
name = 'trigger-jumps'
|
44 |
|
|
45 |
|
def __init__(self):
|
46 |
|
Command.__init__(self, [
|
47 |
|
make_option('--trigger', metavar='TRIGGER', action='store',
|
48 |
|
dest='trigger', help='(mandatory) trigger name'),
|
49 |
|
make_option('--vhost', metavar='VHOST', action='store',
|
50 |
|
dest='vhost'),
|
51 |
|
make_option('--workflow-id', metavar='WORKFLOW_ID', action='store',
|
52 |
|
dest='workflow_id'),
|
53 |
|
make_option('--formdef-id', metavar='FORMDEF_ID', action='store',
|
54 |
|
dest='formdef_id'),
|
55 |
|
make_option('--all-formdata', metavar='ALL_FORMDATA',
|
56 |
|
action='store_true', dest='all_formdata'),
|
57 |
|
])
|
58 |
|
|
59 |
|
def execute(self, base_options, sub_options, args):
|
60 |
|
|
61 |
|
if not sub_options.all_formdata and not args:
|
62 |
|
exit('missing input json file(s) (or --all-formdata flag)')
|
63 |
|
if not sub_options.vhost:
|
64 |
|
exit('you must specify --vhost')
|
65 |
|
if not sub_options.trigger:
|
66 |
|
exit('you must specify --trigger')
|
67 |
|
if sub_options.formdef_id and sub_options.workflow_id:
|
68 |
|
exit('specify --workflow-id or --formdef-id')
|
69 |
|
|
70 |
|
import publisher
|
71 |
|
self.config.remove_option('main', 'error_log')
|
72 |
|
publisher.WcsPublisher.configure(self.config)
|
73 |
|
publisher = publisher.WcsPublisher.create_publisher(
|
74 |
|
register_tld_names=False)
|
75 |
|
publisher.app_dir = os.path.join(publisher.app_dir, sub_options.vhost)
|
76 |
|
publisher.set_config()
|
77 |
|
|
78 |
|
trigger = sub_options.trigger
|
79 |
|
if not sub_options.all_formdata:
|
80 |
|
rows = list(get_rows(args))
|
|
43 |
def add_arguments(self, parser):
|
|
44 |
parser.add_argument('-d', '--domain', metavar='DOMAIN', required=True)
|
|
45 |
parser.add_argument('--trigger', metavar='TRIGGER', required=True)
|
|
46 |
parser.add_argument('--workflow-id', metavar='WORKFLOW_ID')
|
|
47 |
parser.add_argument('--formdef-id', metavar='FORMDEF_ID')
|
|
48 |
parser.add_argument('--all-formdata', action='store_true')
|
|
49 |
parser.add_argument('filenames', metavar='FILENAME', nargs='+')
|
|
50 |
|
|
51 |
def init_tenant_publisher(self, domain):
|
|
52 |
publisher_class = get_publisher_class()
|
|
53 |
publisher = publisher_class.create_publisher()
|
|
54 |
if not domain in publisher.get_tenants():
|
|
55 |
raise CommandError('unknown tenant')
|
|
56 |
publisher.app_dir = os.path.join(publisher.app_dir, domain)
|
|
57 |
return publisher
|
|
58 |
|
|
59 |
def handle(self, filenames, domain, trigger, workflow_id, formdef_id,
|
|
60 |
all_formdata, **options):
|
|
61 |
|
|
62 |
self.init_tenant_publisher(domain)
|
|
63 |
|
|
64 |
if not all_formdata:
|
|
65 |
rows = list(get_rows(filenames))
|
81 |
66 |
else:
|
82 |
67 |
rows = '__all__'
|
83 |
68 |
|
84 |
|
if sub_options.formdef_id:
|
85 |
|
formdef = FormDef.get(id=sub_options.formdef_id, ignore_errors=True)
|
|
69 |
if formdef_id:
|
|
70 |
formdef = FormDef.get(id=formdef_id, ignore_errors=True)
|
86 |
71 |
if not formdef:
|
87 |
|
exit('formdef-id does not exist')
|
|
72 |
raise CommandError('formdef-id does not exist')
|
88 |
73 |
select_and_jump_formdata(formdef, trigger, rows)
|
89 |
74 |
else:
|
90 |
|
if sub_options.workflow_id:
|
91 |
|
workflow = Workflow.get(id=sub_options.workflow_id, ignore_errors=True)
|
|
75 |
if workflow_id:
|
|
76 |
workflow = Workflow.get(id=workflow_id, ignore_errors=True)
|
92 |
77 |
if not workflow:
|
93 |
|
exit('workflow does not exist')
|
|
78 |
raise CommandError('workflow does not exist')
|
94 |
79 |
workflows = [workflow]
|
95 |
80 |
else:
|
96 |
81 |
workflows = Workflow.select()
|
... | ... | |
101 |
86 |
select_and_jump_formdata(formdef, trigger, rows, status_ids)
|
102 |
87 |
|
103 |
88 |
|
104 |
|
CmdTriggerJumps.register()
|
105 |
|
|
106 |
|
def exit(message):
|
107 |
|
print >> sys.stderr, message
|
108 |
|
sys.exit(1)
|
109 |
|
|
110 |
89 |
def get_rows(args):
|
111 |
90 |
for arg in args:
|
112 |
91 |
for row in json.load(file(arg)):
|