aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pydis_site/apps/content/tests/test_utils.py19
-rw-r--r--pydis_site/apps/content/utils.py42
-rw-r--r--pydis_site/apps/content/views/article.py5
3 files changed, 29 insertions, 37 deletions
diff --git a/pydis_site/apps/content/tests/test_utils.py b/pydis_site/apps/content/tests/test_utils.py
index 82e1ac5f..84007b27 100644
--- a/pydis_site/apps/content/tests/test_utils.py
+++ b/pydis_site/apps/content/tests/test_utils.py
@@ -1,4 +1,5 @@
import os
+from pathlib import Path
from unittest.mock import patch
from django.conf import settings
@@ -8,7 +9,7 @@ from markdown import Markdown
from pydis_site.apps.content import utils
-BASE_PATH = os.path.join(settings.BASE_DIR, "pydis_site", "apps", "content", "tests", "test_content")
+BASE_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "content", "tests", "test_content")
class TestGetBasePath(TestCase):
@@ -16,7 +17,7 @@ class TestGetBasePath(TestCase):
"""Test does function return content base path."""
self.assertEqual(
utils._get_base_path(),
- os.path.join(settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content")
+ Path(settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content")
)
@@ -59,8 +60,7 @@ class TestGetArticles(TestCase):
for case in ["test", "test2"]:
with self.subTest(guide=case):
md = Markdown(extensions=['meta'])
- with open(os.path.join(BASE_PATH, f"{case}.md")) as f:
- md.convert(f.read())
+ md.convert(BASE_PATH.joinpath(f"{case}.md").read_text())
self.assertIn(case, result)
self.assertEqual(md.Meta, result[case])
@@ -71,8 +71,7 @@ class TestGetArticles(TestCase):
result = utils.get_articles("category")
md = Markdown(extensions=['meta'])
- with open(os.path.join(BASE_PATH, "category", "test3.md")) as f:
- md.convert(f.read())
+ md.convert(BASE_PATH.joinpath("category", "test3.md").read_text())
self.assertIn("test3", result)
self.assertEqual(md.Meta, result["test3"])
@@ -85,9 +84,7 @@ class TestGetArticle(TestCase):
result = utils.get_article("test", None)
md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
-
- with open(os.path.join(BASE_PATH, "test.md")) as f:
- html = md.convert(f.read())
+ html = md.convert(BASE_PATH.joinpath("test.md").read_text())
self.assertEqual(result, {"article": html, "metadata": md.Meta})
@@ -103,9 +100,7 @@ class TestGetArticle(TestCase):
result = utils.get_article("test3", "category")
md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
-
- with open(os.path.join(BASE_PATH, "category", "test3.md")) as f:
- html = md.convert(f.read())
+ html = md.convert(BASE_PATH.joinpath("category", "test3.md").read_text())
self.assertEqual(result, {"article": html, "metadata": md.Meta})
diff --git a/pydis_site/apps/content/utils.py b/pydis_site/apps/content/utils.py
index 57905a69..32c750c3 100644
--- a/pydis_site/apps/content/utils.py
+++ b/pydis_site/apps/content/utils.py
@@ -1,4 +1,5 @@
import os
+from pathlib import Path
from typing import Dict, Optional, Union
import yaml
@@ -7,19 +8,18 @@ from django.http import Http404
from markdown import Markdown
-def _get_base_path() -> str:
+def _get_base_path() -> Path:
"""Have extra function for base path getting for testability."""
- return os.path.join(settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content")
+ return Path(settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content")
def get_category(category: str) -> Dict[str, str]:
"""Load category information by name from _info.yml."""
- path = os.path.join(_get_base_path(), category)
- if not os.path.exists(path) or not os.path.isdir(path):
+ path = _get_base_path().joinpath(category)
+ if not path.exists() or not path.is_dir():
raise Http404("Category not found.")
- with open(os.path.join(path, "_info.yml")) as f:
- return yaml.safe_load(f.read())
+ return yaml.safe_load(path.joinpath("_info.yml").read_text())
def get_categories() -> Dict[str, Dict]:
@@ -27,9 +27,9 @@ def get_categories() -> Dict[str, Dict]:
base_path = _get_base_path()
categories = {}
- for name in os.listdir(base_path):
- if os.path.isdir(os.path.join(base_path, name)):
- categories[name] = get_category(name)
+ for name in base_path.iterdir():
+ if name.is_dir():
+ categories[name.name] = get_category(name.name)
return categories
@@ -39,18 +39,16 @@ def get_articles(category: Optional[str] = None) -> Dict[str, Dict]:
if category is None:
base_dir = _get_base_path()
else:
- base_dir = os.path.join(_get_base_path(), category)
+ base_dir = _get_base_path().joinpath(category)
articles = {}
- for filename in os.listdir(base_dir):
- full_path = os.path.join(base_dir, filename)
- if os.path.isfile(full_path) and filename.endswith(".md"):
+ for item in base_dir.iterdir():
+ if item.is_file() and item.name.endswith(".md"):
md = Markdown(extensions=['meta'])
- with open(full_path) as f:
- md.convert(f.read())
+ md.convert(item.read_text())
- articles[os.path.splitext(filename)[0]] = md.Meta
+ articles[os.path.splitext(item.name)[0]] = md.Meta
return articles
@@ -60,18 +58,16 @@ def get_article(article: str, category: Optional[str]) -> Dict[str, Union[str, D
if category is None:
base_path = _get_base_path()
else:
- base_path = os.path.join(_get_base_path(), category)
+ base_path = _get_base_path().joinpath(category)
- if not os.path.exists(base_path) or not os.path.isdir(base_path):
+ if not base_path.exists() or not base_path.is_dir():
raise Http404("Category not found.")
- article_path = os.path.join(base_path, f"{article}.md")
- if not os.path.exists(article_path) or not os.path.isfile(article_path):
+ article_path = base_path.joinpath(f"{article}.md")
+ if not article_path.exists() or not article_path.is_file():
raise Http404("Article not found.")
md = Markdown(extensions=['meta', 'attr_list', 'fenced_code'])
-
- with open(article_path) as f:
- html = md.convert(f.read())
+ html = md.convert(article_path.read_text())
return {"article": html, "metadata": md.Meta}
diff --git a/pydis_site/apps/content/views/article.py b/pydis_site/apps/content/views/article.py
index b34ca3ee..34404719 100644
--- a/pydis_site/apps/content/views/article.py
+++ b/pydis_site/apps/content/views/article.py
@@ -1,5 +1,6 @@
import os
from datetime import datetime
+from pathlib import Path
from typing import Optional
from django.conf import settings
@@ -19,11 +20,11 @@ class ArticleView(View):
article_result = get_article(article, category)
if category is not None:
- path = os.path.join(
+ path = Path(
settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content", category, f"{article}.md"
)
else:
- path = os.path.join(
+ path = Path(
settings.BASE_DIR, "pydis_site", "apps", "content", "resources", "content", f"{article}.md"
)