Repository: akvo/akvo-rsr

Issue:
Non-existing fields give error in API
## Issue description
- A try, except to catch the FieldDoesNotExist should handle this. See http://sentry.support.akvo-ops.org/rsr/live/group/885/.
- Not existing values give a ValueError. See http://sentry.support.akvo-ops.org/rsr/live/group/886/.
## Test plan
### FieldDoesNotExist
1. Open `/rest/v1/project_map_location/?location_target__partners=1&limit=500&image_thumb_name=big&image_thumb_big_width=664&last_modified_at__gt=2016-05-10&publishingstatus__status=published&is_public=True&page=1`
2. This should not give a FieldDoesNotExist error
### ValueError
1. Open `/rest/v1/project_update/?project=&limit=3&format=json&ordering=-created_at`
2. This should not give a ValueError



Discussion:

Resolving patch:
diff --git a/akvo/rest/viewsets.py b/akvo/rest/viewsets.py
--- a/akvo/rest/viewsets.py
+++ b/akvo/rest/viewsets.py
@@ -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
 
@@ -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
 
@@ -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


Test patch: