diff --git a/src/api/tests/test_available.py b/src/api/tests/test_available.py index 0529882f2..061b6274c 100644 --- a/src/api/tests/test_available.py +++ b/src/api/tests/test_available.py @@ -2,7 +2,6 @@ import json -from django.core.exceptions import BadRequest from django.contrib.auth import get_user_model from django.test import TestCase, RequestFactory @@ -85,8 +84,8 @@ class AvailableViewTest(TestCase): bad_string = "blah!;" request = self.factory.get(API_BASE_PATH + bad_string) request.user = self.user - with self.assertRaisesMessage(BadRequest, "Invalid"): - available(request, domain=bad_string) + response = available(request, domain=bad_string) + self.assertFalse(json.loads(response.content)["available"]) class AvailableAPITest(TestCase): @@ -108,9 +107,3 @@ class AvailableAPITest(TestCase): with less_console_noise(): response = self.client.post(API_BASE_PATH + "nonsense") self.assertEqual(response.status_code, 405) - - def test_available_bad_input(self): - self.client.force_login(self.user) - with less_console_noise(): - response = self.client.get(API_BASE_PATH + "blah!;") - self.assertEqual(response.status_code, 400) diff --git a/src/api/views.py b/src/api/views.py index deaacf0c6..9d23dcf85 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -17,13 +17,13 @@ DOMAIN_FILE_URL = ( DOMAIN_API_MESSAGES = { "required": "Enter the .gov domain you want. Don’t include “www” or “.gov.”" - " For example, if you want www.city.gov, you would enter “city”" - " (without the quotes).", + " For example, if you want www.city.gov, you would enter “city”" + " (without the quotes).", "extra_dots": "Enter the .gov domain you want without any periods.", "unavailable": "That domain isn’t available. Try entering another one." - " Contact us if you need help coming up with a domain.", + " Contact us if you need help coming up with a domain.", "invalid": "Enter a domain using only letters," - " numbers, or hyphens (though we don't recommend using hyphens).", + " numbers, or hyphens (though we don't recommend using hyphens).", "success": "That domain is available!", } @@ -83,18 +83,15 @@ def available(request, domain=""): Domain.string_could_be_domain(domain) or Domain.string_could_be_domain(domain + ".gov") ): - return JsonResponse({ - "available": False, - "message": DOMAIN_API_MESSAGES["invalid"] - }) + return JsonResponse( + {"available": False, "message": DOMAIN_API_MESSAGES["invalid"]} + ) # a domain is available if it is NOT in the list of current domains if in_domains(domain): - return JsonResponse({ - "available": False, - "message": DOMAIN_API_MESSAGES["unavailable"] - }) + return JsonResponse( + {"available": False, "message": DOMAIN_API_MESSAGES["unavailable"]} + ) else: - return JsonResponse({ - "available": True, - "message": DOMAIN_API_MESSAGES["success"] - }) + return JsonResponse( + {"available": True, "message": DOMAIN_API_MESSAGES["success"]} + ) diff --git a/src/registrar/forms/application_wizard.py b/src/registrar/forms/application_wizard.py index b1033a52d..0b7f00e69 100644 --- a/src/registrar/forms/application_wizard.py +++ b/src/registrar/forms/application_wizard.py @@ -416,6 +416,7 @@ CurrentSitesFormSet = forms.formset_factory( formset=BaseCurrentSitesFormSet, ) + class AlternativeDomainForm(RegistrarForm): def clean_alternative_domain(self): """Validation code for domain names.""" @@ -423,19 +424,23 @@ class AlternativeDomainForm(RegistrarForm): requested = self.cleaned_data.get("alternative_domain", None) validated = Domain.validate(requested, blank_ok=True) except errors.ExtraDotsError: - raise forms.ValidationError(code="extra_dots") + raise forms.ValidationError( + DOMAIN_API_MESSAGES["extra_dots"], code="extra_dots" + ) except errors.DomainUnavailableError: - raise forms.ValidationError(code="unavailable") + raise forms.ValidationError( + DOMAIN_API_MESSAGES["unavailable"], code="unavailable" + ) except ValueError: - raise forms.ValidationError(code="invalid") + raise forms.ValidationError(DOMAIN_API_MESSAGES["invalid"], code="invalid") return validated alternative_domain = forms.CharField( required=False, label="Alternative domain", - error_messages=DOMAIN_API_MESSAGES ) + class BaseAlternativeDomainFormSet(RegistrarFormSet): JOIN = "alternative_domains" @@ -508,19 +513,22 @@ class DotGovDomainForm(RegistrarForm): requested = self.cleaned_data.get("requested_domain", None) validated = Domain.validate(requested) except errors.BlankValueError: - raise forms.ValidationError(code="required") + raise forms.ValidationError( + DOMAIN_API_MESSAGES["required"], code="required" + ) except errors.ExtraDotsError: - raise forms.ValidationError(code="extra_dots") + raise forms.ValidationError( + DOMAIN_API_MESSAGES["extra_dots"], code="extra_dots" + ) except errors.DomainUnavailableError: - raise forms.ValidationError(code="unavailable") + raise forms.ValidationError( + DOMAIN_API_MESSAGES["unavailable"], code="unavailable" + ) except ValueError: - raise forms.ValidationError(code="invalid") + raise forms.ValidationError(DOMAIN_API_MESSAGES["invalid"], code="invalid") return validated - requested_domain = forms.CharField( - label="What .gov domain do you want?", - error_messages=DOMAIN_API_MESSAGES - ) + requested_domain = forms.CharField(label="What .gov domain do you want?") class PurposeForm(RegistrarForm): diff --git a/src/registrar/templates/application_anything_else.html b/src/registrar/templates/application_anything_else.html index aba7ded8a..0ed6bd2ca 100644 --- a/src/registrar/templates/application_anything_else.html +++ b/src/registrar/templates/application_anything_else.html @@ -7,7 +7,6 @@ {% block form_fields %} - {% csrf_token %} {% with add_label_class="usa-sr-only" attr_maxlength=500 %} {% input_with_errors forms.0.anything_else %} {% endwith %} diff --git a/src/registrar/templates/application_authorizing_official.html b/src/registrar/templates/application_authorizing_official.html index 3ef5c3892..10c5e71eb 100644 --- a/src/registrar/templates/application_authorizing_official.html +++ b/src/registrar/templates/application_authorizing_official.html @@ -21,7 +21,6 @@ {% block form_fields %} - {% csrf_token %}
Who is the authorizing official for your organization? diff --git a/src/registrar/templates/application_current_sites.html b/src/registrar/templates/application_current_sites.html index a99da2095..50ca7f13b 100644 --- a/src/registrar/templates/application_current_sites.html +++ b/src/registrar/templates/application_current_sites.html @@ -1,6 +1,5 @@ {% extends 'application_form.html' %} -{% load static %} -{% load field_helpers %} +{% load static field_helpers %} {% block form_instructions %}

