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

[#2163] Catch unknown fields in old API filtering #2164

Merged
merged 2 commits into from
May 11, 2016
Merged
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
30 changes: 17 additions & 13 deletions akvo/rest/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
# See more details in the license.txt file located at the root folder of the Akvo RSR module.
# For additional details on the GNU license please see < http://www.gnu.org/licenses/agpl.html >.

from django.db.models.fields.related import ForeignKey, ForeignObject
from django.db.models.fields import FieldDoesNotExist
from django.db.models.fields.related import ForeignObject
from django.core.exceptions import FieldError

from akvo.rest.models import TastyTokenAuthentication

from rest_framework import authentication, filters, permissions, viewsets
from rest_framework.exceptions import ParseError

from .filters import RSRGenericFilterBackend

Expand Down Expand Up @@ -68,16 +68,20 @@ def get_lookups_from_filters(legacy_filters):
parts = parts[:-1]
model = queryset.model
for part in parts:
field_object, related_model, direct, m2m = model._meta.get_field_by_name(
part)
if direct:
if issubclass(field_object.__class__, ForeignObject):
model = field_object.related.parent_model
try:
field_object, related_model, direct, m2m = model._meta.\
get_field_by_name(part)

if direct:
if issubclass(field_object.__class__, ForeignObject):
model = field_object.related.parent_model
else:
value = field_object.to_python(value)
break
else:
value = field_object.to_python(value)
break
else:
model = related_model
model = related_model
except FieldDoesNotExist:
pass
query_set_lookups += [{key: value}]
return query_set_lookups

Expand All @@ -91,9 +95,9 @@ def get_lookups_from_filters(legacy_filters):
for lookup in lookups:
try:
queryset = queryset.filter(**lookup)
except FieldError:
except (FieldError, ValueError):
# In order to mimick 'old' behaviour of the API, we should ignore non-valid
# parameters. Returning a warning would be more preferable.
# parameters or values. Returning a warning would be more preferable.
pass

return queryset
Expand Down