aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-09-21 19:58:53 +0300
committerGravatar ks129 <[email protected]>2020-09-21 19:58:53 +0300
commit593b6789a4867edb208b5004ffad70aa1612446a (patch)
tree41b6623b29c7d5ae6360d65d161725767c0e4d4c
parentUpdate `get_guides` function for better testability (diff)
Create new function `_get_base_path` for guides for better patching
-rw-r--r--pydis_site/apps/guides/utils.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/pydis_site/apps/guides/utils.py b/pydis_site/apps/guides/utils.py
index 1785fd2c..0220e586 100644
--- a/pydis_site/apps/guides/utils.py
+++ b/pydis_site/apps/guides/utils.py
@@ -7,9 +7,14 @@ from django.http import Http404
from markdown import Markdown
+def _get_base_path() -> str:
+ """Have extra function for base path getting for testability."""
+ return os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
+
+
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)
+ path = os.path.join(_get_base_path(), category)
if not os.path.exists(path) or not os.path.isdir(path):
raise Http404("Category not found.")
@@ -19,7 +24,7 @@ def get_category(category: str) -> Dict[str, str]:
def get_categories() -> Dict[str, Dict]:
"""Get all categories information."""
- base_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides")
+ base_path = _get_base_path()
categories = {}
for name in os.listdir(base_path):
@@ -32,9 +37,9 @@ def get_categories() -> Dict[str, Dict]:
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")
+ base_dir = _get_base_path()
else:
- base_dir = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
+ base_dir = os.path.join(_get_base_path(), category)
guides = {}
@@ -53,9 +58,9 @@ def get_guides(category: Optional[str] = None) -> Dict[str, Dict]:
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")
+ base_path = _get_base_path()
else:
- base_path = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "guides", "resources", "guides", category)
+ base_path = os.path.join(_get_base_path(), category)
if not os.path.exists(base_path) or not os.path.isdir(base_path):
raise Http404("Category not found.")