Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions graphene_django_extras/fields.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# -*- coding: utf-8 -*-
import operator
import re
import six
from functools import partial

from graphene import Field, List, ID, Argument
from graphene import Field, List, ID, Int, Argument
from graphene.types.structures import Structure
from graphene_django.filter.utils import get_filtering_args_from_filterset
from graphene_django.forms.converter import convert_form_field
from graphene_django.utils import maybe_queryset, is_valid_django_model, DJANGO_FILTER_INSTALLED
from graphene_django_extras.settings import graphql_api_settings

Expand All @@ -13,6 +15,29 @@
from .paginations.pagination import BaseDjangoGraphqlPagination
from .utils import get_extra_filters, queryset_factory, get_related_fields, find_field, get_kwarg_processors

DECIMAL_FIELD_NAMES = {'DecimalField', 'DecimalInField'}
ID_FIELD_PATTERN = r'(^id$|^id_|.*_id_.*|.*_id$)'

def get_filtering_args_from_filterset(filterset_class, type):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we should be extending the old functionality instead of rewriting it. Something like:

from graphene_django.filter.utils import get_filtering_args_from_filterset as _get_filtering_args_from_filterset

...

def get_filtering_args_from_filterset(filterset_class, type):
  args = _get_filtering_args_from_filterset(filteset_class, type)
  ...new stuff here...
  return args

""" Inspect a FilterSet and produce the arguments to pass to
a Graphene Field. These arguments will be available to
filter against in the GraphQL
"""
args = {}
for name, filter_field in six.iteritems(filterset_class.base_filters):
# Avoid converting an ID field to a Float
if filter_field.field_class.__name__ in DECIMAL_FIELD_NAMES and re.match(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use isinstance(filter_field.field_class, (class1, class2, etc.)) unless the DECIMAL_FIELD_NAMES are not importable and are just strings.

ID_FIELD_PATTERN, name, re.IGNORECASE):
field_type = Int(
description=filter_field.field.help_text,
required=filter_field.field.required
).Argument()
else:
field_type = convert_form_field(filter_field.field).Argument()
field_type.description = filter_field.label
args[name] = field_type

return args

# *********************************************** #
# *********** FIELD FOR SINGLE OBJECT *********** #
Expand Down