diff options
-rw-r--r-- | pydis_site/apps/resources/utils.py | 25 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources.py | 10 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources_list.py | 4 | ||||
-rw-r--r-- | pydis_site/templates/resources/resources.html | 41 |
4 files changed, 69 insertions, 11 deletions
diff --git a/pydis_site/apps/resources/utils.py b/pydis_site/apps/resources/utils.py index bb812ec0..7f238431 100644 --- a/pydis_site/apps/resources/utils.py +++ b/pydis_site/apps/resources/utils.py @@ -15,7 +15,28 @@ default_categories = [ ] -def get_resources() -> list[dict[str, t.Union[list[str], str]]]: +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")] @@ -24,7 +45,7 @@ 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_resources(): + 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)) diff --git a/pydis_site/apps/resources/views/resources.py b/pydis_site/apps/resources/views/resources.py index 2414e48b..ffb4f4a8 100644 --- a/pydis_site/apps/resources/views/resources.py +++ b/pydis_site/apps/resources/views/resources.py @@ -1,26 +1,28 @@ from django.http import HttpRequest, HttpResponse from django.shortcuts import render -from pydis_site.apps.resources.utils import get_resources_meta +from pydis_site.apps.resources.utils import get_all_resources, get_resources_from_search, get_resources_meta RESOURCE_META_TAGS = get_resources_meta() def format_checkbox_options(options: str) -> list[str]: - """Split up the comma separated parameters into a list.""" + """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 = format_checkbox_options(request.GET.get("checkboxOptions")) return render( request, template_name="resources/resources.html", context={ - "checkboxOptions": format_checkbox_options(request.GET.get("checkboxOptions")), + "checkboxOptions": checkbox_options, "topics": RESOURCE_META_TAGS.get("topics"), "tag_types": RESOURCE_META_TAGS.get("type"), "payment_tiers": RESOURCE_META_TAGS.get("payment_tiers"), - "complexities": RESOURCE_META_TAGS.get("complexity") + "complexities": RESOURCE_META_TAGS.get("complexity"), + "resources": get_resources_from_search(checkbox_options) } ) diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py index 0ec74d78..bd43be33 100644 --- a/pydis_site/apps/resources/views/resources_list.py +++ b/pydis_site/apps/resources/views/resources_list.py @@ -2,7 +2,7 @@ from typing import Any, Dict from django.views.generic import TemplateView -from pydis_site.apps.resources.utils import get_resources +from pydis_site.apps.resources.utils import get_all_resources class ResourcesListView(TemplateView): @@ -13,6 +13,6 @@ class ResourcesListView(TemplateView): def get_context_data(self, **kwargs) -> Dict[str, Any]: """Add resources and subcategories data into context.""" context = super().get_context_data(**kwargs) - context["resources"] = get_resources() + context["resources"] = get_all_resources() return context diff --git a/pydis_site/templates/resources/resources.html b/pydis_site/templates/resources/resources.html index fca3d0da..e2275e89 100644 --- a/pydis_site/templates/resources/resources.html +++ b/pydis_site/templates/resources/resources.html @@ -1,9 +1,11 @@ {% extends 'base/base.html' %} +{% load as_icon %} {% load static %} {% block title %}Resources{% endblock %} {% block head %} - <link rel="stylesheet" href="{% static "css/resources/resources.css" %}"> + <link rel="stylesheet" href="{% static "css/resources/resources.css" %}"> + <link rel="stylesheet" href="{% static "css/resources/resources_list.css" %}"> {% endblock %} {% block content %} @@ -89,16 +91,49 @@ </div> </div> </section> + + {% if resources|length > 0 %} + <section class="section"> + <div class="container"> + <div class="content"> + <div> + {% for resource in resources %} + {% include "resources/resource_box.html" %} + {% endfor %} + + {% for subcategory in subcategories %} + <h2 id="{{ subcategory.category_info.raw_name }}"> + <a href="{% url "resources:resources" category=category_info.raw_name %}#{{ subcategory.category_info.raw_name }}"> + {{ subcategory.category_info.name }} + </a> + </h2> + <p>{{ subcategory.category_info.description|safe }}</p> + + {% for resource in subcategory.resources %} + {% with category_info=subcategory.category_info %} + {% include "resources/resource_box.html" %} + {% endwith %} + {% endfor %} + {% endfor %} + </div> + </div> + </div> + </section> + {% else %} + <p>No resources matching search.</p> + {% endif %} + <script> const initialParams = new URLSearchParams(window.location.search).get("checkboxOptions"); const checkBoxes = document.querySelectorAll("input[name='checkboxOption']"); - console.log(initialParams); - checkBoxes.forEach((x) => { + if(initialParams != null){ + checkBoxes.forEach((x) => { if(initialParams.includes(x.value)){ x.checked = true; } }); + } function buildQueryParams(){ let params = new URLSearchParams(window.location.search); |