diff options
-rw-r--r-- | pydis_site/apps/resources/utils.py | 42 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources_list.py | 38 |
2 files changed, 52 insertions, 28 deletions
diff --git a/pydis_site/apps/resources/utils.py b/pydis_site/apps/resources/utils.py new file mode 100644 index 00000000..1855fc80 --- /dev/null +++ b/pydis_site/apps/resources/utils.py @@ -0,0 +1,42 @@ +import typing as t +from pathlib import Path + +import yaml + + +def get_resources(path: Path) -> t.List[t.Dict]: + """Loads resource YAMLs from provided path.""" + resources = [] + + for item in path.iterdir(): + if item.is_file() and item.suffix == ".yaml" and item.name != "_category_info.yaml": + resources.append(yaml.safe_load(item.read_text())) + + return resources + + +def get_subcategories(path: Path) -> t.List[t.Dict]: + """Loads resources subcategories with their resources by provided path.""" + subcategories = [] + + for item in path.iterdir(): + if item.is_dir() and item.joinpath("_category_info.yaml").exists(): + subcategories.append({ + "category_info": { + **yaml.safe_load( + item.joinpath("_category_info.yaml").read_text() + ), + "raw_name": item.name + }, + "resources": [ + yaml.safe_load(subitem.read_text()) + for subitem in item.iterdir() + if ( + subitem.is_file() + and subitem.suffix == ".yaml" + and subitem.name != "_category_info.yaml" + ) + ] + }) + + return subcategories diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py index bcb7ea67..b09793d7 100644 --- a/pydis_site/apps/resources/views/resources_list.py +++ b/pydis_site/apps/resources/views/resources_list.py @@ -6,6 +6,8 @@ from django.conf import settings from django.http import Http404 from django.views.generic import TemplateView +from pydis_site.apps.resources.utils import get_resources, get_subcategories + RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources") @@ -25,33 +27,13 @@ class ResourcesListView(TemplateView): ): raise Http404 - resources = [] - subcategories = [] - for item in resource_path.iterdir(): - if item.is_file() and item.suffix == ".yaml" and item.name != "_category_info.yaml": - resources.append(yaml.safe_load(item.read_text())) - elif item.is_dir() and item.joinpath("_category_info.yaml").exists(): - subcategories.append({ - "category_info": {**yaml.safe_load( - item.joinpath("_category_info.yaml").read_text() - ), "raw_name": item.name}, - "resources": sorted([ - yaml.safe_load(subitem.read_text()) - for subitem in item.iterdir() - if ( - subitem.is_file() - and subitem.suffix == ".yaml" - and subitem.name != "_category_info.yaml" - ) - ], key=lambda k: k.get('position', 100)) - }) - - context["resources"] = sorted(resources, key=lambda k: k.get('position', 100)) - context["subcategories"] = sorted( - subcategories, key=lambda k: k.get('category_info', {}).get('position', 100) - ) - context["category_info"] = {**yaml.safe_load( - resource_path.joinpath("_category_info.yaml").read_text() - ), "raw_name": resource_path.name} + context["resources"] = get_resources(resource_path) + context["subcategories"] = get_subcategories(resource_path) + context["category_info"] = { + **yaml.safe_load( + resource_path.joinpath("_category_info.yaml").read_text() + ), + "raw_name": resource_path.name + } return context |