From d0a5ed3153518fe10c5048e8011612eb12144f5d Mon Sep 17 00:00:00 2001 From: Radomir Dopieralski Date: Tue, 25 Feb 2014 09:45:53 +0100 Subject: [PATCH] Allow redisplay of the workflow from its handle() Let the handle() method of the workflow raise a ValidationError to indicate, that the operation failed and the whole workflow has to be redisplayed for the user to correct the data and try again. It is assumed that the handle() method will inject the right error messages in the right steps and fields of the workflow. This patch also cleans up the logic of the code a little, to make it easier to read, and replaces the "next" variable that conflicts with a python build-in with a more descriptive name. Change-Id: I2fddb5ae8abbff72b8bf8258001a6f61aef3d1ba Implements: bp/redisplay-workflow-after-handle-error --- horizon/workflows/views.py | 48 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/horizon/workflows/views.py b/horizon/workflows/views.py index 1c691e52a4..c2babceaee 100644 --- a/horizon/workflows/views.py +++ b/horizon/workflows/views.py @@ -17,6 +17,7 @@ import copy import json +from django import forms from django import http from django import shortcuts from django.views import generic @@ -183,28 +184,27 @@ class WorkflowView(generic.TemplateView): validate_step_end) return http.HttpResponse(json.dumps(data), content_type="application/json") - if workflow.is_valid(): - try: - success = workflow.finalize() - except Exception: - success = False - exceptions.handle(request) - next = self.request.REQUEST.get(workflow.redirect_param_name, None) - if success: - msg = workflow.format_status_message(workflow.success_message) - messages.success(request, msg) - else: - msg = workflow.format_status_message(workflow.failure_message) - messages.error(request, msg) - - if "HTTP_X_HORIZON_ADD_TO_FIELD" in self.request.META: - field_id = self.request.META["HTTP_X_HORIZON_ADD_TO_FIELD"] - data = [self.get_object_id(workflow.object), - self.get_object_display(workflow.object)] - response = http.HttpResponse(json.dumps(data)) - response["X-Horizon-Add-To-Field"] = field_id - return response - else: - return shortcuts.redirect(next or workflow.get_success_url()) - else: + if not workflow.is_valid(): return self.render_to_response(context) + try: + success = workflow.finalize() + except forms.ValidationError: + return self.render_to_response(context) + except Exception: + success = False + exceptions.handle(request) + if success: + msg = workflow.format_status_message(workflow.success_message) + messages.success(request, msg) + else: + msg = workflow.format_status_message(workflow.failure_message) + messages.error(request, msg) + if "HTTP_X_HORIZON_ADD_TO_FIELD" in self.request.META: + field_id = self.request.META["HTTP_X_HORIZON_ADD_TO_FIELD"] + data = [self.get_object_id(workflow.object), + self.get_object_display(workflow.object)] + response = http.HttpResponse(json.dumps(data)) + response["X-Horizon-Add-To-Field"] = field_id + return response + next_url = self.request.REQUEST.get(workflow.redirect_param_name, None) + return shortcuts.redirect(next_url or workflow.get_success_url())