Remove filter order_by as Django Filters have deprecated it in 1.0

This commit is contained in:
Syrus Akbary 2016-11-23 02:15:05 -08:00
parent 48993ddca0
commit 8dfe7bbcf5
8 changed files with 26 additions and 68 deletions

View File

@ -8,7 +8,7 @@ if not DJANGO_FILTER_INSTALLED:
)
else:
from .fields import DjangoFilterConnectionField
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter
from .filterset import GlobalIDFilter, GlobalIDMultipleChoiceFilter
__all__ = ['DjangoFilterConnectionField', 'GrapheneFilterSet',
__all__ = ['DjangoFilterConnectionField',
'GlobalIDFilter', 'GlobalIDMultipleChoiceFilter']

View File

@ -1,20 +1,19 @@
from functools import partial
from django_filters import OrderingFilter
from ..fields import DjangoConnectionField
from .utils import get_filtering_args_from_filterset, get_filterset_class
class DjangoFilterConnectionField(DjangoConnectionField):
def __init__(self, type, fields=None, order_by=None,
extra_filter_meta=None, filterset_class=None,
*args, **kwargs):
def __init__(self, type, fields=None, extra_filter_meta=None,
filterset_class=None, *args, **kwargs):
self.order_by = order_by or type._meta.filter_order_by
self.fields = fields or type._meta.filter_fields
meta = dict(model=type._meta.model,
fields=self.fields,
order_by=self.order_by)
fields=self.fields)
if extra_filter_meta:
meta.update(extra_filter_meta)
self.filterset_class = get_filterset_class(filterset_class, **meta)
@ -27,12 +26,8 @@ class DjangoFilterConnectionField(DjangoConnectionField):
def connection_resolver(resolver, connection, default_manager, filterset_class, filtering_args,
root, args, context, info):
filter_kwargs = {k: v for k, v in args.items() if k in filtering_args}
order = args.get('order_by', None)
qs = default_manager.get_queryset()
if order:
qs = qs.order_by(order)
qs = filterset_class(data=filter_kwargs, queryset=qs)
qs = filterset_class(data=filter_kwargs, queryset=qs).qs
return DjangoConnectionField.connection_resolver(resolver, connection, qs, root, args, context, info)
def get_resolver(self, parent_resolver):

View File

