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/src/articles/context_processors.py

65 lines
1.8 KiB
Python
Raw Normal View History

import copy
2023-01-30 20:58:18 +01:00
from pathlib import Path
2021-12-28 22:31:53 +01:00
from typing import Any
2020-09-12 17:02:29 +02:00
from django.conf import settings
2022-09-23 21:47:19 +02:00
from django.core.handlers.wsgi import WSGIRequest
2020-09-12 17:02:29 +02:00
from articles.models import Article
2020-11-28 20:09:37 +01:00
from attachments.models import Attachment
2020-08-17 09:57:24 +02:00
2020-09-05 09:04:45 +02:00
IGNORED_PATHS = [
"/robots.txt",
]
2020-08-17 09:57:24 +02:00
2022-09-23 21:47:19 +02:00
def drafts_count(request: WSGIRequest) -> dict[str, Any]:
2020-09-05 09:04:45 +02:00
if request.path in IGNORED_PATHS:
return {}
if not request.user.is_authenticated:
return {}
return {"drafts_count": Article.objects.filter(status=Article.DRAFT).count()}
2022-09-23 21:47:19 +02:00
def date_format(request: WSGIRequest) -> dict[str, Any]:
2020-09-05 09:04:45 +02:00
if request.path in IGNORED_PATHS:
return {}
2020-12-06 16:51:49 +01:00
return {"CUSTOM_ISO": r"Y-m-d\TH:i:sO", "ISO_DATE": "Y-m-d"}
2020-09-09 18:32:46 +02:00
2022-09-23 21:47:19 +02:00
def git_version(request: WSGIRequest) -> dict[str, Any]:
2020-09-09 18:32:46 +02:00
if request.path in IGNORED_PATHS:
return {}
try:
2023-01-30 20:58:18 +01:00
with Path("/app/git/git-commit").open() as f:
2020-09-12 17:02:29 +02:00
version = f.read().strip()
url = settings.BLOG["repo"]["commit_url"].format(commit_sha=version)
version = version[:8]
2020-09-09 18:32:46 +02:00
except FileNotFoundError:
version = "latest"
2020-09-12 17:02:29 +02:00
url = settings.BLOG["repo"]["log"]
return {"git_version": version, "git_version_url": url}
2023-01-30 20:58:18 +01:00
def analytics(_: WSGIRequest) -> dict[str, Any]:
2021-01-04 21:32:03 +01:00
return {
"goatcounter_domain": settings.GOATCOUNTER_DOMAIN,
}
2020-11-28 20:09:37 +01:00
2022-09-23 21:47:19 +02:00
def open_graph_image_url(request: WSGIRequest) -> dict[str, Any]:
2020-11-28 20:29:55 +01:00
if request.path in IGNORED_PATHS:
return {}
open_graph_image = Attachment.objects.get_open_graph_image()
2020-11-28 20:29:55 +01:00
url = ""
2021-12-31 12:29:13 +01:00
if open_graph_image and open_graph_image.processed_file is not None:
2020-11-28 20:29:55 +01:00
url = open_graph_image.processed_file.get_full_absolute_url(request)
return {"open_graph_image_url": url}
2023-01-30 20:58:18 +01:00
def blog_metadata(_request: WSGIRequest) -> dict[str, Any]:
blog_settings = copy.deepcopy(settings.BLOG)
2021-12-28 22:31:53 +01:00
return {
"blog": blog_settings,
2021-12-28 22:31:53 +01:00
}