Created
October 3, 2015 20:56
-
-
Save jamiecounsell/7956fd89869c327a0310 to your computer and use it in GitHub Desktop.
Django urlpatterns to dictionary for easy JSON serialization (redacting `/admin/` urls)
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
def recursively_build__url_dict(d, urlpatterns): | |
for i in urlpatterns: | |
if isinstance(i, RegexURLResolver): | |
d[str(i.__dict__['_regex'])] = {} | |
if str(i.__dict__['_regex']) != "^admin/": | |
recursively_build__url_dict( | |
d[str(i.__dict__['_regex'])], i.url_patterns | |
) | |
else: | |
d[str(i.__dict__['_regex'])] = "REDACTED" | |
elif isinstance(i, RegexURLPattern): | |
d[str(i.regex.pattern)] = i.callback.__name__ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just call it
And the response will be something like: