mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-26 05:31:05 +03:00 
			
		
		
		
	Improve imports if not django_filters installed. Improved Django command fixing warning in Django 1.8+
This commit is contained in:
		
							parent
							
								
									20a493b9d1
								
							
						
					
					
						commit
						2acd8691cf
					
				|  | @ -4,7 +4,7 @@ from ...core.types.base import FieldType | ||||||
| from ...core.types.definitions import List | from ...core.types.definitions import List | ||||||
| from ...relay import ConnectionField | from ...relay import ConnectionField | ||||||
| from ...relay.utils import is_node | 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): | class DjangoConnectionField(ConnectionField): | ||||||
|  | @ -37,7 +37,8 @@ class DjangoConnectionField(ConnectionField): | ||||||
| class ConnectionOrListField(Field): | class ConnectionOrListField(Field): | ||||||
| 
 | 
 | ||||||
|     def internal_type(self, schema): |     def internal_type(self, schema): | ||||||
|         from .filter.fields import DjangoFilterConnectionField |         if DJANGO_FILTER_INSTALLED: | ||||||
|  |             from .filter.fields import DjangoFilterConnectionField | ||||||
| 
 | 
 | ||||||
|         model_field = self.type |         model_field = self.type | ||||||
|         field_object_type = model_field.get_object_type(schema) |         field_object_type = model_field.get_object_type(schema) | ||||||
|  |  | ||||||
|  | @ -1,13 +1,14 @@ | ||||||
|  | import warnings | ||||||
| from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED | from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED | ||||||
| 
 | 
 | ||||||
| if not DJANGO_FILTER_INSTALLED: | if not DJANGO_FILTER_INSTALLED: | ||||||
|     raise Exception( |     warnings.warn( | ||||||
|         "Use of django filtering requires the django-filter package " |         "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 | ||||||
| 
 | 
 | ||||||
| from .fields import DjangoFilterConnectionField |     __all__ = ['DjangoFilterConnectionField', 'GrapheneFilterSet', | ||||||
| from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter |                'GlobalIDFilter', 'GlobalIDMultipleChoiceFilter'] | ||||||
| 
 |  | ||||||
| __all__ = ['DjangoFilterConnectionField', 'GrapheneFilterSet', |  | ||||||
|            'GlobalIDFilter', 'GlobalIDMultipleChoiceFilter'] |  | ||||||
|  |  | ||||||
|  | @ -1,47 +1,54 @@ | ||||||
| import importlib | import importlib | ||||||
| import json | import json | ||||||
| from optparse import make_option | 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 | from django.core.management.base import BaseCommand, CommandError | ||||||
| 
 | 
 | ||||||
|  | LT_DJANGO_1_8 = StrictVersion(get_django_version()) < StrictVersion('1.8') | ||||||
| 
 | 
 | ||||||
| class Command(BaseCommand): | if LT_DJANGO_1_8: | ||||||
|  |     class CommandArguments(BaseCommand): | ||||||
|  |         option_list = BaseCommand.option_list + ( | ||||||
|  |             make_option( | ||||||
|  |                 '--schema', | ||||||
|  |                 type=str, | ||||||
|  |                 dest='schema', | ||||||
|  |                 default='', | ||||||
|  |                 help='Django app containing schema to dump, e.g. myproject.core.schema', | ||||||
|  |             ), | ||||||
|  |             make_option( | ||||||
|  |                 '--out', | ||||||
|  |                 type=str, | ||||||
|  |                 dest='out', | ||||||
|  |                 default='', | ||||||
|  |                 help='Output file (default: schema.json)' | ||||||
|  |             ), | ||||||
|  |         ) | ||||||
|  | else: | ||||||
|  |     class CommandArguments(BaseCommand): | ||||||
|  |         def add_arguments(self, parser): | ||||||
|  |             from django.conf import settings | ||||||
|  |             parser.add_argument( | ||||||
|  |                 '--schema', | ||||||
|  |                 type=str, | ||||||
|  |                 dest='schema', | ||||||
|  |                 default=getattr(settings, 'GRAPHENE_SCHEMA', ''), | ||||||
|  |                 help='Django app containing schema to dump, e.g. myproject.core.schema') | ||||||
|  | 
 | ||||||
|  |             parser.add_argument( | ||||||
|  |                 '--out', | ||||||
|  |                 type=str, | ||||||
|  |                 dest='out', | ||||||
|  |                 default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), | ||||||
|  |                 help='Output file (default: schema.json)') | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class Command(CommandArguments): | ||||||
|     help = 'Dump Graphene schema JSON to file' |     help = 'Dump Graphene schema JSON to file' | ||||||
|     can_import_settings = True |     can_import_settings = True | ||||||
| 
 | 
 | ||||||
|     option_list = BaseCommand.option_list + ( |  | ||||||
|         make_option( |  | ||||||
|             '--schema', |  | ||||||
|             type=str, |  | ||||||
|             dest='schema', |  | ||||||
|             default='', |  | ||||||
|             help='Django app containing schema to dump, e.g. myproject.core.schema', |  | ||||||
|         ), |  | ||||||
|         make_option( |  | ||||||
|             '--out', |  | ||||||
|             type=str, |  | ||||||
|             dest='out', |  | ||||||
|             default='', |  | ||||||
|             help='Output file (default: schema.json)' |  | ||||||
|         ), |  | ||||||
|     ) |  | ||||||
| 
 |  | ||||||
|     def add_arguments(self, parser): |  | ||||||
|         from django.conf import settings |  | ||||||
|         parser.add_argument( |  | ||||||
|             '--schema', |  | ||||||
|             type=str, |  | ||||||
|             dest='schema', |  | ||||||
|             default=getattr(settings, 'GRAPHENE_SCHEMA', ''), |  | ||||||
|             help='Django app containing schema to dump, e.g. myproject.core.schema') |  | ||||||
| 
 |  | ||||||
|         parser.add_argument( |  | ||||||
|             '--out', |  | ||||||
|             type=str, |  | ||||||
|             dest='out', |  | ||||||
|             default=getattr(settings, 'GRAPHENE_SCHEMA_OUTPUT', 'schema.json'), |  | ||||||
|             help='Output file (default: schema.json)') |  | ||||||
| 
 |  | ||||||
|     def save_file(self, out, schema_dict): |     def save_file(self, out, schema_dict): | ||||||
|         with open(out, 'w') as outfile: |         with open(out, 'w') as outfile: | ||||||
|             json.dump(schema_dict, outfile) |             json.dump(schema_dict, outfile) | ||||||
|  |  | ||||||
|  | @ -9,7 +9,8 @@ from .compat import RelatedObject | ||||||
| try: | try: | ||||||
|     import django_filters  # noqa |     import django_filters  # noqa | ||||||
|     DJANGO_FILTER_INSTALLED = True |     DJANGO_FILTER_INSTALLED = True | ||||||
| except ImportError: | except (ImportError, AttributeError): | ||||||
|  |     # AtributeError raised if DjangoFilters installed with a incompatible Django Version | ||||||
|     DJANGO_FILTER_INSTALLED = False |     DJANGO_FILTER_INSTALLED = False | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user