diff options
Diffstat (limited to 'pydis_site/apps/resources/utils.py')
-rw-r--r-- | pydis_site/apps/resources/utils.py | 35 |
1 files changed, 16 insertions, 19 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()} |