Skip to content

Commit 72e8f43

Browse files
authored
Merge pull request #1166 from python-discord/resource-filter-fetch
Add endpoint to fetch filters in JSON format
2 parents af681db + c63ab0b commit 72e8f43

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

pydis_site/apps/resources/tests/test_views.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,14 @@ def test_resources_with_invalid_argument(self):
2727
url = reverse("resources:index", kwargs={"resource_type": "urinal-cake"})
2828
response = self.client.get(url)
2929
self.assertEqual(response.status_code, 404)
30+
31+
32+
class TestResourceFilterView(TestCase):
33+
def test_resource_filter_response(self):
34+
"""Check that the filter endpoint returns JSON-formatted filters."""
35+
url = reverse('resources:filters')
36+
response = self.client.get(url)
37+
self.assertEqual(response.status_code, 200)
38+
content = response.json()
39+
self.assertIn('difficulty', content)
40+
self.assertIsInstance(content['difficulty'], list)

pydis_site/apps/resources/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from django_distill import distill_path
22

3-
from pydis_site.apps.resources.views import ResourceView
3+
from pydis_site.apps.resources.views import ResourceView, ResourceFilterView
44

55
app_name = "resources"
66
urlpatterns = [
77
# Using `distill_path` instead of `path` allows this to be available
88
# in static preview builds.
99
distill_path("", ResourceView.as_view(), name="index"),
10+
distill_path("filters", ResourceFilterView.as_view(), name="filters"),
1011
distill_path("<resource_type>/", ResourceView.as_view(), name="index"),
1112
]

pydis_site/apps/resources/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.apps import apps
44
from django.core.handlers.wsgi import WSGIRequest
5-
from django.http import HttpResponse, HttpResponseNotFound
5+
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse
66
from django.shortcuts import render
77
from django.views import View
88

@@ -38,3 +38,12 @@ def get(self, request: WSGIRequest, resource_type: str | None = None) -> HttpRes
3838
"resource_type": resource_type,
3939
}
4040
)
41+
42+
43+
class ResourceFilterView(View):
44+
"""Exposes resource filters for the bot."""
45+
46+
def get(self, request: WSGIRequest) -> HttpResponse:
47+
"""Return resource filters as JSON."""
48+
app = apps.get_app_config(APP_NAME)
49+
return JsonResponse(app.valid_filters)

0 commit comments

Comments
 (0)