Repository: cisagov/manage.get.gov

Issue:
Make a shorthand template variable to point to the public site
A/C: There is a simple way in Django to refer to a page on our public site such as {% public_site_page /domains/requirements %}

The Django application has many links out to the public site. Rather than having to specify the base of the public site URL in every single link out, we can provide a configuration setting and a way to use it in our HTML templates so that we can write links simply, and also when the public site changes URL (e.g. from federalist preview to beta.get.gov to get.gov), we will be able to make all those links point to the correct location easily.


Discussion:

Resolving patch:
diff --git a/src/registrar/config/settings.py b/src/registrar/config/settings.py
--- a/src/registrar/config/settings.py
+++ b/src/registrar/config/settings.py
@@ -62,6 +62,8 @@
 secret_registry_key_passphrase = secret("REGISTRY_KEY_PASSPHRASE", "")
 secret_registry_hostname = secret("REGISTRY_HOSTNAME")
 
+secret_getgov_public_site_url = secret("GETGOV_PUBLIC_SITE_URL", "")
+
 # region: Basic Django Config-----------------------------------------------###
 
 # Build paths inside the project like this: BASE_DIR / "subdir".
@@ -505,6 +507,10 @@
 # Must be relative and end with "/"
 STATIC_URL = "public/"
 
+# Base URL of our separate static public website. Used by the
+# {% public_site_url subdir/path %} template tag
+GETGOV_PUBLIC_SITE_URL = secret_getgov_public_site_url
+
 # endregion
 # region: Registry----------------------------------------------------------###
 
diff --git a/src/registrar/templatetags/url_helpers.py b/src/registrar/templatetags/url_helpers.py
--- a/src/registrar/templatetags/url_helpers.py
+++ b/src/registrar/templatetags/url_helpers.py
@@ -1,6 +1,8 @@
 from django import template
 from django.urls import reverse
 
+from django.conf import settings
+
 register = template.Library()
 
 
@@ -15,3 +17,16 @@ def startswith(text, starts):
     if isinstance(text, str):
         return text.startswith(starts)
     return False
+
+
+@register.simple_tag
+def public_site_url(url_path):
+    """Make a full URL for this path at our public site.
+
+    The public site base url is set by a GETGOV_PUBLIC_SITE_URL environment
+    variable.
+    """
+    base_url = settings.GETGOV_PUBLIC_SITE_URL
+    # join the two halves with a single slash
+    public_url = "/".join([base_url.rstrip("/"), url_path.lstrip("/")])
+    return public_url


Test patch:
diff --git a/src/registrar/tests/test_templatetags.py b/src/registrar/tests/test_templatetags.py
new file mode 100644
--- /dev/null
+++ b/src/registrar/tests/test_templatetags.py
@@ -0,0 +1,31 @@
+"""Test template tags."""
+
+from django.conf import settings
+from django.test import TestCase
+from django.template import Context, Template
+
+
+class TestTemplateTags(TestCase):
+    def _render_template(self, string, context=None):
+        """Helper method to render a template given as a string.
+
+        Originally from https://stackoverflow.com/a/1690879
+        """
+        context = context or {}
+        context = Context(context)
+        return Template(string).render(context)
+
+    def test_public_site_url(self):
+        result = self._render_template(
+            "{% load url_helpers %}{% public_site_url 'directory/page' %}"
+        )
+        self.assertTrue(result.startswith(settings.GETGOV_PUBLIC_SITE_URL))
+        self.assertTrue(result.endswith("/directory/page"))
+
+    def test_public_site_url_leading_slash(self):
+        result = self._render_template(
+            "{% load url_helpers %}{% public_site_url '/directory/page' %}"
+        )
+        self.assertTrue(result.startswith(settings.GETGOV_PUBLIC_SITE_URL))
+        # slash-slash host slash directory slash page
+        self.assertEqual(result.count("/"), 4)