Simplified DjangoFilterConnectionField logic

This commit is contained in:
Syrus Akbary 2017-03-02 17:54:15 -08:00
parent acff3d59db
commit 4cc46736bf
2 changed files with 22 additions and 44 deletions

View File

@ -32,9 +32,13 @@ class DjangoConnectionField(ConnectionField):
self.on = kwargs.pop('on', False)
super(DjangoConnectionField, self).__init__(*args, **kwargs)
@property
def node_type(self):
return self.type._meta.node
@property
def model(self):
return self.type._meta.node._meta.model
return self.node_type._meta.model
def get_manager(self):
if self.on:

View File

@ -1,11 +1,9 @@
import inspect
from collections import OrderedDict
from functools import partial
# from graphene.relay import is_node
from graphene.types.argument import to_arguments
from ..fields import DjangoConnectionField
from graphene.relay import is_node
from .utils import get_filtering_args_from_filterset, get_filterset_class
@ -15,47 +13,12 @@ class DjangoFilterConnectionField(DjangoConnectionField):
extra_filter_meta=None, filterset_class=None,
*args, **kwargs):
self._fields = fields
self._type = type
self._filterset_class = filterset_class
self._provided_filterset_class = filterset_class
self._filterset_class = None
self._extra_filter_meta = extra_filter_meta
self._base_args = None
super(DjangoFilterConnectionField, self).__init__(type, *args, **kwargs)
@property
def node_type(self):
if inspect.isfunction(self._type) or inspect.ismethod(self._type):
return self._type()
return self._type
@property
def meta(self):
if is_node(self.node_type):
_model = self.node_type._meta.model
else:
# ConnectionFields can also be passed Connections,
# in which case, we need to use the Node of the connection
# to get our relevant args.
_model = self.node_type._meta.node._meta.model
meta = dict(model=_model,
fields=self.fields)
if self._extra_filter_meta:
meta.update(self._extra_filter_meta)
return meta
@property
def fields(self):
if self._fields:
return self._fields
if is_node(self.node_type):
return self.node_type._meta.filter_fields
else:
# ConnectionFields can also be passed Connections,
# in which case, we need to use the Node of the connection
# to get our relevant args.
return self.node_type._meta.node._meta.filter_fields
@property
def args(self):
return to_arguments(self._base_args or OrderedDict(), self.filtering_args)
@ -66,7 +29,16 @@ class DjangoFilterConnectionField(DjangoConnectionField):
@property
def filterset_class(self):
return get_filterset_class(self._filterset_class, **self.meta)
if not self._filterset_class:
fields = self._fields or self.node_type._meta.filter_fields
meta = dict(model=self.model,
fields=fields)
if self._extra_filter_meta:
meta.update(self._extra_filter_meta)
self._filterset_class = get_filterset_class(self._provided_filterset_class, **meta)
return self._filterset_class
@property
def filtering_args(self):
@ -76,8 +48,10 @@ 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}
qs = default_manager.get_queryset()
qs = filterset_class(data=filter_kwargs, queryset=qs).qs
qs = filterset_class(
data=filter_kwargs,
queryset=default_manager.get_queryset()
).qs
return DjangoConnectionField.connection_resolver(resolver, connection, qs, root, args, context, info)
def get_resolver(self, parent_resolver):