aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/guides/utils.py
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/utils.py
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/utils.py')
-rw-r--r--pydis_site/apps/guides/utils.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/pydis_site/apps/guides/utils.py b/pydis_site/apps/guides/utils.py
new file mode 100644
index 00000000..c7d03dc3
--- /dev/null
+++ b/pydis_site/apps/guides/utils.py
@@ -0,0 +1,71 @@
+import os
+from typing import Dict, Optional, Union
+
+import yaml
+from django.conf import settings
+from django.http import Http404
+from markdown import Markdown
+
+
+def get_category(category: str) -> Dict[str, str]:
+ """Load category information by name from _info.yml."""
+ 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:
+ return yaml.load(f.read())
+
+
+def get_categories() -> Dict[str, Dict]:
+ """Get all categories information."""
+ base_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
+ categories = {}
+
+ for name in os.listdir(base_path):
+ if os.path.isdir(os.path.join(base_path, name)):
+ categories[name] = get_category(name)
+
+ return categories
+
+
+def get_guides(category: Optional[str] = None) -> Dict[str, Dict]:
+ """Get all root guides when category is not specified. Otherwise get all this category guides."""
+ if category is None:
+ base_dir = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
+ else:
+ base_dir = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
+
+ guides = {}
+
+ for filename in os.listdir(base_dir):
+ if os.path.isfile(os.path.join(base_dir, filename)) and filename.endswith(".md"):
+ md = Markdown(extensions=['meta'])
+ with open(os.path.join(base_dir, filename)) as f:
+ md.convert(f.read())
+
+ guides[os.path.splitext(filename)[0]] = md.Meta
+
+ return guides
+
+
+def get_guide(guide: str, category: Optional[str]) -> Dict[str, Union[str, Dict]]:
+ """Get one specific guide. When category is specified, get it from there."""
+ if category is None:
+ base_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
+ else:
+ base_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
+
+ if not os.path.exists(base_path) or not os.path.isdir(base_path):
+ raise Http404("Category not found.")
+
+ guide_path = os.path.join(base_path, f"{guide}.md")
+ if not os.path.exists(guide_path) or not os.path.isfile(guide_path):
+ raise Http404("Guide not found.")
+
+ md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
+
+ with open(guide_path) as f:
+ html = md.convert(f.read())
+
+ return {"guide": html, "metadata": md.Meta}