From a0ba1eb9c2b5c80be174d81aeda03378641ef1d3 Mon Sep 17 00:00:00 2001 From: Greg Kempe Date: Mon, 24 Jul 2023 13:06:26 +0200 Subject: [PATCH] basics of microsites --- obl_microsites/__init__.py | 0 obl_microsites/apps.py | 6 +++++ obl_microsites/context_processors.py | 4 +++ obl_microsites/middleware.py | 27 +++++++++++++++++++ obl_microsites/settings.py | 11 ++++++++ .../open_by_laws/municipal_by_laws_list.html | 2 ++ .../templates/peachjam/_header.html | 7 +++++ .../peachjam/layouts/document_detail.html | 2 ++ obl_microsites/urls.py | 9 +++++++ obl_microsites/views.py | 13 +++++++++ 10 files changed, 81 insertions(+) create mode 100644 obl_microsites/__init__.py create mode 100644 obl_microsites/apps.py create mode 100644 obl_microsites/context_processors.py create mode 100644 obl_microsites/middleware.py create mode 100644 obl_microsites/settings.py create mode 100644 obl_microsites/templates/open_by_laws/municipal_by_laws_list.html create mode 100644 obl_microsites/templates/peachjam/_header.html create mode 100644 obl_microsites/templates/peachjam/layouts/document_detail.html create mode 100644 obl_microsites/urls.py create mode 100644 obl_microsites/views.py diff --git a/obl_microsites/__init__.py b/obl_microsites/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/obl_microsites/apps.py b/obl_microsites/apps.py new file mode 100644 index 000000000..bbaae30b6 --- /dev/null +++ b/obl_microsites/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class OpenByLawsMicrositesConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "obl_microsites" diff --git a/obl_microsites/context_processors.py b/obl_microsites/context_processors.py new file mode 100644 index 000000000..b09490513 --- /dev/null +++ b/obl_microsites/context_processors.py @@ -0,0 +1,4 @@ +def obl_microsites(request): + return { + "LOCALITY": request.obl_locality, + } diff --git a/obl_microsites/middleware.py b/obl_microsites/middleware.py new file mode 100644 index 000000000..d566f621b --- /dev/null +++ b/obl_microsites/middleware.py @@ -0,0 +1,27 @@ +from django.http import Http404 +from django.shortcuts import get_object_or_404 + +from peachjam.models import Locality + + +class LocalityMiddleware(object): + """Middleware to determine the locality for the microsite, based on the domain of the request.""" + + def __init__(self, get_response): + self.get_response = get_response + + def __call__(self, request): + host = request.get_host() + + if host.startswith("127.0.0.1") or host.startswith("localhost"): + # TODO: falls back to CPT for debugging + code = "cpt" + else: + if "." in host: + host = host.split(".", 1)[0] + code = {"bergrivier": "wc013"}.get(host, None) + if not code: + raise Http404 + + request.obl_locality = get_object_or_404(Locality.objects, code=code) + return self.get_response(request) diff --git a/obl_microsites/settings.py b/obl_microsites/settings.py new file mode 100644 index 000000000..3eb0ab6f0 --- /dev/null +++ b/obl_microsites/settings.py @@ -0,0 +1,11 @@ +from open_by_laws.settings import * # noqa + +INSTALLED_APPS = ["obl_microsites"] + INSTALLED_APPS # noqa + +ROOT_URLCONF = "obl_microsites.urls" + +MIDDLEWARE = ["obl_microsites.middleware.LocalityMiddleware"] + MIDDLEWARE # noqa + +TEMPLATES[0]["OPTIONS"]["context_processors"].append( # noqa + "obl_microsites.context_processors.obl_microsites" +) diff --git a/obl_microsites/templates/open_by_laws/municipal_by_laws_list.html b/obl_microsites/templates/open_by_laws/municipal_by_laws_list.html new file mode 100644 index 000000000..49878f5f0 --- /dev/null +++ b/obl_microsites/templates/open_by_laws/municipal_by_laws_list.html @@ -0,0 +1,2 @@ +{% extends "open_by_laws/municipal_by_laws_list.html" %} +{% block breadcrumbs %}{% endblock %} diff --git a/obl_microsites/templates/peachjam/_header.html b/obl_microsites/templates/peachjam/_header.html new file mode 100644 index 000000000..f77a8abf4 --- /dev/null +++ b/obl_microsites/templates/peachjam/_header.html @@ -0,0 +1,7 @@ +{% extends 'peachjam/_header.html' %} +{% block nav-items %} + +{% endblock %} diff --git a/obl_microsites/templates/peachjam/layouts/document_detail.html b/obl_microsites/templates/peachjam/layouts/document_detail.html new file mode 100644 index 000000000..69652b971 --- /dev/null +++ b/obl_microsites/templates/peachjam/layouts/document_detail.html @@ -0,0 +1,2 @@ +{% extends 'peachjam/layouts/document_detail.html' %} +{% block breadcrumbs %}{% endblock %} diff --git a/obl_microsites/urls.py b/obl_microsites/urls.py new file mode 100644 index 000000000..fa5f79650 --- /dev/null +++ b/obl_microsites/urls.py @@ -0,0 +1,9 @@ +from django.urls import include, path + +from obl_microsites.views import RedirectHomeView + +urlpatterns = [ + # redirect the homepage view to the appropriate municipality listing view + path("", RedirectHomeView.as_view()), + path("", include("open_by_laws.urls")), +] diff --git a/obl_microsites/views.py b/obl_microsites/views.py new file mode 100644 index 000000000..a48642489 --- /dev/null +++ b/obl_microsites/views.py @@ -0,0 +1,13 @@ +from django.http import Http404 +from django.urls import reverse +from django.views.generic import RedirectView + + +class RedirectHomeView(RedirectView): + def get_redirect_url(self, *args, **kwargs): + if not getattr(self.request, "obl_locality", None): + raise Http404() + + return reverse( + "municipal_by_laws", kwargs={"code": self.request.obl_locality.code} + )