diff --git a/lib/main/custom_filters.py b/lib/main/custom_filters.py new file mode 100644 index 0000000000..60ee74541a --- /dev/null +++ b/lib/main/custom_filters.py @@ -0,0 +1,51 @@ +# Copyright (c) 2013 AnsibleWorks, Inc. +# +# This file is part of Ansible Commander. +# +# Ansible Commander is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3 of the License. +# +# Ansible Commander is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible Commander. If not, see . + +from rest_framework.filters import BaseFilterBackend +from django.core.exceptions import PermissionDenied + +# TODO: does order_by still work? + +class CustomFilterBackend(object): + + def filter_queryset(self, request, queryset, view): + + keys = request.GET.keys() + + terms = {} + order_by = None + + for key in keys: + + value = request.GET[key] + + if key == 'order_by': + order_by = value + continue + + key2 = key + if key2.endswith("__int"): + key2 = key.replace("__int","") + value = int(value) + + terms[key2] = value + + qs = queryset.filter(**terms) + + if order_by: + qs = qs.order_by(order_by) + + return qs diff --git a/lib/settings/defaults.py b/lib/settings/defaults.py index 5ccfcace53..c2e24d85b0 100644 --- a/lib/settings/defaults.py +++ b/lib/settings/defaults.py @@ -38,7 +38,7 @@ ADMINS = ( MANAGERS = ADMINS REST_FRAMEWORK = { - 'FILTER_BACKEND': 'rest_framework.filters.DjangoFilterBackend', + 'FILTER_BACKEND': 'lib.main.custom_filters.CustomFilterBackend', 'PAGINATE_BY': 25, 'PAGINATE_BY_PARAM': 'page_size', 'DEFAULT_AUTHENTICATION_CLASSES': (