2013-06-13 16:39:28 +02:00
|
|
|
# w.c.s. - web application for online forms
|
2017-09-15 12:09:45 +02:00
|
|
|
# Copyright (C) 2005-2017 Entr'ouvert
|
2013-06-13 16:39:28 +02:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2019-11-12 08:59:27 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2013-06-13 16:39:28 +02:00
|
|
|
import json
|
2017-09-15 12:09:45 +02:00
|
|
|
import os
|
2013-06-13 16:39:28 +02:00
|
|
|
|
2019-06-30 13:18:58 +02:00
|
|
|
from django.core.management.base import CommandError
|
2016-11-14 18:12:38 +01:00
|
|
|
|
2017-09-15 12:09:45 +02:00
|
|
|
from quixote import get_publisher
|
2013-06-13 16:39:28 +02:00
|
|
|
|
|
|
|
from wcs.formdef import FormDef
|
2013-06-20 11:43:09 +02:00
|
|
|
from wcs.workflows import Workflow
|
2016-11-14 18:12:38 +01:00
|
|
|
from wcs.wf.jump import JumpWorkflowStatusItem, jump_and_perform as wcs_jump_and_perform
|
2013-06-13 16:39:28 +02:00
|
|
|
|
2019-06-30 13:18:58 +02:00
|
|
|
from . import TenantCommand
|
2013-06-13 16:39:28 +02:00
|
|
|
|
2019-06-30 13:18:58 +02:00
|
|
|
|
|
|
|
class Command(TenantCommand):
|
2013-06-13 16:39:28 +02:00
|
|
|
'''Triggers all "jump trigger" for a formdef, given host publisher context
|
|
|
|
|
|
|
|
source.json file format:
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"data": { "info_for_wf_data": 42, ... },
|
|
|
|
"select": { "form_number": 1, ... }
|
|
|
|
},
|
|
|
|
...
|
|
|
|
]
|
|
|
|
'''
|
|
|
|
|
2017-09-15 12:09:45 +02:00
|
|
|
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 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))
|
2013-06-20 11:43:09 +02:00
|
|
|
else:
|
|
|
|
rows = '__all__'
|
|
|
|
|
2017-09-15 12:09:45 +02:00
|
|
|
if formdef_id:
|
|
|
|
formdef = FormDef.get(id=formdef_id, ignore_errors=True)
|
2013-06-20 11:43:09 +02:00
|
|
|
if not formdef:
|
2017-09-15 12:09:45 +02:00
|
|
|
raise CommandError('formdef-id does not exist')
|
2013-06-20 11:43:09 +02:00
|
|
|
select_and_jump_formdata(formdef, trigger, rows)
|
|
|
|
else:
|
2017-09-15 12:09:45 +02:00
|
|
|
if workflow_id:
|
|
|
|
workflow = Workflow.get(id=workflow_id, ignore_errors=True)
|
2013-06-20 11:43:09 +02:00
|
|
|
if not workflow:
|
2017-09-15 12:09:45 +02:00
|
|
|
raise CommandError('workflow does not exist')
|
2013-06-20 11:43:09 +02:00
|
|
|
workflows = [workflow]
|
|
|
|
else:
|
|
|
|
workflows = Workflow.select()
|
|
|
|
for workflow in workflows:
|
|
|
|
status_ids = list(get_status_ids_accepting_trigger(workflow, trigger))
|
|
|
|
if status_ids:
|
|
|
|
for formdef in [f for f in FormDef.select() if f.workflow_id == workflow.id]:
|
|
|
|
select_and_jump_formdata(formdef, trigger, rows, status_ids)
|
2013-06-13 16:39:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_rows(args):
|
|
|
|
for arg in args:
|
|
|
|
for row in json.load(file(arg)):
|
|
|
|
yield row
|
|
|
|
|
2013-06-20 11:43:09 +02:00
|
|
|
def get_status_ids_accepting_trigger(workflow, trigger):
|
2013-06-13 16:39:28 +02:00
|
|
|
for status in workflow.possible_status:
|
|
|
|
for item in status.items:
|
|
|
|
if isinstance(item, JumpWorkflowStatusItem) and \
|
|
|
|
item.trigger == trigger:
|
2019-10-29 13:23:34 +01:00
|
|
|
yield 'wf-%s' % status.id, item
|
2013-06-13 16:39:28 +02:00
|
|
|
break
|
|
|
|
|
2013-06-20 11:43:09 +02:00
|
|
|
def get_formdata_accepting_trigger(formdef, trigger, status_ids=None):
|
|
|
|
if status_ids is None:
|
|
|
|
workflow = formdef.get_workflow()
|
|
|
|
status_ids = get_status_ids_accepting_trigger(workflow, trigger)
|
2013-06-13 16:39:28 +02:00
|
|
|
formdata_ids = []
|
|
|
|
|
|
|
|
data_class = formdef.data_class()
|
2019-10-29 13:23:34 +01:00
|
|
|
for status_id, action_item in status_ids:
|
2013-06-20 11:43:09 +02:00
|
|
|
formdata_ids = data_class.get_ids_with_indexed_value('status', status_id)
|
2013-06-13 16:39:28 +02:00
|
|
|
for formdata_id in formdata_ids:
|
2019-10-29 13:23:34 +01:00
|
|
|
yield data_class.get(id=formdata_id), action_item
|
2013-06-13 16:39:28 +02:00
|
|
|
|
2013-10-16 12:19:51 +02:00
|
|
|
def match_row(substitution_variables, row):
|
2013-06-13 16:39:28 +02:00
|
|
|
select = row['select']
|
2019-11-12 19:47:26 +01:00
|
|
|
for key, value in select.items():
|
2013-06-13 16:39:28 +02:00
|
|
|
if str(substitution_variables.get(key)) != str(value):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2019-10-29 13:23:34 +01:00
|
|
|
def jump_and_perform(formdata, action, workflow_data=None):
|
2016-11-14 18:12:38 +01:00
|
|
|
get_publisher().substitutions.reset()
|
|
|
|
get_publisher().substitutions.feed(get_publisher())
|
|
|
|
get_publisher().substitutions.feed(formdata.formdef)
|
|
|
|
get_publisher().substitutions.feed(formdata)
|
2019-11-12 08:59:27 +01:00
|
|
|
print('formdata %s jumps to status %s' % (formdata, action.status))
|
2019-10-29 13:23:34 +01:00
|
|
|
wcs_jump_and_perform(formdata, action, workflow_data=workflow_data)
|
2016-11-14 18:12:38 +01:00
|
|
|
|
2013-06-20 11:43:09 +02:00
|
|
|
def select_and_jump_formdata(formdef, trigger, rows, status_ids=None):
|
2019-10-29 13:23:34 +01:00
|
|
|
for formdata, action_item in get_formdata_accepting_trigger(formdef, trigger, status_ids):
|
2013-06-20 11:43:09 +02:00
|
|
|
if rows == '__all__':
|
2019-10-29 13:23:34 +01:00
|
|
|
jump_and_perform(formdata, action=action_item)
|
2013-06-20 11:43:09 +02:00
|
|
|
else:
|
2013-10-16 12:19:51 +02:00
|
|
|
substitution_variables = formdata.get_substitution_variables()
|
2013-06-20 11:43:09 +02:00
|
|
|
for row in rows:
|
2013-10-16 12:19:51 +02:00
|
|
|
if match_row(substitution_variables, row):
|
2019-10-29 13:23:34 +01:00
|
|
|
jump_and_perform(formdata, action_item, row.get('data'))
|
2013-06-20 11:43:09 +02:00
|
|
|
break # next formdata
|