From d89c65a8fdd0ca6c997f186bd7b5af581c23be2f Mon Sep 17 00:00:00 2001 From: swfarnsworth Date: Fri, 3 Sep 2021 16:48:35 -0400 Subject: Rename `utils.py` to `resource_search.py`. --- pydis_site/apps/resources/resource_search.py | 57 +++++++++++++++++++++++ pydis_site/apps/resources/tests/test_views.py | 2 +- pydis_site/apps/resources/utils.py | 57 ----------------------- pydis_site/apps/resources/views/resources.py | 2 +- pydis_site/apps/resources/views/resources_list.py | 2 +- 5 files changed, 60 insertions(+), 60 deletions(-) create mode 100644 pydis_site/apps/resources/resource_search.py delete mode 100644 pydis_site/apps/resources/utils.py (limited to 'pydis_site/apps') diff --git a/pydis_site/apps/resources/resource_search.py b/pydis_site/apps/resources/resource_search.py new file mode 100644 index 00000000..8454451a --- /dev/null +++ b/pydis_site/apps/resources/resource_search.py @@ -0,0 +1,57 @@ +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() +}) + + +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() + ) + 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 568f4d13..2e9efc1d 100644 --- a/pydis_site/apps/resources/tests/test_views.py +++ b/pydis_site/apps/resources/tests/test_views.py @@ -25,7 +25,7 @@ class TestResourcesListView(TestCase): response = self.client.get(url) self.assertEqual(response.status_code, 200) - @patch("pydis_site.apps.resources.utils.RESOURCES_PATH", TESTING_RESOURCES_PATH) + @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/utils.py b/pydis_site/apps/resources/utils.py deleted file mode 100644 index 8454451a..00000000 --- a/pydis_site/apps/resources/utils.py +++ /dev/null @@ -1,57 +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() -}) - - -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() - ) - for category, labels in search_categories.items() - ) - ) - return [RESOURCES[name_] for name_ in resource_names_that_match] diff --git a/pydis_site/apps/resources/views/resources.py b/pydis_site/apps/resources/views/resources.py index 087cdfb6..a7c631dc 100644 --- a/pydis_site/apps/resources/views/resources.py +++ b/pydis_site/apps/resources/views/resources.py @@ -1,7 +1,7 @@ from django.http import HttpRequest, HttpResponse from django.shortcuts import render -from pydis_site.apps.resources.utils import RESOURCE_TABLE, get_resources_from_search +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()} diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py index 9d74be91..30498c8f 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 RESOURCES +from pydis_site.apps.resources.resource_search import RESOURCES class ResourcesListView(TemplateView): -- cgit v1.2.3