Remove some compatility layers for django 1.6

This commit is contained in:
Patrick Arminio 2017-06-23 12:28:29 +01:00
parent 575e1da0ee
commit 245ebe3d91
5 changed files with 13 additions and 53 deletions

View File

@ -1,18 +1,7 @@
from django.db import models
class MissingType(object): class MissingType(object):
pass pass
try:
DurationField = models.DurationField
UUIDField = models.UUIDField
except AttributeError:
# Improved compatibility for Django 1.6
DurationField = MissingType
UUIDField = MissingType
try: try:
from django.db.models.related import RelatedObject from django.db.models.related import RelatedObject
except: except:
@ -20,13 +9,6 @@ except:
RelatedObject = MissingType RelatedObject = MissingType
try:
# Postgres fields are only available in Django 1.8+
from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
except ImportError:
ArrayField, HStoreField, JSONField, RangeField = (MissingType, ) * 4
try: try:
# Postgres fields are only available in Django 1.9+ # Postgres fields are only available in Django 1.9+
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField

View File

@ -1,5 +1,6 @@
from django.db import models from django.db import models
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List, from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
NonNull, String) NonNull, String)
@ -9,8 +10,8 @@ from graphene.types.json import JSONString
from graphene.utils.str_converters import to_camel_case, to_const from graphene.utils.str_converters import to_camel_case, to_const
from graphql import assert_valid_name from graphql import assert_valid_name
from .compat import (ArrayField, HStoreField, JSONField, RangeField,
RelatedObject, UUIDField, DurationField) from .compat import JSONField, RelatedObject
from .fields import get_connection_field, DjangoListField from .fields import get_connection_field, DjangoListField
from .utils import get_related_model, import_single_dispatch from .utils import get_related_model, import_single_dispatch
@ -80,7 +81,7 @@ def convert_field_to_string(field, registry=None):
@convert_django_field.register(models.AutoField) @convert_django_field.register(models.AutoField)
@convert_django_field.register(UUIDField) @convert_django_field.register(models.UUIDField)
def convert_field_to_id(field, registry=None): def convert_field_to_id(field, registry=None):
return ID(description=field.help_text, required=not field.null) return ID(description=field.help_text, required=not field.null)
@ -106,7 +107,7 @@ def convert_field_to_nullboolean(field, registry=None):
@convert_django_field.register(models.DecimalField) @convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.FloatField) @convert_django_field.register(models.FloatField)
@convert_django_field.register(DurationField) @convert_django_field.register(models.DurationField)
def convert_field_to_float(field, registry=None): def convert_field_to_float(field, registry=None):
return Float(description=field.help_text, required=not field.null) return Float(description=field.help_text, required=not field.null)

View File

@ -3,7 +3,6 @@ import pytest
import graphene import graphene
from graphene.relay import Node from graphene.relay import Node
from graphene_django import DjangoConnectionField, DjangoObjectType from graphene_django import DjangoConnectionField, DjangoObjectType
from graphene_django.utils import DJANGO_FILTER_INSTALLED
from ...tests.models import Reporter from ...tests.models import Reporter
from ..middleware import DjangoDebugMiddleware from ..middleware import DjangoDebugMiddleware
@ -167,8 +166,6 @@ def test_should_query_connection():
assert result.data['__debug']['sql'][1]['rawSql'] == query assert result.data['__debug']['sql'][1]['rawSql'] == query
@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
reason="requires django-filter")
def test_should_query_connectionfilter(): def test_should_query_connectionfilter():
from ...filter import DjangoFilterConnectionField from ...filter import DjangoFilterConnectionField

View File

