This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/articles/context_processors.py

54 lines
1.3 KiB
Python

from django.conf import settings
from articles.models import Article, Page
from attachments.models import Attachment
IGNORED_PATHS = [
"/robots.txt",
]
def pages(request):
if request.path in IGNORED_PATHS:
return {}
return {"pages": Page.objects.filter(status=Article.PUBLISHED).exclude(position=0)}
def drafts_count(request):
if request.path in IGNORED_PATHS:
return {}
return {"drafts_count": Article.objects.filter(status=Article.DRAFT).count()}
def date_format(request):
if request.path in IGNORED_PATHS:
return {}
return {"CUSTOM_ISO": r"Y-m-d\TH:i:sO"}
def git_version(request):
if request.path in IGNORED_PATHS:
return {}
try:
with open("/app/.version") as f:
version = f.read().strip()
url = settings.BLOG["repo"]["commit_url"].format(commit_sha=version)
version = version[:8]
except FileNotFoundError:
version = "latest"
url = settings.BLOG["repo"]["log"]
return {"git_version": version, "git_version_url": url}
def plausible(request):
return {"plausible_domain": settings.PLAUSIBLE_DOMAIN}
def open_graph_image_url(request):
open_graph_image = Attachment.objects.get_open_graph_image()
return {
"open_graph_image_url": open_graph_image.processed_file.get_full_absolute_url(
request
)
}