aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/content/views
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-11-28 09:59:13 +0200
committerGravatar ks129 <[email protected]>2020-11-28 09:59:13 +0200
commit8519a11d9f44bbf95e4c4621e720f5c92d804295 (patch)
treef6aaa4d4969589bea492d4b60df9dc6537795f2a /pydis_site/apps/content/views
parentMove articles writing guide to PyDis guides directory (diff)
Create view for showing articles and categories
Diffstat (limited to 'pydis_site/apps/content/views')
-rw-r--r--pydis_site/apps/content/views/article_category.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/pydis_site/apps/content/views/article_category.py b/pydis_site/apps/content/views/article_category.py
new file mode 100644
index 00000000..0c22b5e8
--- /dev/null
+++ b/pydis_site/apps/content/views/article_category.py
@@ -0,0 +1,75 @@
+import typing as t
+
+from django.conf import settings
+from django.http import Http404
+from django.views.generic import TemplateView
+
+from pydis_site.apps.content import utils
+
+
+class ArticleOrCategoryView(TemplateView):
+ """Handles article and category pages."""
+
+ def get_template_names(self) -> t.List[str]:
+ """Checks does this use article template or listing template."""
+ location = self.kwargs["location"].split("/")
+ full_location = settings.ARTICLES_PATH.joinpath(*location)
+
+ if full_location.is_dir():
+ template_name = "content/listing.html"
+ elif full_location.with_suffix(".md").is_file():
+ template_name = "content/article.html"
+ else:
+ raise Http404
+
+ return [template_name]
+
+ def get_context_data(self, **kwargs) -> t.Dict[str, t.Any]:
+ """Assign proper context variables based on what resource user requests."""
+ context = super().get_context_data(**kwargs)
+
+ location: list = self.kwargs["location"].split("/")
+ full_location = settings.ARTICLES_PATH.joinpath(*location)
+
+ if full_location.is_dir():
+ context["category_info"] = utils.get_category(location)
+ context["content"] = utils.get_articles(location)
+ context["categories"] = utils.get_categories(location)
+ # Add trailing slash here to simplify template
+ context["path"] = "/".join(location) + "/"
+ context["in_category"] = True
+ elif full_location.with_suffix(".md").is_file():
+ article_result = utils.get_article(location)
+
+ if len(location) > 1:
+ context["category_data"] = utils.get_category(location[:-1])
+ context["category_data"]["raw_name"] = location[:-1][-1]
+ else:
+ context["category_data"] = {"name": None, "raw_name": None}
+
+ context["article"] = article_result
+ context["relevant_links"] = {
+ link: value for link, value in zip(
+ article_result["metadata"].get("relevant_links", "").split(","),
+ article_result["metadata"].get("relevant_link_values", "").split(",")
+ ) if link != "" and value != ""
+ }
+ context["github_data"] = utils.get_github_information(location)
+ else:
+ raise Http404
+
+ location.pop()
+ breadcrumb_items = []
+ while len(location):
+ breadcrumb_items.insert(
+ 0,
+ {
+ "name": utils.get_category(location)["name"],
+ "path": "/".join(location)
+ }
+ )
+ location.pop()
+
+ context["breadcrumb_items"] = breadcrumb_items
+
+ return context