Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit d0a5ed3153518fe10c5048e8011612eb12144f5d 1 parent 0c35da7
@deshipu deshipu authored
Showing with 24 additions and 24 deletions.
  1. +24 −24 horizon/workflows/views.py
View
48 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 @@ def post(self, request, *args, **kwargs):
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())
Please sign in to comment.
Something went wrong with that request. Please try again.