Skip to content

Commit

Permalink
Revert "Improved the global registry"
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary authored Feb 20, 2017
1 parent c635db5 commit 562d27a
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 202 deletions.
1 change: 0 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ Contents:
authorization
debug
introspection
registry
60 changes: 0 additions & 60 deletions docs/registry.rst

This file was deleted.

8 changes: 4 additions & 4 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def convert_onetoone_field_to_djangomodel(field, registry=None):
model = get_related_model(field)

def dynamic_type():
_type = registry.get_unique_type_for_model(model)
_type = registry.get_type_for_model(model)
if not _type:
return

Expand All @@ -145,7 +145,7 @@ def convert_field_to_list_or_connection(field, registry=None):
model = get_related_model(field)

def dynamic_type():
_type = registry.get_unique_type_for_model(model)
_type = registry.get_type_for_model(model)
if not _type:
return

Expand All @@ -163,7 +163,7 @@ def convert_relatedfield_to_djangomodel(field, registry=None):
model = field.model

def dynamic_type():
_type = registry.get_unique_type_for_model(model)
_type = registry.get_type_for_model(model)
if not _type:
return

Expand All @@ -183,7 +183,7 @@ def convert_field_to_djangomodel(field, registry=None):
model = get_related_model(field)

def dynamic_type():
_type = registry.get_unique_type_for_model(model)
_type = registry.get_type_for_model(model)
if not _type:
return

Expand Down
5 changes: 0 additions & 5 deletions graphene_django/debug/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
from graphene_django.utils import DJANGO_FILTER_INSTALLED

from ...tests.models import Reporter
from ...registry import reset_global_registry
from ..middleware import DjangoDebugMiddleware
from ..types import DjangoDebug


def setup_function(function):
reset_global_registry()


class context(object):
pass

Expand Down
21 changes: 4 additions & 17 deletions graphene_django/filter/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
GlobalIDMultipleChoiceField)
from graphene_django.tests.models import Article, Pet, Reporter
from graphene_django.utils import DJANGO_FILTER_INSTALLED
from graphene_django.registry import Registry, reset_global_registry

pytestmark = []

Expand All @@ -25,8 +24,6 @@


if DJANGO_FILTER_INSTALLED:
reset_global_registry()

class ArticleNode(DjangoObjectType):

class Meta:
Expand All @@ -50,10 +47,6 @@ class Meta:

# schema = Schema()

@pytest.fixture
def _registry():
return Registry()


def get_args(field):
return field.args
Expand Down Expand Up @@ -141,36 +134,33 @@ def test_filter_shortcut_filterset_extra_meta():
assert 'headline' not in field.filterset_class.get_fields()


def test_filter_filterset_information_on_meta(_registry):
def test_filter_filterset_information_on_meta():
class ReporterFilterNode(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
registry = _registry

field = DjangoFilterConnectionField(ReporterFilterNode)
assert_arguments(field, 'first_name', 'articles')
assert_not_orderable(field)


def test_filter_filterset_information_on_meta_related(_registry):
def test_filter_filterset_information_on_meta_related():
class ReporterFilterNode(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
registry = _registry

class ArticleFilterNode(DjangoObjectType):

class Meta:
model = Article
interfaces = (Node, )
filter_fields = ['headline', 'reporter']
registry = _registry

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
Expand All @@ -184,22 +174,20 @@ class Query(ObjectType):
assert_not_orderable(articles_field)


def test_filter_filterset_related_results(_registry):
def test_filter_filterset_related_results():
class ReporterFilterNode(DjangoObjectType):

class Meta:
model = Reporter
interfaces = (Node, )
filter_fields = ['first_name', 'articles']
registry = _registry

class ArticleFilterNode(DjangoObjectType):

class Meta:
interfaces = (Node, )
model = Article
filter_fields = ['headline', 'reporter']
registry = _registry

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
Expand Down Expand Up @@ -327,7 +315,7 @@ class Meta:
assert multiple_filter.field_class == GlobalIDMultipleChoiceField


def test_filter_filterset_related_results(_registry):
def test_filter_filterset_related_results():
class ReporterFilterNode(DjangoObjectType):

class Meta:
Expand All @@ -336,7 +324,6 @@ class Meta:
filter_fields = {
'first_name': ['icontains']
}
registry = _registry

class Query(ObjectType):
all_reporters = DjangoFilterConnectionField(ReporterFilterNode)
Expand Down
36 changes: 9 additions & 27 deletions graphene_django/registry.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,22 @@
from collections import defaultdict


class Registry(object):

def __init__(self):
self._registry = defaultdict(list)
self._registry = {}
self._registry_models = {}

def register(self, cls):
from .types import DjangoObjectType
model = cls._meta.model
assert issubclass(
cls, DjangoObjectType), 'Only DjangoObjectTypes can be registered, received "{}"'.format(
cls.__name__)
assert cls._meta.registry == self, 'Registry for a Model have to match.'
self._registry[model].append(cls)

def get_unique_type_for_model(self, model):
types = self.get_types_for_model(model)
if not types:
return None

# If there is more than one type for the model, we should
# raise an error so both types don't collide in the same schema.
assert len(types) == 1, (
'Found multiple ObjectTypes associated with the same Django Model "{}.{}": {}. '
'You can use a different registry for each or skip '
'the global Registry with Meta.skip_global_registry = True". '
'Read more at http://docs.graphene-python.org/projects/django/en/latest/registry/ .'
).format(
model._meta.app_label,
model._meta.object_name,
repr(types),
)
return types[0]

def get_types_for_model(self, model):
# assert self.get_type_for_model(cls._meta.model) == cls, (
# 'Multiple DjangoObjectTypes registered for "{}"'.format(cls._meta.model)
# )
if not getattr(cls._meta, 'skip_registry', False):
self._registry[cls._meta.model] = cls

def get_type_for_model(self, model):
return self._registry.get(model)


Expand Down
5 changes: 2 additions & 3 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
from ..compat import (ArrayField, HStoreField, JSONField, MissingType,
RangeField, UUIDField, DurationField)
from ..converter import convert_django_field, convert_django_field_with_choices
from ..registry import Registry, reset_global_registry
from ..registry import Registry
from ..types import DjangoObjectType
from .models import Article, Film, FilmDetails, Reporter


def setup_function(function):
reset_global_registry()
# from graphene.core.types.custom_scalars import DateTime, Time, JSONString


def assert_conversion(django_field, graphene_field, *args, **kwargs):
Expand Down
5 changes: 0 additions & 5 deletions graphene_django/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@
from ..compat import MissingType, RangeField
from ..fields import DjangoConnectionField
from ..types import DjangoObjectType
from ..registry import reset_global_registry
from .models import Article, Reporter

pytestmark = pytest.mark.django_db


def setup_function(function):
reset_global_registry()


def test_should_query_only_fields():
with raises(Exception):
class ReporterType(DjangoObjectType):
Expand Down
64 changes: 0 additions & 64 deletions graphene_django/tests/test_registry.py

This file was deleted.

6 changes: 1 addition & 5 deletions graphene_django/tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from py.test import raises

from ..registry import Registry, reset_global_registry
from ..registry import Registry
from ..types import DjangoObjectType
from .models import Reporter


def setup_function(function):
reset_global_registry()


def test_should_raise_if_no_model():
with raises(Exception) as excinfo:
class Character1(DjangoObjectType):
Expand Down
Loading

0 comments on commit 562d27a

Please sign in to comment.