aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/resources/utils.py
blob: 7f238431eb18cbd25142eb2f426d8aefedeea124 (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
52
53
import typing as t
from pathlib import Path

import yaml
from django.conf import settings

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[str, t.Union[list[str], str]], search_terms: list[str]) -> bool:
    match_count = 0
    search_len = len(search_terms)
    for search in search_terms:
        for _, values in yaml_data["tags"].items():
            if search.lower() in values:
                match_count += 1
                if match_count >= search_len:
                    return True
    return False


def get_resources_from_search(search_categories: list[str]) -> list[dict[str, t.Union[list[str], str]]]:
    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()}