diff --git a/feeds/context_processors.py b/feeds/context_processors.py new file mode 100644 index 0000000..9f5860d --- /dev/null +++ b/feeds/context_processors.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from django.http import HttpRequest + + +def add_global_context(request: HttpRequest) -> dict[str, str | int]: # noqa: ARG001 + """Add global context to all templates. + + Args: + request: The request object. + + Returns: + A dictionary with the global context. + """ + from feeds.stats import get_db_size # noqa: PLC0415 + + from .models import Feed # noqa: PLC0415 + + db_size: str = get_db_size() + amount_of_feeds: int = Feed.objects.count() + return { + "db_size": db_size, + "amount_of_feeds": amount_of_feeds, + } diff --git a/feeds/views.py b/feeds/views.py index 176a063..11a3120 100644 --- a/feeds/views.py +++ b/feeds/views.py @@ -17,7 +17,6 @@ from django.views.generic.list import ListView from feeds.add_feeds import add_feed from feeds.models import Entry, Feed -from feeds.stats import get_db_size if TYPE_CHECKING: from django.contrib.auth.models import User @@ -30,8 +29,6 @@ class IndexView(View): """Load the index page.""" template = loader.get_template(template_name="index.html") context = { - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": "FeedVault allows users to archive and search their favorite web feeds.", "keywords": "feed, rss, atom, archive, rss list", "author": "TheLovinator", @@ -55,8 +52,6 @@ class FeedView(View): context = { "feed": feed, "entries": entries, - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": f"Archive of {feed.href}", "keywords": "feed, rss, atom, archive, rss list", "author": f"{feed.author_detail.name if feed.author_detail else "FeedVault"}", @@ -77,8 +72,6 @@ class FeedsView(ListView): def get_context_data(self, **kwargs) -> dict: # noqa: ANN003 """Get the context data.""" context = super().get_context_data(**kwargs) - context["db_size"] = get_db_size() - context["amount_of_feeds"] = Feed.objects.count() context["description"] = "Archive of all feeds" context["keywords"] = "feed, rss, atom, archive, rss list" context["author"] = "TheLovinator" @@ -93,8 +86,6 @@ class AddView(View): """Load the index page.""" template = loader.get_template(template_name="index.html") context = { - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": "FeedVault allows users to archive and search their favorite web feeds.", "keywords": "feed, rss, atom, archive, rss list", "author": "TheLovinator", @@ -132,8 +123,6 @@ class UploadView(View): """Load the index page.""" template = loader.get_template(template_name="index.html") context = { - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": "FeedVault allows users to archive and search their favorite web feeds.", "keywords": "feed, rss, atom, archive, rss list", "author": "TheLovinator", @@ -205,8 +194,6 @@ class ProfileView(View): """Load the profile page.""" template = loader.get_template(template_name="accounts/profile.html") context = { - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": "FeedVault allows users to archive and search their favorite web feeds.", "keywords": "feed, rss, atom, archive, rss list", "author": "TheLovinator", @@ -222,8 +209,6 @@ class APIView(View): """Load the API page.""" template = loader.get_template(template_name="api.html") context = { - "db_size": get_db_size(), - "amount_of_feeds": Feed.objects.count(), "description": "FeedVault allows users to archive and search their favorite web feeds.", "keywords": "feed, rss, atom, archive, rss list", "author": "TheLovinator", diff --git a/feedvault/settings.py b/feedvault/settings.py index e5f002f..828763e 100644 --- a/feedvault/settings.py +++ b/feedvault/settings.py @@ -40,21 +40,6 @@ MIDDLEWARE = [ ROOT_URLCONF = "feedvault.urls" -TEMPLATES = [ - { - "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [], - "APP_DIRS": True, - "OPTIONS": { - "context_processors": [ - "django.template.context_processors.debug", - "django.template.context_processors.request", - "django.contrib.auth.context_processors.auth", - "django.contrib.messages.context_processors.messages", - ], - }, - }, -] WSGI_APPLICATION = "feedvault.wsgi.application" @@ -103,6 +88,7 @@ TEMPLATES = [ "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", + "feeds.context_processors.add_global_context", ], "loaders": [ (