From 679025f299ea7473d21b04eb78d3ebc2239bb429 Mon Sep 17 00:00:00 2001 From: Mark Cottman-Fields Date: Thu, 28 Sep 2023 19:40:14 +1000 Subject: [PATCH] feat: add django-distill for static site generation --- requirements-dev.txt | 1 + src/gather_vision/apps/electricity/urls.py | 1 + .../templates/explore/about/index.html | 2 +- .../explore/templates/explore/home/index.html | 26 +++++++++---------- .../explore/templates/explore/layout.html | 14 +++++----- src/gather_vision/apps/explore/urls.py | 7 +++-- src/gather_vision/apps/legislatures/urls.py | 1 + src/gather_vision/apps/music/urls.py | 1 + src/gather_vision/apps/transport/urls.py | 1 + src/gather_vision/apps/water/urls.py | 1 + src/gather_vision/proj/settings.py | 17 +++++++++--- src/gather_vision/proj/urls.py | 26 ++++++++++++------- templates/layout/nav.html | 12 ++++----- 13 files changed, 67 insertions(+), 43 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 11b7bdd..d37c865 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -6,6 +6,7 @@ pip-audit # local dev django-debug-toolbar +django-distill # tests pytest diff --git a/src/gather_vision/apps/electricity/urls.py b/src/gather_vision/apps/electricity/urls.py index 3196869..671588e 100644 --- a/src/gather_vision/apps/electricity/urls.py +++ b/src/gather_vision/apps/electricity/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path app_name = "electricity" diff --git a/src/gather_vision/apps/explore/templates/explore/about/index.html b/src/gather_vision/apps/explore/templates/explore/about/index.html index a1e1c47..f2b6482 100644 --- a/src/gather_vision/apps/explore/templates/explore/about/index.html +++ b/src/gather_vision/apps/explore/templates/explore/about/index.html @@ -5,7 +5,7 @@ {% block page_title %}{% view_attr 'page_title' %}{% endblock %} {% block page_body %} - {% bs_breadcrumb 'vision:home-index' 'vision:about-index' %} +{# {% bs_breadcrumb 'vision:home-index' 'vision:about-index' %}#}

About

