aboutsummaryrefslogtreecommitdiffstats
path: root/pysite/views/main/info/resources.py
blob: 2642e1ec1dea83d5c7e2a554a3e0d8880819957c (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
54
55
56
57
58
59
# coding=utf-8
import json
from logging import getLogger

from pysite.base_route import RouteView

ICON_STYLES = {
    "branding": "fab",
    "regular": "far",
    "solid": "fas",
    "light": "fal"
}

logger = getLogger("Resources")

try:
    with open("static/resources.json") as fh:
        categories = json.load(fh)

        for category, items in categories.items():
            to_remove = []

            for name, resource in items["resources"].items():
                for url_obj in resource["urls"]:
                    icon = url_obj["icon"].lower()

                    if "/" not in icon:
                        to_remove.append(name)
                        logger.error(
                            f"Resource {name} in category {category} has an invalid icon. Icons should be of the"
                            f"form `style/name`."
                        )
                        continue

                    style, icon_name = icon.split("/")

                    if style not in ICON_STYLES:
                        to_remove.append(name)
                        logger.error(
                            f"Resource {name} in category {category} has an invalid icon style. Icon style must "
                            f"be one of {', '.join(ICON_STYLES.keys())}."
                        )
                        continue

                    url_obj["classes"] = f"{ICON_STYLES[style]} fa-{icon_name}"

            for name in to_remove:
                del items["resources"][name]
except Exception:
    getLogger("Resources").exception("Failed to load resources.json")
    categories = None


class ResourcesView(RouteView):
    path = "/info/resources"
    name = "info.resources"

    def get(self):
        return self.render("main/info/resources.html", categories=categories)