@ -1,6 +1,8 @@
import pytest import pytest
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.contrib.postgres.fields import ArrayField, HStoreField
from py.test import raises from py.test import raises
import graphene import graphene
@ -8,8 +10,7 @@ from graphene.relay import ConnectionField, Node
from graphene.types.datetime import DateTime, Time from graphene.types.datetime import DateTime, Time
from graphene.types.json import JSONString from graphene.types.json import JSONString
from ..compat import (ArrayField, HStoreField, JSONField, MissingType, from ..compat import JSONField, MissingType
RangeField, UUIDField, DurationField)
from ..converter import convert_django_field, convert_django_field_with_choices from ..converter import convert_django_field, convert_django_field_with_choices
from ..registry import Registry from ..registry import Registry
from ..types import DjangoObjectType from ..types import DjangoObjectType
@ -84,14 +85,12 @@ def test_should_auto_convert_id():
assert_conversion(models.AutoField, graphene.ID, primary_key=True) assert_conversion(models.AutoField, graphene.ID, primary_key=True)
@pytest.mark.skipif(UUIDField == MissingType, reason="requires Django UUIDField")
def test_should_auto_convert_id(): def test_should_auto_convert_id():
assert_conversion(UUIDField, graphene.ID) assert_conversion(models.UUIDField, graphene.ID)
@pytest.mark.skipif(DurationField == MissingType, reason="requires Django DurationField")
def test_should_auto_convert_duration(): def test_should_auto_convert_duration():
assert_conversion(DurationField, graphene.Float) assert_conversion(models.DurationField, graphene.Float)
def test_should_positive_integer_convert_int(): def test_should_positive_integer_convert_int():
@ -265,8 +264,6 @@ def test_should_onetoone_reverse_convert_model():
assert dynamic_field.type == A assert dynamic_field.type == A
@pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist")
def test_should_postgres_array_convert_list(): def test_should_postgres_array_convert_list():
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100)) field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
@ -274,8 +271,6 @@ def test_should_postgres_array_convert_list():
assert field.type.of_type.of_type == graphene.String assert field.type.of_type.of_type == graphene.String
@pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist")
def test_should_postgres_array_multiple_convert_list(): def test_should_postgres_array_multiple_convert_list():
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100))) field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
@ -284,8 +279,6 @@ def test_should_postgres_array_multiple_convert_list():
assert field.type.of_type.of_type.of_type == graphene.String assert field.type.of_type.of_type.of_type == graphene.String
@pytest.mark.skipif(HStoreField is MissingType,
reason="HStoreField should exist")
def test_should_postgres_hstore_convert_string(): def test_should_postgres_hstore_convert_string():
assert_conversion(HStoreField, JSONString) assert_conversion(HStoreField, JSONString)
@ -296,8 +289,6 @@ def test_should_postgres_json_convert_string():
assert_conversion(JSONField, JSONString) assert_conversion(JSONField, JSONString)
@pytest.mark.skipif(RangeField is MissingType,
reason="RangeField should exist")
def test_should_postgres_range_convert_list(): def test_should_postgres_range_convert_list():
from django.contrib.postgres.fields import IntegerRangeField from django.contrib.postgres.fields import IntegerRangeField
field = assert_conversion(IntegerRangeField, graphene.List) field = assert_conversion(IntegerRangeField, graphene.List)

View File

@ -3,8 +3,6 @@ import inspect
from django.db import models from django.db import models
from django.db.models.manager import Manager from django.db.models.manager import Manager
from .compat import RelatedObject
# from graphene.utils import LazyList # from graphene.utils import LazyList
@ -13,12 +11,8 @@ class LazyList(object):
pass pass
try: import django_filters # noqa
import django_filters # noqa DJANGO_FILTER_INSTALLED = True
DJANGO_FILTER_INSTALLED = True
except (ImportError, AttributeError):
# AtributeError raised if DjangoFilters installed with a incompatible Django Version
DJANGO_FILTER_INSTALLED = False
def get_reverse_fields(model): def get_reverse_fields(model):
@ -26,12 +20,7 @@ def get_reverse_fields(model):
# Django =>1.9 uses 'rel', django <1.9 uses 'related' # Django =>1.9 uses 'rel', django <1.9 uses 'related'
related = getattr(attr, 'rel', None) or \ related = getattr(attr, 'rel', None) or \
getattr(attr, 'related', None) getattr(attr, 'related', None)
if isinstance(related, RelatedObject): if isinstance(related, models.ManyToOneRel):
# Hack for making it compatible with Django 1.6
new_related = RelatedObject(related.parent_model, related.model, related.field)
new_related.name = name
yield (name, new_related)
elif isinstance(related, models.ManyToOneRel):
yield (name, related) yield (name, related)
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical: elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
yield (name, related) yield (name, related)