From b14336bdffc053b735811e38c08271691ca87eb3 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Wed, 22 Feb 2017 15:33:47 -0500 Subject: [PATCH] Alias unified job template search fields for presentation The polymorphic accessors have a name that is different than our normal presentation name for these types. This aliases them for the presentation layer and then handles processing them when they are given as filters. --- awx/api/filters.py | 8 +++++++- awx/api/generics.py | 5 ++++- .../tests/functional/api/test_unified_job_template.py | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 awx/main/tests/functional/api/test_unified_job_template.py diff --git a/awx/api/filters.py b/awx/api/filters.py index 6590e79990..e5c9c39264 100644 --- a/awx/api/filters.py +++ b/awx/api/filters.py @@ -112,7 +112,13 @@ class FieldLookupBackend(BaseFilterBackend): elif name == 'pk': field = model._meta.pk else: - field = model._meta.get_field_by_name(name)[0] + name_alt = name.replace("_", "") + if name_alt in model._meta.fields_map.keys(): + field = model._meta.fields_map[name_alt] + new_parts.pop() + new_parts.append(name_alt) + else: + field = model._meta.get_field_by_name(name)[0] if isinstance(field, ForeignObjectRel) and getattr(field.field, '__prevent_search__', False): raise PermissionDenied(_('Filtering on %s is not allowed.' % name)) elif getattr(field, '__prevent_search__', False): diff --git a/awx/api/generics.py b/awx/api/generics.py index 07affed319..48f84f830b 100644 --- a/awx/api/generics.py +++ b/awx/api/generics.py @@ -9,6 +9,7 @@ import time # Django from django.conf import settings from django.db import connection +from django.db.models.fields import FieldDoesNotExist from django.http import QueryDict from django.shortcuts import get_object_or_404 from django.template.loader import render_to_string @@ -285,7 +286,7 @@ class ListAPIView(generics.ListAPIView, GenericAPIView): if getattr(field, 'related_model', None): fields.append('{}__search'.format(field.name)) for rel in self.model._meta.related_objects: - name = rel.get_accessor_name() + name = rel.related_model._meta.verbose_name.replace(" ", "_") if name is None: continue if name.endswith('_set'): @@ -308,6 +309,8 @@ class ListAPIView(generics.ListAPIView, GenericAPIView): FieldLookupBackend().get_field_from_lookup(self.model, field) except PermissionDenied: pass + except FieldDoesNotExist: + allowed_fields.append(field) else: allowed_fields.append(field) return allowed_fields diff --git a/awx/main/tests/functional/api/test_unified_job_template.py b/awx/main/tests/functional/api/test_unified_job_template.py new file mode 100644 index 0000000000..695bd51d23 --- /dev/null +++ b/awx/main/tests/functional/api/test_unified_job_template.py @@ -0,0 +1,11 @@ +import pytest + +from django.core.urlresolvers import reverse + + +@pytest.mark.django_db +def test_aliased_forward_reverse_field_searches(instance, options, get, admin): + url = reverse('api:unified_job_template_list') + response = options(url, None, admin) + assert 'job_template__search' in response.data['related_search_fields'] + get(reverse("api:unified_job_template_list") + "?job_template__search=anything", user=admin, expect=200)