From f3bccff688a7f5a9eb619dbed3f726ef2c07c402 Mon Sep 17 00:00:00 2001 From: Leon Sandøy Date: Tue, 1 Feb 2022 20:54:39 +0100 Subject: Refactor as_css_class to 'to_kebabcase'. We're making a few changes here - Changing the name to 'to_kebabcase' - Covering all edge cases - Adding a unit test for this utility --- pydis_site/apps/resources/views/resources.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'pydis_site/apps/resources/views/resources.py') diff --git a/pydis_site/apps/resources/views/resources.py b/pydis_site/apps/resources/views/resources.py index ac9e8355..2375f722 100644 --- a/pydis_site/apps/resources/views/resources.py +++ b/pydis_site/apps/resources/views/resources.py @@ -9,7 +9,7 @@ from django.shortcuts import render from django.views import View from pydis_site import settings -from pydis_site.apps.resources.templatetags.as_css_class import as_css_class +from pydis_site.apps.resources.templatetags.to_kebabcase import to_kebabcase RESOURCES_PATH = Path(settings.BASE_DIR, "pydis_site", "apps", "resources", "resources") @@ -58,9 +58,7 @@ class ResourceView(View): # Make a CSS class friendly representation too, while we're already iterating. for tag in tags: - css_tag = f"{tag_type}-{tag}" - css_tag = css_tag.replace("_", "-") - css_tag = css_tag.replace(" ", "-") + css_tag = to_kebabcase(f"{tag_type}-{tag}") css_classes.append(css_tag) # Now add the css classes back to the resource, so we can use them in the template. @@ -96,12 +94,12 @@ class ResourceView(View): # A complete list of valid filter names self.valid_filters = { - "topics": [as_css_class(topic) for topic in self.filters["Topics"]["filters"]], + "topics": [to_kebabcase(topic) for topic in self.filters["Topics"]["filters"]], "payment_tiers": [ - as_css_class(tier) for tier in self.filters["Payment tiers"]["filters"] + to_kebabcase(tier) for tier in self.filters["Payment tiers"]["filters"] ], - "type": [as_css_class(type_) for type_ in self.filters["Type"]["filters"]], - "difficulty": [as_css_class(tier) for tier in self.filters["Difficulty"]["filters"]], + "type": [to_kebabcase(type_) for type_ in self.filters["Type"]["filters"]], + "difficulty": [to_kebabcase(tier) for tier in self.filters["Difficulty"]["filters"]], } def get(self, request: WSGIRequest, resource_type: t.Optional[str] = None) -> HttpResponse: -- cgit v1.2.3