diff options
author | 2021-08-16 02:47:54 +0300 | |
---|---|---|
committer | 2021-08-16 02:50:25 +0300 | |
commit | acdd9e3aea882f4c08e40b46e8b6fa1d91c56874 (patch) | |
tree | 5080327ac0fdef075c79f587c9e6ed45a5db5563 | |
parent | Merge branch 'main' into pull/506 (diff) |
Adds Github Path Meta Taggithub-button
Adds a new meta tag to all templates "github-path", which contains the
relative path to the template.
Signed-off-by: Hassan Abouelela <[email protected]>
-rw-r--r-- | pydis_site/context_processors.py | 26 | ||||
-rw-r--r-- | pydis_site/settings.py | 3 | ||||
-rw-r--r-- | pydis_site/templates/base/base.html | 1 |
3 files changed, 28 insertions, 2 deletions
diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index 6937a3db..0befdf9d 100644 --- a/pydis_site/context_processors.py +++ b/pydis_site/context_processors.py @@ -1,8 +1,32 @@ +import inspect +from pathlib import Path + +from django.http import HttpRequest from django.template import RequestContext from pydis_site.constants import GIT_SHA -def git_sha_processor(_: RequestContext) -> dict: +def git_sha_processor(_: HttpRequest) -> dict: """Expose the git SHA for this repo to all views.""" return {'git_sha': GIT_SHA} + + +def path_processor(_: HttpRequest) -> dict: + """Expose each view's path to itself.""" + # This is a disgusting hack, here's how it works in detail: + # + # 1. We use inspect to pull all the local variables from the previous frame, and access self + # 1a. The previous frame is a django internal which calls context processors, + # and has access to the template + # 1b. The caller is a method in RequestContext, hence self is an instance of RequestContext + # + # 2. We use RequestContext.template to get the Template object, + # which has an absolute path to the template + # + # 3. We use pathlib to create a relative path to the root directory of the project + + context: RequestContext = inspect.currentframe().f_back.f_locals["self"] + + path = Path(context.template.origin.name).relative_to(Path.cwd()) + return {"path": path} diff --git a/pydis_site/settings.py b/pydis_site/settings.py index 7df7ad85..e87df94e 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -118,7 +118,8 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', - "pydis_site.context_processors.git_sha_processor" + "pydis_site.context_processors.git_sha_processor", + "pydis_site.context_processors.path_processor" ], }, }, diff --git a/pydis_site/templates/base/base.html b/pydis_site/templates/base/base.html index 906fc577..fdc97711 100644 --- a/pydis_site/templates/base/base.html +++ b/pydis_site/templates/base/base.html @@ -31,6 +31,7 @@ <link rel="stylesheet" href="{% static "css/base/base.css" %}"> {% block head %}{% endblock %} + <meta name="github-path" content="{{ path }}"/> </head> <body class="site"> <!-- Git hash for this release: {{ git_sha }} --> |