From b01936ce92eea7ffa984a0b50b980de85389df14 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sun, 21 Aug 2022 01:41:35 +0200 Subject: [PATCH 1/4] use a casecade of paths --- cbv/urls.py | 90 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 51 insertions(+), 39 deletions(-) diff --git a/cbv/urls.py b/cbv/urls.py index 91016f4c..b448f5e6 100644 --- a/cbv/urls.py +++ b/cbv/urls.py @@ -13,7 +13,7 @@ """ -from django.urls import path, re_path, reverse_lazy +from django.urls import include, path, reverse_lazy from django.views.generic import RedirectView from cbv import views @@ -21,42 +21,54 @@ urlpatterns = [ path("", RedirectView.as_view(url=reverse_lazy("home"))), - re_path( - r"^(?P[\w-]+)/$", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - ), - re_path( - r"^(?P[\w-]+)/latest/$", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - name="latest-version-detail", - ), - re_path( - r"^(?P[\w-]+)/(?P[^/]+)/$", - views.VersionDetailView.as_view(), - name="version-detail", - ), - re_path( - r"^(?P[\w-]+)/latest/(?P[\w\.]+)/$", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "module-detail"}, - name="latest-module-detail", - ), - re_path( - r"^(?P[\w-]+)/(?P[^/]+)/(?P[\w\.]+)/$", - views.ModuleDetailView.as_view(), - name="module-detail", - ), - re_path( - r"^(?P[\w-]+)/latest/(?P[\w\.]+)/(?P[\w]+)/$", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "klass-detail"}, - name="latest-klass-detail", - ), - re_path( - r"^(?P[\w-]+)/(?P[^/]+)/(?P[\w\.]+)/(?P[\w]+)/$", - views.KlassDetailView.as_view(), - name="klass-detail", - ), + path("/", include([ + path( + "", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "version-detail"}, + ), + path( + "latest/", include([ + path( + "", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "version-detail"}, + name="latest-version-detail", + ), + path("/", include([ + path( + "", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "module-detail"}, + name="latest-module-detail" + ), + path( + "/", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "klass-detail"}, + name="latest-klass-detail", + ) + ])), + ])), + path( + "/", include([ + path( + "", + views.VersionDetailView.as_view(), + name="version-detail", + ), + path("/", include([ + path( + "", + views.ModuleDetailView.as_view(), + name="module-detail" + ), + path( + "/", + views.KlassDetailView.as_view(), + name="klass-detail", + ) + ])), + ])) + ])) ] From d275386ae1028bc591961efc090a5f32458f3461 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sun, 21 Aug 2022 01:42:48 +0200 Subject: [PATCH 2/4] black reformatting --- cbv/urls.py | 109 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 43 deletions(-) diff --git a/cbv/urls.py b/cbv/urls.py index b448f5e6..988168c4 100644 --- a/cbv/urls.py +++ b/cbv/urls.py @@ -21,54 +21,77 @@ urlpatterns = [ path("", RedirectView.as_view(url=reverse_lazy("home"))), - path("/", include([ - path( - "", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - ), - path( - "latest/", include([ - path( - "", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - name="latest-version-detail", - ), - path("/", include([ + path( + "/", + include( + [ path( "", views.RedirectToLatestVersionView.as_view(), - {"url_name": "module-detail"}, - name="latest-module-detail" + {"url_name": "version-detail"}, ), path( - "/", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "klass-detail"}, - name="latest-klass-detail", - ) - ])), - ])), - path( - "/", include([ - path( - "", - views.VersionDetailView.as_view(), - name="version-detail", - ), - path("/", include([ - path( - "", - views.ModuleDetailView.as_view(), - name="module-detail" + "latest/", + include( + [ + path( + "", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "version-detail"}, + name="latest-version-detail", + ), + path( + "/", + include( + [ + path( + "", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "module-detail"}, + name="latest-module-detail", + ), + path( + "/", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "klass-detail"}, + name="latest-klass-detail", + ), + ] + ), + ), + ] + ), ), path( - "/", - views.KlassDetailView.as_view(), - name="klass-detail", - ) - ])), - ])) - ])) + "/", + include( + [ + path( + "", + views.VersionDetailView.as_view(), + name="version-detail", + ), + path( + "/", + include( + [ + path( + "", + views.ModuleDetailView.as_view(), + name="module-detail", + ), + path( + "/", + views.KlassDetailView.as_view(), + name="klass-detail", + ), + ] + ), + ), + ] + ), + ), + ] + ), + ), ] From 8111fac6c4cfd87408598bd8c5595e99c6630988 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Sun, 21 Aug 2022 13:17:51 +0200 Subject: [PATCH 3/4] remove blank line --- cbv/urls.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cbv/urls.py b/cbv/urls.py index 988168c4..60bb2c5e 100644 --- a/cbv/urls.py +++ b/cbv/urls.py @@ -10,7 +10,6 @@ django/1.41a django/1.41a/core django/1.41a/core/DjangoRuntimeWarning - """ from django.urls import include, path, reverse_lazy From 9fb398fdbc4024e6d19505e7053ae34fedb1322a Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Tue, 23 Aug 2022 00:36:32 +0200 Subject: [PATCH 4/4] towards flat path(..)s --- cbv/shortcut_urls.py | 6 +-- cbv/urls.py | 107 +++++++++++++++---------------------------- inspector/urls.py | 4 +- 3 files changed, 41 insertions(+), 76 deletions(-) diff --git a/cbv/shortcut_urls.py b/cbv/shortcut_urls.py index e3e4f9ac..16a24999 100644 --- a/cbv/shortcut_urls.py +++ b/cbv/shortcut_urls.py @@ -1,11 +1,11 @@ -from django.urls import re_path +from django.urls import path from cbv import views urlpatterns = [ - re_path( - r"(?P[a-zA-Z_-]+)/$", + path( + "/", views.LatestKlassDetailView.as_view(), name="klass-detail-shortcut", ), diff --git a/cbv/urls.py b/cbv/urls.py index 60bb2c5e..0b4aeedd 100644 --- a/cbv/urls.py +++ b/cbv/urls.py @@ -12,7 +12,7 @@ django/1.41a/core/DjangoRuntimeWarning """ -from django.urls import include, path, reverse_lazy +from django.urls import path, reverse_lazy from django.views.generic import RedirectView from cbv import views @@ -22,75 +22,40 @@ path("", RedirectView.as_view(url=reverse_lazy("home"))), path( "/", - include( - [ - path( - "", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - ), - path( - "latest/", - include( - [ - path( - "", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "version-detail"}, - name="latest-version-detail", - ), - path( - "/", - include( - [ - path( - "", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "module-detail"}, - name="latest-module-detail", - ), - path( - "/", - views.RedirectToLatestVersionView.as_view(), - {"url_name": "klass-detail"}, - name="latest-klass-detail", - ), - ] - ), - ), - ] - ), - ), - path( - "/", - include( - [ - path( - "", - views.VersionDetailView.as_view(), - name="version-detail", - ), - path( - "/", - include( - [ - path( - "", - views.ModuleDetailView.as_view(), - name="module-detail", - ), - path( - "/", - views.KlassDetailView.as_view(), - name="klass-detail", - ), - ] - ), - ), - ] - ), - ), - ] - ), + views.RedirectToLatestVersionView.as_view(), + {"url_name": "version-detail"}, + ), + path( + "/latest/", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "version-detail"}, + name="latest-version-detail", + ), + path( + "//", + views.VersionDetailView.as_view(), + name="version-detail", + ), + path( + "/latest//", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "module-detail"}, + name="latest-module-detail", + ), + path( + "///", + views.ModuleDetailView.as_view(), + name="module-detail", + ), + path( + "/latest///", + views.RedirectToLatestVersionView.as_view(), + {"url_name": "klass-detail"}, + name="latest-klass-detail", + ), + path( + "////", + views.KlassDetailView.as_view(), + name="klass-detail", ), ] diff --git a/inspector/urls.py b/inspector/urls.py index fb2f3cb5..d23145e4 100644 --- a/inspector/urls.py +++ b/inspector/urls.py @@ -1,6 +1,6 @@ from django.conf import settings from django.conf.urls.static import static -from django.urls import include, path, re_path +from django.urls import include, path from django.views.generic import TemplateView from cbv.views import HomeView, Sitemap @@ -9,7 +9,7 @@ urlpatterns = [ path("", HomeView.as_view(), name="home"), path("projects/", include("cbv.urls")), - re_path(r"^sitemap\.xml$", Sitemap.as_view(), name="sitemap"), + path("sitemap.xml", Sitemap.as_view(), name="sitemap"), path("", include("cbv.shortcut_urls"), {"package": "Django"}), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)