Skip to content

Commit

Permalink
Simplified DjangoFilterConnectionField logic
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Mar 3, 2017
1 parent acff3d5 commit 4cc4673
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 44 deletions.
6 changes: 5 additions & 1 deletion graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ def __init__(self, *args, **kwargs):
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:
Expand Down
60 changes: 17 additions & 43 deletions graphene_django/filter/fields.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -15,47 +13,12 @@ def __init__(self, type, fields=None, order_by=None,
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)
Expand All @@ -66,7 +29,16 @@ def args(self, args):

@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):
Expand All @@ -76,8 +48,10 @@ def filtering_args(self):
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):
Expand Down

0 comments on commit 4cc4673

Please sign in to comment.