diff options
author | Parker Berberian <pberberian@iol.unh.edu> | 2019-07-02 17:33:09 -0400 |
---|---|---|
committer | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2019-07-08 14:32:53 +0000 |
commit | bf45f8f4d05d9842b285013cfcb0a0235ce51c5d (patch) | |
tree | 528ceb8bf7627a65ed771ac3738031778e8750c8 /src/workflow | |
parent | e0b8bb3ee3c218a65562acdc5e76f714aab92926 (diff) |
Hacking on AJAX
This change incorporates the multiple changes we've made
in the past few days in order to actually get AJAX working.
Change-Id: Ia61a0395e7dbac26a143ca6e15049e6a4bdc8b0d
Signed-off-by: Parker Berberian <pberberian@iol.unh.edu>
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
Diffstat (limited to 'src/workflow')
-rw-r--r-- | src/workflow/models.py | 6 | ||||
-rw-r--r-- | src/workflow/urls.py | 6 | ||||
-rw-r--r-- | src/workflow/views.py | 59 | ||||
-rw-r--r-- | src/workflow/workflow_manager.py | 36 |
4 files changed, 53 insertions, 54 deletions
diff --git a/src/workflow/models.py b/src/workflow/models.py index 866f442..0521165 100644 --- a/src/workflow/models.py +++ b/src/workflow/models.py @@ -318,6 +318,9 @@ class Confirmation_Step(WorkflowStep): default_flow_style=False ).strip() + if self.valid == WorkflowStepStatus.VALID: + context["confirm_succeeded"] = "true" + return context def flush_to_db(self): @@ -329,9 +332,7 @@ class Confirmation_Step(WorkflowStep): form = ConfirmationForm(post_data) if form.is_valid(): data = form.cleaned_data['confirm'] - context = self.get_context() if data == "True": - context["bypassed"] = "true" errors = self.flush_to_db() if errors: self.set_invalid("ERROR OCCURRED: " + errors) @@ -339,7 +340,6 @@ class Confirmation_Step(WorkflowStep): self.set_valid("Confirmed") elif data == "False": - context["bypassed"] = "true" self.set_valid("Canceled") else: self.set_invalid("Bad Form Contents") diff --git a/src/workflow/urls.py b/src/workflow/urls.py index ae620d0..298db95 100644 --- a/src/workflow/urls.py +++ b/src/workflow/urls.py @@ -11,7 +11,7 @@ from django.conf.urls import url from django.conf import settings -from workflow.views import delete_session, manager_view, viewport_view, add_workflow, cancel_workflow +from workflow.views import manager_view, viewport_view, add_workflow, remove_workflow, create_workflow from workflow.models import Repository from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info from workflow.booking_workflow import SWConfig_Select, Booking_Resource_Select, Booking_Meta @@ -19,10 +19,10 @@ from workflow.booking_workflow import SWConfig_Select, Booking_Resource_Select, app_name = 'workflow' urlpatterns = [ - url(r'^finish/$', delete_session, name='delete_session'), url(r'^manager/$', manager_view, name='manager'), url(r'^add/$', add_workflow, name='add_workflow'), - url(r'^cancel/$', cancel_workflow, name='cancel_workflow'), + url(r'^create/$', create_workflow, name='create_workflow'), + url(r'^pop/$', remove_workflow, name='remove_workflow'), url(r'^$', viewport_view, name='viewport') ] diff --git a/src/workflow/views.py b/src/workflow/views.py index 47241e2..3ab4d30 100644 --- a/src/workflow/views.py +++ b/src/workflow/views.py @@ -8,9 +8,8 @@ ############################################################################## -from django.http import HttpResponse, JsonResponse +from django.http import HttpResponse, JsonResponse, HttpResponseRedirect from django.shortcuts import render -from django.urls import reverse import uuid @@ -31,33 +30,17 @@ def attempt_auth(request): return None -def get_redirect_response(result): - if not result: - return {} - - # need to get type of result, and switch on the type - # since has_result, result must be populated with a valid object - if isinstance(result, Booking): - return { - 'redir_url': reverse('booking:booking_detail', kwargs={'booking_id': result.id}) - } - else: - return {} - - -def delete_session(request): +def remove_workflow(request): manager = attempt_auth(request) if not manager: return no_workflow(request) - not_last_workflow, result = manager.pop_workflow() + has_more_workflows, result = manager.pop_workflow() - if not_last_workflow: # this was not the last workflow, so don't redirect away - return JsonResponse({}) - else: + if not has_more_workflows: # this was the last workflow, so delete the reference to it in the tracker del ManagerTracker.managers[request.session['manager_session']] - return JsonResponse(get_redirect_response(result)) + return manager.render(request) def add_workflow(request): @@ -73,15 +56,6 @@ def add_workflow(request): return manager.render(request) # do we want this? -def cancel_workflow(request): - manager = attempt_auth(request) - if not manager: - return no_workflow(request) - - if not manager.pop_workflow(): - del ManagerTracker.managers[request.session['manager_session']] - - def manager_view(request): manager = attempt_auth(request) if not manager: @@ -98,16 +72,27 @@ def viewport_view(request): if manager is None: return no_workflow(request) - if request.method == 'GET': - return render(request, 'workflow/viewport-base.html') - else: - pass + if request.method != 'GET': + return HttpResponse(status=405) + return render(request, 'workflow/viewport-base.html') + + +def create_workflow(request): + if request.method != 'POST': + return HttpResponse(status=405) + workflow_type = request.POST.get('workflow_type') + try: + workflow_type = int(workflow_type) + except Exception: + return HttpResponse(status=400) + mgr_uuid = create_session(workflow_type, request=request,) + request.session['manager_session'] = mgr_uuid + return HttpResponse() def create_session(wf_type, request): - wf = int(wf_type) smgr = SessionManager(request=request) - smgr.add_workflow(workflow_type=wf, target_id=request.POST.get("target")) + smgr.add_workflow(workflow_type=wf_type, target_id=request.POST.get("target")) manager_uuid = uuid.uuid4().hex ManagerTracker.getInstance().managers[manager_uuid] = smgr diff --git a/src/workflow/workflow_manager.py b/src/workflow/workflow_manager.py index 605eee7..4677829 100644 --- a/src/workflow/workflow_manager.py +++ b/src/workflow/workflow_manager.py @@ -10,6 +10,7 @@ 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 @@ -32,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() @@ -62,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: @@ -69,9 +74,13 @@ 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): return { @@ -82,7 +91,7 @@ class SessionManager(): def handle_post(self, request): form = ManagerForm(request.POST) - if form.is_valid: + if form.is_valid(): self.get_active_step().post( QueryDict(form.cleaned_data['step_form']), user=request.user @@ -98,13 +107,18 @@ class SessionManager(): def handle_request(self, request): if request.method == 'POST': self.handle_post(request) - return self.render() + return self.render(request) def render(self, request, **kwargs): - return JsonResponse({ - "meta": self.status(), - "content": self.get_active_step().render_to_string(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) |