workflows: add redirect to url action (#11245)

This commit is contained in:
Thomas NOËL 2017-01-23 17:20:11 +01:00
parent 2aab6759ff
commit e723b345ff
3 changed files with 78 additions and 0 deletions

View File

@ -35,6 +35,7 @@ from wcs.wf.wscall import WebserviceCallStatusItem
from wcs.wf.export_to_model import transform_to_pdf
from wcs.wf.geolocate import GeolocateWorkflowStatusItem
from wcs.wf.backoffice_fields import SetBackofficeFieldsWorkflowStatusItem
from wcs.wf.redirect_to_url import RedirectToUrlWorkflowStatusItem
from utilities import (create_temporary_pub, MockSubstitutionVariables, emails,
http_requests, clean_temporary_pub, sms_mocking)
@ -2193,3 +2194,25 @@ def test_set_backoffice_field_items(pub):
formdata = formdef.data_class().get(formdata.id)
assert formdata.data['bo1'] == ['a', 'b']
assert formdata.data['bo1_display'] == 'aa, bb'
def test_redirect_to_url(pub):
formdef = FormDef()
formdef.name = 'baz'
formdef.fields = [
StringField(id='1', label='Test', type='string', varname='foo'),
]
formdef.store()
formdata = formdef.data_class()()
formdata.data = {'1': 'bar'}
item = RedirectToUrlWorkflowStatusItem()
assert item.render_as_line() == 'Redirect to URL (not configured)'
item.url = 'https://www.example.net/?foo=[form_var_foo]'
assert item.render_as_line() == 'Redirect to URL "https://www.example.net/?foo=[form_var_foo]"'
pub.substitutions.feed(formdata)
assert item.perform(formdata) == 'https://www.example.net/?foo=bar'
item.url = '[if-any nada]https://www.example.net/[end]'
pub.substitutions.feed(formdata)
assert item.perform(formdata) == None

54
wcs/wf/redirect_to_url.py Normal file
View File

@ -0,0 +1,54 @@
# w.c.s. - web application for online forms
# 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
# 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/>.
from qommon.form import ComputedExpressionWidget
from wcs.workflows import WorkflowStatusItem, register_item_class
class RedirectToUrlWorkflowStatusItem(WorkflowStatusItem):
description = N_('Redirect to URL')
key = 'redirect_to_url'
endpoint = False
support_substitution_variables = True
url = None
def render_as_line(self):
if self.url:
return _('Redirect to URL "%s"') % self.url
else:
return _('Redirect to URL (not configured)')
def get_parameters(self):
return ('url',)
def add_parameters_widgets(self, form, parameters, prefix='', formdef=None):
if 'url' in parameters:
widget = form.add(ComputedExpressionWidget, '%surl' % prefix,
title=_('URL'), value=self.url,
hint=_('Common substitution variables are available with the [variable] syntax.'))
widget.extra_css_class = 'grid-1-1'
def perform(self, formdata):
if not self.url:
# action not yet configured: don't redirect
return
url = self.compute(self.url)
if not url:
return # don't redirect
return url
register_item_class(RedirectToUrlWorkflowStatusItem)

View File

@ -2393,5 +2393,6 @@ def load_extra():
import wf.criticality
import wf.profile
import wf.backoffice_fields
import wf.redirect_to_url
from wf.export_to_model import ExportToModel