Enter your organization’s public website, if you have one. For example, @@ -9,7 +8,6 @@ {% block form_fields %} - {% csrf_token %} {{ forms.0.management_form }} {% for form in forms.0 %} diff --git a/src/registrar/templates/application_org_contact.html b/src/registrar/templates/application_org_contact.html index 12a475c83..f5f773647 100644 --- a/src/registrar/templates/application_org_contact.html +++ b/src/registrar/templates/application_org_contact.html @@ -15,7 +15,6 @@ {% block form_fields %} - {% csrf_token %}

What is the name and mailing address of your organization? diff --git a/src/registrar/templates/application_org_election.html b/src/registrar/templates/application_org_election.html index 321711afe..fa6e1611b 100644 --- a/src/registrar/templates/application_org_election.html +++ b/src/registrar/templates/application_org_election.html @@ -9,7 +9,6 @@ {% block form_fields %} - {% csrf_token %} {% with add_class="usa-radio__input--tile" %} {% input_with_errors forms.0.is_election_board %} {% endwith %} diff --git a/src/registrar/templates/application_org_federal.html b/src/registrar/templates/application_org_federal.html index d0cb8dfcb..f51ed7c7a 100644 --- a/src/registrar/templates/application_org_federal.html +++ b/src/registrar/templates/application_org_federal.html @@ -9,7 +9,6 @@ {% block form_fields %} - {% csrf_token %} {% with add_class="usa-radio__input--tile" %} {% input_with_errors forms.0.federal_type %} {% endwith %} diff --git a/src/registrar/templates/application_org_type.html b/src/registrar/templates/application_org_type.html index 2f59340f4..0f06df737 100644 --- a/src/registrar/templates/application_org_type.html +++ b/src/registrar/templates/application_org_type.html @@ -9,7 +9,6 @@ {% block form_fields %} - {% csrf_token %} {% with add_class="usa-radio__input--tile" %} {% input_with_errors forms.0.organization_type %} {% endwith %} diff --git a/src/registrar/templates/application_other_contacts.html b/src/registrar/templates/application_other_contacts.html index f2d2d5e6f..3a8422d7b 100644 --- a/src/registrar/templates/application_other_contacts.html +++ b/src/registrar/templates/application_other_contacts.html @@ -1,6 +1,5 @@ {% extends 'application_form.html' %} -{% load static %} -{% load field_helpers %} +{% load static field_helpers %} {% block form_instructions %}

