Build capability to add sublabels with links to simple form components, with supporting custom filters and unit tests for the custom filters

This commit is contained in:
rachidatecs 2023-08-02 17:34:32 -04:00
parent f500a4ff50
commit 22a89e030d
No known key found for this signature in database
GPG key ID: 3CEBBFA7325E5525
5 changed files with 68 additions and 16 deletions

View file

@ -4,10 +4,12 @@
{% block form_fields %} {% block form_fields %}
{% with sublabel_text="Please include the entire name of your tribe as recognized by the" %} {% with sublabel_text="Please include the entire name of your tribe as recognized by the Bureau of Indian Affairs." %}
{% with link_text="Bureau of Indian Affairs" %} {% with link_text="Bureau of Indian Affairs" %}
{% with link_href="https://rachid.me" %} {% with link_href="https://www.federalregister.gov/documents/2023/01/12/2023-00504/indian-entities-recognized-by-and-eligible-to-receive-services-from-the-united-states-bureau-of" %}
{% input_with_errors forms.0.tribe_name %} {% with target_blank="true" %}
{% input_with_errors forms.0.tribe_name %}
{% endwith %}
{% endwith %} {% endwith %}
{% endwith %} {% endwith %}
{% endwith %} {% endwith %}

View file

@ -3,6 +3,8 @@ Template include for form fields with classes and their corresponding
error messages, if necessary. error messages, if necessary.
{% endcomment %} {% endcomment %}
{% load custom_filters %}
{% load widget_tweaks %} {% load widget_tweaks %}
{% if widget.attrs.maxlength %} {% if widget.attrs.maxlength %}
@ -30,12 +32,18 @@ error messages, if necessary.
{% if sublabel_text %} {% if sublabel_text %}
<p id="{{ widget.attrs.id }}__sublabel" class="text-base margin-top-2px margin-bottom-1"> <p id="{{ widget.attrs.id }}__sublabel" class="text-base margin-top-2px margin-bottom-1">
{{ sublabel_text }} {% comment %} If the link_text appears twice, the first instance will be a link and the second instance will be ignored {% endcomment %}
{% if link_text %} {% if link_text and link_text in sublabel_text %}
<a href="{{ link_href }}">{{ link_text }}</a>. {% with link_index=sublabel_text|find_index:link_text %}
{{ sublabel_text|slice:link_index }}
{% comment %} HTML will convert a new line into a space, resulting with a space before the fullstop in case link_text is at the end of sublabel_text, hence the unfortunate line below {% endcomment %}
<a {% if target_blank == "true" %}target="_blank" {% endif %}href="{{ link_href }}">{{ link_text }}</a>{% with sublabel_part_after=sublabel_text|slice_after:link_text %}{{ sublabel_part_after }}{% endwith %}
{% endwith %}
{% else %}
{{ sublabel_text }}
{% endif %} {% endif %}
</p> </p>
{% endif %} {% endif %}
{% if field.errors %} {% if field.errors %}
<div id="{{ widget.attrs.id }}__error-message"> <div id="{{ widget.attrs.id }}__error-message">

View file

@ -23,3 +23,20 @@ def extract_a_text(value):
extracted_text = "" extracted_text = ""
return extracted_text return extracted_text
@register.filter
def find_index(haystack, needle):
try:
return haystack.index(needle)
except ValueError:
return -1
@register.filter
def slice_after(value, substring):
index = value.find(substring)
if index != -1:
result = value[index + len(substring) :]
return result
return value

View file

@ -3,6 +3,12 @@
from django.conf import settings from django.conf import settings
from django.test import TestCase from django.test import TestCase
from django.template import Context, Template from django.template import Context, Template
from registrar.templatetags.custom_filters import (
extract_value,
extract_a_text,
find_index,
slice_after,
)
class TestTemplateTags(TestCase): class TestTemplateTags(TestCase):
@ -33,8 +39,6 @@ class TestTemplateTags(TestCase):
class CustomFiltersTestCase(TestCase): class CustomFiltersTestCase(TestCase):
def test_extract_value_filter(self): def test_extract_value_filter(self):
from registrar.templatetags.custom_filters import extract_value
html_input = ( html_input = (
'<input type="checkbox" name="_selected_action" value="123" ' '<input type="checkbox" name="_selected_action" value="123" '
'id="label_123" class="action-select">' 'id="label_123" class="action-select">'
@ -50,8 +54,6 @@ class CustomFiltersTestCase(TestCase):
self.assertEqual(result, "abc") self.assertEqual(result, "abc")
def test_extract_a_text_filter(self): def test_extract_a_text_filter(self):
from registrar.templatetags.custom_filters import extract_a_text
input_text = '<a href="#">Link Text</a>' input_text = '<a href="#">Link Text</a>'
result = extract_a_text(input_text) result = extract_a_text(input_text)
self.assertEqual(result, "Link Text") self.assertEqual(result, "Link Text")
@ -59,3 +61,25 @@ class CustomFiltersTestCase(TestCase):
input_text = '<a href="/example">Another Link</a>' input_text = '<a href="/example">Another Link</a>'
result = extract_a_text(input_text) result = extract_a_text(input_text)
self.assertEqual(result, "Another Link") self.assertEqual(result, "Another Link")
def test_find_index(self):
haystack = "Hello, World!"
needle = "lo"
result = find_index(haystack, needle)
self.assertEqual(result, 3)
needle = "XYZ"
result = find_index(haystack, needle)
self.assertEqual(result, -1)
def test_slice_after(self):
value = "Hello, World!"
substring = "lo"
result = slice_after(value, substring)
self.assertEqual(result, ", World!")
substring = "XYZ"
result = slice_after(value, substring)
self.assertEqual(
result, value
) # Should return the original value if substring not found

View file

@ -24,11 +24,12 @@ class DomainPermission(PermissionsLoginMixin):
The user is in self.request.user and the domain needs to be looked The user is in self.request.user and the domain needs to be looked
up from the domain's primary key in self.kwargs["pk"] up from the domain's primary key in self.kwargs["pk"]
""" """
# ticket 806 # ticket 806
# if self.request.user is staff or admin and domain.application__status = 'approved' or 'rejected' or 'action needed' # if self.request.user is staff or admin and
# domain.application__status = 'approved' or 'rejected' or 'action needed'
# return True # return True
if not self.request.user.is_authenticated: if not self.request.user.is_authenticated:
return False return False
@ -37,10 +38,10 @@ class DomainPermission(PermissionsLoginMixin):
user=self.request.user, domain__id=self.kwargs["pk"] user=self.request.user, domain__id=self.kwargs["pk"]
).exists(): ).exists():
return False return False
# ticket 796 # ticket 796
# if domain.application__status != 'approved' # if domain.application__status != 'approved'
# return false # return false
# if we need to check more about the nature of role, do it here. # if we need to check more about the nature of role, do it here.
return True return True