Skip to content

Commit

Permalink
basics of microsites
Browse files Browse the repository at this point in the history
  • Loading branch information
longhotsummer committed Jul 24, 2023
1 parent bd6fd7d commit a0ba1eb
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 0 deletions.
Empty file added obl_microsites/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions obl_microsites/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class OpenByLawsMicrositesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "obl_microsites"
4 changes: 4 additions & 0 deletions obl_microsites/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def obl_microsites(request):
return {
"LOCALITY": request.obl_locality,
}
27 changes: 27 additions & 0 deletions obl_microsites/middleware.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions obl_microsites/settings.py
Original file line number Diff line number Diff line change
@@ -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"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends "open_by_laws/municipal_by_laws_list.html" %}
{% block breadcrumbs %}{% endblock %}
7 changes: 7 additions & 0 deletions obl_microsites/templates/peachjam/_header.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends 'peachjam/_header.html' %}
{% block nav-items %}
<li class="nav-item">
<a class="nav-link"
href="{% url 'municipal_by_laws' code=LOCALITY.code %}">{{ LOCALITY.name }} By-laws</a>
</li>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% extends 'peachjam/layouts/document_detail.html' %}
{% block breadcrumbs %}{% endblock %}
9 changes: 9 additions & 0 deletions obl_microsites/urls.py
Original file line number Diff line number Diff line change
@@ -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")),
]
13 changes: 13 additions & 0 deletions obl_microsites/views.py
Original file line number Diff line number Diff line change
@@ -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}
)

0 comments on commit a0ba1eb

Please sign in to comment.