Update module name

This will handle more than just URLs
This commit is contained in:
Jake Howard 2022-08-28 20:24:59 +01:00
parent 442aeb21b8
commit 6f073c6cfc
Signed by: jake
GPG key ID: 57AFB45680EDD477
9 changed files with 15 additions and 15 deletions

View file

@ -1,6 +1,6 @@
from django.template import Library from django.template import Library
from website.contrib.singleton_url.utils import SingletonURLCache from website.contrib.singleton_page.utils import SingletonPageCache
from website.home.models import HomePage from website.home.models import HomePage
register = Library() register = Library()
@ -9,5 +9,5 @@ register = Library()
@register.inclusion_tag("common/footer.html") @register.inclusion_tag("common/footer.html")
def footer() -> dict: def footer() -> dict:
return { return {
"homepage_url": SingletonURLCache.get_url(HomePage), "homepage_url": SingletonPageCache.get_url(HomePage),
} }

View file

@ -1,6 +1,6 @@
from django.template import Library from django.template import Library
from website.contrib.singleton_url.utils import SingletonURLCache from website.contrib.singleton_page.utils import SingletonPageCache
from website.home.models import HomePage from website.home.models import HomePage
from website.search.models import SearchPage from website.search.models import SearchPage
@ -17,5 +17,5 @@ def navbar() -> dict:
.public() .public()
.filter(show_in_menus=True) .filter(show_in_menus=True)
.order_by("title"), .order_by("title"),
"search_page_url": SingletonURLCache.get_url(SearchPage), "search_page_url": SingletonPageCache.get_url(SearchPage),
} }

View file

@ -3,14 +3,14 @@ from django.test import TestCase
from website.common.models import ContentPage from website.common.models import ContentPage
from website.home.models import HomePage from website.home.models import HomePage
from .utils import SingletonURLCache from .utils import SingletonPageCache
class SingletonURLTestCase(TestCase): class SingletonURLTestCase(TestCase):
def test_gets_url(self) -> None: def test_gets_url(self) -> None:
with self.assertNumQueries(2): with self.assertNumQueries(2):
self.assertEqual(SingletonURLCache.get_url(HomePage), "http://localhost/") self.assertEqual(SingletonPageCache.get_url(HomePage), "http://localhost/")
def test_missing_page(self) -> None: def test_missing_page(self) -> None:
with self.assertNumQueries(1): with self.assertNumQueries(1):
self.assertIsNone(SingletonURLCache.get_url(ContentPage)) self.assertIsNone(SingletonPageCache.get_url(ContentPage))

View file

@ -5,7 +5,7 @@ from django.http.request import HttpRequest
from wagtail.models import Page from wagtail.models import Page
class SingletonURLCache: class SingletonPageCache:
@classmethod @classmethod
def get_url_cache_key(cls, model: Type[Page]) -> str: def get_url_cache_key(cls, model: Type[Page]) -> str:
return f"singleton_url_{model.__name__}" return f"singleton_url_{model.__name__}"

View file

@ -3,7 +3,7 @@ from wagtail import hooks
from website.common.utils import get_page_models from website.common.utils import get_page_models
from .utils import SingletonURLCache from .utils import SingletonPageCache
@hooks.register("after_move_page") @hooks.register("after_move_page")
@ -12,5 +12,5 @@ def clear_singleton_url_cache(**kwargs: dict) -> None:
Clear all page caches, in case a parent has moved Clear all page caches, in case a parent has moved
""" """
cache.delete_many( cache.delete_many(
[SingletonURLCache.get_url_cache_key(model) for model in get_page_models()] [SingletonPageCache.get_url_cache_key(model) for model in get_page_models()]
) )

View file

@ -6,7 +6,7 @@ from wagtail.images.models import Image
from wagtailmetadata.models import WagtailImageMetadataMixin from wagtailmetadata.models import WagtailImageMetadataMixin
from website.common.models import BasePage from website.common.models import BasePage
from website.contrib.singleton_url.utils import SingletonURLCache from website.contrib.singleton_page.utils import SingletonPageCache
class HomePage(BasePage, WagtailImageMetadataMixin): class HomePage(BasePage, WagtailImageMetadataMixin):
@ -49,5 +49,5 @@ class HomePage(BasePage, WagtailImageMetadataMixin):
.order_by("-date") .order_by("-date")
.first() .first()
) )
context["search_page_url"] = SingletonURLCache.get_url(SearchPage) context["search_page_url"] = SingletonPageCache.get_url(SearchPage)
return context return context

View file

@ -41,7 +41,7 @@ INSTALLED_APPS = [
"website.contrib.code_block", "website.contrib.code_block",
"website.contrib.mermaid_block", "website.contrib.mermaid_block",
"website.contrib.unsplash", "website.contrib.unsplash",
"website.contrib.singleton_url", "website.contrib.singleton_page",
"wagtail.contrib.forms", "wagtail.contrib.forms",
"wagtail.contrib.redirects", "wagtail.contrib.redirects",
"wagtail.contrib.modeladmin", "wagtail.contrib.modeladmin",

View file

@ -8,7 +8,7 @@ from django.views.decorators.cache import cache_page
from django.views.generic import TemplateView from django.views.generic import TemplateView
from website.contact.models import ContactPage from website.contact.models import ContactPage
from website.contrib.singleton_url.utils import SingletonURLCache from website.contrib.singleton_page.utils import SingletonPageCache
class SecurityView(TemplateView): class SecurityView(TemplateView):
@ -24,7 +24,7 @@ class SecurityView(TemplateView):
def get_context_data(self, **kwargs: dict) -> dict: def get_context_data(self, **kwargs: dict) -> dict:
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["security_txt"] = self.request.build_absolute_uri(self.request.path) context["security_txt"] = self.request.build_absolute_uri(self.request.path)
context["contact_page_url"] = SingletonURLCache.get_url(ContactPage) context["contact_page_url"] = SingletonPageCache.get_url(ContactPage)
context["expires"] = ( context["expires"] = (
(timezone.now() + self.expires).replace(microsecond=0).isoformat() (timezone.now() + self.expires).replace(microsecond=0).isoformat()
) )