aboutsummaryrefslogtreecommitdiffstats
path: root/pydis_site/apps/resources/views/resources.py
blob: 60ec6d8fc8021f16aafe622b6d2beb42953bb9a8 (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
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render

from pydis_site.apps.resources.resource_search import RESOURCE_TABLE, get_resources_from_search

RESOURCE_META_TAGS = {k: list(v) for k, v in RESOURCE_TABLE.items()}


def format_checkbox_options(options: str) -> list[str]:
    """Split up the comma separated query parameters for checkbox options into a list."""
    return options.split(",")[:-1] if options else []


def resource_view(request: HttpRequest) -> HttpResponse:
    """View for resources index page."""
    checkbox_options = {
        a: set(format_checkbox_options(request.GET.get(b)))
        for a, b in (
            ('topics', 'topic'),
            ('type', 'type'),
            ('payment_tiers', 'payment'),
            ('complexity', 'complexity'),
        )
    }

    topics = sorted(RESOURCE_META_TAGS.get("topics"))

    return render(
        request,
        template_name="resources/resources.html",
        context={
            "checkboxOptions": checkbox_options,
            "topics_1": topics[:len(topics) // 2],
            "topics_2": topics[len(topics) // 2:],
            "tag_types": sorted(RESOURCE_META_TAGS.get("type")),
            "payment_tiers": sorted(RESOURCE_META_TAGS.get("payment_tiers")),
            "complexities": sorted(RESOURCE_META_TAGS.get("complexity")),
            "resources": get_resources_from_search(checkbox_options)
        }
    )