From b9f2589c7b2b5f171c47f1a614229ff999341b29 Mon Sep 17 00:00:00 2001 From: bast Date: Sun, 30 May 2021 14:46:27 -0700 Subject: Raise ValidationError for new bot/infractions filter if the types are invalid If the before time is after the after time, or if both `type` and `types` are specified --- pydis_site/apps/api/viewsets/bot/infraction.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'pydis_site/apps/api/viewsets') diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index b0c7d332..00dd05b8 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -54,6 +54,8 @@ class InfractionViewSet( - **expires_before** `isodatetime`: the latest expires_at time to return infractions for Invalid query parameters are ignored. + Only one of `type` and `types` may be provided. If both `expires_before` and `expires_after` + are provided, `expires_after` must come after `expires_before`. #### Response format Response is paginated but the result is returned without any pagination metadata. @@ -192,8 +194,19 @@ class InfractionViewSet( except ValueError: raise ValidationError({'expires_before': ['failed to convert to datetime']}) + if 'expires_at__lte' in additional_filters and 'expires_at__gte' in additional_filters: + if additional_filters['expires_at__gte'] < additional_filters['expires_at__lte']: + raise ValidationError({ + 'expires_before': ['cannot be after expires_after'], + 'expires_after': ['cannot be before expires_before'], + }) + filter_types = self.request.query_params.get('types') if filter_types: + if self.request.query_params.get('type'): + raise ValidationError({ + 'types': ['you must provide only one of "type" or "types"'], + }) additional_filters['type__in'] = [i.strip() for i in filter_types.split(",")] return self.queryset.filter(**additional_filters) -- cgit v1.2.3