aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/resources/utils.py
blob: 1b307ae12173f8d08f4bd9e6c3bc228a12282cca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import typing as t
from itertools import chain
from pathlib import Path

import yaml
from django.conf import settings

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 = [
    "topics",
    "payment_tiers",
    "complexity",
    "type"
]


def yaml_file_matches_search(yaml_data: dict, search_terms: list[str]) -> bool:
    """Checks which resources contain tags for every search term passed."""
    search_terms = [x.lower() for x in search_terms]
    matching_tags = [x for x in chain(*yaml_data["tags"].values()) if x in search_terms]
    return len(matching_tags) >= len(search_terms)


def get_resources_from_search(search_categories: list[str]) -> list[Resource]:
    """Returns a list of all resources that match the given search terms."""
    out = []
    for item in RESOURCES_PATH.rglob("*.yaml"):
        this_dict = yaml.safe_load(item.read_text())
        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()}