diff options
author | 2022-01-27 10:16:52 +0100 | |
---|---|---|
committer | 2022-01-27 10:16:52 +0100 | |
commit | 4ee8d527a2cf0ac7e4fd977ffd29e560b3cd48cb (patch) | |
tree | 86037770cbcd15e8055f8469166d7139dbbf8d53 /pydis_site/apps/resources | |
parent | Fix broken yaml in kivy.yaml. (diff) |
Greatly simplify the backend.
Here we're getting rid of all filtering and search functionality
on the backend. We'll be handling this on the client-side from now on.
Diffstat (limited to 'pydis_site/apps/resources')
-rw-r--r-- | pydis_site/apps/resources/resource_search.py | 59 | ||||
-rw-r--r-- | pydis_site/apps/resources/tests/test_views.py | 13 | ||||
-rw-r--r-- | pydis_site/apps/resources/urls.py | 3 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/__init__.py | 5 | ||||
-rw-r--r-- | pydis_site/apps/resources/views/resources_list.py | 18 |
5 files changed, 3 insertions, 95 deletions
diff --git a/pydis_site/apps/resources/resource_search.py b/pydis_site/apps/resources/resource_search.py deleted file mode 100644 index 1e23089b..00000000 --- a/pydis_site/apps/resources/resource_search.py +++ /dev/null @@ -1,59 +0,0 @@ -import typing as t -from collections import defaultdict -from functools import reduce -from operator import and_, or_ -from pathlib import Path -from types import MappingProxyType - -import yaml -from django.conf import settings - - -def _transform_name(resource_name: str) -> str: - return resource_name.title().replace('And', 'and', -1) - - -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") - -RESOURCES: MappingProxyType[str, Resource] = MappingProxyType({ - path.stem: yaml.safe_load(path.read_text()) - for path in RESOURCES_PATH.rglob("*.yaml") -}) - -_resource_table = {category: defaultdict(set) for category in ( - "topics", - "payment_tiers", - "complexity", - "type" -)} - -for name, resource in RESOURCES.items(): - for category, tags in resource['tags'].items(): - for tag in tags: - _resource_table[category][_transform_name(tag)].add(name) - -# Freeze the resources table -RESOURCE_TABLE = MappingProxyType({ - category: MappingProxyType(d) - for category, d in _resource_table.items() -}) - -ALL_RESOURCE_NAMES = frozenset(RESOURCES.keys()) - - -def get_resources_from_search(search_categories: dict[str, set[str]]) -> list[Resource]: - """Returns a list of all resources that match the given search terms.""" - resource_names_that_match = reduce( - and_, - ( - reduce( - or_, - (RESOURCE_TABLE[category][label] for label in labels), - set() - ) or ALL_RESOURCE_NAMES - for category, labels in search_categories.items() - ) - ) - return [RESOURCES[name_] for name_ in resource_names_that_match] diff --git a/pydis_site/apps/resources/tests/test_views.py b/pydis_site/apps/resources/tests/test_views.py index 2e9efc1d..f96a04b0 100644 --- a/pydis_site/apps/resources/tests/test_views.py +++ b/pydis_site/apps/resources/tests/test_views.py @@ -16,16 +16,3 @@ class TestResourcesView(TestCase): url = reverse("resources:index") response = self.client.get(url) self.assertEqual(response.status_code, 200) - - -class TestResourcesListView(TestCase): - def test_valid_resource_list_200(self): - """Check does site return code 200 when visiting valid resource list.""" - url = reverse("resources:resources") - response = self.client.get(url) - self.assertEqual(response.status_code, 200) - - @patch("pydis_site.apps.resources.resource_search.RESOURCES_PATH", TESTING_RESOURCES_PATH) - def test_filter_resource_list(self): - """TODO: Check that we can correctly filter resources with GET parameters.""" - pass diff --git a/pydis_site/apps/resources/urls.py b/pydis_site/apps/resources/urls.py index c8d441df..3db26417 100644 --- a/pydis_site/apps/resources/urls.py +++ b/pydis_site/apps/resources/urls.py @@ -4,6 +4,5 @@ from pydis_site.apps.resources import views app_name = "resources" urlpatterns = [ - path("", views.resources.resource_view, name="index"), - path("list/", views.ResourcesListView.as_view(), name="resources") + path("", views.resources.ResourceView.as_view(), name="index"), ] diff --git a/pydis_site/apps/resources/views/__init__.py b/pydis_site/apps/resources/views/__init__.py index c89071c5..986f3e10 100644 --- a/pydis_site/apps/resources/views/__init__.py +++ b/pydis_site/apps/resources/views/__init__.py @@ -1,4 +1,3 @@ -from .resources import resource_view -from .resources_list import ResourcesListView +from .resources import ResourceView -__all__ = ["resource_view", "ResourcesListView"] +__all__ = ["ResourceView"] diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py deleted file mode 100644 index 30498c8f..00000000 --- a/pydis_site/apps/resources/views/resources_list.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import Any, Dict - -from django.views.generic import TemplateView - -from pydis_site.apps.resources.resource_search import RESOURCES - - -class ResourcesListView(TemplateView): - """Shows specific resources list.""" - - template_name = "resources/resources_list.html" - - def get_context_data(self, **kwargs) -> Dict[str, Any]: - """Add resources and subcategories data into context.""" - context = super().get_context_data(**kwargs) - context["resources"] = RESOURCES - - return context |