Updating imports to work when singledispatch is available natively

This commit is contained in:
Adam Charnock 2015-12-04 17:48:33 +00:00
parent 166a143c18
commit b4b42db73e
4 changed files with 38 additions and 4 deletions

View File

@ -1,8 +1,10 @@
from django.db import models from django.db import models
from singledispatch import singledispatch
from .utils import import_single_dispatch
from ...core.types.scalars import ID, Boolean, Float, Int, String from ...core.types.scalars import ID, Boolean, Float, Int, String
singledispatch = import_single_dispatch()
try: try:
UUIDField = models.UUIDField UUIDField = models.UUIDField
except AttributeError: except AttributeError:

View File

@ -1,3 +1,11 @@
try:
import django_filters
except:
raise Exception(
"Use of django filtering requires the django-filter package "
"be installed. You can do so using `pip install django-filter`"
)
from .fields import DjangoFilterConnectionField from .fields import DjangoFilterConnectionField
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter
from .resolvers import FilterConnectionResolver from .resolvers import FilterConnectionResolver

View File

@ -1,10 +1,10 @@
from django import forms from django import forms
from django.forms.fields import BaseTemporalField from django.forms.fields import BaseTemporalField
from singledispatch import singledispatch
from graphene import String, Int, Boolean, Float, ID from graphene import String, Int, Boolean, Float, ID
from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField
from graphene.contrib.django.utils import import_single_dispatch
from graphene.core.types.definitions import List from graphene.core.types.definitions import List
singledispatch = import_single_dispatch()
try: try:
UUIDField = forms.UUIDField UUIDField = forms.UUIDField

View File

@ -3,7 +3,6 @@ from django.db import models
from django.db.models.manager import Manager from django.db.models.manager import Manager
from graphene import Argument, String from graphene import Argument, String
from graphene.contrib.django.form_converter import convert_form_field
def get_type_for_model(schema, model): def get_type_for_model(schema, model):
@ -36,6 +35,8 @@ def get_filtering_args_from_filterset(filterset_class, type):
a Graphene Field. These arguments will be available to a Graphene Field. These arguments will be available to
filter against in the GraphQL filter against in the GraphQL
""" """
from graphene.contrib.django.form_converter import convert_form_field
args = {} args = {}
for name, filter_field in six.iteritems(filterset_class.base_filters): for name, filter_field in six.iteritems(filterset_class.base_filters):
field_type = Argument(convert_form_field(filter_field.field)) field_type = Argument(convert_form_field(filter_field.field))
@ -46,3 +47,26 @@ def get_filtering_args_from_filterset(filterset_class, type):
# Also add the 'order_by' field # Also add the 'order_by' field
args[filterset_class.order_by_field] = Argument(String) args[filterset_class.order_by_field] = Argument(String)
return args return args
def import_single_dispatch():
singledispatch = None
try:
from functools import singledispatch
except ImportError:
pass
try:
from singledispatch import singledispatch
except ImportError:
pass
if not singledispatch:
raise Exception(
"It seems your python version does not include "
"functools.singledispatch. Please install the 'singledispatch' "
"package. More information here: "
"https://pypi.python.org/pypi/singledispatch"
)
return singledispatch