diff options
author | 2021-08-03 09:58:51 -0400 | |
---|---|---|
committer | 2021-08-03 09:58:51 -0400 | |
commit | f3b17e79142e1883904c1ad92db433da765198b5 (patch) | |
tree | d4dae27b756ebed9525dc7abe171ba275be9f372 /pydis_site/apps/resources | |
parent | Create Resource type annotation. (diff) |
Restructure table of resources for new query logic.
Diffstat (limited to 'pydis_site/apps/resources')
-rw-r--r-- | pydis_site/apps/resources/utils.py | 35 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources.py | 4 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources_list.py | 4 |
3 files changed, 20 insertions, 23 deletions
diff --git a/pydis_site/apps/resources/utils.py b/pydis_site/apps/resources/utils.py index 1b307ae1..6efa2d1a 100644 --- a/pydis_site/apps/resources/utils.py +++ b/pydis_site/apps/resources/utils.py @@ -1,20 +1,34 @@ import typing as t +from collections import defaultdict from itertools import chain from pathlib import Path import yaml from django.conf import settings + +def _transform_name(resource_name: str) -> str: + return resource_name.title().replace('And', 'and', -1) + + Resource = dict[str, t.Union[str, list[dict[str, str]], dict[str, list[str]]]] RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources") -default_categories = [ +RESOURCES: dict[str, Resource] = {path.stem: yaml.safe_load(path.read_text()) for path + in RESOURCES_PATH.rglob("*.yaml")} + +RESOURCE_TABLE = {category: defaultdict(set) for category in ( "topics", "payment_tiers", "complexity", "type" -] +)} + +for name, resource in RESOURCES.items(): + for category, tags in resource['tags'].items(): + for tag in tags: + RESOURCE_TABLE[category][_transform_name(tag)].add(name) def yaml_file_matches_search(yaml_data: dict, search_terms: list[str]) -> bool: @@ -32,20 +46,3 @@ def get_resources_from_search(search_categories: list[str]) -> list[Resource]: if yaml_file_matches_search(this_dict, search_categories): out.append(this_dict) return out - - -def get_all_resources() -> list[dict[str, t.Union[list[str], str]]]: - """Loads resource YAMLs from provided path.""" - return [yaml.safe_load(item.read_text()) for item in RESOURCES_PATH.rglob("*.yaml")] - - -def get_resources_meta() -> dict[str, list[str]]: - """Combines the tags from each resource into one dictionary of unique tags.""" - resource_meta_tags = {x: set() for x in default_categories} - - for resource in get_all_resources(): - for tag_key, tag_values in resource.get("tags").items(): - for tag_item in tag_values: - resource_meta_tags[tag_key].add(tag_item.title().replace('And', 'and', -1)) - - return {key: sorted(value) for key, value in resource_meta_tags.items()} diff --git a/pydis_site/apps/resources/views/resources.py b/pydis_site/apps/resources/views/resources.py index ffb4f4a8..693eebaf 100644 --- a/pydis_site/apps/resources/views/resources.py +++ b/pydis_site/apps/resources/views/resources.py @@ -1,9 +1,9 @@ from django.http import HttpRequest, HttpResponse from django.shortcuts import render -from pydis_site.apps.resources.utils import get_all_resources, get_resources_from_search, get_resources_meta +from pydis_site.apps.resources.utils import RESOURCE_TABLE, get_resources_from_search -RESOURCE_META_TAGS = get_resources_meta() +RESOURCE_META_TAGS = {k: list(v) for k, v in RESOURCE_TABLE.items()} def format_checkbox_options(options: str) -> list[str]: diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py index bd43be33..9d74be91 100644 --- a/pydis_site/apps/resources/views/resources_list.py +++ b/pydis_site/apps/resources/views/resources_list.py @@ -2,7 +2,7 @@ from typing import Any, Dict from django.views.generic import TemplateView -from pydis_site.apps.resources.utils import get_all_resources +from pydis_site.apps.resources.utils import RESOURCES class ResourcesListView(TemplateView): @@ -13,6 +13,6 @@ class ResourcesListView(TemplateView): def get_context_data(self, **kwargs) -> Dict[str, Any]: """Add resources and subcategories data into context.""" context = super().get_context_data(**kwargs) - context["resources"] = get_all_resources() + context["resources"] = RESOURCES return context |