aboutsummaryrefslogtreecommitdiffstats
path: root/src/workflow/workflow_manager.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/workflow/workflow_manager.py')
-rw-r--r--src/workflow/workflow_manager.py76
1 files changed, 51 insertions, 25 deletions
diff --git a/src/workflow/workflow_manager.py b/src/workflow/workflow_manager.py
index 80b8a67..4677829 100644
--- a/src/workflow/workflow_manager.py
+++ b/src/workflow/workflow_manager.py
@@ -9,6 +9,8 @@
from django.http import JsonResponse
+from django.http.request import QueryDict
+from django.urls import reverse
from booking.models import Booking
from workflow.workflow_factory import WorkflowFactory
@@ -19,6 +21,7 @@ from resource_inventory.models import (
HostConfiguration,
OPNFVConfig
)
+from workflow.forms import ManagerForm
import logging
logger = logging.getLogger(__name__)
@@ -30,10 +33,9 @@ class SessionManager():
def __init__(self, request=None):
self.workflows = []
-
self.owner = request.user
-
self.factory = WorkflowFactory()
+ self.result = None
def set_step_statuses(self, superclass_type, desired_enabled=True):
workflow = self.active_workflow()
@@ -45,10 +47,7 @@ class SessionManager():
else:
step.disable()
- def add_workflow(self, workflow_type=None, target_id=None, **kwargs):
- if target_id is not None:
- self.prefill_repo(target_id, workflow_type)
-
+ def add_workflow(self, workflow_type=None, **kwargs):
repo = Repository()
if(len(self.workflows) >= 1):
defaults = self.workflows[-1].repository.get_child_defaults()
@@ -63,6 +62,11 @@ class SessionManager():
)
)
+ def get_redirect(self):
+ if isinstance(self.result, Booking):
+ return reverse('booking:booking_detail', kwargs={'booking_id': self.result.id})
+ return "/"
+
def pop_workflow(self):
multiple_wfs = len(self.workflows) > 1
if multiple_wfs:
@@ -70,29 +74,51 @@ class SessionManager():
key = self.workflows[-1].repository.el[Repository.RESULT_KEY]
result = self.workflows[-1].repository.el[Repository.RESULT]
self.workflows[-2].repository.el[key] = result
- self.workflows.pop()
- current_repo = self.workflows[-1].repository
- return (multiple_wfs, current_repo.el[current_repo.RESULT])
+ prev_workflow = self.workflows.pop()
+ if self.workflows:
+ current_repo = self.workflows[-1].repository
+ else:
+ current_repo = prev_workflow.repository
+ self.result = current_repo.el[current_repo.RESULT]
+ return multiple_wfs, self.result
def status(self, request):
- try:
- meta_json = []
- for step in self.active_workflow().steps:
- meta_json.append(step.to_json())
- responsejson = {}
- responsejson["steps"] = meta_json
- responsejson["active"] = self.active_workflow().repository.el['active_step']
- responsejson["workflow_count"] = len(self.workflows)
- return JsonResponse(responsejson, safe=False)
- except Exception:
- pass
+ return {
+ "steps": [step.to_json() for step in self.active_workflow().steps],
+ "active": self.active_workflow().repository.el['active_step'],
+ "workflow_count": len(self.workflows)
+ }
+
+ def handle_post(self, request):
+ form = ManagerForm(request.POST)
+ if form.is_valid():
+ self.get_active_step().post(
+ QueryDict(form.cleaned_data['step_form']),
+ user=request.user
+ )
+ # change step
+ if form.cleaned_data['step'] == 'prev':
+ self.go_prev()
+ if form.cleaned_data['step'] == 'next':
+ self.go_next()
+ else:
+ pass # Exception?
+
+ def handle_request(self, request):
+ if request.method == 'POST':
+ self.handle_post(request)
+ return self.render(request)
def render(self, request, **kwargs):
- # filter out when a step needs to handle post/form data
- # if 'workflow' in post data, this post request was meant for me, not step
- if request.method == 'POST' and request.POST.get('workflow', None) is None:
- return self.active_workflow().steps[self.active_workflow().active_index].post_render(request)
- return self.active_workflow().steps[self.active_workflow().active_index].render(request)
+ if self.workflows:
+ return JsonResponse({
+ "meta": self.status(request),
+ "content": self.get_active_step().render_to_string(request),
+ })
+ else:
+ return JsonResponse({
+ "redirect": self.get_redirect()
+ })
def post_render(self, request):
return self.active_workflow().steps[self.active_workflow().active_index].post_render(request)