diff --git a/pydis_site/apps/content/templatetags/__init__.py b/pydis_site/apps/content/templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pydis_site/apps/content/templatetags/page_github.py b/pydis_site/apps/content/templatetags/page_github.py new file mode 100644 index 000000000..7a4356fe7 --- /dev/null +++ b/pydis_site/apps/content/templatetags/page_github.py @@ -0,0 +1,21 @@ +import requests +from django import template + +register = template.Library() + + +@register.filter() +def page_github(current_uri: str) -> str: + """ + Return the corresponding GitHub page for the current site page. + + Return a directory if the page is a directory on GitHub otherwise return the Markdown file + """ + github_uri = current_uri.replace( + "http://pythondiscord.local:8000/pages/", + "https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/" + ) + github_uri = github_uri[:-1] if github_uri.endswith("/") else github_uri + if requests.get(github_uri).status_code == 404: + github_uri += ".md" + return github_uri diff --git a/pydis_site/context_processors.py b/pydis_site/context_processors.py index 6937a3db6..0befdf9da 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 7df7ad854..e87df94ec 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -118,7 +118,8 @@ '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 906fc577e..fdc97711c 100644 --- a/pydis_site/templates/base/base.html +++ b/pydis_site/templates/base/base.html @@ -31,6 +31,7 @@ {% block head %}{% endblock %} + diff --git a/pydis_site/templates/content/base.html b/pydis_site/templates/content/base.html index 218954798..813702220 100644 --- a/pydis_site/templates/content/base.html +++ b/pydis_site/templates/content/base.html @@ -1,5 +1,6 @@ {% extends 'base/base.html' %} {% load static %} +{% load page_github %} {% block title %}{{ page_title }}{% endblock %} {% block head %} @@ -33,7 +34,17 @@
-

{{ page_title }}

+
+

{{ page_title }}

+ {# TODO: Bikeshed the button design #} + +
{% block page_content %}{% endblock %}
diff --git a/pydis_site/templates/home/index.html b/pydis_site/templates/home/index.html index 072e3817e..982fba718 100644 --- a/pydis_site/templates/home/index.html +++ b/pydis_site/templates/home/index.html @@ -99,8 +99,8 @@

Who are we?

- - + +