aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks129 <[email protected]>2020-11-14 09:38:36 +0200
committerGravatar ks129 <[email protected]>2020-11-14 09:38:36 +0200
commit7631ccfef001a6213c395bf8a79645220c658e08 (patch)
tree0a8301db45f51643a2b9ad671dc1eb7cb1b9af01
parentUpdate resources YAML files to match with new system (diff)
Create as_icon templatetag and tests for it
-rw-r--r--pydis_site/apps/resources/templatetags/__init__.py3
-rw-r--r--pydis_site/apps/resources/templatetags/as_icon.py14
-rw-r--r--pydis_site/apps/resources/tests/test_as_icon.py28
3 files changed, 45 insertions, 0 deletions
diff --git a/pydis_site/apps/resources/templatetags/__init__.py b/pydis_site/apps/resources/templatetags/__init__.py
new file mode 100644
index 00000000..2b266b94
--- /dev/null
+++ b/pydis_site/apps/resources/templatetags/__init__.py
@@ -0,0 +1,3 @@
+from .as_icon import as_icon
+
+__all__ = ["as_icon"]
diff --git a/pydis_site/apps/resources/templatetags/as_icon.py b/pydis_site/apps/resources/templatetags/as_icon.py
new file mode 100644
index 00000000..b211407c
--- /dev/null
+++ b/pydis_site/apps/resources/templatetags/as_icon.py
@@ -0,0 +1,14 @@
+from django import template
+
+register = template.Library()
+
+
+def as_icon(icon: str) -> str:
+ """Convert icon string in format 'type/icon' to fa-icon HTML classes."""
+ icon_type, icon_name = icon.split("/")
+ if icon_type.lower() == "branding":
+ icon_type = "fab"
+ else:
+ icon_type = "fas"
+ return f'{icon_type} fa-{icon_name}'
diff --git a/pydis_site/apps/resources/tests/test_as_icon.py b/pydis_site/apps/resources/tests/test_as_icon.py
new file mode 100644
index 00000000..5b33910d
--- /dev/null
+++ b/pydis_site/apps/resources/tests/test_as_icon.py
@@ -0,0 +1,28 @@
+from django.test import TestCase
+
+from pydis_site.apps.resources.templatetags import as_icon
+
+
+class TestAsIcon(TestCase):
+ """Tests for `as_icon` templatetag."""
+
+ def test_as_icon(self):
+ """Should return proper icon type class and icon class based on input."""
+ test_cases = [
+ {
+ "input": "regular/icon",
+ "output": "fas fa-icon",
+ },
+ {
+ "input": "branding/brand",
+ "output": "fab fa-brand",
+ },
+ {
+ "input": "fake/my-icon",
+ "output": "fas fa-my-icon",
+ }
+ ]
+
+ for case in test_cases:
+ with self.subTest(input=case["input"], output=case["output"]):
+ self.assertEqual(case["output"], as_icon(case["input"]))