diff options
author | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2019-07-01 15:17:52 -0400 |
---|---|---|
committer | Sawyer Bergeron <sbergeron@iol.unh.edu> | 2019-07-02 08:22:36 -0400 |
commit | 08ed74b95826a907ba8f9797002ef98eb2e82424 (patch) | |
tree | f4dc0936b418fed16a6e18f3338a5b6760d241bf | |
parent | 8149a888fe6f7c1c3b1b3be2b3996c6d225d5a53 (diff) |
add post to steps (LAAS-32)
Change-Id: I3fa53c399f914d7daa8ffb5d8640a153de555421
Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu>
-rw-r--r-- | src/workflow/booking_workflow.py | 8 | ||||
-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 |
7 files changed, 49 insertions, 176 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/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): |