mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-26 03:24:07 +03:00
Remove some compatility layers for django 1.6
This commit is contained in:
parent
575e1da0ee
commit
245ebe3d91
|
@ -1,18 +1,7 @@
|
|||
from django.db import models
|
||||
|
||||
|
||||
class MissingType(object):
|
||||
pass
|
||||
|
||||
|
||||
try:
|
||||
DurationField = models.DurationField
|
||||
UUIDField = models.UUIDField
|
||||
except AttributeError:
|
||||
# Improved compatibility for Django 1.6
|
||||
DurationField = MissingType
|
||||
UUIDField = MissingType
|
||||
|
||||
try:
|
||||
from django.db.models.related import RelatedObject
|
||||
except:
|
||||
|
@ -20,13 +9,6 @@ except:
|
|||
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:
|
||||
# Postgres fields are only available in Django 1.9+
|
||||
from django.contrib.postgres.fields import JSONField
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
from django.db import models
|
||||
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,
|
||||
NonNull, String)
|
||||
|
@ -9,8 +10,8 @@ from graphene.types.json import JSONString
|
|||
from graphene.utils.str_converters import to_camel_case, to_const
|
||||
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 .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(UUIDField)
|
||||
@convert_django_field.register(models.UUIDField)
|
||||
def convert_field_to_id(field, registry=None):
|
||||
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.FloatField)
|
||||
@convert_django_field.register(DurationField)
|
||||
@convert_django_field.register(models.DurationField)
|
||||
def convert_field_to_float(field, registry=None):
|
||||
return Float(description=field.help_text, required=not field.null)
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ import pytest
|
|||
import graphene
|
||||
from graphene.relay import Node
|
||||
from graphene_django import DjangoConnectionField, DjangoObjectType
|
||||
from graphene_django.utils import DJANGO_FILTER_INSTALLED
|
||||
|
||||
from ...tests.models import Reporter
|
||||
from ..middleware import DjangoDebugMiddleware
|
||||
|
@ -167,8 +166,6 @@ def test_should_query_connection():
|
|||
assert result.data['__debug']['sql'][1]['rawSql'] == query
|
||||
|
||||
|
||||
@pytest.mark.skipif(not DJANGO_FILTER_INSTALLED,
|
||||
reason="requires django-filter")
|
||||
def test_should_query_connectionfilter():
|
||||
from ...filter import DjangoFilterConnectionField
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import pytest
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.contrib.postgres.fields import ArrayField, HStoreField
|
||||
|
||||
from py.test import raises
|
||||
|
||||
import graphene
|
||||
|
@ -8,8 +10,7 @@ from graphene.relay import ConnectionField, Node
|
|||
from graphene.types.datetime import DateTime, Time
|
||||
from graphene.types.json import JSONString
|
||||
|
||||
from ..compat import (ArrayField, HStoreField, JSONField, MissingType,
|
||||
RangeField, UUIDField, DurationField)
|
||||
from ..compat import JSONField, MissingType
|
||||
from ..converter import convert_django_field, convert_django_field_with_choices
|
||||
from ..registry import Registry
|
||||
from ..types import DjangoObjectType
|
||||
|
@ -84,14 +85,12 @@ def test_should_auto_convert_id():
|
|||
assert_conversion(models.AutoField, graphene.ID, primary_key=True)
|
||||
|
||||
|
||||
@pytest.mark.skipif(UUIDField == MissingType, reason="requires Django UUIDField")
|
||||
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():
|
||||
assert_conversion(DurationField, graphene.Float)
|
||||
assert_conversion(models.DurationField, graphene.Float)
|
||||
|
||||
|
||||
def test_should_positive_integer_convert_int():
|
||||
|
@ -265,8 +264,6 @@ def test_should_onetoone_reverse_convert_model():
|
|||
assert dynamic_field.type == A
|
||||
|
||||
|
||||
@pytest.mark.skipif(ArrayField is MissingType,
|
||||
reason="ArrayField should exist")
|
||||
def test_should_postgres_array_convert_list():
|
||||
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.skipif(ArrayField is MissingType,
|
||||
reason="ArrayField should exist")
|
||||
def test_should_postgres_array_multiple_convert_list():
|
||||
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
|
||||
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
|
||||
|
||||
|
||||
@pytest.mark.skipif(HStoreField is MissingType,
|
||||
reason="HStoreField should exist")
|
||||
def test_should_postgres_hstore_convert_string():
|
||||
assert_conversion(HStoreField, JSONString)
|
||||
|
||||
|
@ -296,8 +289,6 @@ def test_should_postgres_json_convert_string():
|
|||
assert_conversion(JSONField, JSONString)
|
||||
|
||||
|
||||
@pytest.mark.skipif(RangeField is MissingType,
|
||||
reason="RangeField should exist")
|
||||
def test_should_postgres_range_convert_list():
|
||||
from django.contrib.postgres.fields import IntegerRangeField
|
||||
field = assert_conversion(IntegerRangeField, graphene.List)
|
||||
|
|
|
@ -3,8 +3,6 @@ import inspect
|
|||
from django.db import models
|
||||
from django.db.models.manager import Manager
|
||||
|
||||
from .compat import RelatedObject
|
||||
|
||||
|
||||
# from graphene.utils import LazyList
|
||||
|
||||
|
@ -13,12 +11,8 @@ class LazyList(object):
|
|||
pass
|
||||
|
||||
|
||||
try:
|
||||
import django_filters # noqa
|
||||
DJANGO_FILTER_INSTALLED = True
|
||||
except (ImportError, AttributeError):
|
||||
# AtributeError raised if DjangoFilters installed with a incompatible Django Version
|
||||
DJANGO_FILTER_INSTALLED = False
|
||||
import django_filters # noqa
|
||||
DJANGO_FILTER_INSTALLED = True
|
||||
|
||||
|
||||
def get_reverse_fields(model):
|
||||
|
@ -26,12 +20,7 @@ def get_reverse_fields(model):
|
|||
# Django =>1.9 uses 'rel', django <1.9 uses 'related'
|
||||
related = getattr(attr, 'rel', None) or \
|
||||
getattr(attr, 'related', None)
|
||||
if isinstance(related, RelatedObject):
|
||||
# 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):
|
||||
if isinstance(related, models.ManyToOneRel):
|
||||
yield (name, related)
|
||||
elif isinstance(related, models.ManyToManyRel) and not related.symmetrical:
|
||||
yield (name, related)
|
||||
|
|
Loading…
Reference in New Issue
Block a user