aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/views
diff options
context:
space:
mode:
authorGravatar Hassan Abouelela <[email protected]>2022-08-14 05:34:27 +0200
committerGravatar Hassan Abouelela <[email protected]>2022-08-14 05:46:36 +0200
commit45cdb27a82297ede18d7bd908213dde54fef06a9 (patch)
treeb31c1aa6fa7d9676837f7dd8488b5e5a8eed6b4f /pydis_site/apps/content/views
parentMove Tag URL To Property And Add Group (diff)
Add Tag Group Support
Adds support for tag groups in content. This involves some modification to the routing, and templating. Signed-off-by: Hassan Abouelela <[email protected]>
Diffstat (limited to 'pydis_site/apps/content/views')
-rw-r--r--pydis_site/apps/content/views/page_category.py5
-rw-r--r--pydis_site/apps/content/views/tags.py79
2 files changed, 68 insertions, 16 deletions
diff --git a/pydis_site/apps/content/views/page_category.py b/pydis_site/apps/content/views/page_category.py
index 01ce8402..062c2bc1 100644
--- a/pydis_site/apps/content/views/page_category.py
+++ b/pydis_site/apps/content/views/page_category.py
@@ -5,7 +5,7 @@ from django.conf import settings
from django.http import Http404, HttpRequest, HttpResponse
from django.views.generic import TemplateView
-from pydis_site.apps.content import utils
+from pydis_site.apps.content import models, utils
class PageOrCategoryView(TemplateView):
@@ -91,4 +91,7 @@ class PageOrCategoryView(TemplateView):
"page_title": category["title"],
"page_description": category["description"],
"icon": category.get("icon"),
+ "app_name": "content:page_category",
+ "is_tag_listing": "/resources/tags" in path.as_posix(),
+ "tag_url": models.Tag.URL_BASE,
}
diff --git a/pydis_site/apps/content/views/tags.py b/pydis_site/apps/content/views/tags.py
index 5295537d..a8df65db 100644
--- a/pydis_site/apps/content/views/tags.py
+++ b/pydis_site/apps/content/views/tags.py
@@ -1,4 +1,5 @@
import re
+import typing
import frontmatter
import markdown
@@ -16,23 +17,65 @@ COMMAND_REGEX = re.compile(r"`*!tags? (?P<name>[\w\d-]+)`*")
class TagView(TemplateView):
"""Handles tag pages."""
- template_name = "content/tag.html"
+ tag: typing.Union[Tag, list[Tag]]
+ is_group: bool
+
+ def setup(self, *args, **kwargs) -> None:
+ """Look for a tag, and configure the view."""
+ super().setup(*args, **kwargs)
- def get_context_data(self, **kwargs) -> dict:
- """Get the relevant context for this tag page."""
try:
- tag = utils.get_tag(kwargs.get("name"))
+ self.tag = utils.get_tag(kwargs.get("location"))
+ self.is_group = isinstance(self.tag, list)
except Tag.DoesNotExist:
raise Http404
+ def get_template_names(self) -> list[str]:
+ """Either return the tag page template, or the listing."""
+ if self.is_group:
+ template_name = "content/listing.html"
+ else:
+ template_name = "content/tag.html"
+
+ return [template_name]
+
+ def get_context_data(self, **kwargs) -> dict:
+ """Get the relevant context for this tag page or group."""
context = super().get_context_data(**kwargs)
- context["page_title"] = tag.name
+ context["breadcrumb_items"] = [{
+ "name": utils.get_category(settings.CONTENT_PAGES_PATH / location)["title"],
+ "path": location,
+ } for location in (".", "tags")]
+
+ if self.is_group:
+ self._set_group_context(context, self.tag)
+ else:
+ self._set_tag_context(context, self.tag)
+
+ return context
+
+ @staticmethod
+ def _set_tag_context(context: dict[str, any], tag: Tag) -> None:
+ """Update the context with the information for a tag page."""
+ context.update({
+ "page_title": tag.name,
+ "tag": tag,
+ })
+
+ if tag.group:
+ # Add group names to the breadcrumbs
+ context["breadcrumb_items"].append({
+ "name": tag.group,
+ "path": f"tags/{tag.group}",
+ })
+
+ # Clean up tag body
body = frontmatter.parse(tag.body)
content = body[1]
# Check for tags which can be hyperlinked
def sub(match: re.Match) -> str:
- link = reverse("content:tag", kwargs={"name": match.group("name")})
+ link = reverse("content:tag", kwargs={"location": match.group("name")})
return f"[{match.group()}]({link})"
content = COMMAND_REGEX.sub(sub, content)
@@ -42,14 +85,20 @@ class TagView(TemplateView):
if image := embed.get("image"):
content = f"![{embed['title']}]({image['url']})\n\n" + content
+ # Insert the content
+ context["page"] = markdown.markdown(content, extensions=["pymdownx.superfences"])
+
+ @staticmethod
+ def _set_group_context(context: dict[str, any], tags: list[Tag]) -> None:
+ """Update the context with the information for a group of tags."""
+ group = tags[0].group
context.update({
- "page": markdown.markdown(content, extensions=["pymdownx.superfences"]),
- "tag": tag,
+ "categories": {},
+ "pages": utils.get_tag_category(tags, collapse_groups=False),
+ "page_title": group,
+ "icon": "fab fa-tags",
+ "is_tag_listing": True,
+ "app_name": "content:tag",
+ "path": f"{group}/",
+ "tag_url": f"{tags[0].URL_BASE}/{group}"
})
-
- context["breadcrumb_items"] = [{
- "name": utils.get_category(settings.CONTENT_PAGES_PATH / location)["title"],
- "path": str(location)
- } for location in [".", "tags"]]
-
- return context