Skip to content

Commit

Permalink
Merge pull request #2292 from akvo/#2291-invalid-api-filters
Browse files Browse the repository at this point in the history
[#2291] Catch invalid API filters and pass error on to user
  • Loading branch information
KasperBrandt authored Jul 6, 2016
2 parents 4763c2f + 06bed9d commit dd82b9c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions akvo/rest/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import ast

from django.db.models import Q
from django.core.exceptions import FieldError

from rest_framework import filters
from rest_framework import filters, status
from rest_framework.exceptions import APIException


class RSRGenericFilterBackend(filters.BaseFilterBackend):
Expand Down Expand Up @@ -57,10 +59,14 @@ def eval_query_value(request, key):
args_or_kwargs = eval_query_value(request, param)
if args_or_kwargs:
# filter and exclude are called with a dict kwarg, the _related methods with a list
if param in ['filter', 'exclude',]:
queryset = getattr(queryset, param)(**args_or_kwargs)
else:
queryset = getattr(queryset, param)(*args_or_kwargs)
try:
if param in ['filter', 'exclude',]:
queryset = getattr(queryset, param)(**args_or_kwargs)
else:
queryset = getattr(queryset, param)(*args_or_kwargs)

except FieldError as e:
raise APIException("Error in request: {message}".format(message=e.message))

# support for Q expressions, limited to OR-concatenated filtering
if request.QUERY_PARAMS.get('q_filter1', None):
Expand Down

0 comments on commit dd82b9c

Please sign in to comment.