-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor: centralize management of Django routes #2308
Conversation
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
013a964
to
e5d86d2
Compare
e807f90
to
903f76f
Compare
e5d86d2
to
6e043de
Compare
6e043de
to
d0a27cd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good to me and I didn't see any dangling routes (searched for the regex "[A-Za-z0-9_]*:[A-Za-z0-9_]*"
and the results are all in routes.py
). I only saw one place where the route changed but I think the change makes sense 👍
@@ -37,7 +29,7 @@ def index(request, agency=None): | |||
else: | |||
session.update(request, eligible=False, origin=agency.index_url) | |||
else: | |||
session.update(request, agency=agency, eligible=False, origin=reverse(ROUTE_CORE_INDEX)) | |||
session.update(request, agency=agency, eligible=False, origin=agency.index_url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct, since if the session has an agency, the origin is agency.index_url
--> routes.AGENCY_INDEX
--> "core:agency_index"
(url "/cst"
). But originally origin
was set to "core:index"
via ROUTE_CORE_INDEX
(url "/"
) so just wanted to make sure this change was intended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for noticing this and asking.
I did intentionally make this change because it seemed wrong to direct the user to core:index
if they had an agency selected already.
I think it would be good if @angela-tran takes a look before this is merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This refactor looks good to me. I left a question about how the routes
context processor works, which I think is more just for my understanding rather than pointing out a needed change.
374d3e6
to
c37ef28
Compare
c37ef28
to
161b70e
Compare
each @Property defined on the benefits.routes.Routes instance of is turned into context for the templates like: {"routes": { "INDEX": "core:index", "HELP": "core:help", ... }} in templates, to get the corresponding Django route string: {{ routes.ROUTE_NAME }} or e.g. {% url routes.ROUTE_NAME %}
add tests and improve clarity of the condition
161b70e
to
99fce51
Compare
Closes #2303
Part of #2252
Context setting
Right now we have Django route strings, e.g.
app_name:view_name
all over the place: in views, in middleware, in models, in templates, in URL config, in tests... it is truly a mess and difficult to get a handle on.This PR refactors every route string in the app into a top-level
benefits/routes.py
module. Everywhere else mentioned above that needs a route string, uses this central module to get it.Implementation details
benefits.routes.Routes
with an@property
for every route in the appROUTE_NAME
variables, cleaned up and organized by app e.g.INDEX
for the homepageHELP
for the help pageELIGIBILITY_INDEX
for the entry to eligibility phaseENROLLMENT_INDEX
for the entry to enrollment phaseCORE_
routes, it felt a little extra... but happy to add that for consistency."""Doc strings"""
are used to describe what the route is for, handy for devs using these propsroutes = Routes()
Routes.name(route: str) -> str
gets the name portion, e.g.name
fromapp:name
urls.py
uses thisRoutes.name()
helper when registering URLs, avoiding duplicationbenefits.core.context_processors.routes()
puts every route in the template context, so all hardcoded routes could be removed from templates: