Skip to content

Commit

Permalink
Merge pull request #268 from uoxiu/master
Browse files Browse the repository at this point in the history
Add support for the last version of DRF and Django 2 + add DateField
  • Loading branch information
nixon authored Sep 20, 2019
2 parents 2fe6de5 + 29abf77 commit ced3b1d
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ __pycache__/
# Distribution / packaging
.Python
.env/
venv/
bin/
build/
develop-eggs/
Expand Down
29 changes: 6 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,23 @@
language: python

python:
- "3.5"
- "3.6"

services: mongodb

sudo: false

env:
- TOX_ENV=py27-lint
# We don't have documentation source in the repo, so no py27-docs yet:
# - TOX_ENV=py27-docs
- TOX_ENV=dj18-py27-me09
- TOX_ENV=dj18-py35-me09
- TOX_ENV=dj18-py27-me010
- TOX_ENV=dj18-py35-me010
- TOX_ENV=dj18-py27-me011
- TOX_ENV=dj18-py35-me011
- TOX_ENV=dj19-py27-me09
- TOX_ENV=dj19-py35-me09
- TOX_ENV=dj19-py27-me010
- TOX_ENV=dj19-py35-me010
- TOX_ENV=dj19-py27-me011
- TOX_ENV=dj19-py35-me011
- TOX_ENV=dj110-py27-me09
- TOX_ENV=dj110-py35-me09
- TOX_ENV=dj110-py27-me010
- TOX_ENV=dj110-py35-me010
- TOX_ENV=dj110-py27-me011
- TOX_ENV=dj110-py35-me011
- TOX_ENV=dj111-py36-me016
- TOX_ENV=dj111-py36-me018
- TOX_ENV=dj22-py36-me016
- TOX_ENV=dj22-py36-me018

matrix:
fast_finish: true

install:
- pip install tox "virtualenv<14" djangorestframework codecov
- pip install tox virtualenv djangorestframework codecov

script:
- tox -e $TOX_ENV
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements-testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pytest-cov
pytest-django
mock
six

pytz
4 changes: 2 additions & 2 deletions rest_framework_mongoengine/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from mongoengine.base import BaseDocument
from mongoengine.fields import BaseField
from mongoengine.queryset import QuerySet
from rest_framework.compat import unicode_repr
from rest_framework.fields import Field

from rest_framework_mongoengine.fields import DictField
Expand Down Expand Up @@ -39,6 +38,7 @@ def mongo_doc_repr(value):
u = '[Bad Unicode data]'
return force_str('<%s: %s>' % (value.__class__.__name__, u))


uni_lit_re = re.compile("u'(.*?)'")


Expand All @@ -55,7 +55,7 @@ def smart_repr(value):
if isinstance(value, Field):
return field_repr(value)

value = unicode_repr(value)
value = repr(value)

# Representations like u'help text'
# should simply be presented as 'help text'
Expand Down
5 changes: 3 additions & 2 deletions rest_framework_mongoengine/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from mongoengine.errors import ValidationError as me_ValidationError
from rest_framework import fields as drf_fields
from rest_framework import serializers
from rest_framework.compat import unicode_to_repr
from rest_framework.serializers import ALL_FIELDS
from rest_framework.utils.field_mapping import ClassLookupDict

Expand Down Expand Up @@ -87,6 +86,7 @@ class DocumentSerializer(serializers.ModelSerializer):
* ``UUIDField``
* ``GeoPointField``
* ``GeoJsonBaseField`` (all those fields)
* ``DateField``
Compound fields: ``ListField`` and ``DictField`` are mapped to corresponding DRF fields, with respect to nested field specification.
Expand Down Expand Up @@ -115,6 +115,7 @@ class DocumentSerializer(serializers.ModelSerializer):
me_fields.DecimalField: drf_fields.DecimalField,
me_fields.BooleanField: drf_fields.BooleanField,
me_fields.DateTimeField: drf_fields.DateTimeField,
me_fields.DateField: drf_fields.DateField,
me_fields.ComplexDateTimeField: drf_fields.DateTimeField,
me_fields.ObjectIdField: drfm_fields.ObjectIdField,
me_fields.FileField: drfm_fields.FileField,
Expand Down Expand Up @@ -796,7 +797,7 @@ def get_unique_for_date_validators(self):
return []

def __repr__(self):
return unicode_to_repr(serializer_repr(self, indent=1))
return serializer_repr(self, indent=1)


class EmbeddedDocumentSerializer(DocumentSerializer):
Expand Down
9 changes: 4 additions & 5 deletions rest_framework_mongoengine/validators.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import unicode_literals

from rest_framework import validators
from rest_framework.compat import unicode_to_repr
from rest_framework.exceptions import ValidationError
from rest_framework.fields import SkipField

Expand All @@ -28,10 +27,10 @@ def __call__(self, value):
raise ValidationError(self.message.format())

def __repr__(self):
return unicode_to_repr('<%s(queryset=%s)>' % (
return '<%s(queryset=%s)>' % (
self.__class__.__name__,
smart_repr(self.queryset)
))
)


class UniqueTogetherValidator(MongoValidatorMixin, validators.UniqueTogetherValidator):
Expand All @@ -56,11 +55,11 @@ def __call__(self, attrs):
raise ValidationError(self.message.format(field_names=field_names))

