diff --git a/CHANGES b/CHANGES index 26e974c..4a46d3d 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,4 @@ +- Added dispatch_slug_path. 0.2.17 2018-12-17 - PageTitlesMixin: Slimdown name for get_short_title. - Daytime utils. diff --git a/shared/utils/url_helpers.py b/shared/utils/url_helpers.py new file mode 100644 index 0000000..ad4b438 --- /dev/null +++ b/shared/utils/url_helpers.py @@ -0,0 +1,33 @@ +from django.http import Http404 + + +def dispatch_slug_path(*views): + """ + Dispatch full path slug in iterating through a set of views. + Http404 exceptions raised by a view lead to trying the next view + in the list. + + This allows to plug different slug systems to the same root URL. + + Usages:: + + # in urls.py + path('/', dispatch_slug_path( + views.CategoryDetailView.as_view(), + views.ArticleDetailView.as_view())), + ) + """ + def wrapper(request, slug_path): + args = [] + kwargs = {'slug_path': slug_path} + + not_found_exception = Http404 + for view in views: + try: + return view(request, *args, **kwargs) + except Http404 as e: + not_found_exception = e # assign to use it outside of except block + continue + raise not_found_exception + + return wrapper