mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-10-31 16:07:27 +03:00 
			
		
		
		
	Replacing django-filter detection with GRAPHENE_ENABLE_FILTERING setting
Also moving GRAPHENE_ORDER_BY_FIELD into settings.py which centralises use of django settings and their default values
This commit is contained in:
		
							parent
							
								
									0f35f4de8d
								
							
						
					
					
						commit
						880807dd2f
					
				|  | @ -1,9 +1,10 @@ | ||||||
| from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED | from graphene.contrib.django import settings | ||||||
| 
 | 
 | ||||||
| if not DJANGO_FILTER_INSTALLED: | if not settings.GRAPHENE_ENABLE_FILTERING: | ||||||
|     raise Exception( |     raise Exception( | ||||||
|         "Use of django filtering requires the django-filter package " |         "To make use of filtering you configure " | ||||||
|         "be installed. You can do so using `pip install django-filter`" |         "GRAPHENE_ENABLE_FILTERING=True. This will also require " | ||||||
|  |         "django-filter be installed" | ||||||
|     ) |     ) | ||||||
| 
 | 
 | ||||||
| from .fields import DjangoFilterConnectionField | from .fields import DjangoFilterConnectionField | ||||||
|  |  | ||||||
|  | @ -1,11 +1,11 @@ | ||||||
| import six | import six | ||||||
| from django.conf import settings |  | ||||||
| from django.db import models | from django.db import models | ||||||
| from django.utils.text import capfirst | from django.utils.text import capfirst | ||||||
| from django_filters import Filter, MultipleChoiceFilter | from django_filters import Filter, MultipleChoiceFilter | ||||||
| from django_filters.filterset import FilterSetMetaclass, FilterSet | from django_filters.filterset import FilterSetMetaclass, FilterSet | ||||||
| from graphql_relay.node.node import from_global_id | from graphql_relay.node.node import from_global_id | ||||||
| 
 | 
 | ||||||
