diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index 7c492d56b..aec74320e 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -3,11 +3,14 @@ import logging from django import forms +from django.shortcuts import redirect from django.contrib.auth.mixins import LoginRequiredMixin from formtools.wizard.views import NamedUrlSessionWizardView # type: ignore +from registrar.models import DomainApplication, Website + logger = logging.getLogger(__name__) @@ -113,5 +116,34 @@ class ApplicationWizard(LoginRequiredMixin, NamedUrlSessionWizardView): context["form_titles"] = TITLES return context - def done(self, form_list, **kwargs): + def forms_to_object(self, form_dict: dict) -> DomainApplication: + """Unpack the form responses onto the model object properties.""" + application = DomainApplication.objects.create(creator=self.request.user) + + # organization information + organization_data = form_dict["organization"].cleaned_data + application.organization_type = organization_data["organization_type"] + application.federal_branch = organization_data["federal_type"] + application.is_election_office = organization_data["is_election_board"] + + # contact information + contact_data = form_dict["contact"].cleaned_data + application.organization_name = contact_data["organization_name"] + application.street_address = contact_data["street_address"] + # TODO: add the rest of these fields when they are created in the forms + + # This isn't really the requested_domain field + # but we need something in this field to make the form submittable + requested_site, _ = Website.objects.get_or_create( + website=contact_data["organization_name"] + ".gov" + ) + application.requested_domain = requested_site + return application + + def done(self, form_list, form_dict, **kwargs): logger.info("Application form submitted.") + application = self.forms_to_object(form_dict) + application.submit() # change the status to submitted + application.save() + logger.info("Application object saved.") + return redirect("home") diff --git a/src/registrar/tests/test_views.py b/src/registrar/tests/test_views.py index 0c3c00b2c..92b6df4df 100644 --- a/src/registrar/tests/test_views.py +++ b/src/registrar/tests/test_views.py @@ -94,3 +94,26 @@ class FormTests(TestWithUser, WebTest): result = page.form.submit().follow() # Got the next form page self.assertIn("contact information", result) + + def test_application_form_submission(self): + """Can fill out the entire form and submit. + + As we add additional form pages, we need to include them here to make + this test work. + """ + page = self.app.get(reverse("application")).follow() + form = page.form + form["organization-organization_type"] = "Federal" + form["organization-federal_type"] = "Executive" + result = page.form.submit().follow() + # Got the next form page + contact_form = result.form + contact_form["contact-organization_name"] = "test" + contact_form["contact-street_address"] = "100 Main Street" + result = page.form.submit() + # final submission results in a redirect + self.assertEquals(result.status_code, 302) + page = result.follow() + self.assertContains(page, "registrar") + # TODO: when we have a page that lists applications, visit it and + # make sure that the new one exists