Merge branch 'master' into fix-filter-and-resolver

This commit is contained in:
Syrus Akbary 2017-04-15 00:04:19 -07:00
commit 055c6e2359
7 changed files with 54 additions and 15 deletions

View File

@ -111,7 +111,7 @@ py.test graphene_django --cov=graphene_django # Use -v -s for verbose mode
### Documentation ### Documentation
The documentation is generated using the excellent [Sphinx](http://www.sphinx-doc.org/) and a custom theme. The [documentation](http://docs.graphene-python.org/projects/django/en/latest/) is generated using the excellent [Sphinx](http://www.sphinx-doc.org/) and a custom theme.
The documentation dependencies are installed by running: The documentation dependencies are installed by running:

View File

@ -120,7 +120,7 @@ After developing, the full test suite can be evaluated by running:
Documentation Documentation
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
The documentation is generated using the excellent The `documentation <http://docs.graphene-python.org/projects/django/en/latest/>`__ is generated using the excellent
`Sphinx <http://www.sphinx-doc.org/>`__ and a custom theme. `Sphinx <http://www.sphinx-doc.org/>`__ and a custom theme.
The documentation dependencies are installed by running: The documentation dependencies are installed by running:

View File

@ -1,5 +1,4 @@
graphene graphene
graphene-django graphene-django
django_graphiql
graphql-core graphql-core
django==1.9 django==1.9

View File

@ -1,6 +1,5 @@
graphene graphene
graphene-django graphene-django
django_graphiql
graphql-core graphql-core
django==1.9 django==1.9
django-filter==0.11.0 django-filter==0.11.0

View File

@ -3,12 +3,12 @@ from mock import patch
from graphene import Interface, ObjectType, Schema from graphene import Interface, ObjectType, Schema
from graphene.relay import Node from graphene.relay import Node
from ..registry import reset_global_registry from .. import registry
from ..types import DjangoObjectType from ..types import DjangoObjectType
from .models import Article as ArticleModel from .models import Article as ArticleModel
from .models import Reporter as ReporterModel from .models import Reporter as ReporterModel
reset_global_registry() registry.reset_global_registry()
class Reporter(DjangoObjectType): class Reporter(DjangoObjectType):
@ -124,3 +124,42 @@ type RootQuery {
} }
""".lstrip() """.lstrip()
assert str(schema) == expected assert str(schema) == expected
def with_local_registry(func):
def inner(*args, **kwargs):
old = registry.get_global_registry()
registry.reset_global_registry()
try:
retval = func(*args, **kwargs)
except Exception as e:
registry.registry = old
raise e
else:
registry.registry = old
return retval
return inner
@with_local_registry
def test_django_objecttype_only_fields():
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
only_fields = ('id', 'email', 'films')
fields = list(Reporter._meta.fields.keys())
assert fields == ['id', 'email', 'films']
@with_local_registry
def test_django_objecttype_exclude_fields():
class Reporter(DjangoObjectType):
class Meta:
model = ReporterModel
exclude_fields = ('email')
fields = list(Reporter._meta.fields.keys())
assert 'email' not in fields

View File

@ -21,8 +21,7 @@ def construct_fields(options):
exclude_fields = options.exclude_fields exclude_fields = options.exclude_fields
fields = OrderedDict() fields = OrderedDict()
for field in _model_fields: for name, field in _model_fields:
name = field.name
is_not_in_only = only_fields and name not in options.only_fields is_not_in_only = only_fields and name not in options.only_fields
is_already_created = name in options.fields is_already_created = name in options.fields
is_excluded = name in exclude_fields or is_already_created is_excluded = name in exclude_fields or is_already_created
@ -34,8 +33,6 @@ def construct_fields(options):
# Or when there is no back reference. # Or when there is no back reference.
continue continue
converted = convert_django_field_with_choices(field, options.registry) converted = convert_django_field_with_choices(field, options.registry)
if not converted:
continue
fields[name] = converted fields[name] = converted
return fields return fields

View File

@ -30,11 +30,11 @@ def get_reverse_fields(model):
# Hack for making it compatible with Django 1.6 # Hack for making it compatible with Django 1.6
new_related = RelatedObject(related.parent_model, related.model, related.field) new_related = RelatedObject(related.parent_model, related.model, related.field)
new_related.name = name new_related.name = name
yield new_related yield (name, new_related)
elif isinstance(related, models.ManyToOneRel): elif isinstance(related, models.ManyToOneRel):
yield related yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical: elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
yield related yield (name, related)
def maybe_queryset(value): def maybe_queryset(value):
@ -45,8 +45,13 @@ def maybe_queryset(value):
def get_model_fields(model): def get_model_fields(model):
reverse_fields = get_reverse_fields(model) reverse_fields = get_reverse_fields(model)
all_fields = sorted(list(model._meta.fields) + all_fields = [
list(model._meta.local_many_to_many)) (field.name, field)
for field
in sorted(list(model._meta.fields) +
list(model._meta.local_many_to_many))
]
all_fields += list(reverse_fields) all_fields += list(reverse_fields)
return all_fields return all_fields