@ -5,7 +5,7 @@ from django.conf import settings
from django.db import models
from django.utils.text import capfirst
from django_filters import Filter, MultipleChoiceFilter
from django_filters.filterset import FilterSet, FilterSetMetaclass
from django_filters.filterset import BaseFilterSet, FilterSet, FilterSetMetaclass
from django_filters.filterset import FILTER_FOR_DBFIELD_DEFAULTS
from graphql_relay.node.node import from_global_id
@ -29,9 +29,6 @@ class GlobalIDMultipleChoiceFilter(MultipleChoiceFilter):
return super(GlobalIDMultipleChoiceFilter, self).filter(qs, gids)
ORDER_BY_FIELD = getattr(settings, 'GRAPHENE_ORDER_BY_FIELD', 'order_by')
GRAPHENE_FILTER_SET_OVERRIDES = {
models.AutoField: {
'filter_class': GlobalIDFilter,
@ -48,25 +45,7 @@ GRAPHENE_FILTER_SET_OVERRIDES = {
}
# Only useful for Django-filter 0.14-, not necessary in latest version 0.15+
class GrapheneFilterSetMetaclass(FilterSetMetaclass):
def __new__(cls, name, bases, attrs):
new_class = super(GrapheneFilterSetMetaclass, cls).__new__(cls, name, bases, attrs)
# Customise the filter_overrides for Graphene
if hasattr(new_class, '_meta') and hasattr(new_class._meta, 'filter_overrides'):
filter_overrides = new_class._meta.filter_overrides
else:
filter_overrides = new_class.filter_overrides
for k, v in GRAPHENE_FILTER_SET_OVERRIDES.items():
filter_overrides.setdefault(k, v)
return new_class
class GrapheneFilterSetMixin(object):
order_by_field = ORDER_BY_FIELD
class GrapheneFilterSetMixin(BaseFilterSet):
FILTER_DEFAULTS = dict(itertools.chain(
FILTER_FOR_DBFIELD_DEFAULTS.items(),
GRAPHENE_FILTER_SET_OVERRIDES.items()
@ -93,26 +72,17 @@ class GrapheneFilterSetMixin(object):
return GlobalIDFilter(**default)
class GrapheneFilterSet(six.with_metaclass(GrapheneFilterSetMetaclass, GrapheneFilterSetMixin, FilterSet)):
""" Base class for FilterSets used by Graphene
You shouldn't usually need to use this class. The
DjangoFilterConnectionField will wrap FilterSets with this class as
necessary
"""
def setup_filterset(filterset_class):
""" Wrap a provided filterset in Graphene-specific functionality
"""
return type(
'Graphene{}'.format(filterset_class.__name__),
(six.with_metaclass(GrapheneFilterSetMetaclass, GrapheneFilterSetMixin, filterset_class),),
(filterset_class, GrapheneFilterSetMixin),
{},
)
def custom_filterset_factory(model, filterset_base_class=GrapheneFilterSet,
def custom_filterset_factory(model, filterset_base_class=FilterSet,
**meta):
""" Create a filterset for the given model using the provided meta data
"""
@ -122,7 +92,7 @@ def custom_filterset_factory(model, filterset_base_class=GrapheneFilterSet,
meta_class = type(str('Meta'), (object,), meta)
filterset = type(
str('%sFilterSet' % model._meta.object_name),
(filterset_base_class,),
(filterset_base_class, GrapheneFilterSetMixin),
{
'Meta': meta_class
}

View File

@ -1,4 +1,5 @@
import django_filters
from django_filters import OrderingFilter
from graphene_django.tests.models import Article, Pet, Reporter
@ -12,7 +13,8 @@ class ArticleFilter(django_filters.FilterSet):
'pub_date': ['gt', 'lt', 'exact'],
'reporter': ['exact'],
}
order_by = False
order_by = OrderingFilter(fields=('pub_date',))
class ReporterFilter(django_filters.FilterSet):
@ -20,7 +22,8 @@ class ReporterFilter(django_filters.FilterSet):
class Meta:
model = Reporter
fields = ['first_name', 'last_name', 'email', 'pets']
order_by = True
order_by = OrderingFilter(fields=('pub_date',))
class PetFilter(django_filters.FilterSet):
@ -28,4 +31,3 @@ class PetFilter(django_filters.FilterSet):
class Meta:
model = Pet
fields = ['name']
order_by = False

View File

@ -27,6 +27,7 @@ class ArticleNode(DjangoObjectType):
class Meta:
model = Article
interfaces = (Node, )
filter_fields = ('headline', )
class ReporterNode(DjangoObjectType):
@ -110,8 +111,8 @@ def test_filter_explicit_filterset_orderable():
def test_filter_shortcut_filterset_orderable_true():
field = DjangoFilterConnectionField(ReporterNode, order_by=True)
assert_orderable(field)
field = DjangoFilterConnectionField(ReporterNode)
assert_not_orderable(field)
# def test_filter_shortcut_filterset_orderable_headline():
@ -126,9 +127,9 @@ def test_filter_explicit_filterset_not_orderable():
def test_filter_shortcut_filterset_extra_meta():
field = DjangoFilterConnectionField(ArticleNode, extra_filter_meta={
'order_by': True
'exclude': ('headline', )
})
assert_orderable(field)
assert 'headline' not in field.filterset_class.get_fields()
def test_filter_filterset_information_on_meta():
@ -138,11 +139,10 @@ def test_filter_filterset_information_on_meta():
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
filter_order_by = True
field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, 'first_name', 'articles')
assert_orderable(field)
assert_not_orderable(field)
def test_filter_filterset_information_on_meta_related():
@ -152,7 +152,6 @@ def test_filter_filterset_information_on_meta_related():
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
filter_order_by = True
class ArticleFilterNode(DjangoObjectType):
@ -160,7 +159,6 @@ def test_filter_filterset_information_on_meta_related():
model = Article
interfaces = (Node, )
filter_fields = ['headline', 'reporter']
filter_order_by = True
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
@ -171,7 +169,7 @@ def test_filter_filterset_information_on_meta_related():
schema = Schema(query=Query)
articles_field = ReporterFilterNode._meta.fields['articles'].get_type()
assert_arguments(articles_field, 'headline', 'reporter')
assert_orderable(articles_field)
assert_not_orderable(articles_field)
def test_filter_filterset_related_results():
@ -181,7 +179,6 @@ def test_filter_filterset_related_results():
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
filter_order_by = True
class ArticleFilterNode(DjangoObjectType):
@ -189,7 +186,6 @@ def test_filter_filterset_related_results():
interfaces = (Node, )
model = Article
filter_fields = ['headline', 'reporter']
filter_order_by = True
class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)

View File

@ -18,10 +18,6 @@ def get_filtering_args_from_filterset(filterset_class, type):
field_type.description = filter_field.label
args[name] = field_type
# Also add the 'order_by' field
if getattr(filterset_class._meta, 'order_by', None):
args[filterset_class.order_by_field] = String()
return args

View File

@ -65,7 +65,6 @@ class DjangoObjectTypeMeta(ObjectTypeMeta):
# we allow more attributes in Meta
defaults.update(
filter_fields=(),
filter_order_by=(),
)
options = Options(

View File

@ -42,7 +42,7 @@ setup(
'pytest-runner',
],
tests_require=[
'django-filter>=0.10.0',
'django-filter>=1.0.0',
'pytest',
'pytest-django==2.9.1',
'mock',