diff options
Diffstat (limited to 'src/workflow')
-rw-r--r-- | src/workflow/booking_workflow.py | 8 | ||||
-rw-r--r-- | src/workflow/forms.py | 19 | ||||
-rw-r--r-- | src/workflow/models.py | 40 | ||||
-rw-r--r-- | src/workflow/opnfv_workflow.py | 23 | ||||
-rw-r--r-- | src/workflow/resource_bundle_workflow.py | 75 | ||||
-rw-r--r-- | src/workflow/snapshot_workflow.py | 15 | ||||
-rw-r--r-- | src/workflow/sw_bundle_workflow.py | 16 | ||||
-rw-r--r-- | src/workflow/tests/test_steps.py | 48 | ||||
-rw-r--r-- | src/workflow/urls.py | 7 | ||||
-rw-r--r-- | src/workflow/views.py | 57 | ||||
-rw-r--r-- | src/workflow/workflow_manager.py | 52 |
11 files changed, 124 insertions, 236 deletions
diff --git a/src/workflow/booking_workflow.py b/src/workflow/booking_workflow.py index 42372ce..3698164 100644 --- a/src/workflow/booking_workflow.py +++ b/src/workflow/booking_workflow.py @@ -7,7 +7,6 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from django.contrib import messages from django.utils import timezone from datetime import timedelta @@ -171,8 +170,8 @@ class Booking_Meta(WorkflowStep): context['form'] = BookingMetaForm(initial=initial, user_initial=default, owner=owner) return context - def post_render(self, request): - form = BookingMetaForm(data=request.POST, owner=request.user) + def post(self, post_data, user): + form = BookingMetaForm(data=post_data, owner=user) forms = self.repo_get(self.repo.BOOKING_FORMS, {}) @@ -212,9 +211,6 @@ class Booking_Meta(WorkflowStep): self.repo_put(self.repo.BOOKING_MODELS, models) self.repo_put(self.repo.CONFIRMATION, confirm) - messages.add_message(request, messages.SUCCESS, 'Form Validated', fail_silently=True) self.set_valid("Step Completed") else: - messages.add_message(request, messages.ERROR, "Form didn't validate", fail_silently=True) self.set_invalid("Please complete the fields highlighted in red to continue") - return self.render(request) diff --git a/src/workflow/forms.py b/src/workflow/forms.py index ee44ecd..a2746f9 100644 --- a/src/workflow/forms.py +++ b/src/workflow/forms.py @@ -15,6 +15,7 @@ from django.template.loader import render_to_string from django.forms.widgets import NumberInput import json +import urllib from account.models import Lab from account.models import UserProfile @@ -428,6 +429,24 @@ class ConfirmationForm(forms.Form): ) +def validate_step(value): + if value not in ["prev", "next", "current"]: + raise ValidationError(str(value) + " is not allowed") + + +def validate_step_form(value): + try: + urllib.parse.unquote_plus(value) + except Exception: + raise ValidationError("Value is not url encoded data") + + +class ManagerForm(forms.Form): + step = forms.CharField(widget=forms.widgets.HiddenInput, validators=[validate_step]) + step_form = forms.CharField(widget=forms.widgets.HiddenInput, validators=[validate_step_form]) + # other fields? + + class OPNFVSelectionForm(forms.Form): installer = forms.ModelChoiceField(queryset=Installer.objects.all(), required=True) scenario = forms.ModelChoiceField(queryset=Scenario.objects.all(), required=True) diff --git a/src/workflow/models.py b/src/workflow/models.py index 6c6bd9a..866f442 100644 --- a/src/workflow/models.py +++ b/src/workflow/models.py @@ -8,8 +8,7 @@ ############################################################################## -from django.shortcuts import render -from django.contrib import messages +from django.template.loader import get_template from django.http import HttpResponse from django.utils import timezone @@ -219,12 +218,19 @@ class WorkflowStep(object): return context def render(self, request): - self.context = self.get_context() - return render(request, self.template, self.context) + return HttpResponse(self.render_to_string(request)) + + def render_to_string(self, request): + template = get_template(self.template) + return template.render(self.get_context(), request) def post_render(self, request): + self.post(request.POST, request.user) return self.render(request) + def post(self, post_content, user): + raise Exception("WorkflowStep subclass of type " + str(type(self)) + " has no concrete post() method") + def test_render(self, request): if request.method == "POST": return self.post_render(request) @@ -263,22 +269,18 @@ class AbstractSelectOrCreate(WorkflowStep): def alert_bundle_missing(self): # override in subclasses to change message if field isn't filled out self.set_invalid("Please select a valid bundle") - def post_render(self, request): - context = self.get_context() - form = self.form(request.POST, queryset=self.get_form_queryset()) + def post(self, post_data, user): + form = self.form(post_data, queryset=self.get_form_queryset()) if form.is_valid(): bundle = form.get_validated_bundle() if not bundle: self.alert_bundle_missing() - return render(request, self.template, context) + return self.repo_put(self.select_repo_key, bundle) self.put_confirm_info(bundle) self.set_valid("Step Completed") else: self.alert_bundle_missing() - messages.add_message(request, messages.ERROR, "Form Didn't Validate", fail_silently=True) - - return self.render(request) def get_context(self): default = [] @@ -323,8 +325,8 @@ class Confirmation_Step(WorkflowStep): if errors: return errors - def post_render(self, request): - form = ConfirmationForm(request.POST) + def post(self, post_data, user): + form = ConfirmationForm(post_data) if form.is_valid(): data = form.cleaned_data['confirm'] context = self.get_context() @@ -332,20 +334,18 @@ class Confirmation_Step(WorkflowStep): context["bypassed"] = "true" errors = self.flush_to_db() if errors: - messages.add_message(request, messages.ERROR, "ERROR OCCURRED: " + errors) + self.set_invalid("ERROR OCCURRED: " + errors) else: - messages.add_message(request, messages.SUCCESS, "Confirmed") + self.set_valid("Confirmed") - return HttpResponse('') elif data == "False": context["bypassed"] = "true" - messages.add_message(request, messages.SUCCESS, "Canceled") - return render(request, self.template, context) + self.set_valid("Canceled") else: - pass + self.set_invalid("Bad Form Contents") else: - pass + self.set_invalid("Bad Form Contents") class Repository(): diff --git a/src/workflow/opnfv_workflow.py b/src/workflow/opnfv_workflow.py index 7d499ec..a192d6e 100644 --- a/src/workflow/opnfv_workflow.py +++ b/src/workflow/opnfv_workflow.py @@ -74,8 +74,8 @@ class Pick_Installer(WorkflowStep): context["form"] = OPNFVSelectionForm(initial=initial) return context - def post_render(self, request): - form = OPNFVSelectionForm(request.POST) + def post(self, post_data, user): + form = OPNFVSelectionForm(post_data) if form.is_valid(): installer = form.cleaned_data['installer'] scenario = form.cleaned_data['scenario'] @@ -88,8 +88,6 @@ class Pick_Installer(WorkflowStep): else: self.set_invalid("Please select an Installer and Scenario") - return self.render(request) - class Assign_Network_Roles(WorkflowStep): template = 'config_bundle/steps/assign_network_roles.html' @@ -150,11 +148,11 @@ class Assign_Network_Roles(WorkflowStep): confirm['network roles'][role['role']] = role['network'].name self.repo_put(self.repo.CONFIRMATION, confirm) - def post_render(self, request): + def post(self, post_data, user): models = self.repo_get(self.repo.OPNFV_MODELS, {}) config_bundle = self.repo_get(self.repo.SELECTED_CONFIG_BUNDLE) roles = OPNFV_SETTINGS.NETWORK_ROLES - net_role_formset = self.create_netformset(roles, config_bundle, data=request.POST) + net_role_formset = self.create_netformset(roles, config_bundle, data=post_data) if net_role_formset.is_valid(): results = [] for form in net_role_formset: @@ -168,7 +166,6 @@ class Assign_Network_Roles(WorkflowStep): self.update_confirmation() else: self.set_invalid("Please complete all fields") - return self.render(request) class Assign_Host_Roles(WorkflowStep): # taken verbatim from Define_Software in sw workflow, merge the two? @@ -227,8 +224,8 @@ class Assign_Host_Roles(WorkflowStep): # taken verbatim from Define_Software in confirm['host roles'][role['host_name']] = role['role'].name self.repo_put(self.repo.CONFIRMATION, confirm) - def post_render(self, request): - formset = self.create_host_role_formset(data=request.POST) + def post(self, post_data, user): + formset = self.create_host_role_formset(data=post_data) models = self.repo_get(self.repo.OPNFV_MODELS, {}) host_roles = models.get("host_roles", []) @@ -254,8 +251,6 @@ class Assign_Host_Roles(WorkflowStep): # taken verbatim from Define_Software in else: self.set_invalid("Please complete all fields") - return self.render(request) - class MetaInfo(WorkflowStep): template = 'config_bundle/steps/config_software.html' @@ -280,11 +275,11 @@ class MetaInfo(WorkflowStep): confirm['description'] = meta['description'] self.repo_put(self.repo.CONFIRMATION, confirm) - def post_render(self, request): + def post(self, post_data, user): models = self.repo_get(self.repo.OPNFV_MODELS, {}) info = models.get("meta", {}) - form = BasicMetaForm(request.POST) + form = BasicMetaForm(post_data) if form.is_valid(): info['name'] = form.cleaned_data['name'] info['description'] = form.cleaned_data['description'] @@ -294,6 +289,4 @@ class MetaInfo(WorkflowStep): self.set_valid("Complete") else: self.set_invalid("Please correct the errors shown below") - self.repo_put(self.repo.OPNFV_MODELS, models) - return self.render(request) diff --git a/src/workflow/resource_bundle_workflow.py b/src/workflow/resource_bundle_workflow.py index 06737d2..2f4aa5d 100644 --- a/src/workflow/resource_bundle_workflow.py +++ b/src/workflow/resource_bundle_workflow.py @@ -8,8 +8,6 @@ ############################################################################## -from django.shortcuts import render -from django.forms import formset_factory from django.conf import settings import json @@ -22,7 +20,6 @@ from workflow.forms import ( HardwareDefinitionForm, NetworkDefinitionForm, ResourceMetaForm, - GenericHostMetaForm ) from resource_inventory.models import ( GenericResourceBundle, @@ -111,9 +108,9 @@ class Define_Hardware(WorkflowStep): confirm['resource']['lab'] = models['lab'].lab_user.username self.repo_put(self.repo.CONFIRMATION, confirm) - def post_render(self, request): + def post(self, post_data, user): try: - self.form = HardwareDefinitionForm(request.POST) + self.form = HardwareDefinitionForm(post_data) if self.form.is_valid(): self.update_models(self.form.cleaned_data) self.update_confirmation() @@ -122,8 +119,6 @@ class Define_Hardware(WorkflowStep): self.set_invalid("Please complete the fields highlighted in red to continue") except Exception as e: self.set_invalid(str(e)) - self.context = self.get_context() - return render(request, self.template, self.context) class Define_Nets(WorkflowStep): @@ -206,13 +201,13 @@ class Define_Nets(WorkflowStep): return context - def post_render(self, request): + def post(self, post_data, user): models = self.repo_get(self.repo.GRESOURCE_BUNDLE_MODELS, {}) if 'hosts' in models: host_set = set([h.resource.name + "*" + h.profile.name for h in models['hosts']]) self.repo_put(self.repo.GRB_LAST_HOSTLIST, host_set) try: - xmlData = request.POST.get("xml") + xmlData = post_data.get("xml") self.updateModels(xmlData) # update model with xml self.set_valid("Networks applied successfully") @@ -220,7 +215,6 @@ class Define_Nets(WorkflowStep): self.set_invalid("Public network not availble") except Exception as e: self.set_invalid("An error occurred when applying networks: " + str(e)) - return self.render(request) def updateModels(self, xmlData): models = self.repo_get(self.repo.GRESOURCE_BUNDLE_MODELS, {}) @@ -380,8 +374,8 @@ class Resource_Meta_Info(WorkflowStep): context['form'] = ResourceMetaForm(initial={"bundle_name": name, "bundle_description": desc}) return context - def post_render(self, request): - form = ResourceMetaForm(request.POST) + def post(self, post_data, user): + form = ResourceMetaForm(post_data) if form.is_valid(): models = self.repo_get(self.repo.GRESOURCE_BUNDLE_MODELS, {}) name = form.cleaned_data['bundle_name'] @@ -402,62 +396,5 @@ class Resource_Meta_Info(WorkflowStep): confirm_info["description"] = tmp self.repo_put(self.repo.CONFIRMATION, confirm) self.set_valid("Step Completed") - else: self.set_invalid("Please correct the fields highlighted in red to continue") - pass - return self.render(request) - - -class Host_Meta_Info(WorkflowStep): - template = "resource/steps/host_info.html" - title = "Host Info" - description = "We need a little bit of information about your chosen machines" - short_title = "host info" - - def __init__(self, *args, **kwargs): - super(Host_Meta_Info, self).__init__(*args, **kwargs) - self.formset = formset_factory(GenericHostMetaForm, extra=0) - - def get_context(self): - context = super(Host_Meta_Info, self).get_context() - GenericHostFormset = self.formset - models = self.repo_get(self.repo.GRESOURCE_BUNDLE_MODELS, {}) - initial_data = [] - if "hosts" not in models: - context['error'] = "Please go back and select your hosts" - else: - for host in models['hosts']: - profile = host.profile.name - name = host.resource.name - if not name: - name = "" - initial_data.append({"host_profile": profile, "host_name": name}) - context['formset'] = GenericHostFormset(initial=initial_data) - return context - - def post_render(self, request): - models = self.repo_get(self.repo.GRESOURCE_BUNDLE_MODELS, {}) - if 'hosts' not in models: - models['hosts'] = [] - hosts = models['hosts'] - i = 0 - confirm_hosts = [] - GenericHostFormset = self.formset - formset = GenericHostFormset(request.POST) - if formset.is_valid(): - for form in formset: - host = hosts[i] - host.resource.name = form.cleaned_data['host_name'] - i += 1 - confirm_hosts.append({"name": host.resource.name, "profile": host.profile.name}) - models['hosts'] = hosts - self.repo_put(self.repo.GRESOURCE_BUNDLE_MODELS, models) - confirm = self.repo_get(self.repo.CONFIRMATION, {}) - if "resource" not in confirm: - confirm['resource'] = {} - confirm['resource']['hosts'] = confirm_hosts - self.repo_put(self.repo.CONFIRMATION, confirm) - else: - pass - return self.render(request) diff --git a/src/workflow/snapshot_workflow.py b/src/workflow/snapshot_workflow.py index 5414784..4266587 100644 --- a/src/workflow/snapshot_workflow.py +++ b/src/workflow/snapshot_workflow.py @@ -49,15 +49,15 @@ class Select_Host_Step(WorkflowStep): context['chosen'] = chosen return context - def post_render(self, request): - host_data = request.POST.get("host") + def post(self, post_data, user): + host_data = post_data.get("host") if not host_data: self.set_invalid("Please select a host") - return self.render(request) + return host = json.loads(host_data) if 'name' not in host or 'booking' not in host: self.set_invalid("Invalid host selected") - return self.render(request) + return name = host['name'] booking_id = host['booking'] booking = Booking.objects.get(pk=booking_id) @@ -76,7 +76,6 @@ class Select_Host_Step(WorkflowStep): confirm['snapshot'] = snap_confirm self.repo_put(self.repo.CONFIRMATION, confirm) self.set_valid("Success") - return self.render(request) class Image_Meta_Step(WorkflowStep): @@ -97,8 +96,8 @@ class Image_Meta_Step(WorkflowStep): context['form'] = form return context - def post_render(self, request): - form = BasicMetaForm(request.POST) + def post(self, post_data, user): + form = BasicMetaForm(post_data) if form.is_valid(): name = form.cleaned_data['name'] self.repo_put(self.repo.SNAPSHOT_NAME, name) @@ -115,5 +114,3 @@ class Image_Meta_Step(WorkflowStep): self.set_valid("Success") else: self.set_invalid("Please Fill out the Form") - - return self.render(request) diff --git a/src/workflow/sw_bundle_workflow.py b/src/workflow/sw_bundle_workflow.py index 0c558fc..4dc0b8e 100644 --- a/src/workflow/sw_bundle_workflow.py +++ b/src/workflow/sw_bundle_workflow.py @@ -104,7 +104,7 @@ class Define_Software(WorkflowStep): return context - def post_render(self, request): + def post(self, post_data, user): models = self.repo_get(self.repo.CONFIG_MODELS, {}) if "bundle" not in models: models['bundle'] = ConfigBundle(owner=self.repo_get(self.repo.SESSION_USER)) @@ -112,8 +112,8 @@ class Define_Software(WorkflowStep): confirm = self.repo_get(self.repo.CONFIRMATION, {}) hosts = self.get_host_list() - models['headnode_index'] = request.POST.get("headnode", 1) - formset = self.create_hostformset(hosts, data=request.POST) + models['headnode_index'] = post_data.get("headnode", 1) + formset = self.create_hostformset(hosts, data=post_data) has_headnode = False if formset.is_valid(): models['host_configs'] = [] @@ -140,7 +140,7 @@ class Define_Software(WorkflowStep): if not has_headnode: self.set_invalid('Must have one "Headnode" per POD') - return self.render(request) + return self.repo_put(self.repo.CONFIG_MODELS, models) if "configuration" not in confirm: @@ -151,8 +151,6 @@ class Define_Software(WorkflowStep): else: self.set_invalid("Please complete all fields") - return self.render(request) - class Config_Software(WorkflowStep): template = 'config_bundle/steps/config_software.html' @@ -172,7 +170,7 @@ class Config_Software(WorkflowStep): context["form"] = BasicMetaForm(initial=initial) return context - def post_render(self, request): + def post(self, post_data, user): models = self.repo_get(self.repo.CONFIG_MODELS, {}) if "bundle" not in models: models['bundle'] = ConfigBundle(owner=self.repo_get(self.repo.SESSION_USER)) @@ -181,7 +179,7 @@ class Config_Software(WorkflowStep): if "configuration" not in confirm: confirm['configuration'] = {} - form = BasicMetaForm(request.POST) + form = BasicMetaForm(post_data) if form.is_valid(): models['bundle'].name = form.cleaned_data['name'] models['bundle'].description = form.cleaned_data['description'] @@ -194,5 +192,3 @@ class Config_Software(WorkflowStep): self.repo_put(self.repo.CONFIG_MODELS, models) self.repo_put(self.repo.CONFIRMATION, confirm) - - return self.render(request) diff --git a/src/workflow/tests/test_steps.py b/src/workflow/tests/test_steps.py index 380102a..cb676c7 100644 --- a/src/workflow/tests/test_steps.py +++ b/src/workflow/tests/test_steps.py @@ -12,7 +12,7 @@ from dashboard.populate_db import Populator from workflow.tests import constants from workflow.workflow_factory import WorkflowFactory from workflow.models import Repository -from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info, Host_Meta_Info +from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info from workflow.sw_bundle_workflow import SWConf_Resource_Select, Define_Software, Config_Software from workflow.booking_workflow import Booking_Resource_Select, SWConfig_Select, Booking_Meta from django.http import QueryDict, HttpRequest @@ -23,9 +23,6 @@ from resource_inventory.models import ( OPNFVRole, Image, GenericResourceBundle, - GenericHost, - HostProfile, - GenericResource, ConfigBundle ) @@ -129,49 +126,6 @@ class DefineHardwareTestCase(BaseStepTestCase): response, context = self.step_test(Define_Hardware, data) -class HostMetaInfoTestCase(BaseStepTestCase): - - def makeRepo(self): - """ - override to provide step with needed host info - """ - repo = super(HostMetaInfoTestCase, self).makeRepo() - # get models - models = {} - models['bundle'] = GenericResourceBundle() - # make generic hosts - gres1 = GenericResource(bundle=models['bundle']) - prof1 = HostProfile.objects.get(name="Test profile 0") - ghost1 = GenericHost(profile=prof1, resource=gres1) - - gres2 = GenericResource(bundle=models['bundle']) - prof2 = HostProfile.objects.get(name="Test profile 3") - ghost2 = GenericHost(profile=prof2, resource=gres2) - models['hosts'] = [ghost1, ghost2] - repo.el[repo.GRESOURCE_BUNDLE_MODELS] = models - return repo - - def test_step_with_good_data(self): - data = {"form-INITIAL_FORMS": 2, "form-MAX_NUM_FORMS": 1000} - data["form-MIN_NUM_FORMS"] = 0 - data["form-TOTAL_FORMS"] = 2 - data['form-0-host_name'] = "first host" - data['form-1-host_name'] = "second host" - response, context = self.step_test(Host_Meta_Info, data) - - def test_step_with_bad_data(self): # TODO - data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000} - data["form-MIN_NUM_FORMS"] = 0 - data["form-TOTAL_FORMS"] = 0 - response, context = self.step_test(Host_Meta_Info, data) - - def test_step_with_empty_data(self): - data = {"form-INITIAL_FORMS": 0, "form-MAX_NUM_FORMS": 1000} - data["form-MIN_NUM_FORMS"] = 0 - data["form-TOTAL_FORMS"] = 0 - response, context = self.step_test(Host_Meta_Info, data) - - class DefineNetsTestCase(BaseStepTestCase): def test_step_with_good_data(self): diff --git a/src/workflow/urls.py b/src/workflow/urls.py index 5a97904..ae620d0 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 step_view, delete_session, manager_view, viewport_view +from workflow.views import delete_session, manager_view, viewport_view, add_workflow, cancel_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,9 +19,10 @@ from workflow.booking_workflow import SWConfig_Select, Booking_Resource_Select, app_name = 'workflow' urlpatterns = [ - url(r'^workflow/$', step_view, name='workflow'), - url(r'^workflow/finish/$', delete_session, name='delete_session'), + 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'^$', viewport_view, name='viewport') ] diff --git a/src/workflow/views.py b/src/workflow/views.py index 7ed9031..47241e2 100644 --- a/src/workflow/views.py +++ b/src/workflow/views.py @@ -8,7 +8,7 @@ ############################################################################## -from django.http import HttpResponseGone, JsonResponse +from django.http import HttpResponse, JsonResponse from django.shortcuts import render from django.urls import reverse @@ -49,7 +49,7 @@ def delete_session(request): manager = attempt_auth(request) if not manager: - return HttpResponseGone("No session found that relates to current request") + return no_workflow(request) not_last_workflow, result = manager.pop_workflow() @@ -60,48 +60,34 @@ def delete_session(request): return JsonResponse(get_redirect_response(result)) -def step_view(request): +def add_workflow(request): manager = attempt_auth(request) if not manager: - # no manager found, redirect to "lost" page return no_workflow(request) - if request.GET.get('step') is not None: - if request.GET.get('step') == 'next': - manager.go_next() - elif request.GET.get('step') == 'prev': - manager.go_prev() - else: - raise Exception("requested action for new step had malformed contents: " + request.GET.get('step')) - return manager.render(request) + try: + workflow_type = int(request.POST.get('workflow_type')) + except ValueError: + return HttpResponse(status=400) + manager.add_workflow(workflow_type=workflow_type) + return manager.render(request) # do we want this? -def manager_view(request): - manager = attempt_auth(request) +def cancel_workflow(request): + manager = attempt_auth(request) if not manager: - return HttpResponseGone("No session found that relates to current request") + return no_workflow(request) - if request.method == 'GET': - # no need for this statement if only intercepting post requests + if not manager.pop_workflow(): + del ManagerTracker.managers[request.session['manager_session']] - # return general context for viewport page - return manager.status(request) - if request.method == 'POST': - if request.POST.get('add') is not None: - logger.debug("add found") - target_id = None - if 'target' in request.POST: - target_id = int(request.POST.get('target')) - manager.add_workflow(workflow_type=int(request.POST.get('add')), target_id=target_id) - elif request.POST.get('edit') is not None and request.POST.get('edit_id') is not None: - logger.debug("edit found") - manager.add_workflow(workflow_type=request.POST.get('edit'), edit_object=int(request.POST.get('edit_id'))) - elif request.POST.get('cancel') is not None: - if not manager.pop_workflow(): - del ManagerTracker.managers[request.session['manager_session']] +def manager_view(request): + manager = attempt_auth(request) + if not manager: + return no_workflow(request) - return manager.status(request) + return manager.handle_request(request) def viewport_view(request): @@ -129,10 +115,7 @@ def create_session(wf_type, request): def no_workflow(request): - - logger.debug("There is no active workflow") - - return render(request, 'workflow/no_workflow.html', {'title': "Not Found"}) + return render(request, 'workflow/no_workflow.html', {'title': "Not Found"}, status=404) def login(request): diff --git a/src/workflow/workflow_manager.py b/src/workflow/workflow_manager.py index 80b8a67..605eee7 100644 --- a/src/workflow/workflow_manager.py +++ b/src/workflow/workflow_manager.py @@ -9,6 +9,7 @@ from django.http import JsonResponse +from django.http.request import QueryDict from booking.models import Booking from workflow.workflow_factory import WorkflowFactory @@ -19,6 +20,7 @@ from resource_inventory.models import ( HostConfiguration, OPNFVConfig ) +from workflow.forms import ManagerForm import logging logger = logging.getLogger(__name__) @@ -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() @@ -75,24 +74,37 @@ class SessionManager(): return (multiple_wfs, current_repo.el[current_repo.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() 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) + return JsonResponse({ + "meta": self.status(), + "content": self.get_active_step().render_to_string(request) + }) def post_render(self, request): return self.active_workflow().steps[self.active_workflow().active_index].post_render(request) |