From b6fb290763a28bd524b4fb537ccb4e226c1596be Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Mon, 23 Jan 2017 17:20:11 +0100 Subject: [PATCH] workflows: add redirect to url action (#11245) --- tests/test_workflows.py | 23 ++++++++++++++++++++ wcs/wf/redirect_to_url.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ wcs/workflows.py | 1 + 3 files changed, 78 insertions(+) create mode 100644 wcs/wf/redirect_to_url.py diff --git a/tests/test_workflows.py b/tests/test_workflows.py index b30e398f..80839446 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -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 diff --git a/wcs/wf/redirect_to_url.py b/wcs/wf/redirect_to_url.py new file mode 100644 index 00000000..9cca2a7b --- /dev/null +++ b/wcs/wf/redirect_to_url.py @@ -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 . + +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) diff --git a/wcs/workflows.py b/wcs/workflows.py index 9acea891..4191cad3 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -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 -- 2.11.0