Skip to content

Return proper error message when wrong category query parameter is provided. #2701

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions tabbycat/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.db.models import Count, Prefetch, Q
from django.utils.translation import gettext as _
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter
from dynamic_preferences.api.serializers import PreferenceSerializer
from dynamic_preferences.api.viewsets import PerInstancePreferenceViewSet
Expand All @@ -18,6 +19,7 @@
from rest_framework.permissions import BasePermission, IsAdminUser
from rest_framework.response import Response
from rest_framework.reverse import reverse
from rest_framework.serializers import ValidationError
from rest_framework.views import APIView
from rest_framework.viewsets import GenericViewSet, ModelViewSet

Expand Down Expand Up @@ -814,9 +816,16 @@ class SubstantiveSpeakerStandingsView(BaseStandingsView):
list_permission = Permission.VIEW_SPEAKERSSTANDINGS

def get_queryset(self):
category = self.request.query_params.get('category', None)
category = self.request.query_params.get('category')
if category is not None:
return super().get_queryset().filter(categories__pk=category)
try:
category_id = int(category)
return super().get_queryset().filter(categories__pk=category_id)
except (ValueError, TypeError):
raise ValidationError({
'category': _("The category specified is not a valid integer."),
})

return super().get_queryset()

@property
Expand Down Expand Up @@ -859,7 +868,13 @@ class TeamStandingsView(BaseStandingsView):
def get_queryset(self):
category = self.request.query_params.get('category', None)
if category is not None:
return super().get_queryset().filter(break_categories__pk=category)
try:
category_id = int(category)
return super().get_queryset().filter(break_categories__pk=category_id)
except (ValueError, TypeError):
raise ValidationError({
'category': _("The category specified is not a valid integer."),
})
return super().get_queryset()


Expand Down