Code

Opened 6 years ago

Last modified 14 months ago

#7537 new New feature

Make RegexURLResolver easier to subclass

Reported by: kcarnold Owned by: nobody
Component: Core (URLs) Version: master
Severity: Normal Keywords: url, urlpatterns, resolver
Cc: kenneth.arnold@…, eallik@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Referencing the django-developers discussion, here's some simple patches to make the URL resolver easier to customize.

First is a nearly minimal patch to convert the url_patterns property to an accessor, get_url_patterns, to ease subclassing, and make the other methods use that consistently instead of duplicating its functionality. With only this patch you can make your own URL resolver without duplicating code, but it's still not clean.

The second patch cleans up the situation by abstracting the core functionality of a regex URL resolver into BaseRegexURLResolver.

The third patch is an example of how newforms-admin might use this for URL dispatching. I don't have a deep understanding about what's going on, so treat what I did just as an example. Perhaps the cleanest way to accomplish some of what the old ad-hoc resolver did is to override resolve, and if super().resolve returns None, do the ad-hoc stuff.

You'd use a custom resolver in your urlconf like this:

urlpatterns += [AdminSite(r'^admin/')]

i.e., like a normal include (at least, what that include looks like under the hood).

The newforms-admin change would be backwards-incompatible, so if people think it's a good idea, it should get a separate ticket.

About the accessor: properties don't inherit well. Could add a url_patterns = property(lambda self: self.get_url_patterns()) if that's not too ugly.

Attachments (3)

dj-urlresolver-patch2.patch (2.9 KB) - added by kcarnold 6 years ago.
Patch 2: extract base functionality
dj-nfa-urlresolver.diff (5.2 KB) - added by kcarnold 6 years ago.
Example nfa change (Example Only)
dj-urlresolver-patch1.patch (1.9 KB) - added by kcarnold 6 years ago.
nearly minimal patch

Download all attachments as: .zip

Change History (12)

Changed 6 years ago by kcarnold

Patch 2: extract base functionality

Changed 6 years ago by kcarnold

Example nfa change (Example Only)

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Changed 6 years ago by kcarnold

nearly minimal patch

comment:2 Changed 6 years ago by kcarnold

  • Cc kenneth.arnold@… added

comment:3 Changed 6 years ago by RaceCondition

  • Cc eallik@… added

comment:4 Changed 6 years ago by RaceCondition

Wouldn't this also solve the issue of not being able to reverse or {% url %} "into" the admin interface?

comment:5 Changed 6 years ago by Alex

Yes, but so does #6470

comment:6 Changed 6 years ago by RaceCondition

I cannot see how. If #6470 was applied, would I be able to do {% url admin_edit_view mymodel %}?

comment:7 Changed 3 years ago by lukeplant

  • Severity set to Normal
  • Type set to New feature

comment:8 Changed 3 years ago by Alex

  • Easy pickings unset
  • Triage Stage changed from Design decision needed to Accepted
  • UI/UX unset

me of a few years ago was silly, any way to better organize the code is a good idea.

comment:9 Changed 14 months ago by aaugustin

  • Component changed from Core (Other) to Core (URLs)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.