-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8378 from cfpb/tccp/detail-jinja
TCCP: Render card contact info using Python
- Loading branch information
Showing
6 changed files
with
167 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{% if urls or phone_numbers -%} | ||
<div class="o-contact-info"> | ||
{% for value in urls %} | ||
{%- include "v1/includes/molecules/contact-hyperlink.html" -%} | ||
{% endfor %} | ||
|
||
{% for phone_number in phone_numbers %} | ||
{% with value = {"phones": [{"number": phone_number}]} %} | ||
{%- include "v1/includes/molecules/contact-phone.html" -%} | ||
{% endwith %} | ||
{%- endfor %} | ||
</div> | ||
{%- endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import re | ||
from urllib.parse import urlparse | ||
|
||
from django.template.loader import render_to_string | ||
from django.utils.safestring import mark_safe | ||
|
||
|
||
# Contact website schemes must be either http or https. | ||
_valid_website_scheme = re.compile(r"https?") | ||
|
||
|
||
# Contact website hosts must have at least one . in them. | ||
_valid_website_netloc = re.compile(r".+\..+") | ||
|
||
|
||
# We strip www. from the beginning of displayed URLs. | ||
_leading_www = re.compile(r"^www\.") | ||
|
||
|
||
def _format_contact_website(url): | ||
parsed_url = urlparse(url) | ||
|
||
if _valid_website_scheme.match( | ||
parsed_url.scheme or "" | ||
) and _valid_website_netloc.match(parsed_url.netloc or ""): | ||
return {"url": url, "text": _leading_www.sub("", parsed_url.netloc)} | ||
else: | ||
return { | ||
"text": url, | ||
} | ||
|
||
|
||
def _format_contact_phone_number(phone_number): | ||
phone_number = "".join( | ||
c for c in phone_number if c.isalpha() or c.isdigit() | ||
) | ||
return phone_number[phone_number.startswith("1") :] | ||
|
||
|
||
def _format_contact_info(card): | ||
def fmt_list(format_fn, value): | ||
return list(map(format_fn, (value or "").split())) | ||
|
||
return { | ||
"urls": fmt_list( | ||
_format_contact_website, card["website_for_consumer"] | ||
), | ||
"phone_numbers": fmt_list( | ||
_format_contact_phone_number, | ||
card["telephone_number_for_consumers"], | ||
), | ||
} | ||
|
||
|
||
def render_contact_info(card): | ||
return mark_safe( | ||
render_to_string( | ||
"tccp/includes/contact_info.html", | ||
_format_contact_info(card), | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import re | ||
|
||
from django.test import SimpleTestCase | ||
|
||
from tccp.jinja2tags import ( | ||
_format_contact_phone_number, | ||
_format_contact_website, | ||
render_contact_info, | ||
) | ||
|
||
|
||
class FormatContactPhoneNumberTests(SimpleTestCase): | ||
def test_phone_number_formatting(self): | ||
for value, expected in [ | ||
("FOOBAR", "FOOBAR"), | ||
("888-FOO-BAR", "888FOOBAR"), | ||
("18885551234", "8885551234"), | ||
("(800) 555-1234", "8005551234"), | ||
]: | ||
with self.subTest(value=value, expected=expected): | ||
self.assertEqual(_format_contact_phone_number(value), expected) | ||
|
||
|
||
class FormatContactWebsiteTests(SimpleTestCase): | ||
def test_website_formatting(self): | ||
for value, expected in [ | ||
( | ||
"http://example.com", | ||
{"text": "example.com", "url": "http://example.com"}, | ||
), | ||
( | ||
"https://example.com", | ||
{"text": "example.com", "url": "https://example.com"}, | ||
), | ||
( | ||
"https://www.example.com", | ||
{ | ||
"text": "example.com", | ||
"url": "https://www.example.com", | ||
}, | ||
), | ||
( | ||
"https://subdomain.example.com", | ||
{ | ||
"text": "subdomain.example.com", | ||
"url": "https://subdomain.example.com", | ||
}, | ||
), | ||
( | ||
"https://example.com/path/", | ||
{ | ||
"text": "example.com", | ||
"url": "https://example.com/path/", | ||
}, | ||
), | ||
( | ||
"invalid://example.com", | ||
{ | ||
"text": "invalid://example.com", | ||
}, | ||
), | ||
( | ||
"example.com", | ||
{ | ||
"text": "example.com", | ||
}, | ||
), | ||
]: | ||
with self.subTest(value=value, expected=expected): | ||
self.assertEqual(_format_contact_website(value), expected) | ||
|
||
|
||
class TestRenderContactInfo(SimpleTestCase): | ||
def test_render(self): | ||
html = render_contact_info( | ||
{ | ||
"website_for_consumer": "https://example.com foo.com", | ||
"telephone_number_for_consumers": "212-555-1234", | ||
} | ||
) | ||
|
||
self.assertEqual(len(re.findall("m-contact-hyperlink", html)), 2) | ||
self.assertIn('<a href="https://example.com">', html) | ||
self.assertNotIn('<a href="foo.com">', html) | ||
self.assertEqual(len(re.findall("m-contact-phone", html)), 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters