Skip to content

Commit

Permalink
Refactor: centralize management of Django routes (#2308)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekaveman authored Aug 20, 2024
2 parents 1aabe8a + 99fce51 commit 7664917
Show file tree
Hide file tree
Showing 50 changed files with 403 additions and 268 deletions.
8 changes: 8 additions & 0 deletions benefits/core/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from django.conf import settings

from benefits.routes import routes as app_routes

from . import models, session


Expand Down Expand Up @@ -92,3 +94,9 @@ def origin(request):
return {"origin": origin}
else:
return {}


def routes(request):
"""Context processor adds information about each application route to the context."""

return {"routes": app_routes.to_dict()}
8 changes: 4 additions & 4 deletions benefits/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
from django.utils.deprecation import MiddlewareMixin
from django.views import i18n

from benefits.routes import routes
from . import analytics, recaptcha, session


logger = logging.getLogger(__name__)

HEALTHCHECK_PATH = "/healthcheck"
ROUTE_INDEX = "core:index"
TEMPLATE_USER_ERROR = "200-user-error.html"


Expand Down Expand Up @@ -135,7 +135,7 @@ def process_view(self, request, view_func, view_args, view_kwargs):
# pass through
return None

return redirect("oauth:login")
return redirect(routes.OAUTH_LOGIN)


class RecaptchaEnabled(MiddlewareMixin):
Expand All @@ -152,13 +152,13 @@ def process_request(self, request):


class IndexOrAgencyIndexOrigin(MiddlewareMixin):
"""Middleware sets the session.origin to either the core:index or core:agency_index depending on agency config."""
"""Middleware sets the session.origin to either the index or agency index route, depending on agency config."""

def process_request(self, request):
if session.active_agency(request):
session.update(request, origin=session.agency(request).index_url)
else:
session.update(request, origin=reverse(ROUTE_INDEX))
session.update(request, origin=reverse(routes.INDEX))
return None


Expand Down
10 changes: 3 additions & 7 deletions benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import requests

from benefits.routes import routes
from benefits.secrets import NAME_VALIDATOR, get_secret_by_name


Expand Down Expand Up @@ -355,17 +356,12 @@ def __str__(self):
@property
def index_url(self):
"""Public-facing URL to the TransitAgency's landing page."""
return reverse("core:agency_index", args=[self.slug])
return reverse(routes.AGENCY_INDEX, args=[self.slug])

@property
def eligibility_index_url(self):
"""Public facing URL to the TransitAgency's eligibility page."""
return reverse("eligibility:agency_index", args=[self.slug])

@property
def eligibility_api_public_key_url(self):
"""Public-facing URL to the TransitAgency's public key."""
return reverse("core:agency_public_key", args=[self.slug])
return reverse(routes.ELIGIBILITY_AGENCY_INDEX, args=[self.slug])

@property
def eligibility_api_private_key_data(self):
Expand Down
7 changes: 4 additions & 3 deletions benefits/core/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from django.urls import reverse

from benefits.routes import routes
from . import models


Expand Down Expand Up @@ -161,8 +162,8 @@ def oauth_claim(request):


def origin(request):
"""Get the origin for the request's session, or the default core:index."""
return request.session.get(_ORIGIN, reverse("core:index"))
"""Get the origin for the request's session, or default to the index route."""
return request.session.get(_ORIGIN, reverse(routes.INDEX))


def reset(request):
Expand All @@ -171,7 +172,7 @@ def reset(request):
request.session[_AGENCY] = None
request.session[_FLOW] = None
request.session[_ELIGIBLE] = False
request.session[_ORIGIN] = reverse("core:index")
request.session[_ORIGIN] = reverse(routes.INDEX)
request.session[_ENROLLMENT_EXP] = None
request.session[_ENROLLMENT_TOKEN] = None
request.session[_ENROLLMENT_TOKEN_EXP] = None
Expand Down
3 changes: 1 addition & 2 deletions benefits/core/templates/core/agency-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
{% endblock title %}

{% block call-to-action %}
{% url "eligibility:index" as url_continue %}
<a href="{{ url_continue }}" class="btn btn-lg btn-primary">{% translate "Get Started" %}</a>
<a href="{% url routes.ELIGIBILITY_INDEX %}" class="btn btn-lg btn-primary">{% translate "Get Started" %}</a>
{% endblock call-to-action %}
4 changes: 2 additions & 2 deletions benefits/core/templates/core/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<div class="container d-none d-lg-block">
<ul class="footer-links m-0 p-0 list-unstyled d-lg-flex gap-lg-4">
<li>
<a class="footer-link m-0 p-0" href="{% url "core:help" %}">{% translate "Help" %}</a>
<a class="footer-link m-0 p-0" href="{% url routes.HELP %}">{% translate "Help" %}</a>
</li>
<li>
<a class="footer-link m-0 p-0" href="https://cdt.ca.gov/privacy-policy/" target="_blank" rel="noopener noreferrer">{% translate "Privacy Policy" %}</a>
Expand All @@ -117,7 +117,7 @@
<div class="d-block d-lg-none container">
<ul class="col-12 footer-links ps-0 mb-0">
<li>
<a class="footer-link" href="{% url "core:help" %}">{% translate "Help" %}</a>
<a class="footer-link" href="{% url routes.HELP %}">{% translate "Help" %}</a>
</li>
</ul>
</div>
Expand Down
3 changes: 1 addition & 2 deletions benefits/core/templates/core/includes/button--index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{% load i18n %}

{% url "core:index" as href %}
{% translate "Return home" as default_button_text %}

<a href="{{ href }}" class="btn btn-lg btn-primary">{{ button_text | default:default_button_text }}</a>
<a href="{% url routes.INDEX %}" class="btn btn-lg btn-primary">{{ button_text | default:default_button_text }}</a>
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{% load i18n %}

{% if authentication.logged_in %}
{% url "oauth:logout" as sign_out_url %}
<a href="{{ sign_out_url }}" class="login p-0 btn btn-lg" role="button">
<a href="{% url routes.OAUTH_LOGOUT %}" class="login p-0 btn btn-lg" role="button">
<span class="fallback-text color-logo">Login.gov</span>
</a>
{% endif %}
3 changes: 1 addition & 2 deletions benefits/core/templates/core/includes/link--sign-out.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{% if authentication.logged_in %}
{% url "oauth:logout" as signout_url %}
<div class="w-100 position-absolute">
<div class="container">
<div class="row nav-button-row">
<div class="col-12 d-flex align-items-center justify-content-end">
<a class="signout-link" href="{{ signout_url }}">
<a class="signout-link" href="{% url routes.OAUTH_LOGOUT %}">
{% block button_text %}
{% endblock button_text %}
</a>
Expand Down
2 changes: 1 addition & 1 deletion benefits/core/templates/core/includes/nocookies.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<span class="navbar-brand">{% translate "Cookies are disabled" %}</span>
<span class="navbar-text">
{% translate "To function properly, this website requires a browser that supports cookies. Please enable cookies for this website and" %}
<a href="{% url "core:index" %}">{% translate "Return home" %}</a>.
<a href="{% url routes.INDEX %}">{% translate "Return home" %}</a>.
</span>
</div>
{% endblock content %}
2 changes: 1 addition & 1 deletion benefits/core/templates/core/includes/noscript.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<span class="navbar-brand">{% translate "JavaScript is disabled" %}</span>
<span class="navbar-text">
{% translate "To function properly, this website requires a browser that supports JavaScript. Please enable JavaScript for this website and" %}
<a href="{% url "core:index" %}">{% translate "Return home" %}</a>.
<a href="{% url routes.INDEX %}">{% translate "Return home" %}</a>.
</span>
</div>
{% endblock content %}
13 changes: 7 additions & 6 deletions benefits/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.urls import path, register_converter

from benefits.routes import routes
from . import models, views


Expand Down Expand Up @@ -45,10 +46,10 @@ def to_url(self, agency):
app_name = "core"

urlpatterns = [
path("", views.index, name="index"),
path("help", views.help, name="help"),
path("<agency:agency>", views.agency_index, name="agency_index"),
path("<agency:agency>/publickey", views.agency_public_key, name="agency_public_key"),
path("logged_out", views.logged_out, name="logged_out"),
path("error", views.server_error, name="server-error"),
path("", views.index, name=routes.name(routes.INDEX)),
path("help", views.help, name=routes.name(routes.HELP)),
path("<agency:agency>", views.agency_index, name=routes.name(routes.AGENCY_INDEX)),
path("<agency:agency>/publickey", views.agency_public_key, name=routes.name(routes.AGENCY_PUBLIC_KEY)),
path("logged_out", views.logged_out, name=routes.name(routes.LOGGED_OUT)),
path("error", views.server_error, name=routes.name(routes.SERVER_ERROR)),
]
5 changes: 0 additions & 5 deletions benefits/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
from . import models, session
from .middleware import pageview_decorator, index_or_agencyindex_origin_decorator

ROUTE_ELIGIBILITY = "eligibility:index"
ROUTE_HELP = "core:help"
ROUTE_LOGGED_OUT = "core:logged_out"
ROUTE_SERVER_ERROR = "core:server-error"

TEMPLATE_INDEX = "core/index.html"
TEMPLATE_AGENCY = "core/agency-index.html"
TEMPLATE_HELP = "core/help.html"
Expand Down
5 changes: 3 additions & 2 deletions benefits/eligibility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django import forms
from django.utils.translation import gettext_lazy as _

from benefits.routes import routes
from benefits.core import models, recaptcha, widgets

logger = logging.getLogger(__name__)
Expand All @@ -15,7 +16,7 @@
class EnrollmentFlowSelectionForm(forms.Form):
"""Form to capture enrollment flow selection."""

action_url = "eligibility:index"
action_url = routes.ELIGIBILITY_INDEX
id = "form-flow-selection"
method = "POST"

Expand Down Expand Up @@ -44,7 +45,7 @@ def clean(self):
class EligibilityVerificationForm(forms.Form):
"""Form to collect eligibility verification details."""

action_url = "eligibility:confirm"
action_url = routes.ELIGIBILITY_CONFIRM
id = "form-eligibility-verification"
method = "POST"

Expand Down
2 changes: 1 addition & 1 deletion benefits/eligibility/templates/eligibility/confirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% endblock page-title %}

{% block nav-buttons %}
{% url "eligibility:start" as url_previous %}
{% url routes.ELIGIBILITY_START as url_previous %}
{% include "core/includes/button--previous-page.html" with url=url_previous %}
{% endblock nav-buttons %}

Expand Down
2 changes: 1 addition & 1 deletion benefits/eligibility/templates/eligibility/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% endblock page-title %}

{% block nav-buttons %}
{% url "core:index" as url %}
{% url routes.INDEX as url %}
{% include "core/includes/button--previous-page.html" with url=url %}
{% endblock nav-buttons %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ <h1>{% translate "You selected a CalFresh Cardholder transit benefit." %}</h1>
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-4 offset-2 offset-lg-0 col-sm-8 col-8 d-flex justify-content-lg-end justify-content-center">
{% url "oauth:login" as button_url %}
{% translate "Get started with" as button_text %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary login">
<a href="{% url routes.OAUTH_LOGIN %}" class="btn btn-lg btn-primary login">
{{ button_text }} <span class="fallback-text white-logo">Login.gov</span>
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ <h1>{% translate "You selected an Agency Card transit benefit." %}</h1>
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-3 offset-2 offset-sm-2 offset-lg-0 col-sm-8 col-8">
{% url "eligibility:confirm" as button_url %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
<a href="{% url routes.ELIGIBILITY_CONFIRM %}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
</div>
</div>
{% endblock call-to-action %}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ <h1>{% translate "You selected a Courtesy Card transit benefit." %}</h1>
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-3 offset-2 offset-sm-2 offset-lg-0 col-sm-8 col-8">
{% url "eligibility:confirm" as button_url %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
<a href="{% url routes.ELIGIBILITY_CONFIRM %}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
</div>
</div>
{% endblock call-to-action %}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ <h1>{% translate "You selected a Reduced Fare Mobility ID transit benefit." %}</
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-3 offset-2 offset-sm-2 offset-lg-0 col-sm-8 col-8">
{% url "eligibility:confirm" as button_url %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
<a href="{% url routes.ELIGIBILITY_CONFIRM %}" class="btn btn-lg btn-primary" role="button">{% translate "Continue" %}</a>
</div>
</div>
{% endblock call-to-action %}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ <h1>{% translate "You selected an Older Adult transit benefit." %}</h1>
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-4 offset-2 offset-lg-0 col-sm-8 col-8 d-flex justify-content-lg-end justify-content-center">
{% url "oauth:login" as button_url %}
{% translate "Get started with" as button_text %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary login">
<a href="{% url routes.OAUTH_LOGIN %}" class="btn btn-lg btn-primary login">
{{ button_text }} <span class="fallback-text white-logo">Login.gov</span>
</a>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ <h1>{% translate "You selected a Veteran transit benefit." %}</h1>
{% block call-to-action %}
<div class="row d-flex justify-content-lg-end">
<div class="col-lg-4 offset-2 offset-lg-0 col-sm-8 col-8 d-flex justify-content-lg-end justify-content-center">
{% url "oauth:login" as button_url %}
{% translate "Get started with" as button_text %}
<a href="{{ button_url }}" class="btn btn-lg btn-primary login">
<a href="{% url routes.OAUTH_LOGIN %}" class="btn btn-lg btn-primary login">
{{ button_text }} <span class="fallback-text white-logo">Login.gov</span>
</a>
</div>
Expand Down
2 changes: 1 addition & 1 deletion benefits/eligibility/templates/eligibility/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% endblock classes %}

{% block nav-buttons %}
{% url "eligibility:index" as url %}
{% url routes.ELIGIBILITY_INDEX as url %}
{% include "core/includes/button--previous-page.html" with url=url %}
{% endblock nav-buttons %}

Expand Down
11 changes: 6 additions & 5 deletions benefits/eligibility/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

from django.urls import path

from benefits.routes import routes
from . import views


app_name = "eligibility"
urlpatterns = [
# /eligibility
path("", views.index, name="index"),
path("<agency:agency>", views.index, name="agency_index"),
path("start", views.start, name="start"),
path("confirm", views.confirm, name="confirm"),
path("unverified", views.unverified, name="unverified"),
path("", views.index, name=routes.name(routes.ELIGIBILITY_INDEX)),
path("<agency:agency>", views.index, name=routes.name(routes.ELIGIBILITY_AGENCY_INDEX)),
path("start", views.start, name=routes.name(routes.ELIGIBILITY_START)),
path("confirm", views.confirm, name=routes.name(routes.ELIGIBILITY_CONFIRM)),
path("unverified", views.unverified, name=routes.name(routes.ELIGIBILITY_UNVERIFIED)),
]
Loading

0 comments on commit 7664917

Please sign in to comment.