def __repr__(self):
return unicode_to_repr('<%s(queryset=%s, fields=%s)>' % (
return '<%s(queryset=%s, fields=%s)>' % (
self.__class__.__name__,
smart_repr(self.queryset),
smart_repr(self.fields)
))
)


class OptionalUniqueTogetherValidator(UniqueTogetherValidator):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_package_data(package):

setup(
name='django-rest-framework-mongoengine',
version='3.3.1',
version='3.4.0',
description='MongoEngine support for Django Rest Framework.',
packages=get_packages('rest_framework_mongoengine'),
package_data=get_package_data('rest_framework_mongoengine'),
Expand Down
17 changes: 8 additions & 9 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from django.test import TestCase
from mongoengine import Document, fields
from rest_framework import serializers
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import DocumentSerializer

Expand Down Expand Up @@ -128,7 +127,7 @@ class Meta:
custom_field = DocumentField(model_field=<tests.test_basic.CustomField: custom_field>, required=False)
""" % regex_repr)

assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_meta_fields(self):
"""
Expand All @@ -145,7 +144,7 @@ class Meta:
str_field = CharField(required=False)
""")

assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_meta_exclude(self):
"""
Expand Down Expand Up @@ -180,7 +179,7 @@ class Meta:
id_field = ObjectIdField(required=False)
""" % regex_repr)

assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_field_options(self):
class TestSerializer(DocumentSerializer):
Expand Down Expand Up @@ -209,7 +208,7 @@ class Meta:
# "('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')",
# "(u'red', u'Red'), (u'blue', u'Blue'), (u'green', u'Green')"
# )
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_method_field(self):
"""
Expand All @@ -226,7 +225,7 @@ class Meta:
id = ObjectIdField(read_only=True)
method = ReadOnlyField()
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_pk_fields(self):
"""
Expand All @@ -242,7 +241,7 @@ class Meta:
pk = IntegerField(read_only=True)
auto_field = IntegerField(read_only=True)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_id_field(self):
"""
Expand All @@ -257,7 +256,7 @@ class Meta:
TestSerializer():
id = ObjectIdField(read_only=True)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_extra_field_kwargs(self):
"""
Expand All @@ -274,7 +273,7 @@ class Meta:
id = ObjectIdField(read_only=True)
str_field = CharField(default='extra')
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_invalid_field(self):
"""
Expand Down
7 changes: 3 additions & 4 deletions tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from django.test import TestCase
from mongoengine import Document, fields
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import DocumentSerializer

Expand Down Expand Up @@ -47,7 +46,7 @@ class Meta:
int_dict_field = DictField(child=IntegerField(required=False), required=False)
int_map_field = DictField(child=IntegerField(required=False), required=False)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_suboptions(self):
class TestSerializer(DocumentSerializer):
Expand All @@ -60,7 +59,7 @@ class Meta:
id = ObjectIdField(read_only=True)
int_list_field = ListField(child=IntegerField(max_value=7, min_value=3, required=False), required=False)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_nested(self):
class TestSerializer(DocumentSerializer):
Expand All @@ -75,7 +74,7 @@ class Meta:
list_dict_field = DictField(child=ListField(required=False), required=False)
list_dict_list_field = ListField(child=DictField(child=ListField(required=False), required=False), required=False)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected


class TestSerializer(DocumentSerializer):
Expand Down
3 changes: 1 addition & 2 deletions tests/test_dumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.test import TestCase
from rest_framework import fields
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import DocumentSerializer

Expand Down Expand Up @@ -49,7 +48,7 @@ class Meta:
""")

# better output then self.assertEqual()
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected


class TestSerializer(DocumentSerializer):
Expand Down
9 changes: 4 additions & 5 deletions tests/test_dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from django.test import TestCase
from rest_framework import fields as drf_fields
from rest_framework.compat import unicode_repr

from rest_framework_mongoengine.serializers import (
DocumentSerializer, DynamicDocumentSerializer, EmbeddedDocumentSerializer
Expand Down Expand Up @@ -83,7 +82,7 @@ class Meta:
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected

def test_extended(self):
class TestSerializer(DynamicDocumentSerializer):
Expand All @@ -100,7 +99,7 @@ class Meta:
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert unicode_repr(TestSerializer()) == expected
assert repr(TestSerializer()) == expected


class TestSerializer(DynamicDocumentSerializer):
Expand Down Expand Up @@ -216,7 +215,7 @@ def test_repr(self):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert unicode_repr(EmbeddingDynamicSerializer()) == expected
assert repr(EmbeddingDynamicSerializer()) == expected


class TestEmbeddingDynamicIntegration(TestCase):
Expand Down Expand Up @@ -326,7 +325,7 @@ def test_repr(self):
name = CharField(required=False)
foo = IntegerField(required=False)
""")
assert unicode_repr(DocumentEmbeddingDynamicSerializer()) == expected
assert repr(DocumentEmbeddingDynamicSerializer()) == expected


class TestDocumentEmbeddingDynamicIntegration(TestCase):
Expand Down
Loading

0 comments on commit ced3b1d

Please sign in to comment.