We’d like to contact other employees with administrative or technical @@ -12,7 +11,6 @@ {% block form_fields %} - {% csrf_token %} {{ forms.0.management_form }} {# forms.0 is a formset and this iterates over its forms #} {% for form in forms.0.forms %} @@ -21,8 +19,6 @@

Contact {{ forloop.counter }}

- {% if form.is_bound %}HAS INITIAL DATA{% else %}NO INITIAL DATA{% endif %} - {% input_with_errors form.first_name %} {% input_with_errors form.middle_name %} diff --git a/src/registrar/templates/application_purpose.html b/src/registrar/templates/application_purpose.html index 326358c5a..e02e3c5c7 100644 --- a/src/registrar/templates/application_purpose.html +++ b/src/registrar/templates/application_purpose.html @@ -14,7 +14,6 @@ {% block form_fields %} - {% csrf_token %} {% with attr_maxlength=500 %} {% input_with_errors forms.0.purpose %} {% endwith %} diff --git a/src/registrar/templates/application_requirements.html b/src/registrar/templates/application_requirements.html index 18b3fc76c..fbab0246e 100644 --- a/src/registrar/templates/application_requirements.html +++ b/src/registrar/templates/application_requirements.html @@ -183,7 +183,6 @@ {% block form_fields %} - {% csrf_token %}

Acknowledgement of .gov domain requirements

diff --git a/src/registrar/templates/application_review.html b/src/registrar/templates/application_review.html index d46a35d3b..bd17f9cb1 100644 --- a/src/registrar/templates/application_review.html +++ b/src/registrar/templates/application_review.html @@ -2,8 +2,6 @@ {% load static url_helpers %} {% block form_fields %} - {% csrf_token %} - {% for step in steps.all|slice:":-1" %}

diff --git a/src/registrar/templates/application_security_email.html b/src/registrar/templates/application_security_email.html index 9a240c264..ff9d73c3c 100644 --- a/src/registrar/templates/application_security_email.html +++ b/src/registrar/templates/application_security_email.html @@ -10,6 +10,5 @@ {% block form_fields %} - {% csrf_token %} {% input_with_errors forms.0.security_email %} {% endblock %} \ No newline at end of file diff --git a/src/registrar/templates/application_tribal_government.html b/src/registrar/templates/application_tribal_government.html index 48e26fcb7..543f96ddf 100644 --- a/src/registrar/templates/application_tribal_government.html +++ b/src/registrar/templates/application_tribal_government.html @@ -3,7 +3,6 @@ {% block form_fields %} - {% csrf_token %} {% input_with_errors forms.0.tribe_name %}
diff --git a/src/registrar/templates/application_type_of_work.html b/src/registrar/templates/application_type_of_work.html index 78650eb01..1cacc1535 100644 --- a/src/registrar/templates/application_type_of_work.html +++ b/src/registrar/templates/application_type_of_work.html @@ -3,7 +3,6 @@ {% block form_fields %} - {% csrf_token %} {% with attr_maxlength=500 %} {% input_with_errors forms.0.type_of_work %} {% input_with_errors forms.0.more_organization_information %} diff --git a/src/registrar/templates/application_your_contact.html b/src/registrar/templates/application_your_contact.html index a5d962c35..ffbf10f8a 100644 --- a/src/registrar/templates/application_your_contact.html +++ b/src/registrar/templates/application_your_contact.html @@ -15,7 +15,6 @@ {% block form_fields %} - {% csrf_token %}
Your contact information diff --git a/src/registrar/templates/profile.html b/src/registrar/templates/profile.html index c2c0fcfa1..9b8305922 100644 --- a/src/registrar/templates/profile.html +++ b/src/registrar/templates/profile.html @@ -7,7 +7,6 @@ Edit your User Profile {% block content %}
- {% csrf_token %}
Your profile

diff --git a/src/registrar/templatetags/field_helpers.py b/src/registrar/templatetags/field_helpers.py index f1be0a605..ca3b23316 100644 --- a/src/registrar/templatetags/field_helpers.py +++ b/src/registrar/templatetags/field_helpers.py @@ -35,6 +35,11 @@ def input_with_errors(context, field=None): # noqa: C901 {% endwith %} {% endfor } + There are a few edge cases to keep in mind: + - a "maxlength" attribute will cause the input to use USWDS Character counter + - the field's `use_fieldset` controls whether the output is label/field or + fieldset/legend/field + - checkbox label styling is different (this is handled, don't worry about it) """ context = context.flatten() context["field"] = field @@ -54,6 +59,12 @@ def input_with_errors(context, field=None): # noqa: C901 classes.append(attrs.pop("class")) # parse context for field attributes and classes + # --- + # here we loop through all items in the context dictionary + # (this is the context which was being used to render the + # outer template in which this {% input_with_errors %} appeared!) + # and look for "magic" keys -- these are used to modify the + # appearance and behavior of the final HTML for key, value in context.items(): if key.startswith("attr_"): attr_name = re.sub("_", "-", key[5:]) diff --git a/src/registrar/templatetags/form_helpers.py b/src/registrar/templatetags/form_helpers.py index 1f7142160..55e25f0f9 100644 --- a/src/registrar/templatetags/form_helpers.py +++ b/src/registrar/templatetags/form_helpers.py @@ -6,4 +6,4 @@ register = template.Library() @register.filter def isformset(value): - return isinstance(value, BaseFormSet) \ No newline at end of file + return isinstance(value, BaseFormSet) diff --git a/src/registrar/views/application.py b/src/registrar/views/application.py index 9a3f95cc5..3dfce376c 100644 --- a/src/registrar/views/application.py +++ b/src/registrar/views/application.py @@ -219,7 +219,11 @@ class ApplicationWizard(LoginRequiredMixin, TemplateView): return render(request, self.template_name, context) def get_all_forms(self, **kwargs) -> list: - """Calls `get_forms` for all steps and returns a flat list.""" + """ + Calls `get_forms` for all steps and returns a flat list. + + All arguments (**kwargs) are passed directly to `get_forms`. + """ nested = (self.get_forms(step=step, **kwargs) for step in self.steps) flattened = [form for lst in nested for form in lst] return flattened