From b4e843ebc1fa35c457e59bfff252b252ef2ef77f Mon Sep 17 00:00:00 2001 From: Hedy Li Date: Fri, 19 Nov 2021 10:11:18 +0800 Subject: feat: Edit on GitHub button for content articles - Using `if pages` to check whether the page is an article or category doesn't work for /pages/guides - I still need to modify the styling and position of the button a bit - Should probably just use a static method and put src_url as context instead of having a template tag --- pydis_site/apps/content/templatetags/__init__.py | 0 pydis_site/apps/content/templatetags/page_src.py | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 pydis_site/apps/content/templatetags/__init__.py create mode 100644 pydis_site/apps/content/templatetags/page_src.py (limited to 'pydis_site/apps/content') diff --git a/pydis_site/apps/content/templatetags/__init__.py b/pydis_site/apps/content/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pydis_site/apps/content/templatetags/page_src.py b/pydis_site/apps/content/templatetags/page_src.py new file mode 100644 index 00000000..33f24b82 --- /dev/null +++ b/pydis_site/apps/content/templatetags/page_src.py @@ -0,0 +1,22 @@ +from django import template + + +register = template.Library() + + +@register.filter() +def page_src_url(request_path: str) -> str: + """ + Return the corresponding GitHub source URL for the current content article. + + request_path is the relative path of an article, as returned by `request.path` in templates. + + For example: /pages/rules/ would return: + https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/rules.md + """ + src_url = request_path.replace( + "/pages/", + "https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/", + ) + src_url = src_url[:-1] + ".md" + return src_url -- cgit v1.2.3 From a34ebfa67838acac9f2651d7eba7a7472a4bd4ec Mon Sep 17 00:00:00 2001 From: hedy Date: Sat, 29 Oct 2022 10:37:45 +0800 Subject: Use content articles source url in django.conf.settings --- pydis_site/apps/content/templatetags/page_src.py | 3 ++- pydis_site/settings.py | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'pydis_site/apps/content') diff --git a/pydis_site/apps/content/templatetags/page_src.py b/pydis_site/apps/content/templatetags/page_src.py index 33f24b82..3a8f1e8b 100644 --- a/pydis_site/apps/content/templatetags/page_src.py +++ b/pydis_site/apps/content/templatetags/page_src.py @@ -1,4 +1,5 @@ from django import template +from django.conf import settings register = template.Library() @@ -16,7 +17,7 @@ def page_src_url(request_path: str) -> str: """ src_url = request_path.replace( "/pages/", - "https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/", + settings.CONTENT_SRC_URL, ) src_url = src_url[:-1] + ".md" return src_url diff --git a/pydis_site/settings.py b/pydis_site/settings.py index e9e0ba67..9da9a156 100644 --- a/pydis_site/settings.py +++ b/pydis_site/settings.py @@ -332,3 +332,8 @@ REDIRECTIONS_PATH = Path(BASE_DIR, "pydis_site", "apps", "redirect", "redirects. # How long to wait for synchronous requests before timing out TIMEOUT_PERIOD = env("TIMEOUT_PERIOD") + +# Source files url for 'Edit on GitHub' link on content articles +CONTENT_SRC_URL = ( + "https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/" +) -- cgit v1.2.3 From e3dcb3e0e965a583c1cb2257207a481800966e1a Mon Sep 17 00:00:00 2001 From: hedy Date: Sat, 29 Oct 2022 10:40:34 +0800 Subject: Simplify line in page_src_url template filter --- pydis_site/apps/content/templatetags/page_src.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'pydis_site/apps/content') diff --git a/pydis_site/apps/content/templatetags/page_src.py b/pydis_site/apps/content/templatetags/page_src.py index 3a8f1e8b..261abbdf 100644 --- a/pydis_site/apps/content/templatetags/page_src.py +++ b/pydis_site/apps/content/templatetags/page_src.py @@ -15,9 +15,6 @@ def page_src_url(request_path: str) -> str: For example: /pages/rules/ would return: https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/rules.md """ - src_url = request_path.replace( - "/pages/", - settings.CONTENT_SRC_URL, - ) + src_url = request_path.replace("/pages/", settings.CONTENT_SRC_URL) src_url = src_url[:-1] + ".md" return src_url -- cgit v1.2.3 From 5489aeb3bdcda8cc2c9b1965dc182d431d11c23e Mon Sep 17 00:00:00 2001 From: hedy Date: Sat, 29 Oct 2022 10:43:26 +0800 Subject: Improve docstring for page_src_url template filter --- pydis_site/apps/content/templatetags/page_src.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'pydis_site/apps/content') diff --git a/pydis_site/apps/content/templatetags/page_src.py b/pydis_site/apps/content/templatetags/page_src.py index 261abbdf..143c420c 100644 --- a/pydis_site/apps/content/templatetags/page_src.py +++ b/pydis_site/apps/content/templatetags/page_src.py @@ -12,6 +12,11 @@ def page_src_url(request_path: str) -> str: request_path is the relative path of an article, as returned by `request.path` in templates. + GitHub source URL is set in settings.py as CONTENT_SRC_URL, prefix for the + url which the request path would be appended to. + + Assumes '.md' file extension for the page source files. + For example: /pages/rules/ would return: https://github.com/python-discord/site/tree/main/pydis_site/apps/content/resources/rules.md """ -- cgit v1.2.3