diff --git a/src/gather_vision/apps/explore/templates/explore/home/index.html b/src/gather_vision/apps/explore/templates/explore/home/index.html index 39b38b5..ef6f82a 100644 --- a/src/gather_vision/apps/explore/templates/explore/home/index.html +++ b/src/gather_vision/apps/explore/templates/explore/home/index.html @@ -4,7 +4,7 @@ {% block page_title %}{% view_attr 'page_title' %}{% endblock %} {% block page_body %} - {% bs_breadcrumb 'vision:home-index' %} +{# {% bs_breadcrumb 'vision:home-index' %}#}
@@ -19,9 +19,9 @@
Public transport notices

Provided in iCalendar (ics), web page, csv, and json api formats.

- - Check the notices - +{# #} +{# Check the notices#} +{# #}
@@ -34,9 +34,9 @@
Online petitions
Keeps track of the number of signatures for petitions over time. Can reveal the growth pattern of signature counts.

- - Follow the signature counts - +{# #} +{# Follow the signature counts#} +{# #} @@ -51,9 +51,9 @@
Electricity outages
Shows how long outages lasted for and any changes in the number of customers affected.

- - Have a look at the outages - +{# #} +{# Have a look at the outages#} +{# #} @@ -66,9 +66,9 @@
Music playlists
Builds music playlists for music streaming services from a number of online music charts.

- - See which tracks are on the playlists - +{# #} +{# See which tracks are on the playlists#} +{# #} diff --git a/src/gather_vision/apps/explore/templates/explore/layout.html b/src/gather_vision/apps/explore/templates/explore/layout.html index ccf8c2f..e85430d 100644 --- a/src/gather_vision/apps/explore/templates/explore/layout.html +++ b/src/gather_vision/apps/explore/templates/explore/layout.html @@ -37,27 +37,27 @@ diff --git a/src/gather_vision/apps/explore/urls.py b/src/gather_vision/apps/explore/urls.py index 1002209..a1e3b1a 100644 --- a/src/gather_vision/apps/explore/urls.py +++ b/src/gather_vision/apps/explore/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path from django.views.decorators.cache import cache_page from gather_vision.apps.explore.views.general import HomeIndexView, AboutIndexView @@ -8,14 +9,16 @@ app_name = "explore" urlpatterns = [ - path( + distill_path( "", cache_page(_cache_sec)(HomeIndexView.as_view()), name="home-index", + distill_file=f"{app_name}/index.html", ), - path( + distill_path( "about/", cache_page(_cache_sec)(AboutIndexView.as_view()), name="about-index", + distill_file=f"{app_name}/about/index.html", ), ] diff --git a/src/gather_vision/apps/legislatures/urls.py b/src/gather_vision/apps/legislatures/urls.py index f47b929..ff15163 100644 --- a/src/gather_vision/apps/legislatures/urls.py +++ b/src/gather_vision/apps/legislatures/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path app_name = "legislatures" diff --git a/src/gather_vision/apps/music/urls.py b/src/gather_vision/apps/music/urls.py index 8902cd0..817861b 100644 --- a/src/gather_vision/apps/music/urls.py +++ b/src/gather_vision/apps/music/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path app_name = "music" diff --git a/src/gather_vision/apps/transport/urls.py b/src/gather_vision/apps/transport/urls.py index f98ae21..0c7a3d9 100644 --- a/src/gather_vision/apps/transport/urls.py +++ b/src/gather_vision/apps/transport/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path app_name = "transport" diff --git a/src/gather_vision/apps/water/urls.py b/src/gather_vision/apps/water/urls.py index 8bc84b1..8b87a73 100644 --- a/src/gather_vision/apps/water/urls.py +++ b/src/gather_vision/apps/water/urls.py @@ -1,4 +1,5 @@ from django.urls import path +from django_distill import distill_path app_name = "water" diff --git a/src/gather_vision/proj/settings.py b/src/gather_vision/proj/settings.py index ee341b7..10fc379 100644 --- a/src/gather_vision/proj/settings.py +++ b/src/gather_vision/proj/settings.py @@ -30,6 +30,7 @@ # Set debug or default to false. DEBUG = env.get_bool(key="DEBUG", default=False) +USE_DEBUG_TOOLBAR = env.get_bool(key="USE_DEBUG_TOOLBAR", default=False) # secret key SECRET_KEY = env.get_str(key="SECRET_KEY") @@ -58,26 +59,33 @@ "gather_vision.apps.water.apps.WaterAppConfig", ] -if DEBUG is True: +if USE_DEBUG_TOOLBAR is True: INSTALLED_APPS.append("debug_toolbar") +if DEBUG is True: + # distill for static site generation needs to be after django apps + # and before the local apps + for index, installed_app in enumerate(INSTALLED_APPS): + if installed_app.startswith("gather_vision.apps."): + INSTALLED_APPS.insert(index, "django_distill") + break + MIDDLEWARE = [ "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.locale.LocaleMiddleware", "django.middleware.common.CommonMiddleware", - "debug_toolbar.middleware.DebugToolbarMiddleware", "django.middleware.csrf.CsrfViewMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", ] -if DEBUG is True: +if USE_DEBUG_TOOLBAR is True: MIDDLEWARE.insert( MIDDLEWARE.index("django.middleware.csrf.CsrfViewMiddleware"), - "debug_toolbar", + "debug_toolbar.middleware.DebugToolbarMiddleware", ) @@ -157,6 +165,7 @@ STATICFILES_DIRS = [ BASE_DIR / "static", ] +STATIC_ROOT = LOCAL_DIR / "staticfiles" # Default primary key field type # https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field diff --git a/src/gather_vision/proj/urls.py b/src/gather_vision/proj/urls.py index 98f7cd2..ff5e603 100644 --- a/src/gather_vision/proj/urls.py +++ b/src/gather_vision/proj/urls.py @@ -14,12 +14,12 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -import debug_toolbar + from django.contrib.auth import views as auth_views -from django.urls import path, include +from django.urls import include, path +from django_distill import distill_path +from gather_vision.proj import views, settings, admin -from gather_vision.proj import views -from gather_vision.proj.admin import admin_site urlpatterns = [ path( @@ -48,7 +48,7 @@ ), path( "admin/", - admin_site.urls, + admin.admin_site.urls, ), path( "electricity", @@ -74,13 +74,19 @@ "explore", include("gather_vision.apps.explore.urls", namespace="explore"), ), - path( + distill_path( "", views.HomePageView.as_view(), name="index", ), - path( - "__debug__/", - include(debug_toolbar.urls), - ), ] + +if settings.USE_DEBUG_TOOLBAR: + import debug_toolbar + + urlpatterns.append( + path( + "__debug__/", + include(debug_toolbar.urls), + ) + ) diff --git a/templates/layout/nav.html b/templates/layout/nav.html index e0337c3..f8fc6e6 100644 --- a/templates/layout/nav.html +++ b/templates/layout/nav.html @@ -18,12 +18,12 @@ {# #} - - +{# #} +{# #}