Improve imports if not django_filters installed. Improved Django command fixing warning in Django 1.8+

This commit is contained in:
Syrus Akbary 2016-01-13 18:18:47 -08:00
parent 20a493b9d1
commit 2acd8691cf
4 changed files with 54 additions and 44 deletions

View File

@ -4,7 +4,7 @@ from ...core.types.base import FieldType
from ...core.types.definitions import List
from ...relay import ConnectionField
from ...relay.utils import is_node
from .utils import get_type_for_model, maybe_queryset
from .utils import get_type_for_model, maybe_queryset, DJANGO_FILTER_INSTALLED
class DjangoConnectionField(ConnectionField):
@ -37,6 +37,7 @@ class DjangoConnectionField(ConnectionField):
class ConnectionOrListField(Field):
def internal_type(self, schema):
if DJANGO_FILTER_INSTALLED:
from .filter.fields import DjangoFilterConnectionField
model_field = self.type

View File

@ -1,11 +1,12 @@
import warnings
from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED
if not DJANGO_FILTER_INSTALLED:
raise Exception(
warnings.warn(
"Use of django filtering requires the django-filter package "
"be installed. You can do so using `pip install django-filter`"
"be installed. You can do so using `pip install django-filter`", ImportWarning
)
else:
from .fields import DjangoFilterConnectionField
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter

View File

@ -1,14 +1,15 @@
import importlib
import json
from optparse import make_option
from distutils.version import StrictVersion
from django import get_version as get_django_version
from django.core.management.base import BaseCommand, CommandError
LT_DJANGO_1_8 = StrictVersion(get_django_version()) < StrictVersion('1.8')
class Command(BaseCommand):
help = 'Dump Graphene schema JSON to file'
can_import_settings = True
if LT_DJANGO_1_8:
class CommandArguments(BaseCommand):
option_list = BaseCommand.option_list + (
make_option(
'--schema',
@ -25,7 +26,8 @@ class Command(BaseCommand):
help='Output file (default: schema.json)'
),
)
else:
class CommandArguments(BaseCommand):
def add_arguments(self, parser):
from django.conf import settings
parser.add_argument(
@ -42,6 +44,11 @@ class Command(BaseCommand):
default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'),
help='Output file (default: schema.json)')
class Command(CommandArguments):
help = 'Dump Graphene schema JSON to file'
can_import_settings = True
def save_file(self, out, schema_dict):
with open(out, 'w') as outfile:
json.dump(schema_dict, outfile)

View File

@ -9,7 +9,8 @@ from .compat import RelatedObject
try:
import django_filters # noqa
DJANGO_FILTER_INSTALLED = True
except ImportError:
except (ImportError, AttributeError):
# AtributeError raised if DjangoFilters installed with a incompatible Django Version
DJANGO_FILTER_INSTALLED = False