diff options
| author | 2020-11-14 11:47:07 +0200 | |
|---|---|---|
| committer | 2020-11-14 11:47:07 +0200 | |
| commit | 74f19a8f879155a3fa2d9455b91bb92c572d3095 (patch) | |
| tree | a540e725abdc44e53e8f3522a5f2cf61900f67b6 | |
| parent | Create CSS for resources list (diff) | |
Create view and tests for this view for resources lists
| -rw-r--r-- | pydis_site/apps/resources/tests/test_views.py | 24 | ||||
| -rw-r--r-- | pydis_site/apps/resources/views/resources_list.py | 58 | 
2 files changed, 82 insertions, 0 deletions
diff --git a/pydis_site/apps/resources/tests/test_views.py b/pydis_site/apps/resources/tests/test_views.py index 497e9bfe..53685eef 100644 --- a/pydis_site/apps/resources/tests/test_views.py +++ b/pydis_site/apps/resources/tests/test_views.py @@ -1,6 +1,14 @@ +from pathlib import Path +from unittest.mock import patch + +from django.conf import settings  from django.test import TestCase  from django_hosts import reverse +TESTING_RESOURCES_PATH = Path( +    settings.BASE_DIR, "pydis_site", "apps", "resources", "tests", "testing_resources" +) +  class TestResourcesView(TestCase):      def test_resources_index_200(self): @@ -8,3 +16,19 @@ class TestResourcesView(TestCase):          url = reverse("resources:index")          response = self.client.get(url)          self.assertEqual(response.status_code, 200) + + +class TestResourcesListView(TestCase): +    @patch("pydis_site.apps.resources.views.resources_list.RESOURCES_PATH", TESTING_RESOURCES_PATH) +    def test_valid_resource_list_200(self): +        """Check does site return code 200 when visiting valid resource list.""" +        url = reverse("resources:resources", ("testing",)) +        response = self.client.get(url) +        self.assertEqual(response.status_code, 200) + +    @patch("pydis_site.apps.resources.views.resources_list.RESOURCES_PATH", TESTING_RESOURCES_PATH) +    def test_invalid_resource_list_404(self): +        """Check does site return code 404 when trying to visit invalid resource list.""" +        url = reverse("resources:resources", ("invalid",)) +        response = self.client.get(url) +        self.assertEqual(response.status_code, 404) diff --git a/pydis_site/apps/resources/views/resources_list.py b/pydis_site/apps/resources/views/resources_list.py new file mode 100644 index 00000000..c15f84f2 --- /dev/null +++ b/pydis_site/apps/resources/views/resources_list.py @@ -0,0 +1,58 @@ +from pathlib import Path +from typing import Any, Dict + +import yaml +from django.conf import settings +from django.http import Http404 +from django.views.generic import TemplateView + +RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources") + + +class ResourcesList(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) + +        resource_path = RESOURCES_PATH / self.kwargs["type"] +        if ( +                not resource_path.exists() +                or not resource_path.is_dir() +                or not resource_path.joinpath("_category_info.yaml").exists() +        ): +            raise Http404 + +        resources = [] +        subcategories = [] +        for item in resource_path.iterdir(): +            if item.is_file() and item.suffix == ".yaml" and item.name != "_category_info.yaml": +                resources.append(yaml.safe_load(item.read_text())) +            elif item.is_dir() and item.joinpath("_category_info.yaml").exists(): +                subcategories.append({ +                    "category_info": {**yaml.safe_load( +                        item.joinpath("_category_info.yaml").read_text() +                    ), "raw_name": item.name}, +                    "resources": sorted([ +                        yaml.safe_load(subitem.read_text()) +                        for subitem in item.iterdir() +                        if ( +                            subitem.is_file() +                            and subitem.suffix == ".yaml" +                            and subitem.name != "_category_info.yaml" +                        ) +                    ], key=lambda k: k.get('position', 100)) +                }) + +        context["resources"] = sorted(resources, key=lambda k: k.get('position', 100)) +        context["subcategories"] = sorted( +            subcategories, key=lambda k: k.get('category_info', {}).get('position', 100) +        ) +        context["category_info"] = {**yaml.safe_load( +            resource_path.joinpath("_category_info.yaml").read_text() +        ), "raw_name": resource_path.name} + +        return context  |