Skip to content

Commit

Permalink
feat: add django-distill for static site generation
Browse files Browse the repository at this point in the history
  • Loading branch information
cofiem committed Sep 28, 2023
1 parent 1b8cfd3 commit 679025f
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 43 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pip-audit

# local dev
django-debug-toolbar
django-distill

# tests
pytest
Expand Down
1 change: 1 addition & 0 deletions src/gather_vision/apps/electricity/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import path
from django_distill import distill_path

app_name = "electricity"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}#}

<h3>About</h3>

Expand Down
26 changes: 13 additions & 13 deletions src/gather_vision/apps/explore/templates/explore/home/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}#}

<div class="row row-cols-1 row-cols-md-2 g-4">
<div class="col">
Expand All @@ -19,9 +19,9 @@ <h5 class="card-title">Public transport notices</h5>
<p class="card-text">
Provided in iCalendar (ics), web page, csv, and json api formats.
</p>
<a href="{% url 'vision:transport-index' %}" class="card-link">
Check the notices
</a>
{# <a href="{% url 'vision:transport-index' %}" class="card-link">#}
{# Check the notices#}
{# </a>#}
</div>
</div>
</div>
Expand All @@ -34,9 +34,9 @@ <h5 class="card-title">Online petitions</h5>
Keeps track of the number of signatures for petitions over time.
Can reveal the growth pattern of signature counts.
</p>
<a href="{% url 'vision:petitions-index' %}" class="card-link">
Follow the signature counts
</a>
{# <a href="{% url 'vision:petitions-index' %}" class="card-link">#}
{# Follow the signature counts#}
{# </a>#}
</div>
</div>
</div>
Expand All @@ -51,9 +51,9 @@ <h5 class="card-title">Electricity outages</h5>
Shows how long outages lasted for and
any changes in the number of customers affected.
</p>
<a href="{% url 'vision:outages-index' %}" class="card-link">
Have a look at the outages
</a>
{# <a href="{% url 'vision:outages-index' %}" class="card-link">#}
{# Have a look at the outages#}
{# </a>#}
</div>
</div>
</div>
Expand All @@ -66,9 +66,9 @@ <h5 class="card-title">Music playlists</h5>
Builds music playlists for music streaming services
from a number of online music charts.
</p>
<a href="{% url 'vision:playlists-index' %}" class="card-link">
See which tracks are on the playlists
</a>
{# <a href="{% url 'vision:playlists-index' %}" class="card-link">#}
{# See which tracks are on the playlists#}
{# </a>#}
</div>
</div>
</div>
Expand Down
14 changes: 7 additions & 7 deletions src/gather_vision/apps/explore/templates/explore/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav me-auto mb-2 mb-md-0">
<li class="nav-item">
{% bs_nav_link 'vision:transport-filter' transport_filter='all' %}
{# {% bs_nav_link 'vision:transport-filter' transport_filter='all' %}#}
</li>
<li class="nav-item">
{% bs_nav_link 'vision:playlists-index' %}
{# {% bs_nav_link 'vision:playlists-index' %}#}
</li>
<li class="nav-item">
{% bs_nav_link 'vision:petitions-index' %}
{# {% bs_nav_link 'vision:petitions-index' %}#}
</li>
<li class="nav-item">
{% bs_nav_link 'vision:outages-index' %}
{# {% bs_nav_link 'vision:outages-index' %}#}
</li>
<li class="nav-item">
{% bs_nav_link 'vision:contact-tracing-index' %}
{# {% bs_nav_link 'vision:contact-tracing-index' %}#}
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item">
{% bs_nav_link 'vision:about-index' 'About' %}
{# {% bs_nav_link 'vision:about-index' 'About' %}#}
</li>
<li class="nav-item">
{% bs_nav_link 'admin:index' 'Admin' %}
{# {% bs_nav_link 'admin:index' 'Admin' %}#}
</li>
</ul>
</div>
Expand Down
7 changes: 5 additions & 2 deletions src/gather_vision/apps/explore/urls.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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",
),
]
1 change: 1 addition & 0 deletions src/gather_vision/apps/legislatures/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import path
from django_distill import distill_path

app_name = "legislatures"

Expand Down
1 change: 1 addition & 0 deletions src/gather_vision/apps/music/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import path
from django_distill import distill_path

app_name = "music"

Expand Down
1 change: 1 addition & 0 deletions src/gather_vision/apps/transport/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import path
from django_distill import distill_path

app_name = "transport"

Expand Down
1 change: 1 addition & 0 deletions src/gather_vision/apps/water/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.urls import path
from django_distill import distill_path

app_name = "water"

Expand Down
17 changes: 13 additions & 4 deletions src/gather_vision/proj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
)


Expand Down Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions src/gather_vision/proj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -48,7 +48,7 @@
),
path(
"admin/",
admin_site.urls,
admin.admin_site.urls,
),
path(
"electricity",
Expand All @@ -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),
)
)
12 changes: 6 additions & 6 deletions templates/layout/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
{# <li class="nav-item">#}
{# <a class="nav-link active" aria-current="page" href="{% url 'finances:index' %}">Finances</a>#}
{# </li>#}
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'finances:accounts' %}">Accounts</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="{% url 'finances:categories' %}">Categories</a>
</li>
{# <li class="nav-item">#}
{# <a class="nav-link active" aria-current="page" href="{% url 'finances:accounts' %}">Accounts</a>#}
{# </li>#}
{# <li class="nav-item">#}
{# <a class="nav-link active" aria-current="page" href="{% url 'finances:categories' %}">Categories</a>#}
{# </li>#}
</ul>
<ul class="navbar-nav">
<li class="nav-item">
Expand Down

0 comments on commit 679025f

Please sign in to comment.