Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint to fetch filters in JSON format #1166

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pydis_site/apps/resources/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,14 @@ def test_resources_with_invalid_argument(self):
url = reverse("resources:index", kwargs={"resource_type": "urinal-cake"})
response = self.client.get(url)
self.assertEqual(response.status_code, 404)


class TestResourceFilterView(TestCase):
def test_resource_filter_response(self):
"""Check that the filter endpoint returns JSON-formatted filters."""
url = reverse('resources:filters')
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
content = response.json()
self.assertIn('difficulty', content)
self.assertIsInstance(content['difficulty'], list)
3 changes: 2 additions & 1 deletion pydis_site/apps/resources/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django_distill import distill_path

from pydis_site.apps.resources.views import ResourceView
from pydis_site.apps.resources.views import ResourceView, ResourceFilterView

app_name = "resources"
urlpatterns = [
# Using `distill_path` instead of `path` allows this to be available
# in static preview builds.
distill_path("", ResourceView.as_view(), name="index"),
distill_path("filters", ResourceFilterView.as_view(), name="filters"),
distill_path("<resource_type>/", ResourceView.as_view(), name="index"),
]
11 changes: 10 additions & 1 deletion pydis_site/apps/resources/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.apps import apps
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse, HttpResponseNotFound
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse
from django.shortcuts import render
from django.views import View

Expand Down Expand Up @@ -38,3 +38,12 @@ def get(self, request: WSGIRequest, resource_type: str | None = None) -> HttpRes
"resource_type": resource_type,
}
)


class ResourceFilterView(View):
"""Exposes resource filters for the bot."""

def get(self, request: WSGIRequest) -> HttpResponse:
"""Return resource filters as JSON."""
app = apps.get_app_config(APP_NAME)
return JsonResponse(app.valid_filters)