mirror of
https://github.com/ansible/awx.git
synced 2026-03-10 05:59:28 -02:30
Allow more advanced django filtering.
This commit is contained in:
51
lib/main/custom_filters.py
Normal file
51
lib/main/custom_filters.py
Normal file
@@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
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
|
||||||
@@ -38,7 +38,7 @@ ADMINS = (
|
|||||||
MANAGERS = ADMINS
|
MANAGERS = ADMINS
|
||||||
|
|
||||||
REST_FRAMEWORK = {
|
REST_FRAMEWORK = {
|
||||||
'FILTER_BACKEND': 'rest_framework.filters.DjangoFilterBackend',
|
'FILTER_BACKEND': 'lib.main.custom_filters.CustomFilterBackend',
|
||||||
'PAGINATE_BY': 25,
|
'PAGINATE_BY': 25,
|
||||||
'PAGINATE_BY_PARAM': 'page_size',
|
'PAGINATE_BY_PARAM': 'page_size',
|
||||||
'DEFAULT_AUTHENTICATION_CLASSES': (
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||||
|
|||||||
Reference in New Issue
Block a user