Skip to content

Commit

Permalink
DetailSerializerMixin compatibility with DRF>=3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Chibisov Gennady committed Oct 9, 2014
1 parent dfa0be6 commit 60e4716
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 17 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ env:
- TOX_ENV=py27-drf2.4.0
- TOX_ENV=py27-drf2.4.1
- TOX_ENV=py27-drf2.4.2
- TOX_ENV=py27-drf3.0
- TOX_ENV=py3
- TOX_ENV=django1.5
- TOX_ENV=django1.6
Expand Down
1 change: 0 additions & 1 deletion rest_framework_extensions/compat_drf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def add_trailing_slash_if_needed(regexp_string):

def get_lookup_allowed_symbols(kwarg_name='pk', force_dot=False):
# todo: test me

if get_rest_framework_features()['use_dot_in_lookup_regex_by_default'] or force_dot:
return '(?P<{0}>[^/.]+)'.format(kwarg_name)
else:
Expand Down
20 changes: 11 additions & 9 deletions rest_framework_extensions/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ class DetailSerializerMixin(object):
def get_serializer_class(self):
error_message = "'{0}' should include a 'serializer_detail_class' attribute".format(self.__class__.__name__)
assert self.serializer_detail_class is not None, error_message
if getattr(self, 'object', None):
if self._is_request_to_detail_endpoint():
return self.serializer_detail_class
else:
return super(DetailSerializerMixin, self).get_serializer_class()

def get_object(self, queryset=None):
if queryset is None:
queryset = self.get_queryset_detail()
return super(DetailSerializerMixin, self).get_object(queryset=queryset)
def get_queryset(self, *args, **kwargs):
if self._is_request_to_detail_endpoint() and self.queryset_detail is not None:
return self.queryset_detail.all() # todo: test all()
else:
return super(DetailSerializerMixin, self).get_queryset(*args, **kwargs)

def get_queryset_detail(self):
if self.queryset_detail is not None:
return self.queryset_detail._clone() # todo: test _clone()
def _is_request_to_detail_endpoint(self):
if hasattr(self, 'lookup_url_kwarg'):
kwarg_name = self.lookup_url_kwarg or self.lookup_field
else:
return self.get_queryset()
kwarg_name = self.pk_url_kwarg or self.slug_url_kwarg
return kwarg_name and kwarg_name in self.kwargs


class PaginateByMaxMixin(object):
Expand Down
3 changes: 2 additions & 1 deletion rest_framework_extensions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def get_rest_framework_features():
'max_paginate_by': get_rest_framework_version() >= (2, 3, 8),
'django_object_permissions_class': get_rest_framework_version() >= (2, 3, 8),
'save_related_serializers': get_rest_framework_version() >= (2, 3, 8), # todo: test me
'write_only_fields': get_rest_framework_version() >= (2, 3, 11) # todo: test me
'write_only_fields': get_rest_framework_version() >= (2, 3, 11), # todo: test me
'get_object_has_queryset_argument': get_rest_framework_version() < (3, 0) # todo: test me
}


Expand Down
2 changes: 1 addition & 1 deletion tests_app/tests/functional/key_constructor/bits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@


class UserModelViewSet(ListETAGMixin, RetrieveETAGMixin, viewsets.ModelViewSet):
model = UserModel
queryset = UserModel.objects.all()
filter_backends = (DjangoFilterBackend,)
filter_fields = ('property',)
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_serializer_detail_class_response(self):
'email': '[email protected]',
'content': 'Hello world',
}
self.assertEqual(resp.data, expected)
self.assertEqual(resp.data, expected, 'should use detail serializer for detail endpoint')

def test_view_with_mixin_and_without__serializer_detail_class__should_raise_exception(self):
msg = "'CommentWithoutDetailSerializerClassViewSet' should include a 'serializer_detail_class' attribute"
Expand Down
11 changes: 7 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ deps=
djangorestframework==2.4.2
django-guardian==1.1.1

[testenv:py27-drf3.0]
deps=
{[testenv]deps}
Django>=1.5,<1.6
https://github.com/tomchristie/django-rest-framework/archive/version-3.0.zip
django-guardian==1.1.1

[testenv:py3]
basepython = python3
deps=
{[testenv]deps}
Django>=1.5,<1.6
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.5]
Expand All @@ -133,7 +139,6 @@ deps=
{[testenv]deps}
Django>=1.5,<1.6
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.6]
Expand All @@ -142,7 +147,6 @@ deps=
{[testenv]deps}
Django>=1.6,<1.7
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.1.1

[testenv:django1.7]
Expand All @@ -151,5 +155,4 @@ deps=
{[testenv]deps}
Django>=1.7,<1.8
djangorestframework==2.4.2
# todo: use 2.3.14 when https://github.com/tomchristie/django-rest-framework/issues/1642 closed
django-guardian==1.2

0 comments on commit 60e4716

Please sign in to comment.