aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/guides/views
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-09-21 16:10:56 +0300
committerGravatar ks129 <[email protected]>2020-09-21 16:10:56 +0300
commit27315dacdc76e07ee910e31fe65c9357de34bcaf (patch)
tree29df0e6026f5c3de1029d8f46d23045b0098c1c4 /pydis_site/apps/guides/views
parentAdd unit tests for guides app (diff)
Move guides and categories getting logic to utils for better testability
Diffstat (limited to 'pydis_site/apps/guides/views')
-rw-r--r--pydis_site/apps/guides/views/category.py31
-rw-r--r--pydis_site/apps/guides/views/guide.py51
-rw-r--r--pydis_site/apps/guides/views/guides.py30
3 files changed, 27 insertions, 85 deletions
diff --git a/pydis_site/apps/guides/views/category.py b/pydis_site/apps/guides/views/category.py
index e51b3fed..33e8c97b 100644
--- a/pydis_site/apps/guides/views/category.py
+++ b/pydis_site/apps/guides/views/category.py
@@ -1,12 +1,9 @@
-import os
-
-import yaml
-from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
-from django.http import HttpResponse, Http404
+from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
-from markdown import Markdown
+
+from pydis_site.apps.guides.utils import get_category, get_guides
class CategoryView(View):
@@ -14,28 +11,8 @@ class CategoryView(View):
def get(self, request: WSGIRequest, category: str) -> HttpResponse:
"""Handles page that displays category guides."""
- path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
- if not os.path.exists(path) or not os.path.isdir(path):
- raise Http404("Category not found.")
-
- with open(os.path.join(path, "_info.yml")) as f:
- category_info = yaml.load(f.read())
-
- guides = {}
-
- for filename in os.listdir(path):
- if filename.endswith(".md"):
- md = Markdown(extensions=["meta"])
- with open(os.path.join(path, filename)) as f:
- md.convert(f.read())
-
- guides[os.path.splitext(filename)[0]] = {
- "title": md.Meta["title"],
- "short_description": md.Meta["shortdescription"]
- }
-
return render(
request,
"guides/category.html",
- {"category_info": category_info, "guides": guides, "category_name": category}
+ {"category_info": get_category(category), "guides": get_guides(category), "category_name": category}
)
diff --git a/pydis_site/apps/guides/views/guide.py b/pydis_site/apps/guides/views/guide.py
index deb1b8c4..bcd68bc4 100644
--- a/pydis_site/apps/guides/views/guide.py
+++ b/pydis_site/apps/guides/views/guide.py
@@ -2,13 +2,13 @@ import os
from datetime import datetime
from typing import Optional
-import yaml
from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
-from django.http import HttpResponse, Http404
+from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
-from markdown import Markdown
+
+from pydis_site.apps.guides.utils import get_category, get_guide
class GuideView(View):
@@ -16,44 +16,33 @@ class GuideView(View):
def get(self, request: WSGIRequest, guide: str, category: Optional[str] = None) -> HttpResponse:
"""Collect guide content and display it. When guide don't exist, return 404."""
- if category is None:
- path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", f"{guide}.md")
- category_name = None
- else:
- dir_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
- if not os.path.exists(dir_path) or not os.path.isdir(dir_path):
- raise Http404("Category not found.")
-
- path = os.path.join(dir_path, f"{guide}.md")
- with open(os.path.join(dir_path, "_info.yml")) as f:
- category_name = yaml.load(f.read())["name"]
-
- if not os.path.exists(path) or not os.path.isfile(path):
- raise Http404(f"Guide not found")
+ guide_result = get_guide(guide, category)
- md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
- with open(path) as f:
- html = md.convert(f.read())
- f.close()
+ if category is not None:
+ path = os.path.join(
+ settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category, f"{guide}.md"
+ )
+ else:
+ path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", f"{guide}.md")
- category_data = {
- "title": category_name,
- "name": category,
- }
+ if category is not None:
+ category_data = get_category(category)
+ category_data["raw_name"] = category
+ else:
+ category_data = {"name": None, "raw_name": None}
return render(
request,
"guides/guide.html",
{
- "guide": html,
- "metadata": md.Meta,
+ "guide": guide_result,
"last_modified": datetime.fromtimestamp(os.path.getmtime(path)).strftime("%dth %B %Y"),
+ "category_data": category_data,
"relevant_links": {
link: value for link, value in zip(
- md.Meta.get("relevantlinks", []),
- md.Meta.get("relevantlinkvalues", [])
+ guide_result["metadata"].get("relevantlinks", []),
+ guide_result["metadata"].get("relevantlinkvalues", [])
)
- },
- "category_data": category_data,
+ }
}
)
diff --git a/pydis_site/apps/guides/views/guides.py b/pydis_site/apps/guides/views/guides.py
index f457adc1..bb8b565e 100644
--- a/pydis_site/apps/guides/views/guides.py
+++ b/pydis_site/apps/guides/views/guides.py
@@ -1,12 +1,9 @@
-import os
-
-import yaml
-from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
from django.shortcuts import render
from django.views import View
-from markdown import Markdown
+
+from pydis_site.apps.guides.utils import get_categories, get_guides
class GuidesView(View):
@@ -14,25 +11,4 @@ class GuidesView(View):
def get(self, request: WSGIRequest) -> HttpResponse:
"""Shows all guides and categories."""
- guides = {}
- categories = {}
-
- guides_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
- for name in os.listdir(guides_path):
- full_path = os.path.join(guides_path, name)
- if os.path.isdir(full_path):
- with open(os.path.join(full_path, "_info.yml")) as f:
- category = yaml.load(f.read())
-
- categories[name] = {"name": category["name"], "description": category["description"]}
- elif os.path.isfile(full_path) and name.endswith(".md"):
- md = Markdown(extensions=['meta'])
- with open(full_path) as f:
- md.convert(f.read())
-
- guides[os.path.splitext(name)[0]] = {
- "name": md.Meta["title"],
- "short_description": md.Meta["shortdescription"]
- }
-
- return render(request, "guides/guides.html", {"guides": guides, "categories": categories})
+ return render(request, "guides/guides.html", {"guides": get_guides(), "categories": get_categories()})