|  | from graphene.contrib.django import settings | ||||||
| from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField | from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -25,9 +25,6 @@ class GlobalIDMultipleChoiceFilter(MultipleChoiceFilter): | ||||||
|         return super(GlobalIDMultipleChoiceFilter, self).filter(qs, gids) |         return super(GlobalIDMultipleChoiceFilter, self).filter(qs, gids) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ORDER_BY_FIELD = getattr(settings, 'GRAPHENE_ORDER_BY_FIELD', 'order_by') |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| GRAPHENE_FILTER_SET_OVERRIDES = { | GRAPHENE_FILTER_SET_OVERRIDES = { | ||||||
|     models.AutoField: { |     models.AutoField: { | ||||||
|         'filter_class': GlobalIDFilter, |         'filter_class': GlobalIDFilter, | ||||||
|  | @ -54,7 +51,7 @@ class GrapheneFilterSetMetaclass(FilterSetMetaclass): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class GrapheneFilterSetMixin(object): | class GrapheneFilterSetMixin(object): | ||||||
|     order_by_field = ORDER_BY_FIELD |     order_by_field = settings.GRAPHENE_ORDER_BY_FIELD | ||||||
| 
 | 
 | ||||||
|     @classmethod |     @classmethod | ||||||
|     def filter_for_reverse_field(cls, f, name): |     def filter_for_reverse_field(cls, f, name): | ||||||
|  |  | ||||||
|  | @ -1,11 +1,11 @@ | ||||||
| from .utils import DJANGO_FILTER_INSTALLED | from graphene.contrib.django import settings | ||||||
| from ...core.classtypes.objecttype import ObjectTypeOptions | from ...core.classtypes.objecttype import ObjectTypeOptions | ||||||
| from ...relay.types import Node | from ...relay.types import Node | ||||||
| from ...relay.utils import is_node | from ...relay.utils import is_node | ||||||
| 
 | 
 | ||||||
| VALID_ATTRS = ('model', 'only_fields', 'exclude_fields') | VALID_ATTRS = ('model', 'only_fields', 'exclude_fields') | ||||||
| 
 | 
 | ||||||
| if DJANGO_FILTER_INSTALLED: | if settings.GRAPHENE_ENABLE_FILTERING: | ||||||
|     VALID_ATTRS += ('filter_fields', 'filter_order_by') |     VALID_ATTRS += ('filter_fields', 'filter_order_by') | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										4
									
								
								graphene/contrib/django/settings.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								graphene/contrib/django/settings.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,4 @@ | ||||||
|  | from django.conf import settings | ||||||
|  | 
 | ||||||
|  | GRAPHENE_ORDER_BY_FIELD = getattr(settings, 'GRAPHENE_ORDER_BY_FIELD', 'order_by') | ||||||
|  | GRAPHENE_ENABLE_FILTERING = getattr(settings, 'GRAPHENE_ENABLE_FILTERING', False) | ||||||
|  | @ -0,0 +1,4 @@ | ||||||
|  | from graphene.contrib.django import settings | ||||||
|  | 
 | ||||||
|  | # Force filtering for tests | ||||||
|  | settings.GRAPHENE_ENABLE_FILTERING = True | ||||||
|  | @ -1,7 +1,7 @@ | ||||||
| import pytest | import pytest | ||||||
| 
 | 
 | ||||||
| from graphene import ObjectType, Schema | from graphene import ObjectType, Schema | ||||||
| from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED | from graphene.contrib.django import settings | ||||||
| from graphene.relay import NodeField | from graphene.relay import NodeField | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | @ -10,7 +10,7 @@ from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleCho | ||||||
| from graphene.contrib.django.tests.models import Article, Pet, Reporter | from graphene.contrib.django.tests.models import Article, Pet, Reporter | ||||||
| 
 | 
 | ||||||
| pytestmark = [] | pytestmark = [] | ||||||
| if DJANGO_FILTER_INSTALLED: | if settings.GRAPHENE_ENABLE_FILTERING: | ||||||
|     import django_filters |     import django_filters | ||||||
|     from graphene.contrib.django.filter import (GlobalIDFilter, DjangoFilterConnectionField, |     from graphene.contrib.django.filter import (GlobalIDFilter, DjangoFilterConnectionField, | ||||||
|                                                 GlobalIDMultipleChoiceFilter) |                                                 GlobalIDMultipleChoiceFilter) | ||||||
|  |  | ||||||
|  | @ -1,17 +1,16 @@ | ||||||
| import pytest | import pytest | ||||||
| from django.core.exceptions import ImproperlyConfigured | from django.core.exceptions import ImproperlyConfigured | ||||||
| 
 | 
 | ||||||
| from graphene.contrib.django.utils import DJANGO_FILTER_INSTALLED | from graphene.contrib.django import settings | ||||||
|  | from graphene.contrib.django.tests.models import Reporter, Article | ||||||
|  | from graphene.contrib.django.tests.test_resolvers import ReporterNode, ArticleNode | ||||||
| 
 | 
 | ||||||
| if DJANGO_FILTER_INSTALLED: | if settings.GRAPHENE_ENABLE_FILTERING: | ||||||
|     from graphene.contrib.django.filter.resolvers import FilterConnectionResolver |     from graphene.contrib.django.filter.resolvers import FilterConnectionResolver | ||||||
|     from graphene.contrib.django.tests.filter.filters import ReporterFilter, ArticleFilter |     from graphene.contrib.django.tests.filter.filters import ReporterFilter, ArticleFilter | ||||||
| else: | else: | ||||||
|     pytestmark = pytest.mark.skipif(True, reason='django_filters not installed') |     pytestmark = pytest.mark.skipif(True, reason='django_filters not installed') | ||||||
| 
 | 
 | ||||||
| from graphene.contrib.django.tests.models import Reporter, Article |  | ||||||
| from graphene.contrib.django.tests.test_resolvers import ReporterNode, ArticleNode |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| def test_filter_get_filterset_class_explicit(): | def test_filter_get_filterset_class_explicit(): | ||||||
|     reporter = Reporter(id=1, first_name='Cookie Monster') |     reporter = Reporter(id=1, first_name='Cookie Monster') | ||||||
|  |  | ||||||
|  | @ -3,9 +3,9 @@ import inspect | ||||||
| import six | import six | ||||||
| from django.db import models | from django.db import models | ||||||
| 
 | 
 | ||||||
|  | from graphene.contrib.django import settings | ||||||
| from ...core.classtypes.objecttype import ObjectType, ObjectTypeMeta | from ...core.classtypes.objecttype import ObjectType, ObjectTypeMeta | ||||||
| from ...relay.types import Connection, Node, NodeMeta | from ...relay.types import Connection, Node, NodeMeta | ||||||
| from .utils import DJANGO_FILTER_INSTALLED |  | ||||||
| from .converter import convert_django_field | from .converter import convert_django_field | ||||||
| from .options import DjangoOptions | from .options import DjangoOptions | ||||||
| from .utils import get_reverse_fields, maybe_queryset | from .utils import get_reverse_fields, maybe_queryset | ||||||
|  | @ -104,7 +104,7 @@ class DjangoConnection(Connection): | ||||||
| 
 | 
 | ||||||
| django_node_meta_bases = (DjangoObjectTypeMeta, NodeMeta) | django_node_meta_bases = (DjangoObjectTypeMeta, NodeMeta) | ||||||
| # Only include filter functionality if available | # Only include filter functionality if available | ||||||
| if DJANGO_FILTER_INSTALLED: | if settings.GRAPHENE_ENABLE_FILTERING: | ||||||
|     django_node_meta_bases = (DjangoFilterObjectTypeMeta,) + django_node_meta_bases |     django_node_meta_bases = (DjangoFilterObjectTypeMeta,) + django_node_meta_bases | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -7,12 +7,6 @@ from graphene.utils import LazyList | ||||||
| 
 | 
 | ||||||
| from graphene import Argument, String | from graphene import Argument, String | ||||||
| 
 | 
 | ||||||
| try: |  | ||||||
|     import django_filters  # noqa |  | ||||||
|     DJANGO_FILTER_INSTALLED = True |  | ||||||
| except ImportError: |  | ||||||
|     DJANGO_FILTER_INSTALLED = False |  | ||||||
| 
 |  | ||||||
| 
 | 
 | ||||||
| def get_type_for_model(schema, model): | def get_type_for_model(schema, model): | ||||||
|     schema = schema |     schema = schema | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user