From 921e4ed2ee11edffd19d2ca40f10d47d2c148ea1 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Mon, 28 Jul 2014 16:59:55 +0900 Subject: [PATCH 001/100] Evaluate content before passing to regex.sub Issue #1708 --- rest_framework/tests/test_description.py | 22 ++++++++++++++++++++++ rest_framework/utils/formatting.py | 4 +--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/rest_framework/tests/test_description.py b/rest_framework/tests/test_description.py index 4c03c1ded..52fa55fb8 100644 --- a/rest_framework/tests/test_description.py +++ b/rest_framework/tests/test_description.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals from django.test import TestCase +from django.utils.encoding import python_2_unicode_compatible from rest_framework.compat import apply_markdown, smart_text from rest_framework.views import APIView from rest_framework.tests.description import ViewWithNonASCIICharactersInDocstring @@ -98,6 +99,27 @@ class TestViewNamesAndDescriptions(TestCase): pass self.assertEqual(MockView().get_view_description(), '') + def test_view_description_can_be_promise(self): + """ + Ensure a view may have a docstring that is actually a lazily evaluated + class that can be converted to a string. + + See: https://github.com/tomchristie/django-rest-framework/issues/1708 + """ + # use a mock object instead of gettext_lazy to ensure that we can't end + # up with a test case string in our l10n catalog + @python_2_unicode_compatible + class MockLazyStr(object): + def __init__(self, string): + self.s = string + def __str__(self): + return self.s + + class MockView(APIView): + __doc__ = MockLazyStr("a gettext string") + + self.assertEqual(MockView().get_view_description(), 'a gettext string') + def test_markdown(self): """ Ensure markdown to HTML works as expected. diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 4b59ba840..12b79b6cd 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -6,8 +6,6 @@ from __future__ import unicode_literals from django.utils.html import escape from django.utils.safestring import mark_safe from rest_framework.compat import apply_markdown -from rest_framework.settings import api_settings -from textwrap import dedent import re @@ -36,7 +34,7 @@ def dedent(content): # unindent the content if needed if whitespace_counts: whitespace_pattern = '^' + (' ' * min(whitespace_counts)) - content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) + content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', unicode(content)) return content.strip() From 66fa40c300b4d3e768b4a7993f020056c44fdda3 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Tue, 29 Jul 2014 22:13:11 +0900 Subject: [PATCH 002/100] evaluate content at function start --- rest_framework/utils/formatting.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 12b79b6cd..2b3cbc957 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -28,13 +28,14 @@ def dedent(content): as it fails to dedent multiline docstrings that include unindented text on the initial line. """ + content = unicode(content) whitespace_counts = [len(line) - len(line.lstrip(' ')) for line in content.splitlines()[1:] if line.lstrip()] # unindent the content if needed if whitespace_counts: whitespace_pattern = '^' + (' ' * min(whitespace_counts)) - content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', unicode(content)) + content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content) return content.strip() From 192201d5840f13c8b96f44fdce4645edeb653f0f Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Thu, 7 Aug 2014 15:48:29 +0900 Subject: [PATCH 003/100] remove dep on python_2_unicode_compatible python_2_unicode_compatible is not available in all Django versions --- rest_framework/tests/test_description.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/tests/test_description.py b/rest_framework/tests/test_description.py index 52fa55fb8..8aa162613 100644 --- a/rest_framework/tests/test_description.py +++ b/rest_framework/tests/test_description.py @@ -2,7 +2,6 @@ from __future__ import unicode_literals from django.test import TestCase -from django.utils.encoding import python_2_unicode_compatible from rest_framework.compat import apply_markdown, smart_text from rest_framework.views import APIView from rest_framework.tests.description import ViewWithNonASCIICharactersInDocstring @@ -108,17 +107,18 @@ class TestViewNamesAndDescriptions(TestCase): """ # use a mock object instead of gettext_lazy to ensure that we can't end # up with a test case string in our l10n catalog - @python_2_unicode_compatible class MockLazyStr(object): def __init__(self, string): self.s = string def __str__(self): return self.s + def __unicode__(self): + return self.s class MockView(APIView): - __doc__ = MockLazyStr("a gettext string") + __doc__ = MockLazyStr(u"a gettext string") - self.assertEqual(MockView().get_view_description(), 'a gettext string') + self.assertEqual(MockView().get_view_description(), u'a gettext string') def test_markdown(self): """ From 3e93c96ece8af010185e1fe1188dd2df569d4528 Mon Sep 17 00:00:00 2001 From: Paul Oswald Date: Tue, 19 Aug 2014 10:09:48 +0900 Subject: [PATCH 004/100] replace unicode call with force_text --- rest_framework/utils/formatting.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 2b3cbc957..40bced5f1 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -5,6 +5,8 @@ from __future__ import unicode_literals from django.utils.html import escape from django.utils.safestring import mark_safe +from django.utils.encoding import force_text + from rest_framework.compat import apply_markdown import re @@ -28,7 +30,7 @@ def dedent(content): as it fails to dedent multiline docstrings that include unindented text on the initial line. """ - content = unicode(content) + content = force_text(content) whitespace_counts = [len(line) - len(line.lstrip(' ')) for line in content.splitlines()[1:] if line.lstrip()] From d8eb9e6d45c227582559ec4318b1f92562c718da Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 10:48:16 +0100 Subject: [PATCH 005/100] Docs whitespace fix. --- docs/api-guide/generic-views.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md index b1c4e65ad..d30b7f9bf 100755 --- a/docs/api-guide/generic-views.md +++ b/docs/api-guide/generic-views.md @@ -19,8 +19,8 @@ Typically when using the generic views, you'll override the view, and set severa from django.contrib.auth.models import User from myapp.serializers import UserSerializer - from rest_framework import generics - from rest_framework.permissions import IsAdminUser + from rest_framework import generics + from rest_framework.permissions import IsAdminUser class UserList(generics.ListCreateAPIView): queryset = User.objects.all() From f62c874ea9621ae67fb56e7e453dca8fd5039051 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 10:48:40 +0100 Subject: [PATCH 006/100] Remove `filter_backend`. Closes #1775. --- rest_framework/generics.py | 20 +------------------- rest_framework/settings.py | 10 ---------- 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index a6f686571..8bacf470b 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -83,7 +83,6 @@ class GenericAPIView(views.APIView): slug_url_kwarg = 'slug' slug_field = 'slug' allow_empty = True - filter_backend = api_settings.FILTER_BACKEND def get_serializer_context(self): """ @@ -191,24 +190,7 @@ class GenericAPIView(views.APIView): """ Returns the list of filter backends that this view requires. """ - if self.filter_backends is None: - filter_backends = [] - else: - # Note that we are returning a *copy* of the class attribute, - # so that it is safe for the view to mutate it if needed. - filter_backends = list(self.filter_backends) - - if not filter_backends and self.filter_backend: - warnings.warn( - 'The `filter_backend` attribute and `FILTER_BACKEND` setting ' - 'are deprecated in favor of a `filter_backends` ' - 'attribute and `DEFAULT_FILTER_BACKENDS` setting, that take ' - 'a *list* of filter backend classes.', - DeprecationWarning, stacklevel=2 - ) - filter_backends = [self.filter_backend] - - return filter_backends + return list(self.filter_backends) # The following methods provide default implementations # that you may want to override for more complex cases. diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 644751f87..bbe7a56ad 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -111,9 +111,6 @@ DEFAULTS = { ), 'TIME_FORMAT': None, - # Pending deprecation - 'FILTER_BACKEND': None, - } @@ -129,7 +126,6 @@ IMPORT_STRINGS = ( 'DEFAULT_PAGINATION_SERIALIZER_CLASS', 'DEFAULT_FILTER_BACKENDS', 'EXCEPTION_HANDLER', - 'FILTER_BACKEND', 'TEST_REQUEST_RENDERER_CLASSES', 'UNAUTHENTICATED_USER', 'UNAUTHENTICATED_TOKEN', @@ -196,15 +192,9 @@ class APISettings(object): if val and attr in self.import_strings: val = perform_import(val, attr) - self.validate_setting(attr, val) - # Cache the result setattr(self, attr, val) return val - def validate_setting(self, attr, val): - if attr == 'FILTER_BACKEND' and val is not None: - # Make sure we can initialize the class - val() api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS) From 0f8fdf4e72b67ff46474c13c8b532bf319a58099 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 10:57:24 +0100 Subject: [PATCH 007/100] Remove `allow_empty`. Closes #1774. --- docs/api-guide/generic-views.md | 2 -- rest_framework/generics.py | 12 +----------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/docs/api-guide/generic-views.md b/docs/api-guide/generic-views.md index d30b7f9bf..49be0cae8 100755 --- a/docs/api-guide/generic-views.md +++ b/docs/api-guide/generic-views.md @@ -212,8 +212,6 @@ Provides a `.list(request, *args, **kwargs)` method, that implements listing a q If the queryset is populated, this returns a `200 OK` response, with a serialized representation of the queryset as the body of the response. The response data may optionally be paginated. -If the queryset is empty this returns a `200 OK` response, unless the `.allow_empty` attribute on the view is set to `False`, in which case it will return a `404 Not Found`. - ## CreateModelMixin Provides a `.create(request, *args, **kwargs)` method, that implements creating and saving a new model instance. diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 8bacf470b..cb8061b75 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -82,7 +82,6 @@ class GenericAPIView(views.APIView): pk_url_kwarg = 'pk' slug_url_kwarg = 'slug' slug_field = 'slug' - allow_empty = True def get_serializer_context(self): """ @@ -140,16 +139,7 @@ class GenericAPIView(views.APIView): if not page_size: return None - if not self.allow_empty: - warnings.warn( - 'The `allow_empty` parameter is deprecated. ' - 'To use `allow_empty=False` style behavior, You should override ' - '`get_queryset()` and explicitly raise a 404 on empty querysets.', - DeprecationWarning, stacklevel=2 - ) - - paginator = self.paginator_class(queryset, page_size, - allow_empty_first_page=self.allow_empty) + paginator = self.paginator_class(queryset, page_size) page_kwarg = self.kwargs.get(self.page_kwarg) page_query_param = self.request.QUERY_PARAMS.get(self.page_kwarg) page = page_kwarg or page_query_param or 1 From b3bbf416707cf8c71861b0fd6e966a557acef412 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 11:09:35 +0100 Subject: [PATCH 008/100] Remove `allow_empty` --- rest_framework/mixins.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 2cc87eef1..dc4c9f353 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -70,24 +70,9 @@ class ListModelMixin(object): """ List a queryset. """ - empty_error = "Empty list and '%(class_name)s.allow_empty' is False." - def list(self, request, *args, **kwargs): self.object_list = self.filter_queryset(self.get_queryset()) - # Default is to allow empty querysets. This can be altered by setting - # `.allow_empty = False`, to raise 404 errors on empty querysets. - if not self.allow_empty and not self.object_list: - warnings.warn( - 'The `allow_empty` parameter is deprecated. ' - 'To use `allow_empty=False` style behavior, You should override ' - '`get_queryset()` and explicitly raise a 404 on empty querysets.', - DeprecationWarning - ) - class_name = self.__class__.__name__ - error_msg = self.empty_error % {'class_name': class_name} - raise Http404(error_msg) - # Switch between paginated or standard style responses page = self.paginate_queryset(self.object_list) if page is not None: From e5e6329a222def3b0745f90fc55ee36de95ada83 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 11:29:26 +0100 Subject: [PATCH 009/100] Remove `pk_url_field`, `slug_url_field`, `slug_field`. Closes #1773. --- rest_framework/generics.py | 36 ++--------- rest_framework/mixins.py | 36 +++-------- rest_framework/relations.py | 117 ++---------------------------------- 3 files changed, 15 insertions(+), 174 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index cb8061b75..e21dc5c7b 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -76,13 +76,6 @@ class GenericAPIView(views.APIView): model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS paginator_class = Paginator - ###################################### - # These are pending deprecation... - - pk_url_kwarg = 'pk' - slug_url_kwarg = 'slug' - slug_field = 'slug' - def get_serializer_context(self): """ Extra context provided to the serializer class. @@ -270,7 +263,7 @@ class GenericAPIView(views.APIView): error_format = "'%s' must define 'queryset' or 'model'" raise ImproperlyConfigured(error_format % self.__class__.__name__) - def get_object(self, queryset=None): + def get_object(self): """ Returns the object the view is displaying. @@ -278,36 +271,14 @@ class GenericAPIView(views.APIView): queryset lookups. Eg if objects are referenced using multiple keyword arguments in the url conf. """ - # Determine the base queryset to use. - if queryset is None: - queryset = self.filter_queryset(self.get_queryset()) - else: - pass # Deprecation warning + queryset = self.filter_queryset(self.get_queryset()) # Perform the lookup filtering. # Note that `pk` and `slug` are deprecated styles of lookup filtering. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field lookup = self.kwargs.get(lookup_url_kwarg, None) - pk = self.kwargs.get(self.pk_url_kwarg, None) - slug = self.kwargs.get(self.slug_url_kwarg, None) - if lookup is not None: - filter_kwargs = {self.lookup_field: lookup} - elif pk is not None and self.lookup_field == 'pk': - warnings.warn( - 'The `pk_url_kwarg` attribute is deprecated. ' - 'Use the `lookup_field` attribute instead', - DeprecationWarning - ) - filter_kwargs = {'pk': pk} - elif slug is not None and self.lookup_field == 'pk': - warnings.warn( - 'The `slug_url_kwarg` attribute is deprecated. ' - 'Use the `lookup_field` attribute instead', - DeprecationWarning - ) - filter_kwargs = {self.slug_field: slug} - else: + if lookup is None: raise ImproperlyConfigured( 'Expected view %s to be called with a URL keyword argument ' 'named "%s". Fix your URL conf, or set the `.lookup_field` ' @@ -315,6 +286,7 @@ class GenericAPIView(views.APIView): (self.__class__.__name__, self.lookup_field) ) + filter_kwargs = {self.lookup_field: lookup} obj = get_object_or_404(queryset, **filter_kwargs) # May raise a permission denied diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index dc4c9f353..ac59d9795 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -12,10 +12,9 @@ from rest_framework import status from rest_framework.response import Response from rest_framework.request import clone_request from rest_framework.settings import api_settings -import warnings -def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None): +def _get_validation_exclusions(obj, lookup_field=None): """ Given a model instance, and an optional pk and slug field, return the full list of all other field names on that model. @@ -23,23 +22,13 @@ def _get_validation_exclusions(obj, pk=None, slug_field=None, lookup_field=None) For use when performing full_clean on a model instance, so we only clean the required fields. """ - include = [] - - if pk: - # Deprecated + if lookup_field == 'pk': pk_field = obj._meta.pk while pk_field.rel: pk_field = pk_field.rel.to._meta.pk - include.append(pk_field.name) + lookup_field = pk_field.name - if slug_field: - # Deprecated - include.append(slug_field) - - if lookup_field and lookup_field != 'pk': - include.append(lookup_field) - - return [field.name for field in obj._meta.fields if field.name not in include] + return [field.name for field in obj._meta.fields if field.name != lookup_field] class CreateModelMixin(object): @@ -146,26 +135,15 @@ class UpdateModelMixin(object): """ Set any attributes on the object that are implicit in the request. """ - # pk and/or slug attributes are implicit in the URL. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - lookup = self.kwargs.get(lookup_url_kwarg, None) - pk = self.kwargs.get(self.pk_url_kwarg, None) - slug = self.kwargs.get(self.slug_url_kwarg, None) - slug_field = slug and self.slug_field or None + lookup_value = self.kwargs[lookup_url_kwarg] - if lookup: - setattr(obj, self.lookup_field, lookup) - - if pk: - setattr(obj, 'pk', pk) - - if slug: - setattr(obj, slug_field, slug) + setattr(obj, self.lookup_field, lookup_value) # Ensure we clean the attributes so that we don't eg return integer # pk using a string representation, as provided by the url conf kwarg. if hasattr(obj, 'full_clean'): - exclude = _get_validation_exclusions(obj, pk, slug_field, self.lookup_field) + exclude = _get_validation_exclusions(obj, self.lookup_field) obj.full_clean(exclude) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 1acbdce26..56870b408 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -16,7 +16,6 @@ from rest_framework.fields import Field, WritableField, get_component, is_simple from rest_framework.reverse import reverse from rest_framework.compat import urlparse from rest_framework.compat import smart_text -import warnings # Relational fields @@ -320,11 +319,6 @@ class HyperlinkedRelatedField(RelatedField): 'incorrect_type': _('Incorrect type. Expected url string, received %s.'), } - # These are all deprecated - pk_url_kwarg = 'pk' - slug_field = 'slug' - slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden - def __init__(self, *args, **kwargs): try: self.view_name = kwargs.pop('view_name') @@ -334,22 +328,6 @@ class HyperlinkedRelatedField(RelatedField): self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.format = kwargs.pop('format', None) - # These are deprecated - if 'pk_url_kwarg' in kwargs: - msg = 'pk_url_kwarg is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - if 'slug_url_kwarg' in kwargs: - msg = 'slug_url_kwarg is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - if 'slug_field' in kwargs: - msg = 'slug_field is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - - self.pk_url_kwarg = kwargs.pop('pk_url_kwarg', self.pk_url_kwarg) - self.slug_field = kwargs.pop('slug_field', self.slug_field) - default_slug_kwarg = self.slug_url_kwarg or self.slug_field - self.slug_url_kwarg = kwargs.pop('slug_url_kwarg', default_slug_kwarg) - super(HyperlinkedRelatedField, self).__init__(*args, **kwargs) def get_url(self, obj, view_name, request, format): @@ -361,39 +339,7 @@ class HyperlinkedRelatedField(RelatedField): """ lookup_field = getattr(obj, self.lookup_field) kwargs = {self.lookup_field: lookup_field} - try: - return reverse(view_name, kwargs=kwargs, request=request, format=format) - except NoReverseMatch: - pass - - if self.pk_url_kwarg != 'pk': - # Only try pk if it has been explicitly set. - # Otherwise, the default `lookup_field = 'pk'` has us covered. - pk = obj.pk - kwargs = {self.pk_url_kwarg: pk} - try: - return reverse(view_name, kwargs=kwargs, request=request, format=format) - except NoReverseMatch: - pass - - slug = getattr(obj, self.slug_field, None) - if slug is not None: - # Only try slug if it corresponds to an attribute on the object. - kwargs = {self.slug_url_kwarg: slug} - try: - ret = reverse(view_name, kwargs=kwargs, request=request, format=format) - if self.slug_field == 'slug' and self.slug_url_kwarg == 'slug': - # If the lookup succeeds using the default slug params, - # then `slug_field` is being used implicitly, and we - # we need to warn about the pending deprecation. - msg = 'Implicit slug field hyperlinked fields are deprecated.' \ - 'You should set `lookup_field=slug` on the HyperlinkedRelatedField.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - return ret - except NoReverseMatch: - pass - - raise NoReverseMatch() + return reverse(view_name, kwargs=kwargs, request=request, format=format) def get_object(self, queryset, view_name, view_args, view_kwargs): """ @@ -402,19 +348,8 @@ class HyperlinkedRelatedField(RelatedField): Takes the matched URL conf arguments, and the queryset, and should return an object instance, or raise an `ObjectDoesNotExist` exception. """ - lookup = view_kwargs.get(self.lookup_field, None) - pk = view_kwargs.get(self.pk_url_kwarg, None) - slug = view_kwargs.get(self.slug_url_kwarg, None) - - if lookup is not None: - filter_kwargs = {self.lookup_field: lookup} - elif pk is not None: - filter_kwargs = {'pk': pk} - elif slug is not None: - filter_kwargs = {self.slug_field: slug} - else: - raise ObjectDoesNotExist() - + lookup_value = view_kwargs[self.lookup_field] + filter_kwargs = {self.lookup_field: lookup_value} return queryset.get(**filter_kwargs) def to_native(self, obj): @@ -486,11 +421,6 @@ class HyperlinkedIdentityField(Field): lookup_field = 'pk' read_only = True - # These are all deprecated - pk_url_kwarg = 'pk' - slug_field = 'slug' - slug_url_kwarg = None # Defaults to same as `slug_field` unless overridden - def __init__(self, *args, **kwargs): try: self.view_name = kwargs.pop('view_name') @@ -502,22 +432,6 @@ class HyperlinkedIdentityField(Field): lookup_field = kwargs.pop('lookup_field', None) self.lookup_field = lookup_field or self.lookup_field - # These are deprecated - if 'pk_url_kwarg' in kwargs: - msg = 'pk_url_kwarg is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - if 'slug_url_kwarg' in kwargs: - msg = 'slug_url_kwarg is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - if 'slug_field' in kwargs: - msg = 'slug_field is deprecated. Use lookup_field instead.' - warnings.warn(msg, DeprecationWarning, stacklevel=2) - - self.slug_field = kwargs.pop('slug_field', self.slug_field) - default_slug_kwarg = self.slug_url_kwarg or self.slug_field - self.pk_url_kwarg = kwargs.pop('pk_url_kwarg', self.pk_url_kwarg) - self.slug_url_kwarg = kwargs.pop('slug_url_kwarg', default_slug_kwarg) - super(HyperlinkedIdentityField, self).__init__(*args, **kwargs) def field_to_native(self, obj, field_name): @@ -569,27 +483,4 @@ class HyperlinkedIdentityField(Field): if lookup_field is None: return None - try: - return reverse(view_name, kwargs=kwargs, request=request, format=format) - except NoReverseMatch: - pass - - if self.pk_url_kwarg != 'pk': - # Only try pk lookup if it has been explicitly set. - # Otherwise, the default `lookup_field = 'pk'` has us covered. - kwargs = {self.pk_url_kwarg: obj.pk} - try: - return reverse(view_name, kwargs=kwargs, request=request, format=format) - except NoReverseMatch: - pass - - slug = getattr(obj, self.slug_field, None) - if slug: - # Only use slug lookup if a slug field exists on the model - kwargs = {self.slug_url_kwarg: slug} - try: - return reverse(view_name, kwargs=kwargs, request=request, format=format) - except NoReverseMatch: - pass - - raise NoReverseMatch() + return reverse(view_name, kwargs=kwargs, request=request, format=format) From b8c8d10a18741b76355ed7035655d0101c1d778a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 11:38:54 +0100 Subject: [PATCH 010/100] Remove `page_size` argument. `paginate_queryset` no longer takes an optional `page_size` argument. --- rest_framework/generics.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index e21dc5c7b..09035303f 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -111,26 +111,14 @@ class GenericAPIView(views.APIView): context = self.get_serializer_context() return pagination_serializer_class(instance=page, context=context) - def paginate_queryset(self, queryset, page_size=None): + def paginate_queryset(self, queryset): """ Paginate a queryset if required, either returning a page object, or `None` if pagination is not configured for this view. """ - deprecated_style = False - if page_size is not None: - warnings.warn('The `page_size` parameter to `paginate_queryset()` ' - 'is deprecated. ' - 'Note that the return style of this method is also ' - 'changed, and will simply return a page object ' - 'when called without a `page_size` argument.', - DeprecationWarning, stacklevel=2) - deprecated_style = True - else: - # Determine the required page size. - # If pagination is not configured, simply return None. - page_size = self.get_paginate_by() - if not page_size: - return None + page_size = self.get_paginate_by() + if not page_size: + return None paginator = self.paginator_class(queryset, page_size) page_kwarg = self.kwargs.get(self.page_kwarg) @@ -152,8 +140,6 @@ class GenericAPIView(views.APIView): 'message': str(exc) }) - if deprecated_style: - return (paginator, page, page.object_list, page.has_other_pages()) return page def filter_queryset(self, queryset): From b3253b42836acd123224e88c0927f1ee6a031d94 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:35:53 +0100 Subject: [PATCH 011/100] Remove `.model` usage in tests. Remove the shortcut `.model` view attribute usage from test cases. --- rest_framework/generics.py | 49 +++++---------------- tests/serializers.py | 7 --- tests/test_filters.py | 65 +++++++++++++++++++--------- tests/test_generics.py | 34 +++++++++++---- tests/test_hyperlinkedserializers.py | 62 ++++++++++++++++++-------- tests/test_nullable_fields.py | 13 +++++- tests/test_pagination.py | 32 ++++++++++---- tests/test_permissions.py | 24 +++++++--- tests/test_response.py | 5 ++- tests/test_validation.py | 4 +- tests/views.py | 8 ---- 11 files changed, 185 insertions(+), 118 deletions(-) delete mode 100644 tests/serializers.py delete mode 100644 tests/views.py diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 09035303f..68222864f 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -51,11 +51,6 @@ class GenericAPIView(views.APIView): queryset = None serializer_class = None - # This shortcut may be used instead of setting either or both - # of the `queryset`/`serializer_class` attributes, although using - # the explicit style is generally preferred. - model = None - # If you want to use object lookups other than pk, set this attribute. # For more complex lookup requirements override `get_object()`. lookup_field = 'pk' @@ -71,9 +66,8 @@ class GenericAPIView(views.APIView): # The filter backend classes to use for queryset filtering filter_backends = api_settings.DEFAULT_FILTER_BACKENDS - # The following attributes may be subject to change, + # The following attribute may be subject to change, # and should be considered private API. - model_serializer_class = api_settings.DEFAULT_MODEL_SERIALIZER_CLASS paginator_class = Paginator def get_serializer_context(self): @@ -199,26 +193,13 @@ class GenericAPIView(views.APIView): (Eg. admins get full serialization, others get basic serialization) """ - serializer_class = self.serializer_class - if serializer_class is not None: - return serializer_class - - warnings.warn( - 'The `.model` attribute on view classes is now deprecated in favor ' - 'of the more explicit `serializer_class` and `queryset` attributes.', - DeprecationWarning, stacklevel=2 + assert self.serializer_class is not None, ( + "'%s' should either include a `serializer_class` attribute, " + "or override the `get_serializer_class()` method." + % self.__class__.__name__ ) - assert self.model is not None, \ - "'%s' should either include a 'serializer_class' attribute, " \ - "or use the 'model' attribute as a shortcut for " \ - "automatically generating a serializer class." \ - % self.__class__.__name__ - - class DefaultSerializer(self.model_serializer_class): - class Meta: - model = self.model - return DefaultSerializer + return self.serializer_class def get_queryset(self): """ @@ -235,19 +216,13 @@ class GenericAPIView(views.APIView): (Eg. return a list of items that is specific to the user) """ - if self.queryset is not None: - return self.queryset._clone() + assert self.queryset is not None, ( + "'%s' should either include a `queryset` attribute, " + "or override the `get_queryset()` method." + % self.__class__.__name__ + ) - if self.model is not None: - warnings.warn( - 'The `.model` attribute on view classes is now deprecated in favor ' - 'of the more explicit `serializer_class` and `queryset` attributes.', - DeprecationWarning, stacklevel=2 - ) - return self.model._default_manager.all() - - error_format = "'%s' must define 'queryset' or 'model'" - raise ImproperlyConfigured(error_format % self.__class__.__name__) + return self.queryset._clone() def get_object(self): """ diff --git a/tests/serializers.py b/tests/serializers.py deleted file mode 100644 index be7b37722..000000000 --- a/tests/serializers.py +++ /dev/null @@ -1,7 +0,0 @@ -from rest_framework import serializers -from tests.models import NullableForeignKeySource - - -class NullableFKSourceSerializer(serializers.ModelSerializer): - class Meta: - model = NullableForeignKeySource diff --git a/tests/test_filters.py b/tests/test_filters.py index 47bffd436..6f24b1abb 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -16,9 +16,14 @@ factory = APIRequestFactory() if django_filters: + class FilterableItemSerializer(serializers.ModelSerializer): + class Meta: + model = FilterableItem + # Basic filter on a list view. class FilterFieldsRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer filter_fields = ['decimal', 'date'] filter_backends = (filters.DjangoFilterBackend,) @@ -33,7 +38,8 @@ if django_filters: fields = ['text', 'decimal', 'date'] class FilterClassRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer filter_class = SeveralFieldsFilter filter_backends = (filters.DjangoFilterBackend,) @@ -46,12 +52,14 @@ if django_filters: fields = ['text'] class IncorrectlyConfiguredRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer filter_class = MisconfiguredFilter filter_backends = (filters.DjangoFilterBackend,) class FilterClassDetailView(generics.RetrieveAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer filter_class = SeveralFieldsFilter filter_backends = (filters.DjangoFilterBackend,) @@ -63,15 +71,12 @@ if django_filters: model = BaseFilterableItem class BaseFilterableItemFilterRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer filter_class = BaseFilterableItemFilter filter_backends = (filters.DjangoFilterBackend,) # Regression test for #814 - class FilterableItemSerializer(serializers.ModelSerializer): - class Meta: - model = FilterableItem - class FilterFieldsQuerysetView(generics.ListCreateAPIView): queryset = FilterableItem.objects.all() serializer_class = FilterableItemSerializer @@ -323,6 +328,11 @@ class SearchFilterModel(models.Model): text = models.CharField(max_length=100) +class SearchFilterSerializer(serializers.ModelSerializer): + class Meta: + model = SearchFilterModel + + class SearchFilterTests(TestCase): def setUp(self): # Sequence of title/text is: @@ -342,7 +352,8 @@ class SearchFilterTests(TestCase): def test_search(self): class SearchListView(generics.ListAPIView): - model = SearchFilterModel + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer filter_backends = (filters.SearchFilter,) search_fields = ('title', 'text') @@ -359,7 +370,8 @@ class SearchFilterTests(TestCase): def test_exact_search(self): class SearchListView(generics.ListAPIView): - model = SearchFilterModel + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer filter_backends = (filters.SearchFilter,) search_fields = ('=title', 'text') @@ -375,7 +387,8 @@ class SearchFilterTests(TestCase): def test_startswith_search(self): class SearchListView(generics.ListAPIView): - model = SearchFilterModel + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer filter_backends = (filters.SearchFilter,) search_fields = ('title', '^text') @@ -392,7 +405,8 @@ class SearchFilterTests(TestCase): def test_search_with_nonstandard_search_param(self): with temporary_setting('SEARCH_PARAM', 'query', module=filters): class SearchListView(generics.ListAPIView): - model = SearchFilterModel + queryset = SearchFilterModel.objects.all() + serializer_class = SearchFilterSerializer filter_backends = (filters.SearchFilter,) search_fields = ('title', 'text') @@ -418,6 +432,11 @@ class OrderingFilterRelatedModel(models.Model): related_name="relateds") +class OrderingFilterSerializer(serializers.ModelSerializer): + class Meta: + model = OrdringFilterModel + + class OrderingFilterTests(TestCase): def setUp(self): # Sequence of title/text is: @@ -440,7 +459,8 @@ class OrderingFilterTests(TestCase): def test_ordering(self): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = ('title',) ordering_fields = ('text',) @@ -459,7 +479,8 @@ class OrderingFilterTests(TestCase): def test_reverse_ordering(self): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = ('title',) ordering_fields = ('text',) @@ -478,7 +499,8 @@ class OrderingFilterTests(TestCase): def test_incorrectfield_ordering(self): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = ('title',) ordering_fields = ('text',) @@ -497,7 +519,8 @@ class OrderingFilterTests(TestCase): def test_default_ordering(self): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = ('title',) oredering_fields = ('text',) @@ -516,7 +539,8 @@ class OrderingFilterTests(TestCase): def test_default_ordering_using_string(self): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = 'title' ordering_fields = ('text',) @@ -545,7 +569,7 @@ class OrderingFilterTests(TestCase): new_related.save() class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = 'title' ordering_fields = '__all__' @@ -567,7 +591,8 @@ class OrderingFilterTests(TestCase): def test_ordering_with_nonstandard_ordering_param(self): with temporary_setting('ORDERING_PARAM', 'order', filters): class OrderingListView(generics.ListAPIView): - model = OrdringFilterModel + queryset = OrdringFilterModel.objects.all() + serializer_class = OrderingFilterSerializer filter_backends = (filters.OrderingFilter,) ordering = ('title',) ordering_fields = ('text',) diff --git a/tests/test_generics.py b/tests/test_generics.py index e9f5bebdd..f50d53e99 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -11,18 +11,30 @@ from tests.models import ForeignKeySource, ForeignKeyTarget factory = APIRequestFactory() +class BasicSerializer(serializers.ModelSerializer): + class Meta: + model = BasicModel + + +class ForeignKeySerializer(serializers.ModelSerializer): + class Meta: + model = ForeignKeySource + + class RootView(generics.ListCreateAPIView): """ Example description for OPTIONS. """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer class InstanceView(generics.RetrieveUpdateDestroyAPIView): """ Example description for OPTIONS. """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer def get_queryset(self): queryset = super(InstanceView, self).get_queryset() @@ -33,7 +45,8 @@ class FKInstanceView(generics.RetrieveUpdateDestroyAPIView): """ FK: example description for OPTIONS. """ - model = ForeignKeySource + queryset = ForeignKeySource.objects.all() + serializer_class = ForeignKeySerializer class SlugSerializer(serializers.ModelSerializer): @@ -48,7 +61,7 @@ class SlugBasedInstanceView(InstanceView): """ A model with a slug-field. """ - model = SlugBasedModel + queryset = SlugBasedModel.objects.all() serializer_class = SlugSerializer lookup_field = 'slug' @@ -503,7 +516,7 @@ class TestOverriddenGetObject(TestCase): """ Example detail view for override of get_object(). """ - model = BasicModel + serializer_class = BasicSerializer def get_object(self): pk = int(self.kwargs['pk']) @@ -573,7 +586,7 @@ class ClassASerializer(serializers.ModelSerializer): class ExampleView(generics.ListCreateAPIView): serializer_class = ClassASerializer - model = ClassA + queryset = ClassA.objects.all() class TestM2MBrowseableAPI(TestCase): @@ -603,7 +616,7 @@ class TwoFieldModel(models.Model): class DynamicSerializerView(generics.ListCreateAPIView): - model = TwoFieldModel + queryset = TwoFieldModel.objects.all() renderer_classes = (renderers.BrowsableAPIRenderer, renderers.JSONRenderer) def get_serializer_class(self): @@ -612,8 +625,11 @@ class DynamicSerializerView(generics.ListCreateAPIView): class Meta: model = TwoFieldModel fields = ('field_b',) - return DynamicSerializer - return super(DynamicSerializerView, self).get_serializer_class() + else: + class DynamicSerializer(serializers.ModelSerializer): + class Meta: + model = TwoFieldModel + return DynamicSerializer class TestFilterBackendAppliedToViews(TestCase): diff --git a/tests/test_hyperlinkedserializers.py b/tests/test_hyperlinkedserializers.py index d45485391..0e8c1ed46 100644 --- a/tests/test_hyperlinkedserializers.py +++ b/tests/test_hyperlinkedserializers.py @@ -39,59 +39,85 @@ class AlbumSerializer(serializers.ModelSerializer): fields = ('title', 'url') +class BasicSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = BasicModel + + +class AnchorSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = Anchor + + +class ManyToManySerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = ManyToManyModel + + +class BlogPostSerializer(serializers.ModelSerializer): + class Meta: + model = BlogPost + + +class OptionalRelationSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = OptionalRelationModel + + class BasicList(generics.ListCreateAPIView): - model = BasicModel - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer class BasicDetail(generics.RetrieveUpdateDestroyAPIView): - model = BasicModel - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer class AnchorDetail(generics.RetrieveAPIView): - model = Anchor - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = Anchor.objects.all() + serializer_class = AnchorSerializer class ManyToManyList(generics.ListAPIView): - model = ManyToManyModel - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = ManyToManyModel.objects.all() + serializer_class = ManyToManySerializer class ManyToManyDetail(generics.RetrieveAPIView): - model = ManyToManyModel - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = ManyToManyModel.objects.all() + serializer_class = ManyToManySerializer class BlogPostCommentListCreate(generics.ListCreateAPIView): - model = BlogPostComment + queryset = BlogPostComment.objects.all() serializer_class = BlogPostCommentSerializer class BlogPostCommentDetail(generics.RetrieveAPIView): - model = BlogPostComment + queryset = BlogPostComment.objects.all() serializer_class = BlogPostCommentSerializer class BlogPostDetail(generics.RetrieveAPIView): - model = BlogPost + queryset = BlogPost.objects.all() + serializer_class = BlogPostSerializer class PhotoListCreate(generics.ListCreateAPIView): - model = Photo - model_serializer_class = PhotoSerializer + queryset = Photo.objects.all() + serializer_class = PhotoSerializer class AlbumDetail(generics.RetrieveAPIView): - model = Album + queryset = Album.objects.all() serializer_class = AlbumSerializer lookup_field = 'title' class OptionalRelationDetail(generics.RetrieveUpdateDestroyAPIView): - model = OptionalRelationModel - model_serializer_class = serializers.HyperlinkedModelSerializer + queryset = OptionalRelationModel.objects.all() + serializer_class = OptionalRelationSerializer urlpatterns = patterns( diff --git a/tests/test_nullable_fields.py b/tests/test_nullable_fields.py index 0c133fc2c..8d0c84bb0 100644 --- a/tests/test_nullable_fields.py +++ b/tests/test_nullable_fields.py @@ -1,10 +1,19 @@ from django.core.urlresolvers import reverse from django.conf.urls import patterns, url +from rest_framework import serializers, generics from rest_framework.test import APITestCase from tests.models import NullableForeignKeySource -from tests.serializers import NullableFKSourceSerializer -from tests.views import NullableFKSourceDetail + + +class NullableFKSourceSerializer(serializers.ModelSerializer): + class Meta: + model = NullableForeignKeySource + + +class NullableFKSourceDetail(generics.RetrieveUpdateDestroyAPIView): + queryset = NullableForeignKeySource.objects.all() + serializer_class = NullableFKSourceSerializer urlpatterns = patterns( diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 80c33e2eb..8f9e0005e 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -4,7 +4,7 @@ from decimal import Decimal from django.core.paginator import Paginator from django.test import TestCase from django.utils import unittest -from rest_framework import generics, status, pagination, filters, serializers +from rest_framework import generics, serializers, status, pagination, filters from rest_framework.compat import django_filters from rest_framework.test import APIRequestFactory from .models import BasicModel, FilterableItem @@ -22,11 +22,22 @@ def split_arguments_from_url(url): return path, args +class BasicSerializer(serializers.ModelSerializer): + class Meta: + model = BasicModel + + +class FilterableItemSerializer(serializers.ModelSerializer): + class Meta: + model = FilterableItem + + class RootView(generics.ListCreateAPIView): """ Example description for OPTIONS. """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer paginate_by = 10 @@ -34,14 +45,16 @@ class DefaultPageSizeKwargView(generics.ListAPIView): """ View for testing default paginate_by_param usage """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer class PaginateByParamView(generics.ListAPIView): """ View for testing custom paginate_by_param usage """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer paginate_by_param = 'page_size' @@ -49,7 +62,8 @@ class MaxPaginateByView(generics.ListAPIView): """ View for testing custom max_paginate_by usage """ - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer paginate_by = 3 max_paginate_by = 5 paginate_by_param = 'page_size' @@ -140,7 +154,8 @@ class IntegrationTestPaginationAndFiltering(TestCase): fields = ['text', 'decimal', 'date'] class FilterFieldsRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer paginate_by = 10 filter_class = DecimalFilter filter_backends = (filters.DjangoFilterBackend,) @@ -188,7 +203,8 @@ class IntegrationTestPaginationAndFiltering(TestCase): return queryset.filter(decimal__lt=Decimal(request.GET['decimal'])) class BasicFilterFieldsRootView(generics.ListCreateAPIView): - model = FilterableItem + queryset = FilterableItem.objects.all() + serializer_class = FilterableItemSerializer paginate_by = 10 filter_backends = (DecimalFilterBackend,) @@ -387,7 +403,7 @@ class TestContextPassedToCustomField(TestCase): def test_with_pagination(self): class ListView(generics.ListCreateAPIView): - model = BasicModel + queryset = BasicModel.objects.all() serializer_class = BasicModelSerializer paginate_by = 1 diff --git a/tests/test_permissions.py b/tests/test_permissions.py index 93f8020f3..b90ba4f19 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -3,7 +3,7 @@ from django.contrib.auth.models import User, Permission, Group from django.db import models from django.test import TestCase from django.utils import unittest -from rest_framework import generics, status, permissions, authentication, HTTP_HEADER_ENCODING +from rest_framework import generics, serializers, status, permissions, authentication, HTTP_HEADER_ENCODING from rest_framework.compat import guardian, get_model_name from rest_framework.filters import DjangoObjectPermissionsFilter from rest_framework.test import APIRequestFactory @@ -13,14 +13,21 @@ import base64 factory = APIRequestFactory() +class BasicSerializer(serializers.ModelSerializer): + class Meta: + model = BasicModel + + class RootView(generics.ListCreateAPIView): - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer authentication_classes = [authentication.BasicAuthentication] permission_classes = [permissions.DjangoModelPermissions] class InstanceView(generics.RetrieveUpdateDestroyAPIView): - model = BasicModel + queryset = BasicModel.objects.all() + serializer_class = BasicSerializer authentication_classes = [authentication.BasicAuthentication] permission_classes = [permissions.DjangoModelPermissions] @@ -167,6 +174,11 @@ class BasicPermModel(models.Model): ) +class BasicPermSerializer(serializers.ModelSerializer): + class Meta: + model = BasicPermModel + + # Custom object-level permission, that includes 'view' permissions class ViewObjectPermissions(permissions.DjangoObjectPermissions): perms_map = { @@ -181,7 +193,8 @@ class ViewObjectPermissions(permissions.DjangoObjectPermissions): class ObjectPermissionInstanceView(generics.RetrieveUpdateDestroyAPIView): - model = BasicPermModel + queryset = BasicPermModel.objects.all() + serializer_class = BasicPermSerializer authentication_classes = [authentication.BasicAuthentication] permission_classes = [ViewObjectPermissions] @@ -189,7 +202,8 @@ object_permissions_view = ObjectPermissionInstanceView.as_view() class ObjectPermissionListView(generics.ListAPIView): - model = BasicPermModel + queryset = BasicPermModel.objects.all() + serializer_class = BasicPermSerializer authentication_classes = [authentication.BasicAuthentication] permission_classes = [ViewObjectPermissions] diff --git a/tests/test_response.py b/tests/test_response.py index 2eff83d3d..004c565c9 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -86,14 +86,15 @@ class HTMLView1(APIView): class HTMLNewModelViewSet(viewsets.ModelViewSet): - model = BasicModel + serializer_class = BasicModelSerializer + queryset = BasicModel.objects.all() class HTMLNewModelView(generics.ListCreateAPIView): renderer_classes = (BrowsableAPIRenderer,) permission_classes = [] serializer_class = BasicModelSerializer - model = BasicModel + queryset = BasicModel.objects.all() new_model_viewset_router = routers.DefaultRouter() diff --git a/tests/test_validation.py b/tests/test_validation.py index e13e4078c..f62d9068b 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -22,7 +22,7 @@ class ValidationModelSerializer(serializers.ModelSerializer): class UpdateValidationModel(generics.RetrieveUpdateDestroyAPIView): - model = ValidationModel + queryset = ValidationModel.objects.all() serializer_class = ValidationModelSerializer @@ -117,7 +117,7 @@ class ValidationMaxValueValidatorModelSerializer(serializers.ModelSerializer): class UpdateMaxValueValidationModel(generics.RetrieveUpdateDestroyAPIView): - model = ValidationMaxValueValidatorModel + queryset = ValidationMaxValueValidatorModel.objects.all() serializer_class = ValidationMaxValueValidatorModelSerializer diff --git a/tests/views.py b/tests/views.py deleted file mode 100644 index 55935e924..000000000 --- a/tests/views.py +++ /dev/null @@ -1,8 +0,0 @@ -from rest_framework import generics -from .models import NullableForeignKeySource -from .serializers import NullableFKSourceSerializer - - -class NullableFKSourceDetail(generics.RetrieveUpdateDestroyAPIView): - model = NullableForeignKeySource - model_serializer_class = NullableFKSourceSerializer From 72c0811576feb89decf6fc6dc4ee5e25eca0aece Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:48:04 +0100 Subject: [PATCH 012/100] Minor tidy up. --- rest_framework/generics.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 68222864f..d0adeaec0 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -3,7 +3,7 @@ Generic views that provide commonly needed behaviour. """ from __future__ import unicode_literals -from django.core.exceptions import ImproperlyConfigured, PermissionDenied +from django.core.exceptions import PermissionDenied from django.core.paginator import Paginator, InvalidPage from django.http import Http404 from django.shortcuts import get_object_or_404 as _get_object_or_404 @@ -235,19 +235,16 @@ class GenericAPIView(views.APIView): queryset = self.filter_queryset(self.get_queryset()) # Perform the lookup filtering. - # Note that `pk` and `slug` are deprecated styles of lookup filtering. lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - lookup = self.kwargs.get(lookup_url_kwarg, None) - if lookup is None: - raise ImproperlyConfigured( - 'Expected view %s to be called with a URL keyword argument ' - 'named "%s". Fix your URL conf, or set the `.lookup_field` ' - 'attribute on the view correctly.' % - (self.__class__.__name__, self.lookup_field) - ) + assert lookup_url_kwarg in self.kwargs, ( + 'Expected view %s to be called with a URL keyword argument ' + 'named "%s". Fix your URL conf, or set the `.lookup_field` ' + 'attribute on the view correctly.' % + (self.__class__.__name__, lookup_url_kwarg) + ) - filter_kwargs = {self.lookup_field: lookup} + filter_kwargs = {self.lookup_field: self.kwargs[lookup_url_kwarg]} obj = get_object_or_404(queryset, **filter_kwargs) # May raise a permission denied From ce7b2cded94abc12ae1be076642de96684d0927b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:48:49 +0100 Subject: [PATCH 013/100] Remove deprecated generic views. `MultipleObjectAPIView` and `SingleObjectAPIView` are no longer required. --- rest_framework/generics.py | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index d0adeaec0..e6cbfca90 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -442,25 +442,3 @@ class RetrieveUpdateDestroyAPIView(mixins.RetrieveModelMixin, def delete(self, request, *args, **kwargs): return self.destroy(request, *args, **kwargs) - - -# Deprecated classes - -class MultipleObjectAPIView(GenericAPIView): - def __init__(self, *args, **kwargs): - warnings.warn( - 'Subclassing `MultipleObjectAPIView` is deprecated. ' - 'You should simply subclass `GenericAPIView` instead.', - DeprecationWarning, stacklevel=2 - ) - super(MultipleObjectAPIView, self).__init__(*args, **kwargs) - - -class SingleObjectAPIView(GenericAPIView): - def __init__(self, *args, **kwargs): - warnings.warn( - 'Subclassing `SingleObjectAPIView` is deprecated. ' - 'You should simply subclass `GenericAPIView` instead.', - DeprecationWarning, stacklevel=2 - ) - super(SingleObjectAPIView, self).__init__(*args, **kwargs) From f87d32558eb3b36f14a798ec48e4943d25380b92 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:53:45 +0100 Subject: [PATCH 014/100] Remove `.link()` and `.action()` decorators. --- rest_framework/decorators.py | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index 449ba0a29..cc5d92c2e 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -130,37 +130,3 @@ def list_route(methods=['get'], **kwargs): func.kwargs = kwargs return func return decorator - - -# These are now pending deprecation, in favor of `detail_route` and `list_route`. - -def link(**kwargs): - """ - Used to mark a method on a ViewSet that should be routed for detail GET requests. - """ - msg = 'link is pending deprecation. Use detail_route instead.' - warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) - - def decorator(func): - func.bind_to_methods = ['get'] - func.detail = True - func.kwargs = kwargs - return func - - return decorator - - -def action(methods=['post'], **kwargs): - """ - Used to mark a method on a ViewSet that should be routed for detail POST requests. - """ - msg = 'action is pending deprecation. Use detail_route instead.' - warnings.warn(msg, PendingDeprecationWarning, stacklevel=2) - - def decorator(func): - func.bind_to_methods = methods - func.detail = True - func.kwargs = kwargs - return func - - return decorator From b552b62540e5144272c9c13c28f120ffe5fcbe45 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:54:03 +0100 Subject: [PATCH 015/100] `get_paginate_by` no longer takes optional `.queryset` --- rest_framework/generics.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index e6cbfca90..40c498440 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -158,7 +158,7 @@ class GenericAPIView(views.APIView): # The following methods provide default implementations # that you may want to override for more complex cases. - def get_paginate_by(self, queryset=None): + def get_paginate_by(self): """ Return the size of pages to use with pagination. @@ -167,11 +167,6 @@ class GenericAPIView(views.APIView): Otherwise defaults to using `self.paginate_by`. """ - if queryset is not None: - warnings.warn('The `queryset` parameter to `get_paginate_by()` ' - 'is deprecated.', - DeprecationWarning, stacklevel=2) - if self.paginate_by_param: try: return strict_positive_int( From 371d30aa8737c4b3aaf28ee10cc2b77a9c4d1fd9 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 12:54:52 +0100 Subject: [PATCH 016/100] Remove unused imports. --- rest_framework/decorators.py | 1 - rest_framework/generics.py | 1 - 2 files changed, 2 deletions(-) diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index cc5d92c2e..d28d6e22a 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -10,7 +10,6 @@ from __future__ import unicode_literals from django.utils import six from rest_framework.views import APIView import types -import warnings def api_view(http_method_names): diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 40c498440..b3bd6ce92 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -11,7 +11,6 @@ from django.utils.translation import ugettext as _ from rest_framework import views, mixins, exceptions from rest_framework.request import clone_request from rest_framework.settings import api_settings -import warnings def strict_positive_int(integer_string, cutoff=None): From 4ac4676a40b121d27cfd1173ff548d96b8d3de2f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 29 Aug 2014 16:46:26 +0100 Subject: [PATCH 017/100] First pass --- rest_framework/fields.py | 1226 ++++++------------------------ rest_framework/generics.py | 10 +- rest_framework/mixins.py | 24 +- rest_framework/pagination.py | 22 +- rest_framework/relations.py | 486 ------------ rest_framework/renderers.py | 10 +- rest_framework/serializers.py | 1200 +++++++++-------------------- rest_framework/utils/encoders.py | 18 +- rest_framework/utils/html.py | 86 +++ 9 files changed, 714 insertions(+), 2368 deletions(-) create mode 100644 rest_framework/utils/html.py diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 9d707c9b5..a83bf94c4 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,1038 +1,308 @@ -""" -Serializer fields perform validation on incoming data. - -They are very similar to Django's form fields. -""" -from __future__ import unicode_literals - -import copy -import datetime -import inspect -import re -import warnings -from decimal import Decimal, DecimalException -from django import forms -from django.core import validators -from django.core.exceptions import ValidationError -from django.conf import settings -from django.db.models.fields import BLANK_CHOICE_DASH -from django.http import QueryDict -from django.forms import widgets -from django.utils import six, timezone -from django.utils.encoding import is_protected_type -from django.utils.translation import ugettext_lazy as _ -from django.utils.datastructures import SortedDict -from django.utils.dateparse import parse_date, parse_datetime, parse_time -from rest_framework import ISO_8601 -from rest_framework.compat import ( - BytesIO, smart_text, - force_text, is_non_str_iterable -) -from rest_framework.settings import api_settings +from rest_framework.utils import html -def is_simple_callable(obj): +class empty: """ - True if the object is a callable that takes no arguments. + This class is used to represent no data being provided for a given input + or output value. + + It is required because `None` may be a valid input or output value. """ - function = inspect.isfunction(obj) - method = inspect.ismethod(obj) - - if not (function or method): - return False - - args, _, _, defaults = inspect.getargspec(obj) - len_args = len(args) if function else len(args) - 1 - len_defaults = len(defaults) if defaults else 0 - return len_args <= len_defaults + pass -def get_component(obj, attr_name): +def get_attribute(instance, attrs): """ - Given an object, and an attribute name, - return that attribute on the object. + Similar to Python's built in `getattr(instance, attr)`, + but takes a list of nested attributes, instead of a single attribute. """ - if isinstance(obj, dict): - val = obj.get(attr_name) - else: - val = getattr(obj, attr_name) - - if is_simple_callable(val): - return val() - return val + for attr in attrs: + instance = getattr(instance, attr) + return instance -def readable_datetime_formats(formats): - format = ', '.join(formats).replace( - ISO_8601, - 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]' - ) - return humanize_strptime(format) - - -def readable_date_formats(formats): - format = ', '.join(formats).replace(ISO_8601, 'YYYY[-MM[-DD]]') - return humanize_strptime(format) - - -def readable_time_formats(formats): - format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]') - return humanize_strptime(format) - - -def humanize_strptime(format_string): - # Note that we're missing some of the locale specific mappings that - # don't really make sense. - mapping = { - "%Y": "YYYY", - "%y": "YY", - "%m": "MM", - "%b": "[Jan-Dec]", - "%B": "[January-December]", - "%d": "DD", - "%H": "hh", - "%I": "hh", # Requires '%p' to differentiate from '%H'. - "%M": "mm", - "%S": "ss", - "%f": "uuuuuu", - "%a": "[Mon-Sun]", - "%A": "[Monday-Sunday]", - "%p": "[AM|PM]", - "%z": "[+HHMM|-HHMM]" - } - for key, val in mapping.items(): - format_string = format_string.replace(key, val) - return format_string - - -def strip_multiple_choice_msg(help_text): +def set_value(dictionary, keys, value): """ - Remove the 'Hold down "control" ...' message that is Django enforces in - select multiple fields on ModelForms. (Required for 1.5 and earlier) + Similar to Python's built in `dictionary[key] = value`, + but takes a list of nested keys instead of a single key. - See https://code.djangoproject.com/ticket/9321 + set_value({'a': 1}, [], {'b': 2}) -> {'a': 1, 'b': 2} + set_value({'a': 1}, ['x'], 2) -> {'a': 1, 'x': 2} + set_value({'a': 1}, ['x', 'y'], 2) -> {'a': 1, 'x': {'y': 2}} """ - multiple_choice_msg = _(' Hold down "Control", or "Command" on a Mac, to select more than one.') - multiple_choice_msg = force_text(multiple_choice_msg) + if not keys: + dictionary.update(value) + return - return help_text.replace(multiple_choice_msg, '') + for key in keys[:-1]: + if key not in dictionary: + dictionary[key] = {} + dictionary = dictionary[key] + + dictionary[keys[-1]] = value + + +class ValidationError(Exception): + pass + + +class SkipField(Exception): + pass class Field(object): - read_only = True - creation_counter = 0 - empty = '' - type_name = None - partial = False - use_files = False - form_field_class = forms.CharField - type_label = 'field' - widget = None + _creation_counter = 0 - def __init__(self, source=None, label=None, help_text=None): - self.parent = None - - self.creation_counter = Field.creation_counter - Field.creation_counter += 1 - - self.source = source - - if label is not None: - self.label = smart_text(label) - else: - self.label = None - - if help_text is not None: - self.help_text = strip_multiple_choice_msg(smart_text(help_text)) - else: - self.help_text = None - - self._errors = [] - self._value = None - self._name = None - - @property - def errors(self): - return self._errors - - def widget_html(self): - if not self.widget: - return '' - - attrs = {} - if 'id' not in self.widget.attrs: - attrs['id'] = self._name - - return self.widget.render(self._name, self._value, attrs=attrs) - - def label_tag(self): - return '' % (self._name, self.label) - - def initialize(self, parent, field_name): - """ - Called to set up a field prior to field_to_native or field_from_native. - - parent - The parent serializer. - field_name - The name of the field being initialized. - """ - self.parent = parent - self.root = parent.root or parent - self.context = self.root.context - self.partial = self.root.partial - if self.partial: - self.required = False - - def field_from_native(self, data, files, field_name, into): - """ - Given a dictionary and a field name, updates the dictionary `into`, - with the field and it's deserialized value. - """ - return - - def field_to_native(self, obj, field_name): - """ - Given an object and a field name, returns the value that should be - serialized for that field. - """ - if obj is None: - return self.empty - - if self.source == '*': - return self.to_native(obj) - - source = self.source or field_name - value = obj - - for component in source.split('.'): - value = get_component(value, component) - if value is None: - break - - return self.to_native(value) - - def to_native(self, value): - """ - Converts the field's value into it's simple representation. - """ - if is_simple_callable(value): - value = value() - - if is_protected_type(value): - return value - elif (is_non_str_iterable(value) and - not isinstance(value, (dict, six.string_types))): - return [self.to_native(item) for item in value] - elif isinstance(value, dict): - # Make sure we preserve field ordering, if it exists - ret = SortedDict() - for key, val in value.items(): - ret[key] = self.to_native(val) - return ret - return force_text(value) - - def attributes(self): - """ - Returns a dictionary of attributes to be used when serializing to xml. - """ - if self.type_name: - return {'type': self.type_name} - return {} - - def metadata(self): - metadata = SortedDict() - metadata['type'] = self.type_label - metadata['required'] = getattr(self, 'required', False) - optional_attrs = ['read_only', 'label', 'help_text', - 'min_length', 'max_length'] - for attr in optional_attrs: - value = getattr(self, attr, None) - if value is not None and value != '': - metadata[attr] = force_text(value, strings_only=True) - return metadata - - -class WritableField(Field): - """ - Base for read/write fields. - """ - write_only = False - default_validators = [] - default_error_messages = { - 'required': _('This field is required.'), - 'invalid': _('Invalid value.'), + MESSAGES = { + 'required': 'This field is required.' } - widget = widgets.TextInput - default = None - def __init__(self, source=None, label=None, help_text=None, - read_only=False, write_only=False, required=None, - validators=[], error_messages=None, widget=None, - default=None, blank=None): + _NOT_READ_ONLY_WRITE_ONLY = 'May not set both `read_only` and `write_only`' + _NOT_READ_ONLY_REQUIRED = 'May not set both `read_only` and `required`' + _NOT_READ_ONLY_DEFAULT = 'May not set both `read_only` and `default`' + _NOT_REQUIRED_DEFAULT = 'May not set both `required` and `default`' + _MISSING_ERROR_MESSAGE = ( + 'ValidationError raised by `{class_name}`, but error key `{key}` does ' + 'not exist in the `MESSAGES` dictionary.' + ) - super(WritableField, self).__init__(source=source, label=label, help_text=help_text) + def __init__(self, read_only=False, write_only=False, + required=None, default=empty, initial=None, source=None, + label=None, style=None): + self._creation_counter = Field._creation_counter + Field._creation_counter += 1 + + # If `required` is unset, then use `True` unless a default is provided. + if required is None: + required = default is empty and not read_only + + # Some combinations of keyword arguments do not make sense. + assert not (read_only and write_only), self._NOT_READ_ONLY_WRITE_ONLY + assert not (read_only and required), self._NOT_READ_ONLY_REQUIRED + assert not (read_only and default is not empty), self._NOT_READ_ONLY_DEFAULT + assert not (required and default is not empty), self._NOT_REQUIRED_DEFAULT self.read_only = read_only self.write_only = write_only + self.required = required + self.default = default + self.source = source + self.initial = initial + self.label = label + self.style = {} if style is None else style - assert not (read_only and write_only), "Cannot set read_only=True and write_only=True" + def bind(self, field_name, parent, root): + """ + Setup the context for the field instance. + """ + self.field_name = field_name + self.parent = parent + self.root = root - if required is None: - self.required = not(read_only) + # `self.label` should deafult to being based on the field name. + if self.label is None: + self.label = self.field_name.replace('_', ' ').capitalize() + + # self.source should default to being the same as the field name. + if self.source is None: + self.source = field_name + + # self.source_attrs is a list of attributes that need to be looked up + # when serializing the instance, or populating the validated data. + if self.source == '*': + self.source_attrs = [] else: - assert not (read_only and required), "Cannot set required=True and read_only=True" - self.required = required + self.source_attrs = self.source.split('.') - messages = {} - for c in reversed(self.__class__.__mro__): - messages.update(getattr(c, 'default_error_messages', {})) - messages.update(error_messages or {}) - self.error_messages = messages + def get_initial(self): + """ + Return a value to use when the field is being returned as a primative + value, without any object instance. + """ + return self.initial - self.validators = self.default_validators + validators - self.default = default if default is not None else self.default + def get_value(self, dictionary): + """ + Given the *incoming* primative data, return the value for this field + that should be validated and transformed to a native value. + """ + return dictionary.get(self.field_name, empty) - # Widgets are only used for HTML forms. - widget = widget or self.widget - if isinstance(widget, type): - widget = widget() - self.widget = widget + def get_attribute(self, instance): + """ + Given the *outgoing* object instance, return the value for this field + that should be returned as a primative value. + """ + return get_attribute(instance, self.source_attrs) - def __deepcopy__(self, memo): - result = copy.copy(self) - memo[id(self)] = result - result.validators = self.validators[:] - return result + def get_default(self): + """ + Return the default value to use when validating data if no input + is provided for this field. - def get_default_value(self): - if is_simple_callable(self.default): - return self.default() + If a default has not been set for this field then this will simply + return `empty`, indicating that no value should be set in the + validated data for this field. + """ + if self.default is empty: + raise SkipField() return self.default - def validate(self, value): - if value in validators.EMPTY_VALUES and self.required: - raise ValidationError(self.error_messages['required']) - - def run_validators(self, value): - if value in validators.EMPTY_VALUES: - return - errors = [] - for v in self.validators: - try: - v(value) - except ValidationError as e: - if hasattr(e, 'code') and e.code in self.error_messages: - message = self.error_messages[e.code] - if e.params: - message = message % e.params - errors.append(message) - else: - errors.extend(e.messages) - if errors: - raise ValidationError(errors) - - def field_to_native(self, obj, field_name): - if self.write_only: - return None - return super(WritableField, self).field_to_native(obj, field_name) - - def field_from_native(self, data, files, field_name, into): + def validate(self, data=empty): """ - Given a dictionary and a field name, updates the dictionary `into`, - with the field and it's deserialized value. + Validate a simple representation and return the internal value. + + The provided data may be `empty` if no representation was included. + May return `empty` if the field should not be included in the + validated data. """ - if self.read_only: - return + if data is empty: + if self.required: + self.fail('required') + return self.get_default() - try: - data = data or {} - if self.use_files: - files = files or {} - try: - native = files[field_name] - except KeyError: - native = data[field_name] - else: - native = data[field_name] - except KeyError: - if self.default is not None and not self.partial: - # Note: partial updates shouldn't set defaults - native = self.get_default_value() - else: - if self.required: - raise ValidationError(self.error_messages['required']) - return + return self.to_native(data) - value = self.from_native(native) - if self.source == '*': - if value: - into.update(value) - else: - self.validate(value) - self.run_validators(value) - into[self.source or field_name] = value - - def from_native(self, value): + def to_native(self, data): """ - Reverts a simple representation back to the field's value. + Transform the *incoming* primative data into a native value. + """ + return data + + def to_primative(self, value): + """ + Transform the *outgoing* native value into primative data. """ return value - -class ModelField(WritableField): - """ - A generic field that can be used against an arbitrary model field. - """ - def __init__(self, *args, **kwargs): + def fail(self, key, **kwargs): + """ + A helper method that simply raises a validation error. + """ try: - self.model_field = kwargs.pop('model_field') + raise ValidationError(self.MESSAGES[key].format(**kwargs)) except KeyError: - raise ValueError("ModelField requires 'model_field' kwarg") + class_name = self.__class__.__name__ + msg = self._MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) + raise AssertionError(msg) - self.min_length = kwargs.pop('min_length', - getattr(self.model_field, 'min_length', None)) - self.max_length = kwargs.pop('max_length', - getattr(self.model_field, 'max_length', None)) - self.min_value = kwargs.pop('min_value', - getattr(self.model_field, 'min_value', None)) - self.max_value = kwargs.pop('max_value', - getattr(self.model_field, 'max_value', None)) - super(ModelField, self).__init__(*args, **kwargs) +class BooleanField(Field): + MESSAGES = { + 'required': 'This field is required.', + 'invalid_value': '`{input}` is not a valid boolean.' + } + TRUE_VALUES = {'t', 'T', 'true', 'True', 'TRUE', '1', 1, True} + FALSE_VALUES = {'f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False} - if self.min_length is not None: - self.validators.append(validators.MinLengthValidator(self.min_length)) - if self.max_length is not None: - self.validators.append(validators.MaxLengthValidator(self.max_length)) - if self.min_value is not None: - self.validators.append(validators.MinValueValidator(self.min_value)) - if self.max_value is not None: - self.validators.append(validators.MaxValueValidator(self.max_value)) + def get_value(self, dictionary): + if html.is_html_input(dictionary): + # HTML forms do not send a `False` value on an empty checkbox, + # so we override the default empty value to be False. + return dictionary.get(self.field_name, False) + return dictionary.get(self.field_name, empty) - def from_native(self, value): - rel = getattr(self.model_field, "rel", None) - if rel is not None: - return rel.to._meta.get_field(rel.field_name).to_python(value) + def to_native(self, data): + if data in self.TRUE_VALUES: + return True + elif data in self.FALSE_VALUES: + return False + self.fail('invalid_value', input=data) + + +class CharField(Field): + MESSAGES = { + 'required': 'This field is required.', + 'blank': 'This field may not be blank.' + } + + def __init__(self, *args, **kwargs): + self.allow_blank = kwargs.pop('allow_blank', False) + super(CharField, self).__init__(*args, **kwargs) + + def to_native(self, data): + if data == '' and not self.allow_blank: + self.fail('blank') + return str(data) + + +class ChoiceField(Field): + MESSAGES = { + 'required': 'This field is required.', + 'invalid_choice': '`{input}` is not a valid choice.' + } + coerce_to_type = str + + def __init__(self, *args, **kwargs): + choices = kwargs.pop('choices') + + assert choices, '`choices` argument is required and may not be empty' + + # Allow either single or paired choices style: + # choices = [1, 2, 3] + # choices = [(1, 'First'), (2, 'Second'), (3, 'Third')] + pairs = [ + isinstance(item, (list, tuple)) and len(item) == 2 + for item in choices + ] + if all(pairs): + self.choices = {key: val for key, val in choices} else: - return self.model_field.to_python(value) + self.choices = {item: item for item in choices} - def field_to_native(self, obj, field_name): - value = self.model_field._get_val_from_obj(obj) - if is_protected_type(value): - return value - return self.model_field.value_to_string(obj) - - def attributes(self): - return { - "type": self.model_field.get_internal_type() + # Map the string representation of choices to the underlying value. + # Allows us to deal with eg. integer choices while supporting either + # integer or string input, but still get the correct datatype out. + self.choice_strings_to_values = { + str(key): key for key in self.choices.keys() } - -# Typed Fields - -class BooleanField(WritableField): - type_name = 'BooleanField' - type_label = 'boolean' - form_field_class = forms.BooleanField - widget = widgets.CheckboxInput - default_error_messages = { - 'invalid': _("'%s' value must be either True or False."), - } - empty = False - - def field_from_native(self, data, files, field_name, into): - # HTML checkboxes do not explicitly represent unchecked as `False` - # we deal with that here... - if isinstance(data, QueryDict) and self.default is None: - self.default = False - - return super(BooleanField, self).field_from_native( - data, files, field_name, into - ) - - def from_native(self, value): - if value in ('true', 't', 'True', '1'): - return True - if value in ('false', 'f', 'False', '0'): - return False - return bool(value) - - -class CharField(WritableField): - type_name = 'CharField' - type_label = 'string' - form_field_class = forms.CharField - - def __init__(self, max_length=None, min_length=None, allow_none=False, *args, **kwargs): - self.max_length, self.min_length = max_length, min_length - self.allow_none = allow_none - super(CharField, self).__init__(*args, **kwargs) - if min_length is not None: - self.validators.append(validators.MinLengthValidator(min_length)) - if max_length is not None: - self.validators.append(validators.MaxLengthValidator(max_length)) - - def from_native(self, value): - if isinstance(value, six.string_types): - return value - - if value is None and not self.allow_none: - return '' - - return smart_text(value) - - -class URLField(CharField): - type_name = 'URLField' - type_label = 'url' - - def __init__(self, **kwargs): - if 'validators' not in kwargs: - kwargs['validators'] = [validators.URLValidator()] - super(URLField, self).__init__(**kwargs) - - -class SlugField(CharField): - type_name = 'SlugField' - type_label = 'slug' - form_field_class = forms.SlugField - - default_error_messages = { - 'invalid': _("Enter a valid 'slug' consisting of letters, numbers," - " underscores or hyphens."), - } - default_validators = [validators.validate_slug] - - def __init__(self, *args, **kwargs): - super(SlugField, self).__init__(*args, **kwargs) - - -class ChoiceField(WritableField): - type_name = 'ChoiceField' - type_label = 'choice' - form_field_class = forms.ChoiceField - widget = widgets.Select - default_error_messages = { - 'invalid_choice': _('Select a valid choice. %(value)s is not one of ' - 'the available choices.'), - } - - def __init__(self, choices=(), blank_display_value=None, *args, **kwargs): - self.empty = kwargs.pop('empty', '') super(ChoiceField, self).__init__(*args, **kwargs) - self.choices = choices - if not self.required: - if blank_display_value is None: - blank_choice = BLANK_CHOICE_DASH - else: - blank_choice = [('', blank_display_value)] - self.choices = blank_choice + self.choices - - def _get_choices(self): - return self._choices - - def _set_choices(self, value): - # Setting choices also sets the choices on the widget. - # choices can be any iterable, but we call list() on it because - # it will be consumed more than once. - self._choices = self.widget.choices = list(value) - - choices = property(_get_choices, _set_choices) - - def metadata(self): - data = super(ChoiceField, self).metadata() - data['choices'] = [{'value': v, 'display_name': n} for v, n in self.choices] - return data - - def validate(self, value): - """ - Validates that the input is in self.choices. - """ - super(ChoiceField, self).validate(value) - if value and not self.valid_value(value): - raise ValidationError(self.error_messages['invalid_choice'] % {'value': value}) - - def valid_value(self, value): - """ - Check to see if the provided value is a valid choice. - """ - for k, v in self.choices: - if isinstance(v, (list, tuple)): - # This is an optgroup, so look inside the group for options - for k2, v2 in v: - if value == smart_text(k2): - return True - else: - if value == smart_text(k) or value == k: - return True - return False - - def from_native(self, value): - value = super(ChoiceField, self).from_native(value) - if value == self.empty or value in validators.EMPTY_VALUES: - return self.empty - return value - - -class EmailField(CharField): - type_name = 'EmailField' - type_label = 'email' - form_field_class = forms.EmailField - - default_error_messages = { - 'invalid': _('Enter a valid email address.'), - } - default_validators = [validators.validate_email] - - def from_native(self, value): - ret = super(EmailField, self).from_native(value) - if ret is None: - return None - return ret.strip() - - -class RegexField(CharField): - type_name = 'RegexField' - type_label = 'regex' - form_field_class = forms.RegexField - - def __init__(self, regex, max_length=None, min_length=None, *args, **kwargs): - super(RegexField, self).__init__(max_length, min_length, *args, **kwargs) - self.regex = regex - - def _get_regex(self): - return self._regex - - def _set_regex(self, regex): - if isinstance(regex, six.string_types): - regex = re.compile(regex) - self._regex = regex - if hasattr(self, '_regex_validator') and self._regex_validator in self.validators: - self.validators.remove(self._regex_validator) - self._regex_validator = validators.RegexValidator(regex=regex) - self.validators.append(self._regex_validator) - - regex = property(_get_regex, _set_regex) - - -class DateField(WritableField): - type_name = 'DateField' - type_label = 'date' - widget = widgets.DateInput - form_field_class = forms.DateField - - default_error_messages = { - 'invalid': _("Date has wrong format. Use one of these formats instead: %s"), - } - empty = None - input_formats = api_settings.DATE_INPUT_FORMATS - format = api_settings.DATE_FORMAT - - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats - self.format = format if format is not None else self.format - super(DateField, self).__init__(*args, **kwargs) - - def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None - - if isinstance(value, datetime.datetime): - if timezone and settings.USE_TZ and timezone.is_aware(value): - # Convert aware datetimes to the default time zone - # before casting them to dates (#17742). - default_timezone = timezone.get_default_timezone() - value = timezone.make_naive(value, default_timezone) - return value.date() - if isinstance(value, datetime.date): - return value - - for format in self.input_formats: - if format.lower() == ISO_8601: - try: - parsed = parse_date(value) - except (ValueError, TypeError): - pass - else: - if parsed is not None: - return parsed - else: - try: - parsed = datetime.datetime.strptime(value, format) - except (ValueError, TypeError): - pass - else: - return parsed.date() - - msg = self.error_messages['invalid'] % readable_date_formats(self.input_formats) - raise ValidationError(msg) - - def to_native(self, value): - if value is None or self.format is None: - return value - - if isinstance(value, datetime.datetime): - value = value.date() - - if self.format.lower() == ISO_8601: - return value.isoformat() - return value.strftime(self.format) - - -class DateTimeField(WritableField): - type_name = 'DateTimeField' - type_label = 'datetime' - widget = widgets.DateTimeInput - form_field_class = forms.DateTimeField - - default_error_messages = { - 'invalid': _("Datetime has wrong format. Use one of these formats instead: %s"), - } - empty = None - input_formats = api_settings.DATETIME_INPUT_FORMATS - format = api_settings.DATETIME_FORMAT - - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats - self.format = format if format is not None else self.format - super(DateTimeField, self).__init__(*args, **kwargs) - - def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None - - if isinstance(value, datetime.datetime): - return value - if isinstance(value, datetime.date): - value = datetime.datetime(value.year, value.month, value.day) - if settings.USE_TZ: - # For backwards compatibility, interpret naive datetimes in - # local time. This won't work during DST change, but we can't - # do much about it, so we let the exceptions percolate up the - # call stack. - warnings.warn("DateTimeField received a naive datetime (%s)" - " while time zone support is active." % value, - RuntimeWarning) - default_timezone = timezone.get_default_timezone() - value = timezone.make_aware(value, default_timezone) - return value - - for format in self.input_formats: - if format.lower() == ISO_8601: - try: - parsed = parse_datetime(value) - except (ValueError, TypeError): - pass - else: - if parsed is not None: - return parsed - else: - try: - parsed = datetime.datetime.strptime(value, format) - except (ValueError, TypeError): - pass - else: - return parsed - - msg = self.error_messages['invalid'] % readable_datetime_formats(self.input_formats) - raise ValidationError(msg) - - def to_native(self, value): - if value is None or self.format is None: - return value - - if self.format.lower() == ISO_8601: - ret = value.isoformat() - if ret.endswith('+00:00'): - ret = ret[:-6] + 'Z' - return ret - return value.strftime(self.format) - - -class TimeField(WritableField): - type_name = 'TimeField' - type_label = 'time' - widget = widgets.TimeInput - form_field_class = forms.TimeField - - default_error_messages = { - 'invalid': _("Time has wrong format. Use one of these formats instead: %s"), - } - empty = None - input_formats = api_settings.TIME_INPUT_FORMATS - format = api_settings.TIME_FORMAT - - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats - self.format = format if format is not None else self.format - super(TimeField, self).__init__(*args, **kwargs) - - def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None - - if isinstance(value, datetime.time): - return value - - for format in self.input_formats: - if format.lower() == ISO_8601: - try: - parsed = parse_time(value) - except (ValueError, TypeError): - pass - else: - if parsed is not None: - return parsed - else: - try: - parsed = datetime.datetime.strptime(value, format) - except (ValueError, TypeError): - pass - else: - return parsed.time() - - msg = self.error_messages['invalid'] % readable_time_formats(self.input_formats) - raise ValidationError(msg) - - def to_native(self, value): - if value is None or self.format is None: - return value - - if isinstance(value, datetime.datetime): - value = value.time() - - if self.format.lower() == ISO_8601: - return value.isoformat() - return value.strftime(self.format) - - -class IntegerField(WritableField): - type_name = 'IntegerField' - type_label = 'integer' - form_field_class = forms.IntegerField - empty = 0 - - default_error_messages = { - 'invalid': _('Enter a whole number.'), - 'max_value': _('Ensure this value is less than or equal to %(limit_value)s.'), - 'min_value': _('Ensure this value is greater than or equal to %(limit_value)s.'), - } - - def __init__(self, max_value=None, min_value=None, *args, **kwargs): - self.max_value, self.min_value = max_value, min_value - super(IntegerField, self).__init__(*args, **kwargs) - - if max_value is not None: - self.validators.append(validators.MaxValueValidator(max_value)) - if min_value is not None: - self.validators.append(validators.MinValueValidator(min_value)) - - def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None + def to_native(self, data): try: - value = int(str(value)) + return self.choice_strings_to_values[str(data)] + except KeyError: + self.fail('invalid_choice', input=data) + + +class MultipleChoiceField(ChoiceField): + MESSAGES = { + 'required': 'This field is required.', + 'invalid_choice': '`{input}` is not a valid choice.', + 'not_a_list': 'Expected a list of items but got type `{input_type}`' + } + + def to_native(self, data): + if not hasattr(data, '__iter__'): + self.fail('not_a_list', input_type=type(data).__name__) + return set([ + super(MultipleChoiceField, self).to_native(item) + for item in data + ]) + + +class IntegerField(Field): + MESSAGES = { + 'required': 'This field is required.', + 'invalid_integer': 'A valid integer is required.' + } + + def to_native(self, data): + try: + data = int(str(data)) except (ValueError, TypeError): - raise ValidationError(self.error_messages['invalid']) - return value - - -class FloatField(WritableField): - type_name = 'FloatField' - type_label = 'float' - form_field_class = forms.FloatField - empty = 0 - - default_error_messages = { - 'invalid': _("'%s' value must be a float."), - } - - def from_native(self, value): - if value in validators.EMPTY_VALUES: - return None - - try: - return float(value) - except (TypeError, ValueError): - msg = self.error_messages['invalid'] % value - raise ValidationError(msg) - - -class DecimalField(WritableField): - type_name = 'DecimalField' - type_label = 'decimal' - form_field_class = forms.DecimalField - empty = Decimal('0') - - default_error_messages = { - 'invalid': _('Enter a number.'), - 'max_value': _('Ensure this value is less than or equal to %(limit_value)s.'), - 'min_value': _('Ensure this value is greater than or equal to %(limit_value)s.'), - 'max_digits': _('Ensure that there are no more than %s digits in total.'), - 'max_decimal_places': _('Ensure that there are no more than %s decimal places.'), - 'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.') - } - - def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): - self.max_value, self.min_value = max_value, min_value - self.max_digits, self.decimal_places = max_digits, decimal_places - super(DecimalField, self).__init__(*args, **kwargs) - - if max_value is not None: - self.validators.append(validators.MaxValueValidator(max_value)) - if min_value is not None: - self.validators.append(validators.MinValueValidator(min_value)) - - def from_native(self, value): - """ - Validates that the input is a decimal number. Returns a Decimal - instance. Returns None for empty values. Ensures that there are no more - than max_digits in the number, and no more than decimal_places digits - after the decimal point. - """ - if value in validators.EMPTY_VALUES: - return None - value = smart_text(value).strip() - try: - value = Decimal(value) - except DecimalException: - raise ValidationError(self.error_messages['invalid']) - return value - - def validate(self, value): - super(DecimalField, self).validate(value) - if value in validators.EMPTY_VALUES: - return - # Check for NaN, Inf and -Inf values. We can't compare directly for NaN, - # since it is never equal to itself. However, NaN is the only value that - # isn't equal to itself, so we can use this to identify NaN - if value != value or value == Decimal("Inf") or value == Decimal("-Inf"): - raise ValidationError(self.error_messages['invalid']) - sign, digittuple, exponent = value.as_tuple() - decimals = abs(exponent) - # digittuple doesn't include any leading zeros. - digits = len(digittuple) - if decimals > digits: - # We have leading zeros up to or past the decimal point. Count - # everything past the decimal point as a digit. We do not count - # 0 before the decimal point as a digit since that would mean - # we would not allow max_digits = decimal_places. - digits = decimals - whole_digits = digits - decimals - - if self.max_digits is not None and digits > self.max_digits: - raise ValidationError(self.error_messages['max_digits'] % self.max_digits) - if self.decimal_places is not None and decimals > self.decimal_places: - raise ValidationError(self.error_messages['max_decimal_places'] % self.decimal_places) - if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places): - raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) - return value - - -class FileField(WritableField): - use_files = True - type_name = 'FileField' - type_label = 'file upload' - form_field_class = forms.FileField - widget = widgets.FileInput - - default_error_messages = { - 'invalid': _("No file was submitted. Check the encoding type on the form."), - 'missing': _("No file was submitted."), - 'empty': _("The submitted file is empty."), - 'max_length': _('Ensure this filename has at most %(max)d characters (it has %(length)d).'), - 'contradiction': _('Please either submit a file or check the clear checkbox, not both.') - } - - def __init__(self, *args, **kwargs): - self.max_length = kwargs.pop('max_length', None) - self.allow_empty_file = kwargs.pop('allow_empty_file', False) - super(FileField, self).__init__(*args, **kwargs) - - def from_native(self, data): - if data in validators.EMPTY_VALUES: - return None - - # UploadedFile objects should have name and size attributes. - try: - file_name = data.name - file_size = data.size - except AttributeError: - raise ValidationError(self.error_messages['invalid']) - - if self.max_length is not None and len(file_name) > self.max_length: - error_values = {'max': self.max_length, 'length': len(file_name)} - raise ValidationError(self.error_messages['max_length'] % error_values) - if not file_name: - raise ValidationError(self.error_messages['invalid']) - if not self.allow_empty_file and not file_size: - raise ValidationError(self.error_messages['empty']) - + self.fail('invalid_integer') return data - def to_native(self, value): - return value.name +class MethodField(Field): + def __init__(self, **kwargs): + kwargs['source'] = '*' + kwargs['read_only'] = True + super(MethodField, self).__init__(**kwargs) -class ImageField(FileField): - use_files = True - type_name = 'ImageField' - type_label = 'image upload' - form_field_class = forms.ImageField - - default_error_messages = { - 'invalid_image': _("Upload a valid image. The file you uploaded was " - "either not an image or a corrupted image."), - } - - def from_native(self, data): - """ - Checks that the file-upload field data contains a valid image (GIF, JPG, - PNG, possibly others -- whatever the Python Imaging Library supports). - """ - f = super(ImageField, self).from_native(data) - if f is None: - return None - - from rest_framework.compat import Image - assert Image is not None, 'Either Pillow or PIL must be installed for ImageField support.' - - # We need to get a file object for PIL. We might have a path or we might - # have to read the data into memory. - if hasattr(data, 'temporary_file_path'): - file = data.temporary_file_path() - else: - if hasattr(data, 'read'): - file = BytesIO(data.read()) - else: - file = BytesIO(data['content']) - - try: - # load() could spot a truncated JPEG, but it loads the entire - # image in memory, which is a DoS vector. See #3848 and #18520. - # verify() must be called immediately after the constructor. - Image.open(file).verify() - except ImportError: - # Under PyPy, it is possible to import PIL. However, the underlying - # _imaging C module isn't available, so an ImportError will be - # raised. Catch and re-raise. - raise - except Exception: # Python Imaging Library doesn't recognize it as an image - raise ValidationError(self.error_messages['invalid_image']) - if hasattr(f, 'seek') and callable(f.seek): - f.seek(0) - return f - - -class SerializerMethodField(Field): - """ - A field that gets its value by calling a method on the serializer it's attached to. - """ - - def __init__(self, method_name, *args, **kwargs): - self.method_name = method_name - super(SerializerMethodField, self).__init__(*args, **kwargs) - - def field_to_native(self, obj, field_name): - value = getattr(self.parent, self.method_name)(obj) - return self.to_native(value) + def to_primative(self, value): + attr = 'get_{field_name}'.format(field_name=self.field_name) + method = getattr(self.parent, attr) + return method(value) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index b3bd6ce92..6705cbb2f 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -79,18 +79,16 @@ class GenericAPIView(views.APIView): 'view': self } - def get_serializer(self, instance=None, data=None, files=None, many=False, - partial=False, allow_add_remove=False): + def get_serializer(self, instance=None, data=None, many=False, partial=False): """ Return the serializer instance that should be used for validating and deserializing input, and for serializing output. """ serializer_class = self.get_serializer_class() context = self.get_serializer_context() - return serializer_class(instance, data=data, files=files, - many=many, partial=partial, - allow_add_remove=allow_add_remove, - context=context) + return serializer_class( + instance, data=data, many=many, partial=partial, context=context + ) def get_pagination_serializer(self, page): """ diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index ac59d9795..ee01cabc7 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -36,12 +36,10 @@ class CreateModelMixin(object): Create a model instance. """ def create(self, request, *args, **kwargs): - serializer = self.get_serializer(data=request.DATA, files=request.FILES) + serializer = self.get_serializer(data=request.DATA) if serializer.is_valid(): - self.pre_save(serializer.object) - self.object = serializer.save(force_insert=True) - self.post_save(self.object, created=True) + self.object = serializer.save() headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) @@ -90,26 +88,20 @@ class UpdateModelMixin(object): partial = kwargs.pop('partial', False) self.object = self.get_object_or_none() - serializer = self.get_serializer(self.object, data=request.DATA, - files=request.FILES, partial=partial) + serializer = self.get_serializer(self.object, data=request.DATA, partial=partial) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - try: - self.pre_save(serializer.object) - except ValidationError as err: - # full_clean on model instance may be called in pre_save, - # so we have to handle eventual errors. - return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST) + lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field + lookup_value = self.kwargs[lookup_url_kwarg] + extras = {self.lookup_field: lookup_value} if self.object is None: - self.object = serializer.save(force_insert=True) - self.post_save(self.object, created=True) + self.object = serializer.save(extras=extras) return Response(serializer.data, status=status.HTTP_201_CREATED) - self.object = serializer.save(force_update=True) - self.post_save(self.object, created=False) + self.object = serializer.save(extras=extras) return Response(serializer.data, status=status.HTTP_200_OK) def partial_update(self, request, *args, **kwargs): diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index d51ea929b..83ef97c5c 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -48,17 +48,17 @@ class DefaultObjectSerializer(serializers.Field): super(DefaultObjectSerializer, self).__init__(source=source) -class PaginationSerializerOptions(serializers.SerializerOptions): - """ - An object that stores the options that may be provided to a - pagination serializer by using the inner `Meta` class. +# class PaginationSerializerOptions(serializers.SerializerOptions): +# """ +# An object that stores the options that may be provided to a +# pagination serializer by using the inner `Meta` class. - Accessible on the instance as `serializer.opts`. - """ - def __init__(self, meta): - super(PaginationSerializerOptions, self).__init__(meta) - self.object_serializer_class = getattr(meta, 'object_serializer_class', - DefaultObjectSerializer) +# Accessible on the instance as `serializer.opts`. +# """ +# def __init__(self, meta): +# super(PaginationSerializerOptions, self).__init__(meta) +# self.object_serializer_class = getattr(meta, 'object_serializer_class', +# DefaultObjectSerializer) class BasePaginationSerializer(serializers.Serializer): @@ -66,7 +66,7 @@ class BasePaginationSerializer(serializers.Serializer): A base class for pagination serializers to inherit from, to make implementing custom serializers more easy. """ - _options_class = PaginationSerializerOptions + # _options_class = PaginationSerializerOptions results_field = 'results' def __init__(self, *args, **kwargs): diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 56870b408..e69de29bb 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -1,486 +0,0 @@ -""" -Serializer fields that deal with relationships. - -These fields allow you to specify the style that should be used to represent -model relationships, including hyperlinks, primary keys, or slugs. -""" -from __future__ import unicode_literals -from django.core.exceptions import ObjectDoesNotExist, ValidationError -from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch -from django import forms -from django.db.models.fields import BLANK_CHOICE_DASH -from django.forms import widgets -from django.forms.models import ModelChoiceIterator -from django.utils.translation import ugettext_lazy as _ -from rest_framework.fields import Field, WritableField, get_component, is_simple_callable -from rest_framework.reverse import reverse -from rest_framework.compat import urlparse -from rest_framework.compat import smart_text - - -# Relational fields - -# Not actually Writable, but subclasses may need to be. -class RelatedField(WritableField): - """ - Base class for related model fields. - - This represents a relationship using the unicode representation of the target. - """ - widget = widgets.Select - many_widget = widgets.SelectMultiple - form_field_class = forms.ChoiceField - many_form_field_class = forms.MultipleChoiceField - null_values = (None, '', 'None') - - cache_choices = False - empty_label = None - read_only = True - many = False - - def __init__(self, *args, **kwargs): - queryset = kwargs.pop('queryset', None) - self.many = kwargs.pop('many', self.many) - if self.many: - self.widget = self.many_widget - self.form_field_class = self.many_form_field_class - - kwargs['read_only'] = kwargs.pop('read_only', self.read_only) - super(RelatedField, self).__init__(*args, **kwargs) - - if not self.required: - # Accessed in ModelChoiceIterator django/forms/models.py:1034 - # If set adds empty choice. - self.empty_label = BLANK_CHOICE_DASH[0][1] - - self.queryset = queryset - - def initialize(self, parent, field_name): - super(RelatedField, self).initialize(parent, field_name) - if self.queryset is None and not self.read_only: - manager = getattr(self.parent.opts.model, self.source or field_name) - if hasattr(manager, 'related'): # Forward - self.queryset = manager.related.model._default_manager.all() - else: # Reverse - self.queryset = manager.field.rel.to._default_manager.all() - - # We need this stuff to make form choices work... - - def prepare_value(self, obj): - return self.to_native(obj) - - def label_from_instance(self, obj): - """ - Return a readable representation for use with eg. select widgets. - """ - desc = smart_text(obj) - ident = smart_text(self.to_native(obj)) - if desc == ident: - return desc - return "%s - %s" % (desc, ident) - - def _get_queryset(self): - return self._queryset - - def _set_queryset(self, queryset): - self._queryset = queryset - self.widget.choices = self.choices - - queryset = property(_get_queryset, _set_queryset) - - def _get_choices(self): - # If self._choices is set, then somebody must have manually set - # the property self.choices. In this case, just return self._choices. - if hasattr(self, '_choices'): - return self._choices - - # Otherwise, execute the QuerySet in self.queryset to determine the - # choices dynamically. Return a fresh ModelChoiceIterator that has not been - # consumed. Note that we're instantiating a new ModelChoiceIterator *each* - # time _get_choices() is called (and, thus, each time self.choices is - # accessed) so that we can ensure the QuerySet has not been consumed. This - # construct might look complicated but it allows for lazy evaluation of - # the queryset. - return ModelChoiceIterator(self) - - def _set_choices(self, value): - # Setting choices also sets the choices on the widget. - # choices can be any iterable, but we call list() on it because - # it will be consumed more than once. - self._choices = self.widget.choices = list(value) - - choices = property(_get_choices, _set_choices) - - # Default value handling - - def get_default_value(self): - default = super(RelatedField, self).get_default_value() - if self.many and default is None: - return [] - return default - - # Regular serializer stuff... - - def field_to_native(self, obj, field_name): - try: - if self.source == '*': - return self.to_native(obj) - - source = self.source or field_name - value = obj - - for component in source.split('.'): - if value is None: - break - value = get_component(value, component) - except ObjectDoesNotExist: - return None - - if value is None: - return None - - if self.many: - if is_simple_callable(getattr(value, 'all', None)): - return [self.to_native(item) for item in value.all()] - else: - # Also support non-queryset iterables. - # This allows us to also support plain lists of related items. - return [self.to_native(item) for item in value] - return self.to_native(value) - - def field_from_native(self, data, files, field_name, into): - if self.read_only: - return - - try: - if self.many: - try: - # Form data - value = data.getlist(field_name) - if value == [''] or value == []: - raise KeyError - except AttributeError: - # Non-form data - value = data[field_name] - else: - value = data[field_name] - except KeyError: - if self.partial: - return - value = self.get_default_value() - - if value in self.null_values: - if self.required: - raise ValidationError(self.error_messages['required']) - into[(self.source or field_name)] = None - elif self.many: - into[(self.source or field_name)] = [self.from_native(item) for item in value] - else: - into[(self.source or field_name)] = self.from_native(value) - - -# PrimaryKey relationships - -class PrimaryKeyRelatedField(RelatedField): - """ - Represents a relationship as a pk value. - """ - read_only = False - - default_error_messages = { - 'does_not_exist': _("Invalid pk '%s' - object does not exist."), - 'incorrect_type': _('Incorrect type. Expected pk value, received %s.'), - } - - # TODO: Remove these field hacks... - def prepare_value(self, obj): - return self.to_native(obj.pk) - - def label_from_instance(self, obj): - """ - Return a readable representation for use with eg. select widgets. - """ - desc = smart_text(obj) - ident = smart_text(self.to_native(obj.pk)) - if desc == ident: - return desc - return "%s - %s" % (desc, ident) - - # TODO: Possibly change this to just take `obj`, through prob less performant - def to_native(self, pk): - return pk - - def from_native(self, data): - if self.queryset is None: - raise Exception('Writable related fields must include a `queryset` argument') - - try: - return self.queryset.get(pk=data) - except ObjectDoesNotExist: - msg = self.error_messages['does_not_exist'] % smart_text(data) - raise ValidationError(msg) - except (TypeError, ValueError): - received = type(data).__name__ - msg = self.error_messages['incorrect_type'] % received - raise ValidationError(msg) - - def field_to_native(self, obj, field_name): - if self.many: - # To-many relationship - - queryset = None - if not self.source: - # Prefer obj.serializable_value for performance reasons - try: - queryset = obj.serializable_value(field_name) - except AttributeError: - pass - if queryset is None: - # RelatedManager (reverse relationship) - source = self.source or field_name - queryset = obj - for component in source.split('.'): - if queryset is None: - return [] - queryset = get_component(queryset, component) - - # Forward relationship - if is_simple_callable(getattr(queryset, 'all', None)): - return [self.to_native(item.pk) for item in queryset.all()] - else: - # Also support non-queryset iterables. - # This allows us to also support plain lists of related items. - return [self.to_native(item.pk) for item in queryset] - - # To-one relationship - try: - # Prefer obj.serializable_value for performance reasons - pk = obj.serializable_value(self.source or field_name) - except AttributeError: - # RelatedObject (reverse relationship) - try: - pk = getattr(obj, self.source or field_name).pk - except (ObjectDoesNotExist, AttributeError): - return None - - # Forward relationship - return self.to_native(pk) - - -# Slug relationships - -class SlugRelatedField(RelatedField): - """ - Represents a relationship using a unique field on the target. - """ - read_only = False - - default_error_messages = { - 'does_not_exist': _("Object with %s=%s does not exist."), - 'invalid': _('Invalid value.'), - } - - def __init__(self, *args, **kwargs): - self.slug_field = kwargs.pop('slug_field', None) - assert self.slug_field, 'slug_field is required' - super(SlugRelatedField, self).__init__(*args, **kwargs) - - def to_native(self, obj): - return getattr(obj, self.slug_field) - - def from_native(self, data): - if self.queryset is None: - raise Exception('Writable related fields must include a `queryset` argument') - - try: - return self.queryset.get(**{self.slug_field: data}) - except ObjectDoesNotExist: - raise ValidationError(self.error_messages['does_not_exist'] % - (self.slug_field, smart_text(data))) - except (TypeError, ValueError): - msg = self.error_messages['invalid'] - raise ValidationError(msg) - - -# Hyperlinked relationships - -class HyperlinkedRelatedField(RelatedField): - """ - Represents a relationship using hyperlinking. - """ - read_only = False - lookup_field = 'pk' - - default_error_messages = { - 'no_match': _('Invalid hyperlink - No URL match'), - 'incorrect_match': _('Invalid hyperlink - Incorrect URL match'), - 'configuration_error': _('Invalid hyperlink due to configuration error'), - 'does_not_exist': _("Invalid hyperlink - object does not exist."), - 'incorrect_type': _('Incorrect type. Expected url string, received %s.'), - } - - def __init__(self, *args, **kwargs): - try: - self.view_name = kwargs.pop('view_name') - except KeyError: - raise ValueError("Hyperlinked field requires 'view_name' kwarg") - - self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) - self.format = kwargs.pop('format', None) - - super(HyperlinkedRelatedField, self).__init__(*args, **kwargs) - - def get_url(self, obj, view_name, request, format): - """ - Given an object, return the URL that hyperlinks to the object. - - May raise a `NoReverseMatch` if the `view_name` and `lookup_field` - attributes are not configured to correctly match the URL conf. - """ - lookup_field = getattr(obj, self.lookup_field) - kwargs = {self.lookup_field: lookup_field} - return reverse(view_name, kwargs=kwargs, request=request, format=format) - - def get_object(self, queryset, view_name, view_args, view_kwargs): - """ - Return the object corresponding to a matched URL. - - Takes the matched URL conf arguments, and the queryset, and should - return an object instance, or raise an `ObjectDoesNotExist` exception. - """ - lookup_value = view_kwargs[self.lookup_field] - filter_kwargs = {self.lookup_field: lookup_value} - return queryset.get(**filter_kwargs) - - def to_native(self, obj): - view_name = self.view_name - request = self.context.get('request', None) - format = self.format or self.context.get('format', None) - - assert request is not None, ( - "`HyperlinkedRelatedField` requires the request in the serializer " - "context. Add `context={'request': request}` when instantiating " - "the serializer." - ) - - # If the object has not yet been saved then we cannot hyperlink to it. - if getattr(obj, 'pk', None) is None: - return - - # Return the hyperlink, or error if incorrectly configured. - try: - return self.get_url(obj, view_name, request, format) - except NoReverseMatch: - msg = ( - 'Could not resolve URL for hyperlinked relationship using ' - 'view name "%s". You may have failed to include the related ' - 'model in your API, or incorrectly configured the ' - '`lookup_field` attribute on this field.' - ) - raise Exception(msg % view_name) - - def from_native(self, value): - # Convert URL -> model instance pk - # TODO: Use values_list - queryset = self.queryset - if queryset is None: - raise Exception('Writable related fields must include a `queryset` argument') - - try: - http_prefix = value.startswith(('http:', 'https:')) - except AttributeError: - msg = self.error_messages['incorrect_type'] - raise ValidationError(msg % type(value).__name__) - - if http_prefix: - # If needed convert absolute URLs to relative path - value = urlparse.urlparse(value).path - prefix = get_script_prefix() - if value.startswith(prefix): - value = '/' + value[len(prefix):] - - try: - match = resolve(value) - except Exception: - raise ValidationError(self.error_messages['no_match']) - - if match.view_name != self.view_name: - raise ValidationError(self.error_messages['incorrect_match']) - - try: - return self.get_object(queryset, match.view_name, - match.args, match.kwargs) - except (ObjectDoesNotExist, TypeError, ValueError): - raise ValidationError(self.error_messages['does_not_exist']) - - -class HyperlinkedIdentityField(Field): - """ - Represents the instance, or a property on the instance, using hyperlinking. - """ - lookup_field = 'pk' - read_only = True - - def __init__(self, *args, **kwargs): - try: - self.view_name = kwargs.pop('view_name') - except KeyError: - msg = "HyperlinkedIdentityField requires 'view_name' argument" - raise ValueError(msg) - - self.format = kwargs.pop('format', None) - lookup_field = kwargs.pop('lookup_field', None) - self.lookup_field = lookup_field or self.lookup_field - - super(HyperlinkedIdentityField, self).__init__(*args, **kwargs) - - def field_to_native(self, obj, field_name): - request = self.context.get('request', None) - format = self.context.get('format', None) - view_name = self.view_name - - assert request is not None, ( - "`HyperlinkedIdentityField` requires the request in the serializer" - " context. Add `context={'request': request}` when instantiating " - "the serializer." - ) - - # By default use whatever format is given for the current context - # unless the target is a different type to the source. - # - # Eg. Consider a HyperlinkedIdentityField pointing from a json - # representation to an html property of that representation... - # - # '/snippets/1/' should link to '/snippets/1/highlight/' - # ...but... - # '/snippets/1/.json' should link to '/snippets/1/highlight/.html' - if format and self.format and self.format != format: - format = self.format - - # Return the hyperlink, or error if incorrectly configured. - try: - return self.get_url(obj, view_name, request, format) - except NoReverseMatch: - msg = ( - 'Could not resolve URL for hyperlinked relationship using ' - 'view name "%s". You may have failed to include the related ' - 'model in your API, or incorrectly configured the ' - '`lookup_field` attribute on this field.' - ) - raise Exception(msg % view_name) - - def get_url(self, obj, view_name, request, format): - """ - Given an object, return the URL that hyperlinks to the object. - - May raise a `NoReverseMatch` if the `view_name` and `lookup_field` - attributes are not configured to correctly match the URL conf. - """ - lookup_field = getattr(obj, self.lookup_field, None) - kwargs = {self.lookup_field: lookup_field} - - # Handle unsaved object case - if lookup_field is None: - return None - - return reverse(view_name, kwargs=kwargs, request=request, format=format) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 748ebac94..e8935b012 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -458,7 +458,7 @@ class BrowsableAPIRenderer(BaseRenderer): ): return - serializer = view.get_serializer(instance=obj, data=data, files=files) + serializer = view.get_serializer(instance=obj, data=data) serializer.is_valid() data = serializer.data @@ -579,10 +579,10 @@ class BrowsableAPIRenderer(BaseRenderer): 'available_formats': [renderer_cls.format for renderer_cls in view.renderer_classes], 'response_headers': response_headers, - 'put_form': self.get_rendered_html_form(view, 'PUT', request), - 'post_form': self.get_rendered_html_form(view, 'POST', request), - 'delete_form': self.get_rendered_html_form(view, 'DELETE', request), - 'options_form': self.get_rendered_html_form(view, 'OPTIONS', request), + #'put_form': self.get_rendered_html_form(view, 'PUT', request), + #'post_form': self.get_rendered_html_form(view, 'POST', request), + #'delete_form': self.get_rendered_html_form(view, 'DELETE', request), + #'options_form': self.get_rendered_html_form(view, 'OPTIONS', request), 'raw_data_put_form': raw_data_put_form, 'raw_data_post_form': raw_data_post_form, diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index be8ad3f24..d121812d6 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -10,21 +10,14 @@ python primitives. 2. The process of marshalling between python primitives and request and response content is handled by parsers and renderers. """ -from __future__ import unicode_literals -import copy -import datetime -import inspect -import types -from decimal import Decimal -from django.contrib.contenttypes.generic import GenericForeignKey -from django.core.paginator import Page from django.db import models -from django.forms import widgets from django.utils import six -from django.utils.datastructures import SortedDict -from django.core.exceptions import ObjectDoesNotExist +from collections import namedtuple, OrderedDict +from rest_framework.fields import empty, set_value, Field, SkipField, ValidationError from rest_framework.settings import api_settings - +from rest_framework.utils import html +import copy +import inspect # Note: We do the following so that users of the framework can use this style: # @@ -37,6 +30,253 @@ from rest_framework.relations import * # NOQA from rest_framework.fields import * # NOQA +FieldResult = namedtuple('FieldResult', ['field', 'value', 'error']) + + +class BaseSerializer(Field): + def __init__(self, instance=None, data=None, **kwargs): + super(BaseSerializer, self).__init__(**kwargs) + self.instance = instance + self._initial_data = data + + def to_native(self, data): + raise NotImplementedError() + + def to_primative(self, instance): + raise NotImplementedError() + + def update(self, instance): + raise NotImplementedError() + + def create(self): + raise NotImplementedError() + + def save(self, extras=None): + if extras is not None: + self._validated_data.update(extras) + + if self.instance is not None: + self.update(self.instance) + else: + self.instance = self.create() + + return self.instance + + def is_valid(self): + try: + self._validated_data = self.to_native(self._initial_data) + except ValidationError as exc: + self._validated_data = {} + self._errors = exc.args[0] + return False + self._errors = {} + return True + + @property + def data(self): + if not hasattr(self, '_data'): + if self.instance is not None: + self._data = self.to_primative(self.instance) + elif self._initial_data is not None: + self._data = { + field_name: field.get_value(self._initial_data) + for field_name, field in self.fields.items() + } + else: + self._data = self.get_initial() + return self._data + + @property + def errors(self): + if not hasattr(self, '_errors'): + msg = 'You must call `.is_valid()` before accessing `.errors`.' + raise AssertionError(msg) + return self._errors + + @property + def validated_data(self): + if not hasattr(self, '_validated_data'): + msg = 'You must call `.is_valid()` before accessing `.validated_data`.' + raise AssertionError(msg) + return self._validated_data + + +class SerializerMetaclass(type): + """ + This metaclass sets a dictionary named `base_fields` on the class. + + Any fields included as attributes on either the class or it's superclasses + will be include in the `base_fields` dictionary. + """ + + @classmethod + def _get_fields(cls, bases, attrs): + fields = [(field_name, attrs.pop(field_name)) + for field_name, obj in list(attrs.items()) + if isinstance(obj, Field)] + fields.sort(key=lambda x: x[1]._creation_counter) + + # If this class is subclassing another Serializer, add that Serializer's + # fields. Note that we loop over the bases in *reverse*. This is necessary + # in order to maintain the correct order of fields. + for base in bases[::-1]: + if hasattr(base, 'base_fields'): + fields = list(base.base_fields.items()) + fields + + return OrderedDict(fields) + + def __new__(cls, name, bases, attrs): + attrs['base_fields'] = cls._get_fields(bases, attrs) + return super(SerializerMetaclass, cls).__new__(cls, name, bases, attrs) + + +@six.add_metaclass(SerializerMetaclass) +class Serializer(BaseSerializer): + + def __new__(cls, *args, **kwargs): + many = kwargs.pop('many', False) + if many: + class DynamicListSerializer(ListSerializer): + child = cls() + return DynamicListSerializer(*args, **kwargs) + return super(Serializer, cls).__new__(cls) + + def __init__(self, *args, **kwargs): + kwargs.pop('context', None) + kwargs.pop('partial', None) + kwargs.pop('many', False) + + super(Serializer, self).__init__(*args, **kwargs) + + # Every new serializer is created with a clone of the field instances. + # This allows users to dynamically modify the fields on a serializer + # instance without affecting every other serializer class. + self.fields = self.get_fields() + + # Setup all the child fields, to provide them with the current context. + for field_name, field in self.fields.items(): + field.bind(field_name, self, self) + + def get_fields(self): + return copy.deepcopy(self.base_fields) + + def bind(self, field_name, parent, root): + # If the serializer is used as a field then when it becomes bound + # it also needs to bind all its child fields. + super(Serializer, self).bind(field_name, parent, root) + for field_name, field in self.fields.items(): + field.bind(field_name, self, root) + + def get_initial(self): + return { + field.field_name: field.get_initial() + for field in self.fields.values() + } + + def get_value(self, dictionary): + # We override the default field access in order to support + # nested HTML forms. + if html.is_html_input(dictionary): + return html.parse_html_dict(dictionary, prefix=self.field_name) + return dictionary.get(self.field_name, empty) + + def to_native(self, data): + """ + Dict of native values <- Dict of primitive datatypes. + """ + ret = {} + errors = {} + fields = [field for field in self.fields.values() if not field.read_only] + + for field in fields: + primitive_value = field.get_value(data) + try: + validated_value = field.validate(primitive_value) + except ValidationError as exc: + errors[field.field_name] = str(exc) + except SkipField: + pass + else: + set_value(ret, field.source_attrs, validated_value) + + if errors: + raise ValidationError(errors) + + return ret + + def to_primative(self, instance): + """ + Object instance -> Dict of primitive datatypes. + """ + ret = OrderedDict() + fields = [field for field in self.fields.values() if not field.write_only] + + for field in fields: + native_value = field.get_attribute(instance) + ret[field.field_name] = field.to_primative(native_value) + + return ret + + def __iter__(self): + errors = self.errors if hasattr(self, '_errors') else {} + for field in self.fields.values(): + value = self.data.get(field.field_name) if self.data else None + error = errors.get(field.field_name) + yield FieldResult(field, value, error) + + +class ListSerializer(BaseSerializer): + child = None + initial = [] + + def __init__(self, *args, **kwargs): + self.child = kwargs.pop('child', copy.deepcopy(self.child)) + assert self.child is not None, '`child` is a required argument.' + + kwargs.pop('context', None) + kwargs.pop('partial', None) + + super(ListSerializer, self).__init__(*args, **kwargs) + self.child.bind('', self, self) + + def bind(self, field_name, parent, root): + # If the list is used as a field then it needs to provide + # the current context to the child serializer. + super(ListSerializer, self).bind(field_name, parent, root) + self.child.bind(field_name, self, root) + + def get_value(self, dictionary): + # We override the default field access in order to support + # lists in HTML forms. + if is_html_input(dictionary): + return html.parse_html_list(dictionary, prefix=self.field_name) + return dictionary.get(self.field_name, empty) + + def to_native(self, data): + """ + List of dicts of native values <- List of dicts of primitive datatypes. + """ + if html.is_html_input(data): + data = html.parse_html_list(data) + + return [self.child.validate(item) for item in data] + + def to_primative(self, data): + """ + List of object instances -> List of dicts of primitive datatypes. + """ + return [self.child.to_primative(item) for item in data] + + def create(self, attrs_list): + return [self.child.create(attrs) for attrs in attrs_list] + + def save(self): + if self.instance is not None: + self.update(self.instance, self.validated_data) + self.instance = self.create(self.validated_data) + return self.instance + + def _resolve_model(obj): """ Resolve supplied `obj` to a Django model class. @@ -58,614 +298,71 @@ def _resolve_model(obj): raise ValueError("{0} is not a Django model".format(obj)) -def pretty_name(name): - """Converts 'first_name' to 'First name'""" - if not name: - return '' - return name.replace('_', ' ').capitalize() - - -class RelationsList(list): - _deleted = [] - - -class NestedValidationError(ValidationError): - """ - The default ValidationError behavior is to stringify each item in the list - if the messages are a list of error messages. - - In the case of nested serializers, where the parent has many children, - then the child's `serializer.errors` will be a list of dicts. In the case - of a single child, the `serializer.errors` will be a dict. - - We need to override the default behavior to get properly nested error dicts. - """ - - def __init__(self, message): - if isinstance(message, dict): - self._messages = [message] - else: - self._messages = message - - @property - def messages(self): - return self._messages - - -class DictWithMetadata(dict): - """ - A dict-like object, that can have additional properties attached. - """ - def __getstate__(self): - """ - Used by pickle (e.g., caching). - Overridden to remove the metadata from the dict, since it shouldn't be - pickled and may in some instances be unpickleable. - """ - return dict(self) - - -class SortedDictWithMetadata(SortedDict): - """ - A sorted dict-like object, that can have additional properties attached. - """ - def __getstate__(self): - """ - Used by pickle (e.g., caching). - Overriden to remove the metadata from the dict, since it shouldn't be - pickle and may in some instances be unpickleable. - """ - return SortedDict(self).__dict__ - - -def _is_protected_type(obj): - """ - True if the object is a native datatype that does not need to - be serialized further. - """ - return isinstance(obj, ( - types.NoneType, - int, long, - datetime.datetime, datetime.date, datetime.time, - float, Decimal, - basestring) - ) - - -def _get_declared_fields(bases, attrs): - """ - Create a list of serializer field instances from the passed in 'attrs', - plus any fields on the base classes (in 'bases'). - - Note that all fields from the base classes are used. - """ - fields = [(field_name, attrs.pop(field_name)) - for field_name, obj in list(six.iteritems(attrs)) - if isinstance(obj, Field)] - fields.sort(key=lambda x: x[1].creation_counter) - - # If this class is subclassing another Serializer, add that Serializer's - # fields. Note that we loop over the bases in *reverse*. This is necessary - # in order to maintain the correct order of fields. - for base in bases[::-1]: - if hasattr(base, 'base_fields'): - fields = list(base.base_fields.items()) + fields - - return SortedDict(fields) - - -class SerializerMetaclass(type): - def __new__(cls, name, bases, attrs): - attrs['base_fields'] = _get_declared_fields(bases, attrs) - return super(SerializerMetaclass, cls).__new__(cls, name, bases, attrs) - - -class SerializerOptions(object): - """ - Meta class options for Serializer - """ - def __init__(self, meta): - self.depth = getattr(meta, 'depth', 0) - self.fields = getattr(meta, 'fields', ()) - self.exclude = getattr(meta, 'exclude', ()) - - -class BaseSerializer(WritableField): - """ - This is the Serializer implementation. - We need to implement it as `BaseSerializer` due to metaclass magicks. - """ - class Meta(object): - pass - - _options_class = SerializerOptions - _dict_class = SortedDictWithMetadata - - def __init__(self, instance=None, data=None, files=None, - context=None, partial=False, many=False, - allow_add_remove=False, **kwargs): - super(BaseSerializer, self).__init__(**kwargs) - self.opts = self._options_class(self.Meta) - self.parent = None - self.root = None - self.partial = partial - self.many = many - self.allow_add_remove = allow_add_remove - - self.context = context or {} - - self.init_data = data - self.init_files = files - self.object = instance - self.fields = self.get_fields() - - self._data = None - self._files = None - self._errors = None - - if many and instance is not None and not hasattr(instance, '__iter__'): - raise ValueError('instance should be a queryset or other iterable with many=True') - - if allow_add_remove and not many: - raise ValueError('allow_add_remove should only be used for bulk updates, but you have not set many=True') - - ##### - # Methods to determine which fields to use when (de)serializing objects. - - def get_default_fields(self): - """ - Return the complete set of default fields for the object, as a dict. - """ - return {} - - def get_fields(self): - """ - Returns the complete set of fields for the object as a dict. - - This will be the set of any explicitly declared fields, - plus the set of fields returned by get_default_fields(). - """ - ret = SortedDict() - - # Get the explicitly declared fields - base_fields = copy.deepcopy(self.base_fields) - for key, field in base_fields.items(): - ret[key] = field - - # Add in the default fields - default_fields = self.get_default_fields() - for key, val in default_fields.items(): - if key not in ret: - ret[key] = val - - # If 'fields' is specified, use those fields, in that order. - if self.opts.fields: - assert isinstance(self.opts.fields, (list, tuple)), '`fields` must be a list or tuple' - new = SortedDict() - for key in self.opts.fields: - new[key] = ret[key] - ret = new - - # Remove anything in 'exclude' - if self.opts.exclude: - assert isinstance(self.opts.exclude, (list, tuple)), '`exclude` must be a list or tuple' - for key in self.opts.exclude: - ret.pop(key, None) - - for key, field in ret.items(): - field.initialize(parent=self, field_name=key) - - return ret - - ##### - # Methods to convert or revert from objects <--> primitive representations. - - def get_field_key(self, field_name): - """ - Return the key that should be used for a given field. - """ - return field_name - - def restore_fields(self, data, files): - """ - Core of deserialization, together with `restore_object`. - Converts a dictionary of data into a dictionary of deserialized fields. - """ - reverted_data = {} - - if data is not None and not isinstance(data, dict): - self._errors['non_field_errors'] = ['Invalid data'] - return None - - for field_name, field in self.fields.items(): - field.initialize(parent=self, field_name=field_name) - try: - field.field_from_native(data, files, field_name, reverted_data) - except ValidationError as err: - self._errors[field_name] = list(err.messages) - - return reverted_data - - def perform_validation(self, attrs): - """ - Run `validate_()` and `validate()` methods on the serializer - """ - for field_name, field in self.fields.items(): - if field_name in self._errors: - continue - - source = field.source or field_name - if self.partial and source not in attrs: - continue - try: - validate_method = getattr(self, 'validate_%s' % field_name, None) - if validate_method: - attrs = validate_method(attrs, source) - except ValidationError as err: - self._errors[field_name] = self._errors.get(field_name, []) + list(err.messages) - - # If there are already errors, we don't run .validate() because - # field-validation failed and thus `attrs` may not be complete. - # which in turn can cause inconsistent validation errors. - if not self._errors: - try: - attrs = self.validate(attrs) - except ValidationError as err: - if hasattr(err, 'message_dict'): - for field_name, error_messages in err.message_dict.items(): - self._errors[field_name] = self._errors.get(field_name, []) + list(error_messages) - elif hasattr(err, 'messages'): - self._errors['non_field_errors'] = err.messages - - return attrs - - def validate(self, attrs): - """ - Stub method, to be overridden in Serializer subclasses - """ - return attrs - - def restore_object(self, attrs, instance=None): - """ - Deserialize a dictionary of attributes into an object instance. - You should override this method to control how deserialized objects - are instantiated. - """ - if instance is not None: - instance.update(attrs) - return instance - return attrs - - def to_native(self, obj): - """ - Serialize objects -> primitives. - """ - ret = self._dict_class() - ret.fields = self._dict_class() - - for field_name, field in self.fields.items(): - if field.read_only and obj is None: - continue - field.initialize(parent=self, field_name=field_name) - key = self.get_field_key(field_name) - value = field.field_to_native(obj, field_name) - method = getattr(self, 'transform_%s' % field_name, None) - if callable(method): - value = method(obj, value) - if not getattr(field, 'write_only', False): - ret[key] = value - ret.fields[key] = self.augment_field(field, field_name, key, value) - - return ret - - def from_native(self, data, files=None): - """ - Deserialize primitives -> objects. - """ - self._errors = {} - - if data is not None or files is not None: - attrs = self.restore_fields(data, files) - if attrs is not None: - attrs = self.perform_validation(attrs) - else: - self._errors['non_field_errors'] = ['No input provided'] - - if not self._errors: - return self.restore_object(attrs, instance=getattr(self, 'object', None)) - - def augment_field(self, field, field_name, key, value): - # This horrible stuff is to manage serializers rendering to HTML - field._errors = self._errors.get(key) if self._errors else None - field._name = field_name - field._value = self.init_data.get(key) if self._errors and self.init_data else value - if not field.label: - field.label = pretty_name(key) - return field - - def field_to_native(self, obj, field_name): - """ - Override default so that the serializer can be used as a nested field - across relationships. - """ - if self.write_only: - return None - - if self.source == '*': - return self.to_native(obj) - - # Get the raw field value - try: - source = self.source or field_name - value = obj - - for component in source.split('.'): - if value is None: - break - value = get_component(value, component) - except ObjectDoesNotExist: - return None - - if is_simple_callable(getattr(value, 'all', None)): - return [self.to_native(item) for item in value.all()] - - if value is None: - return None - - if self.many: - return [self.to_native(item) for item in value] - return self.to_native(value) - - def field_from_native(self, data, files, field_name, into): - """ - Override default so that the serializer can be used as a writable - nested field across relationships. - """ - if self.read_only: - return - - try: - value = data[field_name] - except KeyError: - if self.default is not None and not self.partial: - # Note: partial updates shouldn't set defaults - value = copy.deepcopy(self.default) - else: - if self.required: - raise ValidationError(self.error_messages['required']) - return - - if self.source == '*': - if value: - reverted_data = self.restore_fields(value, {}) - if not self._errors: - into.update(reverted_data) - else: - if value in (None, ''): - into[(self.source or field_name)] = None - else: - # Set the serializer object if it exists - obj = get_component(self.parent.object, self.source or field_name) if self.parent.object else None - - # If we have a model manager or similar object then we need - # to iterate through each instance. - if ( - self.many and - not hasattr(obj, '__iter__') and - is_simple_callable(getattr(obj, 'all', None)) - ): - obj = obj.all() - - kwargs = { - 'instance': obj, - 'data': value, - 'context': self.context, - 'partial': self.partial, - 'many': self.many, - 'allow_add_remove': self.allow_add_remove - } - serializer = self.__class__(**kwargs) - - if serializer.is_valid(): - into[self.source or field_name] = serializer.object - else: - # Propagate errors up to our parent - raise NestedValidationError(serializer.errors) - - def get_identity(self, data): - """ - This hook is required for bulk update. - It is used to determine the canonical identity of a given object. - - Note that the data has not been validated at this point, so we need - to make sure that we catch any cases of incorrect datatypes being - passed to this method. - """ - try: - return data.get('id', None) - except AttributeError: - return None - - @property - def errors(self): - """ - Run deserialization and return error data, - setting self.object if no errors occurred. - """ - if self._errors is None: - data, files = self.init_data, self.init_files - - if self.many is not None: - many = self.many - else: - many = hasattr(data, '__iter__') and not isinstance(data, (Page, dict, six.text_type)) - if many: - warnings.warn('Implicit list/queryset serialization is deprecated. ' - 'Use the `many=True` flag when instantiating the serializer.', - DeprecationWarning, stacklevel=3) - - if many: - ret = RelationsList() - errors = [] - update = self.object is not None - - if update: - # If this is a bulk update we need to map all the objects - # to a canonical identity so we can determine which - # individual object is being updated for each item in the - # incoming data - objects = self.object - identities = [self.get_identity(self.to_native(obj)) for obj in objects] - identity_to_objects = dict(zip(identities, objects)) - - if hasattr(data, '__iter__') and not isinstance(data, (dict, six.text_type)): - for item in data: - if update: - # Determine which object we're updating - identity = self.get_identity(item) - self.object = identity_to_objects.pop(identity, None) - if self.object is None and not self.allow_add_remove: - ret.append(None) - errors.append({'non_field_errors': ['Cannot create a new item, only existing items may be updated.']}) - continue - - ret.append(self.from_native(item, None)) - errors.append(self._errors) - - if update and self.allow_add_remove: - ret._deleted = identity_to_objects.values() - - self._errors = any(errors) and errors or [] - else: - self._errors = {'non_field_errors': ['Expected a list of items.']} - else: - ret = self.from_native(data, files) - - if not self._errors: - self.object = ret - - return self._errors - - def is_valid(self): - return not self.errors - - @property - def data(self): - """ - Returns the serialized data on the serializer. - """ - if self._data is None: - obj = self.object - - if self.many is not None: - many = self.many - else: - many = hasattr(obj, '__iter__') and not isinstance(obj, (Page, dict)) - if many: - warnings.warn('Implicit list/queryset serialization is deprecated. ' - 'Use the `many=True` flag when instantiating the serializer.', - DeprecationWarning, stacklevel=2) - - if many: - self._data = [self.to_native(item) for item in obj] - else: - self._data = self.to_native(obj) - - return self._data - - def save_object(self, obj, **kwargs): - obj.save(**kwargs) - - def delete_object(self, obj): - obj.delete() - - def save(self, **kwargs): - """ - Save the deserialized object and return it. - """ - # Clear cached _data, which may be invalidated by `save()` - self._data = None - - if isinstance(self.object, list): - [self.save_object(item, **kwargs) for item in self.object] - - if self.object._deleted: - [self.delete_object(item) for item in self.object._deleted] - else: - self.save_object(self.object, **kwargs) - - return self.object - - def metadata(self): - """ - Return a dictionary of metadata about the fields on the serializer. - Useful for things like responding to OPTIONS requests, or generating - API schemas for auto-documentation. - """ - return SortedDict( - [ - (field_name, field.metadata()) - for field_name, field in six.iteritems(self.fields) - ] - ) - - -class Serializer(six.with_metaclass(SerializerMetaclass, BaseSerializer)): - pass - - -class ModelSerializerOptions(SerializerOptions): +class ModelSerializerOptions(object): """ Meta class options for ModelSerializer """ def __init__(self, meta): - super(ModelSerializerOptions, self).__init__(meta) - self.model = getattr(meta, 'model', None) - self.read_only_fields = getattr(meta, 'read_only_fields', ()) - self.write_only_fields = getattr(meta, 'write_only_fields', ()) + self.model = getattr(meta, 'model') + self.fields = getattr(meta, 'fields', ()) + self.depth = getattr(meta, 'depth', 0) class ModelSerializer(Serializer): - """ - A serializer that deals with model instances and querysets. - """ - _options_class = ModelSerializerOptions - field_mapping = { models.AutoField: IntegerField, - models.FloatField: FloatField, + # models.FloatField: FloatField, models.IntegerField: IntegerField, models.PositiveIntegerField: IntegerField, models.SmallIntegerField: IntegerField, models.PositiveSmallIntegerField: IntegerField, - models.DateTimeField: DateTimeField, - models.DateField: DateField, - models.TimeField: TimeField, - models.DecimalField: DecimalField, - models.EmailField: EmailField, + # models.DateTimeField: DateTimeField, + # models.DateField: DateField, + # models.TimeField: TimeField, + # models.DecimalField: DecimalField, + # models.EmailField: EmailField, models.CharField: CharField, - models.URLField: URLField, - models.SlugField: SlugField, + # models.URLField: URLField, + # models.SlugField: SlugField, models.TextField: CharField, models.CommaSeparatedIntegerField: CharField, models.BooleanField: BooleanField, models.NullBooleanField: BooleanField, - models.FileField: FileField, - models.ImageField: ImageField, + # models.FileField: FileField, + # models.ImageField: ImageField, } + _options_class = ModelSerializerOptions + + def __init__(self, *args, **kwargs): + self.opts = self._options_class(self.Meta) + super(ModelSerializer, self).__init__(*args, **kwargs) + + def get_fields(self): + # Get the explicitly declared fields. + fields = copy.deepcopy(self.base_fields) + + # Add in the default fields. + for key, val in self.get_default_fields().items(): + if key not in fields: + fields[key] = val + + # If `fields` is set on the `Meta` class, + # then use only those fields, and in that order. + if self.opts.fields: + fields = OrderedDict([ + (key, fields[key]) for key in self.opts.fields + ]) + + return fields + def get_default_fields(self): """ Return all the fields that should be serialized for the model. """ - cls = self.opts.model - assert cls is not None, ( - "Serializer class '%s' is missing 'model' Meta option" % - self.__class__.__name__ - ) opts = cls._meta.concrete_model._meta - ret = SortedDict() + ret = OrderedDict() nested = bool(self.opts.depth) # Deal with adding the primary key field @@ -694,29 +391,9 @@ class ModelSerializer(Serializer): has_through_model = True if model_field.rel and nested: - if len(inspect.getargspec(self.get_nested_field).args) == 2: - warnings.warn( - 'The `get_nested_field(model_field)` call signature ' - 'is deprecated. ' - 'Use `get_nested_field(model_field, related_model, ' - 'to_many) instead', - DeprecationWarning - ) - field = self.get_nested_field(model_field) - else: - field = self.get_nested_field(model_field, related_model, to_many) + field = self.get_nested_field(model_field, related_model, to_many) elif model_field.rel: - if len(inspect.getargspec(self.get_nested_field).args) == 3: - warnings.warn( - 'The `get_related_field(model_field, to_many)` call ' - 'signature is deprecated. ' - 'Use `get_related_field(model_field, related_model, ' - 'to_many) instead', - DeprecationWarning - ) - field = self.get_related_field(model_field, to_many=to_many) - else: - field = self.get_related_field(model_field, related_model, to_many) + field = self.get_related_field(model_field, related_model, to_many) else: field = self.get_field(model_field) @@ -763,38 +440,6 @@ class ModelSerializer(Serializer): ret[accessor_name] = field - # Ensure that 'read_only_fields' is an iterable - assert isinstance(self.opts.read_only_fields, (list, tuple)), '`read_only_fields` must be a list or tuple' - - # Add the `read_only` flag to any fields that have been specified - # in the `read_only_fields` option - for field_name in self.opts.read_only_fields: - assert field_name not in self.base_fields.keys(), ( - "field '%s' on serializer '%s' specified in " - "`read_only_fields`, but also added " - "as an explicit field. Remove it from `read_only_fields`." % - (field_name, self.__class__.__name__)) - assert field_name in ret, ( - "Non-existant field '%s' specified in `read_only_fields` " - "on serializer '%s'." % - (field_name, self.__class__.__name__)) - ret[field_name].read_only = True - - # Ensure that 'write_only_fields' is an iterable - assert isinstance(self.opts.write_only_fields, (list, tuple)), '`write_only_fields` must be a list or tuple' - - for field_name in self.opts.write_only_fields: - assert field_name not in self.base_fields.keys(), ( - "field '%s' on serializer '%s' specified in " - "`write_only_fields`, but also added " - "as an explicit field. Remove it from `write_only_fields`." % - (field_name, self.__class__.__name__)) - assert field_name in ret, ( - "Non-existant field '%s' specified in `write_only_fields` " - "on serializer '%s'." % - (field_name, self.__class__.__name__)) - ret[field_name].write_only = True - return ret def get_pk_field(self, model_field): @@ -825,28 +470,24 @@ class ModelSerializer(Serializer): # TODO: filter queryset using: # .using(db).complex_filter(self.rel.limit_choices_to) - kwargs = { - 'queryset': related_model._default_manager, - 'many': to_many - } + kwargs = {} + # 'queryset': related_model._default_manager, + # 'many': to_many + # } if model_field: kwargs['required'] = not(model_field.null or model_field.blank) - if model_field.help_text is not None: - kwargs['help_text'] = model_field.help_text + # if model_field.help_text is not None: + # kwargs['help_text'] = model_field.help_text if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name - if not model_field.editable: kwargs['read_only'] = True - if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name - if model_field.help_text is not None: - kwargs['help_text'] = model_field.help_text - - return PrimaryKeyRelatedField(**kwargs) + return IntegerField(**kwargs) + # TODO: return PrimaryKeyRelatedField(**kwargs) def get_field(self, model_field): """ @@ -869,8 +510,8 @@ class ModelSerializer(Serializer): if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name - if model_field.help_text is not None: - kwargs['help_text'] = model_field.help_text + # if model_field.help_text is not None: + # kwargs['help_text'] = model_field.help_text # TODO: TypedChoiceField? if model_field.flatchoices: # This ModelField contains choices @@ -880,7 +521,7 @@ class ModelSerializer(Serializer): return ChoiceField(**kwargs) # put this below the ChoiceField because min_value isn't a valid initializer - if issubclass(model_field.__class__, models.PositiveIntegerField) or\ + if issubclass(model_field.__class__, models.PositiveIntegerField) or \ issubclass(model_field.__class__, models.PositiveSmallIntegerField): kwargs['min_value'] = 0 @@ -888,170 +529,27 @@ class ModelSerializer(Serializer): issubclass(model_field.__class__, (models.CharField, models.TextField)): kwargs['allow_none'] = True - attribute_dict = { - models.CharField: ['max_length'], - models.CommaSeparatedIntegerField: ['max_length'], - models.DecimalField: ['max_digits', 'decimal_places'], - models.EmailField: ['max_length'], - models.FileField: ['max_length'], - models.ImageField: ['max_length'], - models.SlugField: ['max_length'], - models.URLField: ['max_length'], - } + # attribute_dict = { + # models.CharField: ['max_length'], + # models.CommaSeparatedIntegerField: ['max_length'], + # models.DecimalField: ['max_digits', 'decimal_places'], + # models.EmailField: ['max_length'], + # models.FileField: ['max_length'], + # models.ImageField: ['max_length'], + # models.SlugField: ['max_length'], + # models.URLField: ['max_length'], + # } - if model_field.__class__ in attribute_dict: - attributes = attribute_dict[model_field.__class__] - for attribute in attributes: - kwargs.update({attribute: getattr(model_field, attribute)}) + # if model_field.__class__ in attribute_dict: + # attributes = attribute_dict[model_field.__class__] + # for attribute in attributes: + # kwargs.update({attribute: getattr(model_field, attribute)}) try: return self.field_mapping[model_field.__class__](**kwargs) except KeyError: - return ModelField(model_field=model_field, **kwargs) - - def get_validation_exclusions(self, instance=None): - """ - Return a list of field names to exclude from model validation. - """ - cls = self.opts.model - opts = cls._meta.concrete_model._meta - exclusions = [field.name for field in opts.fields + opts.many_to_many] - - for field_name, field in self.fields.items(): - field_name = field.source or field_name - if ( - field_name in exclusions - and not field.read_only - and (field.required or hasattr(instance, field_name)) - and not isinstance(field, Serializer) - ): - exclusions.remove(field_name) - return exclusions - - def full_clean(self, instance): - """ - Perform Django's full_clean, and populate the `errors` dictionary - if any validation errors occur. - - Note that we don't perform this inside the `.restore_object()` method, - so that subclasses can override `.restore_object()`, and still get - the full_clean validation checking. - """ - try: - instance.full_clean(exclude=self.get_validation_exclusions(instance)) - except ValidationError as err: - self._errors = err.message_dict - return None - return instance - - def restore_object(self, attrs, instance=None): - """ - Restore the model instance. - """ - m2m_data = {} - related_data = {} - nested_forward_relations = {} - meta = self.opts.model._meta - - # Reverse fk or one-to-one relations - for (obj, model) in meta.get_all_related_objects_with_model(): - field_name = obj.get_accessor_name() - if field_name in attrs: - related_data[field_name] = attrs.pop(field_name) - - # Reverse m2m relations - for (obj, model) in meta.get_all_related_m2m_objects_with_model(): - field_name = obj.get_accessor_name() - if field_name in attrs: - m2m_data[field_name] = attrs.pop(field_name) - - # Forward m2m relations - for field in meta.many_to_many + meta.virtual_fields: - if isinstance(field, GenericForeignKey): - continue - if field.name in attrs: - m2m_data[field.name] = attrs.pop(field.name) - - # Nested forward relations - These need to be marked so we can save - # them before saving the parent model instance. - for field_name in attrs.keys(): - if isinstance(self.fields.get(field_name, None), Serializer): - nested_forward_relations[field_name] = attrs[field_name] - - # Create an empty instance of the model - if instance is None: - instance = self.opts.model() - - for key, val in attrs.items(): - try: - setattr(instance, key, val) - except ValueError: - self._errors[key] = [self.error_messages['required']] - - # Any relations that cannot be set until we've - # saved the model get hidden away on these - # private attributes, so we can deal with them - # at the point of save. - instance._related_data = related_data - instance._m2m_data = m2m_data - instance._nested_forward_relations = nested_forward_relations - - return instance - - def from_native(self, data, files): - """ - Override the default method to also include model field validation. - """ - instance = super(ModelSerializer, self).from_native(data, files) - if not self._errors: - return self.full_clean(instance) - - def save_object(self, obj, **kwargs): - """ - Save the deserialized object. - """ - if getattr(obj, '_nested_forward_relations', None): - # Nested relationships need to be saved before we can save the - # parent instance. - for field_name, sub_object in obj._nested_forward_relations.items(): - if sub_object: - self.save_object(sub_object) - setattr(obj, field_name, sub_object) - - obj.save(**kwargs) - - if getattr(obj, '_m2m_data', None): - for accessor_name, object_list in obj._m2m_data.items(): - setattr(obj, accessor_name, object_list) - del(obj._m2m_data) - - if getattr(obj, '_related_data', None): - related_fields = dict([ - (field.get_accessor_name(), field) - for field, model - in obj._meta.get_all_related_objects_with_model() - ]) - for accessor_name, related in obj._related_data.items(): - if isinstance(related, RelationsList): - # Nested reverse fk relationship - for related_item in related: - fk_field = related_fields[accessor_name].field.name - setattr(related_item, fk_field, obj) - self.save_object(related_item) - - # Delete any removed objects - if related._deleted: - [self.delete_object(item) for item in related._deleted] - - elif isinstance(related, models.Model): - # Nested reverse one-one relationship - fk_field = obj._meta.get_field_by_name(accessor_name)[0].field.name - setattr(related, fk_field, obj) - self.save_object(related) - else: - # Reverse FK or reverse one-one - setattr(obj, accessor_name, related) - del(obj._related_data) + # TODO: Change this to `return ModelField(model_field=model_field, **kwargs)` + return CharField(**kwargs) class HyperlinkedModelSerializerOptions(ModelSerializerOptions): @@ -1066,14 +564,10 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions): class HyperlinkedModelSerializer(ModelSerializer): - """ - A subclass of ModelSerializer that uses hyperlinked relationships, - instead of primary key relationships. - """ _options_class = HyperlinkedModelSerializerOptions _default_view_name = '%(model_name)s-detail' - _hyperlink_field_class = HyperlinkedRelatedField - _hyperlink_identify_field_class = HyperlinkedIdentityField + #_hyperlink_field_class = HyperlinkedRelatedField + #_hyperlink_identify_field_class = HyperlinkedIdentityField def get_default_fields(self): fields = super(HyperlinkedModelSerializer, self).get_default_fields() @@ -1081,15 +575,15 @@ class HyperlinkedModelSerializer(ModelSerializer): if self.opts.view_name is None: self.opts.view_name = self._get_default_view_name(self.opts.model) - if self.opts.url_field_name not in fields: - url_field = self._hyperlink_identify_field_class( - view_name=self.opts.view_name, - lookup_field=self.opts.lookup_field - ) - ret = self._dict_class() - ret[self.opts.url_field_name] = url_field - ret.update(fields) - fields = ret + # if self.opts.url_field_name not in fields: + # url_field = self._hyperlink_identify_field_class( + # view_name=self.opts.view_name, + # lookup_field=self.opts.lookup_field + # ) + # ret = self._dict_class() + # ret[self.opts.url_field_name] = url_field + # ret.update(fields) + # fields = ret return fields @@ -1103,33 +597,25 @@ class HyperlinkedModelSerializer(ModelSerializer): """ # TODO: filter queryset using: # .using(db).complex_filter(self.rel.limit_choices_to) - kwargs = { - 'queryset': related_model._default_manager, - 'view_name': self._get_default_view_name(related_model), - 'many': to_many - } + # kwargs = { + # 'queryset': related_model._default_manager, + # 'view_name': self._get_default_view_name(related_model), + # 'many': to_many + # } + kwargs = {} if model_field: kwargs['required'] = not(model_field.null or model_field.blank) - if model_field.help_text is not None: - kwargs['help_text'] = model_field.help_text + # if model_field.help_text is not None: + # kwargs['help_text'] = model_field.help_text if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name - if self.opts.lookup_field: - kwargs['lookup_field'] = self.opts.lookup_field + return IntegerField(**kwargs) + # if self.opts.lookup_field: + # kwargs['lookup_field'] = self.opts.lookup_field - return self._hyperlink_field_class(**kwargs) - - def get_identity(self, data): - """ - This hook is required for bulk update. - We need to override the default, to use the url as the identity. - """ - try: - return data.get(self.opts.url_field_name, None) - except AttributeError: - return None + # return self._hyperlink_field_class(**kwargs) def _get_default_view_name(self, model): """ diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 00ffdfbae..6a2f61266 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -7,7 +7,7 @@ from django.db.models.query import QuerySet from django.utils.datastructures import SortedDict from django.utils.functional import Promise from rest_framework.compat import force_text -from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata +# from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata import datetime import decimal import types @@ -106,14 +106,14 @@ else: SortedDict, yaml.representer.SafeRepresenter.represent_dict ) - SafeDumper.add_representer( - DictWithMetadata, - yaml.representer.SafeRepresenter.represent_dict - ) - SafeDumper.add_representer( - SortedDictWithMetadata, - yaml.representer.SafeRepresenter.represent_dict - ) + # SafeDumper.add_representer( + # DictWithMetadata, + # yaml.representer.SafeRepresenter.represent_dict + # ) + # SafeDumper.add_representer( + # SortedDictWithMetadata, + # yaml.representer.SafeRepresenter.represent_dict + # ) SafeDumper.add_representer( types.GeneratorType, yaml.representer.SafeRepresenter.represent_list diff --git a/rest_framework/utils/html.py b/rest_framework/utils/html.py new file mode 100644 index 000000000..bf17050df --- /dev/null +++ b/rest_framework/utils/html.py @@ -0,0 +1,86 @@ +""" +Helpers for dealing with HTML input. +""" + +def is_html_input(dictionary): + # MultiDict type datastructures are used to represent HTML form input, + # which may have more than one value for each key. + return hasattr(dictionary, 'getlist') + + +def parse_html_list(dictionary, prefix=''): + """ + Used to suport list values in HTML forms. + Supports lists of primitives and/or dictionaries. + + * List of primitives. + + { + '[0]': 'abc', + '[1]': 'def', + '[2]': 'hij' + } + --> + [ + 'abc', + 'def', + 'hij' + ] + + * List of dictionaries. + + { + '[0]foo': 'abc', + '[0]bar': 'def', + '[1]foo': 'hij', + '[2]bar': 'klm', + } + --> + [ + {'foo': 'abc', 'bar': 'def'}, + {'foo': 'hij', 'bar': 'klm'} + ] + """ + Dict = type(dictionary) + ret = {} + regex = re.compile(r'^%s\[([0-9]+)\](.*)$' % re.escape(prefix)) + for field, value in dictionary.items(): + match = regex.match(field) + if not match: + continue + index, key = match.groups() + index = int(index) + if not key: + ret[index] = value + elif isinstance(ret.get(index), dict): + ret[index][key] = value + else: + ret[index] = Dict({key: value}) + return [ret[item] for item in sorted(ret.keys())] + + +def parse_html_dict(dictionary, prefix): + """ + Used to support dictionary values in HTML forms. + + { + 'profile.username': 'example', + 'profile.email': 'example@example.com', + } + --> + { + 'profile': { + 'username': 'example, + 'email': 'example@example.com' + } + } + """ + ret = {} + regex = re.compile(r'^%s\.(.+)$' % re.escape(prefix)) + for field, value in dictionary.items(): + match = regex.match(field) + if not match: + continue + key = match.groups()[0] + ret[key] = value + return ret From 82d4b2083292659358d5df4d03d2115576e8ae4e Mon Sep 17 00:00:00 2001 From: Timo Tuominen Date: Mon, 1 Sep 2014 12:17:36 +0300 Subject: [PATCH 018/100] Add subclass matching to serializer field mapping. --- rest_framework/serializers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index be8ad3f24..6d25161e2 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -907,6 +907,9 @@ class ModelSerializer(Serializer): try: return self.field_mapping[model_field.__class__](**kwargs) except KeyError: + for model_field_class, serializer_field_class in self.field_mapping.items(): + if isinstance(model_field, model_field_class): + return serializer_field_class(**kwargs) return ModelField(model_field=model_field, **kwargs) def get_validation_exclusions(self, instance=None): From ae84b8b0e8a99261ea2436f77ab5238f21603c0c Mon Sep 17 00:00:00 2001 From: Timo Tuominen Date: Mon, 1 Sep 2014 15:03:39 +0300 Subject: [PATCH 019/100] Traverse the method resolution order when mapping serializer fields. --- rest_framework/serializers.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 6d25161e2..f37fbf980 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -904,13 +904,11 @@ class ModelSerializer(Serializer): for attribute in attributes: kwargs.update({attribute: getattr(model_field, attribute)}) - try: - return self.field_mapping[model_field.__class__](**kwargs) - except KeyError: - for model_field_class, serializer_field_class in self.field_mapping.items(): - if isinstance(model_field, model_field_class): - return serializer_field_class(**kwargs) - return ModelField(model_field=model_field, **kwargs) + for model_field_baseclass in inspect.getmro(model_field.__class__): + serializer_field_class = self.field_mapping.get(model_field_baseclass) + if serializer_field_class: + return serializer_field_class(**kwargs) + return ModelField(model_field=model_field, **kwargs) def get_validation_exclusions(self, instance=None): """ From 582f6fdd4b0fb12a7c0d1fefe265499a284c9b79 Mon Sep 17 00:00:00 2001 From: Timo Tuominen Date: Mon, 1 Sep 2014 15:54:33 +0300 Subject: [PATCH 020/100] Add utility function to match classes in dictionary. --- rest_framework/serializers.py | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index f37fbf980..5c33300c4 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -625,6 +625,21 @@ class ModelSerializerOptions(SerializerOptions): self.write_only_fields = getattr(meta, 'write_only_fields', ()) +def _get_class_mapping(mapping, obj): + """ + Takes a dictionary with classes as keys, and an object. + Traverses the object's inheritance hierarchy in method + resolution order, and returns the first matching value + from the dictionary or None. + + """ + for baseclass in inspect.getmro(obj.__class__): + val = mapping.get(baseclass) + if val: + return val + return None + + class ModelSerializer(Serializer): """ A serializer that deals with model instances and querysets. @@ -899,15 +914,16 @@ class ModelSerializer(Serializer): models.URLField: ['max_length'], } - if model_field.__class__ in attribute_dict: - attributes = attribute_dict[model_field.__class__] + attributes = _get_class_mapping(attribute_dict, model_field) + if attributes: for attribute in attributes: kwargs.update({attribute: getattr(model_field, attribute)}) - for model_field_baseclass in inspect.getmro(model_field.__class__): - serializer_field_class = self.field_mapping.get(model_field_baseclass) - if serializer_field_class: - return serializer_field_class(**kwargs) + serializer_field_class = _get_class_mapping( + self.field_mapping, model_field) + + if serializer_field_class: + return serializer_field_class(**kwargs) return ModelField(model_field=model_field, **kwargs) def get_validation_exclusions(self, instance=None): From e437520217e20d500d641b95482d49484b1f24a7 Mon Sep 17 00:00:00 2001 From: Timo Tuominen Date: Mon, 1 Sep 2014 17:02:48 +0300 Subject: [PATCH 021/100] Generator implementation of class mapping. --- rest_framework/serializers.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 5c33300c4..b3db35823 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -633,11 +633,10 @@ def _get_class_mapping(mapping, obj): from the dictionary or None. """ - for baseclass in inspect.getmro(obj.__class__): - val = mapping.get(baseclass) - if val: - return val - return None + return next( + (mapping[cls] for cls in inspect.getmro(obj.__class__) if cls in mapping), + None + ) class ModelSerializer(Serializer): From ec096a1caceff6a4f5c75a152dd1c7bea9ed281d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 2 Sep 2014 15:07:56 +0100 Subject: [PATCH 022/100] Add relations and get tests running --- rest_framework/fields.py | 30 +++++++-- rest_framework/mixins.py | 1 - rest_framework/relations.py | 111 ++++++++++++++++++++++++++++++++++ rest_framework/renderers.py | 14 ++--- rest_framework/serializers.py | 8 +-- rest_framework/utils/html.py | 2 + tests/test_serializer.py | 4 +- 7 files changed, 151 insertions(+), 19 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a83bf94c4..3e0f7ca47 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -68,7 +68,7 @@ class Field(object): def __init__(self, read_only=False, write_only=False, required=None, default=empty, initial=None, source=None, - label=None, style=None): + label=None, style=None, error_messages=None): self._creation_counter = Field._creation_counter Field._creation_counter += 1 @@ -216,9 +216,11 @@ class CharField(Field): 'blank': 'This field may not be blank.' } - def __init__(self, *args, **kwargs): + def __init__(self, **kwargs): self.allow_blank = kwargs.pop('allow_blank', False) - super(CharField, self).__init__(*args, **kwargs) + self.max_length = kwargs.pop('max_length', None) + self.min_length = kwargs.pop('min_length', None) + super(CharField, self).__init__(**kwargs) def to_native(self, data): if data == '' and not self.allow_blank: @@ -233,7 +235,7 @@ class ChoiceField(Field): } coerce_to_type = str - def __init__(self, *args, **kwargs): + def __init__(self, **kwargs): choices = kwargs.pop('choices') assert choices, '`choices` argument is required and may not be empty' @@ -257,7 +259,7 @@ class ChoiceField(Field): str(key): key for key in self.choices.keys() } - super(ChoiceField, self).__init__(*args, **kwargs) + super(ChoiceField, self).__init__(**kwargs) def to_native(self, data): try: @@ -296,6 +298,24 @@ class IntegerField(Field): return data +class EmailField(CharField): + pass # TODO + + +class RegexField(CharField): + def __init__(self, **kwargs): + self.regex = kwargs.pop('regex') + super(CharField, self).__init__(**kwargs) + + +class DateTimeField(CharField): + pass # TODO + + +class FileField(Field): + pass # TODO + + class MethodField(Field): def __init__(self, **kwargs): kwargs['source'] = '*' diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index ee01cabc7..3e9c9bb33 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -6,7 +6,6 @@ which allows mixin classes to be composed in interesting ways. """ from __future__ import unicode_literals -from django.core.exceptions import ValidationError from django.http import Http404 from rest_framework import status from rest_framework.response import Response diff --git a/rest_framework/relations.py b/rest_framework/relations.py index e69de29bb..42d2c121a 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -0,0 +1,111 @@ +from rest_framework.fields import Field +from django.core.exceptions import ObjectDoesNotExist +from django.core.urlresolvers import resolve, get_script_prefix +from rest_framework.compat import urlparse + + +def get_default_queryset(serializer_class, field_name): + manager = getattr(serializer_class.opts.model, field_name) + if hasattr(manager, 'related'): + # Forward relationships + return manager.related.model._default_manager.all() + # Reverse relationships + return manager.field.rel.to._default_manager.all() + + +class RelatedField(Field): + def __init__(self, **kwargs): + self.queryset = kwargs.pop('queryset', None) + self.many = kwargs.pop('many', False) + super(RelatedField, self).__init__(**kwargs) + + def bind(self, field_name, parent, root): + super(RelatedField, self).bind(field_name, parent, root) + if self.queryset is None and not self.read_only: + self.queryset = get_default_queryset(parent, self.source) + + +class PrimaryKeyRelatedField(RelatedField): + MESSAGES = { + 'required': 'This field is required.', + 'does_not_exist': "Invalid pk '{pk_value}' - object does not exist.", + 'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.', + } + + def from_native(self, data): + try: + return self.queryset.get(pk=data) + except ObjectDoesNotExist: + self.fail('does_not_exist', pk_value=data) + except (TypeError, ValueError): + self.fail('incorrect_type', data_type=type(data).__name__) + + +class HyperlinkedRelatedField(RelatedField): + lookup_field = 'pk' + + MESSAGES = { + 'required': 'This field is required.', + 'no_match': 'Invalid hyperlink - No URL match', + 'incorrect_match': 'Invalid hyperlink - Incorrect URL match.', + 'does_not_exist': "Invalid hyperlink - Object does not exist.", + 'incorrect_type': 'Incorrect type. Expected URL string, received {data_type}.', + } + + def __init__(self, **kwargs): + self.view_name = kwargs.pop('view_name') + self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) + self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) + super(HyperlinkedRelatedField, self).__init__(**kwargs) + + def get_object(self, view_name, view_args, view_kwargs): + """ + Return the object corresponding to a matched URL. + + Takes the matched URL conf arguments, and should return an + object instance, or raise an `ObjectDoesNotExist` exception. + """ + lookup_value = view_kwargs[self.lookup_url_kwarg] + lookup_kwargs = {self.lookup_field: lookup_value} + return self.queryset.get(**lookup_kwargs) + + def from_native(self, value): + try: + http_prefix = value.startswith(('http:', 'https:')) + except AttributeError: + self.fail('incorrect_type', type(value).__name__) + + if http_prefix: + # If needed convert absolute URLs to relative path + value = urlparse.urlparse(value).path + prefix = get_script_prefix() + if value.startswith(prefix): + value = '/' + value[len(prefix):] + + try: + match = resolve(value) + except Exception: + self.fail('no_match') + + if match.view_name != self.view_name: + self.fail('incorrect_match') + + try: + return self.get_object(match.view_name, match.args, match.kwargs) + except (ObjectDoesNotExist, TypeError, ValueError): + self.fail('does_not_exist') + + +class HyperlinkedIdentityField(RelatedField): + lookup_field = 'pk' + + def __init__(self, **kwargs): + self.view_name = kwargs.pop('view_name') + self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) + self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) + super(HyperlinkedIdentityField, self).__init__(**kwargs) + + +class SlugRelatedField(RelatedField): + def __init__(self, **kwargs): + self.slug_field = kwargs.pop('slug_field', None) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index e8935b012..dfc5a39f6 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -436,13 +436,13 @@ class BrowsableAPIRenderer(BaseRenderer): if request.method == method: try: data = request.DATA - files = request.FILES + # files = request.FILES except ParseError: data = None - files = None + # files = None else: data = None - files = None + # files = None with override_method(view, request, method) as request: obj = getattr(view, 'object', None) @@ -579,10 +579,10 @@ class BrowsableAPIRenderer(BaseRenderer): 'available_formats': [renderer_cls.format for renderer_cls in view.renderer_classes], 'response_headers': response_headers, - #'put_form': self.get_rendered_html_form(view, 'PUT', request), - #'post_form': self.get_rendered_html_form(view, 'POST', request), - #'delete_form': self.get_rendered_html_form(view, 'DELETE', request), - #'options_form': self.get_rendered_html_form(view, 'OPTIONS', request), + # 'put_form': self.get_rendered_html_form(view, 'PUT', request), + # 'post_form': self.get_rendered_html_form(view, 'POST', request), + # 'delete_form': self.get_rendered_html_form(view, 'DELETE', request), + # 'options_form': self.get_rendered_html_form(view, 'OPTIONS', request), 'raw_data_put_form': raw_data_put_form, 'raw_data_post_form': raw_data_post_form, diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index d121812d6..2f23b4d9a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -477,8 +477,8 @@ class ModelSerializer(Serializer): if model_field: kwargs['required'] = not(model_field.null or model_field.blank) - # if model_field.help_text is not None: - # kwargs['help_text'] = model_field.help_text + # if model_field.help_text is not None: + # kwargs['help_text'] = model_field.help_text if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name if not model_field.editable: @@ -566,8 +566,8 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions): class HyperlinkedModelSerializer(ModelSerializer): _options_class = HyperlinkedModelSerializerOptions _default_view_name = '%(model_name)s-detail' - #_hyperlink_field_class = HyperlinkedRelatedField - #_hyperlink_identify_field_class = HyperlinkedIdentityField + # _hyperlink_field_class = HyperlinkedRelatedField + # _hyperlink_identify_field_class = HyperlinkedIdentityField def get_default_fields(self): fields = super(HyperlinkedModelSerializer, self).get_default_fields() diff --git a/rest_framework/utils/html.py b/rest_framework/utils/html.py index bf17050df..edc591e9c 100644 --- a/rest_framework/utils/html.py +++ b/rest_framework/utils/html.py @@ -1,6 +1,8 @@ """ Helpers for dealing with HTML input. """ +import re + def is_html_input(dictionary): # MultiDict type datastructures are used to represent HTML form input, diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 90f37cf2e..fa5cafcfb 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -1795,8 +1795,8 @@ class DefaultValuesOnAutogeneratedFieldsTests(TestCase): class MetadataSerializer(serializers.Serializer): - field1 = serializers.CharField(3, required=True) - field2 = serializers.CharField(10, required=False) + field1 = serializers.CharField(max_length=3, required=True) + field2 = serializers.CharField(max_length=10, required=False) class MetadataSerializerTestCase(TestCase): From f2852811f93863f2eed04d51eeb7ef27716b2409 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Tue, 2 Sep 2014 17:41:23 +0100 Subject: [PATCH 023/100] Getting tests passing --- rest_framework/authtoken/serializers.py | 5 +- rest_framework/authtoken/views.py | 3 +- rest_framework/fields.py | 50 +- rest_framework/mixins.py | 41 +- rest_framework/pagination.py | 34 +- rest_framework/relations.py | 2 +- rest_framework/serializers.py | 53 +- tests/test_fields.py | 1920 +++++++++++------------ tests/test_files.py | 150 +- tests/test_genericrelations.py | 244 +-- tests/test_generics.py | 356 +++-- tests/test_hyperlinkedserializers.py | 724 ++++----- tests/test_nullable_fields.py | 54 +- tests/test_pagination.py | 8 +- tests/test_permissions.py | 96 +- tests/test_relations.py | 234 +-- tests/test_relations_hyperlink.py | 1050 ++++++------- tests/test_relations_nested.py | 548 +++---- tests/test_relations_pk.py | 1102 ++++++------- tests/test_relations_slug.py | 444 +++--- 20 files changed, 3565 insertions(+), 3553 deletions(-) diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index 99e99ae3d..edeae857b 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -19,11 +19,12 @@ class AuthTokenSerializer(serializers.Serializer): if not user.is_active: msg = _('User account is disabled.') raise serializers.ValidationError(msg) - attrs['user'] = user - return attrs else: msg = _('Unable to login with provided credentials.') raise serializers.ValidationError(msg) else: msg = _('Must include "username" and "password"') raise serializers.ValidationError(msg) + + attrs['user'] = user + return attrs diff --git a/rest_framework/authtoken/views.py b/rest_framework/authtoken/views.py index 7c03cb766..94e6f061f 100644 --- a/rest_framework/authtoken/views.py +++ b/rest_framework/authtoken/views.py @@ -18,7 +18,8 @@ class ObtainAuthToken(APIView): def post(self, request): serializer = self.serializer_class(data=request.DATA) if serializer.is_valid(): - token, created = Token.objects.get_or_create(user=serializer.object['user']) + user = serializer.validated_data['user'] + token, created = Token.objects.get_or_create(user=user) return Response({'token': token.key}) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 3e0f7ca47..838aa3b0c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,4 +1,5 @@ from rest_framework.utils import html +import inspect class empty: @@ -11,6 +12,22 @@ class empty: pass +def is_simple_callable(obj): + """ + True if the object is a callable that takes no arguments. + """ + function = inspect.isfunction(obj) + method = inspect.ismethod(obj) + + if not (function or method): + return False + + args, _, _, defaults = inspect.getargspec(obj) + len_args = len(args) if function else len(args) - 1 + len_defaults = len(defaults) if defaults else 0 + return len_args <= len_defaults + + def get_attribute(instance, attrs): """ Similar to Python's built in `getattr(instance, attr)`, @@ -98,6 +115,7 @@ class Field(object): self.field_name = field_name self.parent = parent self.root = root + self.context = parent.context # `self.label` should deafult to being based on the field name. if self.label is None: @@ -297,25 +315,55 @@ class IntegerField(Field): self.fail('invalid_integer') return data + def to_primative(self, value): + if value is None: + return None + return int(value) + class EmailField(CharField): pass # TODO +class URLField(CharField): + pass # TODO + + class RegexField(CharField): def __init__(self, **kwargs): self.regex = kwargs.pop('regex') super(CharField, self).__init__(**kwargs) +class DateField(CharField): + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(DateField, self).__init__(**kwargs) + + +class TimeField(CharField): + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(TimeField, self).__init__(**kwargs) + + class DateTimeField(CharField): - pass # TODO + def __init__(self, **kwargs): + self.input_formats = kwargs.pop('input_formats', None) + super(DateTimeField, self).__init__(**kwargs) class FileField(Field): pass # TODO +class ReadOnlyField(Field): + def to_primative(self, value): + if is_simple_callable(value): + return value() + return value + + class MethodField(Field): def __init__(self, **kwargs): kwargs['source'] = '*' diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 3e9c9bb33..359740cee 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -13,23 +13,6 @@ from rest_framework.request import clone_request from rest_framework.settings import api_settings -def _get_validation_exclusions(obj, lookup_field=None): - """ - Given a model instance, and an optional pk and slug field, - return the full list of all other field names on that model. - - For use when performing full_clean on a model instance, - so we only clean the required fields. - """ - if lookup_field == 'pk': - pk_field = obj._meta.pk - while pk_field.rel: - pk_field = pk_field.rel.to._meta.pk - lookup_field = pk_field.name - - return [field.name for field in obj._meta.fields if field.name != lookup_field] - - class CreateModelMixin(object): """ Create a model instance. @@ -92,15 +75,14 @@ class UpdateModelMixin(object): if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - lookup_value = self.kwargs[lookup_url_kwarg] - extras = {self.lookup_field: lookup_value} - if self.object is None: + lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field + lookup_value = self.kwargs[lookup_url_kwarg] + extras = {self.lookup_field: lookup_value} self.object = serializer.save(extras=extras) return Response(serializer.data, status=status.HTTP_201_CREATED) - self.object = serializer.save(extras=extras) + self.object = serializer.save() return Response(serializer.data, status=status.HTTP_200_OK) def partial_update(self, request, *args, **kwargs): @@ -122,21 +104,6 @@ class UpdateModelMixin(object): # return a 404 response. raise - def pre_save(self, obj): - """ - Set any attributes on the object that are implicit in the request. - """ - lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field - lookup_value = self.kwargs[lookup_url_kwarg] - - setattr(obj, self.lookup_field, lookup_value) - - # Ensure we clean the attributes so that we don't eg return integer - # pk using a string representation, as provided by the url conf kwarg. - if hasattr(obj, 'full_clean'): - exclude = _get_validation_exclusions(obj, self.lookup_field) - obj.full_clean(exclude) - class DestroyModelMixin(object): """ diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 83ef97c5c..478d32b49 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -13,7 +13,7 @@ class NextPageField(serializers.Field): """ page_field = 'page' - def to_native(self, value): + def to_primative(self, value): if not value.has_next(): return None page = value.next_page_number() @@ -28,7 +28,7 @@ class PreviousPageField(serializers.Field): """ page_field = 'page' - def to_native(self, value): + def to_primative(self, value): if not value.has_previous(): return None page = value.previous_page_number() @@ -48,25 +48,11 @@ class DefaultObjectSerializer(serializers.Field): super(DefaultObjectSerializer, self).__init__(source=source) -# class PaginationSerializerOptions(serializers.SerializerOptions): -# """ -# An object that stores the options that may be provided to a -# pagination serializer by using the inner `Meta` class. - -# Accessible on the instance as `serializer.opts`. -# """ -# def __init__(self, meta): -# super(PaginationSerializerOptions, self).__init__(meta) -# self.object_serializer_class = getattr(meta, 'object_serializer_class', -# DefaultObjectSerializer) - - class BasePaginationSerializer(serializers.Serializer): """ A base class for pagination serializers to inherit from, to make implementing custom serializers more easy. """ - # _options_class = PaginationSerializerOptions results_field = 'results' def __init__(self, *args, **kwargs): @@ -75,14 +61,16 @@ class BasePaginationSerializer(serializers.Serializer): """ super(BasePaginationSerializer, self).__init__(*args, **kwargs) results_field = self.results_field - object_serializer = self.opts.object_serializer_class + try: + object_serializer = self.Meta.object_serializer_class + except AttributeError: + object_serializer = DefaultObjectSerializer - if 'context' in kwargs: - context_kwarg = {'context': kwargs['context']} - else: - context_kwarg = {} - - self.fields[results_field] = object_serializer(source='object_list', **context_kwarg) + self.fields[results_field] = serializers.ListSerializer( + child=object_serializer(), + source='object_list' + ) + self.fields[results_field].bind(results_field, self, self) # TODO: Support automatic binding class PaginationSerializer(BasePaginationSerializer): diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 42d2c121a..0b01394a8 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -73,7 +73,7 @@ class HyperlinkedRelatedField(RelatedField): try: http_prefix = value.startswith(('http:', 'https:')) except AttributeError: - self.fail('incorrect_type', type(value).__name__) + self.fail('incorrect_type', data_type=type(value).__name__) if http_prefix: # If needed convert absolute URLs to relative path diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 2f23b4d9a..c38d89682 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -142,7 +142,7 @@ class Serializer(BaseSerializer): return super(Serializer, cls).__new__(cls) def __init__(self, *args, **kwargs): - kwargs.pop('context', None) + self.context = kwargs.pop('context', {}) kwargs.pop('partial', None) kwargs.pop('many', False) @@ -202,7 +202,7 @@ class Serializer(BaseSerializer): if errors: raise ValidationError(errors) - return ret + return self.validate(ret) def to_primative(self, instance): """ @@ -217,6 +217,9 @@ class Serializer(BaseSerializer): return ret + def validate(self, attrs): + return attrs + def __iter__(self): errors = self.errors if hasattr(self, '_errors') else {} for field in self.fields.values(): @@ -232,8 +235,7 @@ class ListSerializer(BaseSerializer): def __init__(self, *args, **kwargs): self.child = kwargs.pop('child', copy.deepcopy(self.child)) assert self.child is not None, '`child` is a required argument.' - - kwargs.pop('context', None) + self.context = kwargs.pop('context', {}) kwargs.pop('partial', None) super(ListSerializer, self).__init__(*args, **kwargs) @@ -316,19 +318,19 @@ class ModelSerializer(Serializer): models.PositiveIntegerField: IntegerField, models.SmallIntegerField: IntegerField, models.PositiveSmallIntegerField: IntegerField, - # models.DateTimeField: DateTimeField, - # models.DateField: DateField, - # models.TimeField: TimeField, + models.DateTimeField: DateTimeField, + models.DateField: DateField, + models.TimeField: TimeField, # models.DecimalField: DecimalField, - # models.EmailField: EmailField, + models.EmailField: EmailField, models.CharField: CharField, - # models.URLField: URLField, + models.URLField: URLField, # models.SlugField: SlugField, models.TextField: CharField, models.CommaSeparatedIntegerField: CharField, models.BooleanField: BooleanField, models.NullBooleanField: BooleanField, - # models.FileField: FileField, + models.FileField: FileField, # models.ImageField: ImageField, } @@ -338,6 +340,15 @@ class ModelSerializer(Serializer): self.opts = self._options_class(self.Meta) super(ModelSerializer, self).__init__(*args, **kwargs) + def create(self): + ModelClass = self.opts.model + return ModelClass.objects.create(**self.validated_data) + + def update(self, obj): + for attr, value in self.validated_data.items(): + setattr(obj, attr, value) + obj.save() + def get_fields(self): # Get the explicitly declared fields. fields = copy.deepcopy(self.base_fields) @@ -566,8 +577,8 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions): class HyperlinkedModelSerializer(ModelSerializer): _options_class = HyperlinkedModelSerializerOptions _default_view_name = '%(model_name)s-detail' - # _hyperlink_field_class = HyperlinkedRelatedField - # _hyperlink_identify_field_class = HyperlinkedIdentityField + _hyperlink_field_class = HyperlinkedRelatedField + _hyperlink_identify_field_class = HyperlinkedIdentityField def get_default_fields(self): fields = super(HyperlinkedModelSerializer, self).get_default_fields() @@ -575,15 +586,15 @@ class HyperlinkedModelSerializer(ModelSerializer): if self.opts.view_name is None: self.opts.view_name = self._get_default_view_name(self.opts.model) - # if self.opts.url_field_name not in fields: - # url_field = self._hyperlink_identify_field_class( - # view_name=self.opts.view_name, - # lookup_field=self.opts.lookup_field - # ) - # ret = self._dict_class() - # ret[self.opts.url_field_name] = url_field - # ret.update(fields) - # fields = ret + if self.opts.url_field_name not in fields: + url_field = self._hyperlink_identify_field_class( + view_name=self.opts.view_name, + lookup_field=self.opts.lookup_field + ) + ret = fields.__class__() + ret[self.opts.url_field_name] = url_field + ret.update(fields) + fields = ret return fields diff --git a/tests/test_fields.py b/tests/test_fields.py index 094ac1eb0..53c605382 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,1022 +1,1022 @@ -""" -General serializer field tests. -""" -from __future__ import unicode_literals +# """ +# General serializer field tests. +# """ +# from __future__ import unicode_literals -import datetime -import re -from decimal import Decimal -from uuid import uuid4 -from django.core import validators -from django.db import models -from django.test import TestCase -from django.utils.datastructures import SortedDict -from rest_framework import serializers -from tests.models import RESTFrameworkModel +# import datetime +# import re +# from decimal import Decimal +# from uuid import uuid4 +# from django.core import validators +# from django.db import models +# from django.test import TestCase +# from django.utils.datastructures import SortedDict +# from rest_framework import serializers +# from tests.models import RESTFrameworkModel -class TimestampedModel(models.Model): - added = models.DateTimeField(auto_now_add=True) - updated = models.DateTimeField(auto_now=True) +# class TimestampedModel(models.Model): +# added = models.DateTimeField(auto_now_add=True) +# updated = models.DateTimeField(auto_now=True) -class CharPrimaryKeyModel(models.Model): - id = models.CharField(max_length=20, primary_key=True) +# class CharPrimaryKeyModel(models.Model): +# id = models.CharField(max_length=20, primary_key=True) -class TimestampedModelSerializer(serializers.ModelSerializer): - class Meta: - model = TimestampedModel +# class TimestampedModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = TimestampedModel -class CharPrimaryKeyModelSerializer(serializers.ModelSerializer): - class Meta: - model = CharPrimaryKeyModel +# class CharPrimaryKeyModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = CharPrimaryKeyModel -class TimeFieldModel(models.Model): - clock = models.TimeField() +# class TimeFieldModel(models.Model): +# clock = models.TimeField() -class TimeFieldModelSerializer(serializers.ModelSerializer): - class Meta: - model = TimeFieldModel +# class TimeFieldModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = TimeFieldModel -SAMPLE_CHOICES = [ - ('red', 'Red'), - ('green', 'Green'), - ('blue', 'Blue'), -] +# SAMPLE_CHOICES = [ +# ('red', 'Red'), +# ('green', 'Green'), +# ('blue', 'Blue'), +# ] -class ChoiceFieldModel(models.Model): - choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, max_length=255) +# class ChoiceFieldModel(models.Model): +# choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, max_length=255) -class ChoiceFieldModelSerializer(serializers.ModelSerializer): - class Meta: - model = ChoiceFieldModel - - -class ChoiceFieldModelWithNull(models.Model): - choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, null=True, max_length=255) - - -class ChoiceFieldModelWithNullSerializer(serializers.ModelSerializer): - class Meta: - model = ChoiceFieldModelWithNull - - -class BasicFieldTests(TestCase): - def test_auto_now_fields_read_only(self): - """ - auto_now and auto_now_add fields should be read_only by default. - """ - serializer = TimestampedModelSerializer() - self.assertEqual(serializer.fields['added'].read_only, True) - - def test_auto_pk_fields_read_only(self): - """ - AutoField fields should be read_only by default. - """ - serializer = TimestampedModelSerializer() - self.assertEqual(serializer.fields['id'].read_only, True) - - def test_non_auto_pk_fields_not_read_only(self): - """ - PK fields other than AutoField fields should not be read_only by default. - """ - serializer = CharPrimaryKeyModelSerializer() - self.assertEqual(serializer.fields['id'].read_only, False) - - def test_dict_field_ordering(self): - """ - Field should preserve dictionary ordering, if it exists. - See: https://github.com/tomchristie/django-rest-framework/issues/832 - """ - ret = SortedDict() - ret['c'] = 1 - ret['b'] = 1 - ret['a'] = 1 - ret['z'] = 1 - field = serializers.Field() - keys = list(field.to_native(ret).keys()) - self.assertEqual(keys, ['c', 'b', 'a', 'z']) - - def test_widget_html_attributes(self): - """ - Make sure widget_html() renders the correct attributes - """ - r = re.compile('(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?') - form = TimeFieldModelSerializer().data - attributes = r.findall(form.fields['clock'].widget_html()) - self.assertIn(('name', 'clock'), attributes) - self.assertIn(('id', 'clock'), attributes) +# class ChoiceFieldModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = ChoiceFieldModel + + +# class ChoiceFieldModelWithNull(models.Model): +# choice = models.CharField(choices=SAMPLE_CHOICES, blank=True, null=True, max_length=255) + + +# class ChoiceFieldModelWithNullSerializer(serializers.ModelSerializer): +# class Meta: +# model = ChoiceFieldModelWithNull + + +# class BasicFieldTests(TestCase): +# def test_auto_now_fields_read_only(self): +# """ +# auto_now and auto_now_add fields should be read_only by default. +# """ +# serializer = TimestampedModelSerializer() +# self.assertEqual(serializer.fields['added'].read_only, True) + +# def test_auto_pk_fields_read_only(self): +# """ +# AutoField fields should be read_only by default. +# """ +# serializer = TimestampedModelSerializer() +# self.assertEqual(serializer.fields['id'].read_only, True) + +# def test_non_auto_pk_fields_not_read_only(self): +# """ +# PK fields other than AutoField fields should not be read_only by default. +# """ +# serializer = CharPrimaryKeyModelSerializer() +# self.assertEqual(serializer.fields['id'].read_only, False) + +# def test_dict_field_ordering(self): +# """ +# Field should preserve dictionary ordering, if it exists. +# See: https://github.com/tomchristie/django-rest-framework/issues/832 +# """ +# ret = SortedDict() +# ret['c'] = 1 +# ret['b'] = 1 +# ret['a'] = 1 +# ret['z'] = 1 +# field = serializers.Field() +# keys = list(field.to_native(ret).keys()) +# self.assertEqual(keys, ['c', 'b', 'a', 'z']) + +# def test_widget_html_attributes(self): +# """ +# Make sure widget_html() renders the correct attributes +# """ +# r = re.compile('(\S+)=["\']?((?:.(?!["\']?\s+(?:\S+)=|[>"\']))+.)["\']?') +# form = TimeFieldModelSerializer().data +# attributes = r.findall(form.fields['clock'].widget_html()) +# self.assertIn(('name', 'clock'), attributes) +# self.assertIn(('id', 'clock'), attributes) -class DateFieldTest(TestCase): - """ - Tests for the DateFieldTest from_native() and to_native() behavior - """ - - def test_from_native_string(self): - """ - Make sure from_native() accepts default iso input formats. - """ - f = serializers.DateField() - result_1 = f.from_native('1984-07-31') - - self.assertEqual(datetime.date(1984, 7, 31), result_1) - - def test_from_native_datetime_date(self): - """ - Make sure from_native() accepts a datetime.date instance. - """ - f = serializers.DateField() - result_1 = f.from_native(datetime.date(1984, 7, 31)) - - self.assertEqual(result_1, datetime.date(1984, 7, 31)) - - def test_from_native_custom_format(self): - """ - Make sure from_native() accepts custom input formats. - """ - f = serializers.DateField(input_formats=['%Y -- %d']) - result = f.from_native('1984 -- 31') - - self.assertEqual(datetime.date(1984, 1, 31), result) - - def test_from_native_invalid_default_on_custom_format(self): - """ - Make sure from_native() don't accept default formats if custom format is preset - """ - f = serializers.DateField(input_formats=['%Y -- %d']) - - try: - f.from_native('1984-07-31') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY -- DD"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_empty(self): - """ - Make sure from_native() returns None on empty param. - """ - f = serializers.DateField() - result = f.from_native('') - - self.assertEqual(result, None) - - def test_from_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DateField() - result = f.from_native(None) - - self.assertEqual(result, None) - - def test_from_native_invalid_date(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid date. - """ - f = serializers.DateField() - - try: - f.from_native('1984-13-31') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_invalid_format(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid format. - """ - f = serializers.DateField() - - try: - f.from_native('1984 -- 31') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_to_native(self): - """ - Make sure to_native() returns datetime as default. - """ - f = serializers.DateField() - - result_1 = f.to_native(datetime.date(1984, 7, 31)) - - self.assertEqual(datetime.date(1984, 7, 31), result_1) - - def test_to_native_iso(self): - """ - Make sure to_native() with 'iso-8601' returns iso formated date. - """ - f = serializers.DateField(format='iso-8601') - - result_1 = f.to_native(datetime.date(1984, 7, 31)) - - self.assertEqual('1984-07-31', result_1) - - def test_to_native_custom_format(self): - """ - Make sure to_native() returns correct custom format. - """ - f = serializers.DateField(format="%Y - %m.%d") - - result_1 = f.to_native(datetime.date(1984, 7, 31)) - - self.assertEqual('1984 - 07.31', result_1) - - def test_to_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DateField(required=False) - self.assertEqual(None, f.to_native(None)) - - -class DateTimeFieldTest(TestCase): - """ - Tests for the DateTimeField from_native() and to_native() behavior - """ - - def test_from_native_string(self): - """ - Make sure from_native() accepts default iso input formats. - """ - f = serializers.DateTimeField() - result_1 = f.from_native('1984-07-31 04:31') - result_2 = f.from_native('1984-07-31 04:31:59') - result_3 = f.from_native('1984-07-31 04:31:59.000200') - - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_1) - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_2) - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_3) - - def test_from_native_datetime_datetime(self): - """ - Make sure from_native() accepts a datetime.datetime instance. - """ - f = serializers.DateTimeField() - result_1 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31)) - result_2 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) - result_3 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) - - self.assertEqual(result_1, datetime.datetime(1984, 7, 31, 4, 31)) - self.assertEqual(result_2, datetime.datetime(1984, 7, 31, 4, 31, 59)) - self.assertEqual(result_3, datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) - - def test_from_native_custom_format(self): - """ - Make sure from_native() accepts custom input formats. - """ - f = serializers.DateTimeField(input_formats=['%Y -- %H:%M']) - result = f.from_native('1984 -- 04:59') - - self.assertEqual(datetime.datetime(1984, 1, 1, 4, 59), result) - - def test_from_native_invalid_default_on_custom_format(self): - """ - Make sure from_native() don't accept default formats if custom format is preset - """ - f = serializers.DateTimeField(input_formats=['%Y -- %H:%M']) - - try: - f.from_native('1984-07-31 04:31:59') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_empty(self): - """ - Make sure from_native() returns None on empty param. - """ - f = serializers.DateTimeField() - result = f.from_native('') - - self.assertEqual(result, None) - - def test_from_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DateTimeField() - result = f.from_native(None) - - self.assertEqual(result, None) - - def test_from_native_invalid_datetime(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid datetime. - """ - f = serializers.DateTimeField() - - try: - f.from_native('04:61:59') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " - "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_invalid_format(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid format. - """ - f = serializers.DateTimeField() - - try: - f.from_native('04 -- 31') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " - "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_to_native(self): - """ - Make sure to_native() returns isoformat as default. - """ - f = serializers.DateTimeField() - - result_1 = f.to_native(datetime.datetime(1984, 7, 31)) - result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) - result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) - result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) - - self.assertEqual(datetime.datetime(1984, 7, 31), result_1) - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_2) - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_3) - self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_4) - - def test_to_native_iso(self): - """ - Make sure to_native() with format=iso-8601 returns iso formatted datetime. - """ - f = serializers.DateTimeField(format='iso-8601') - - result_1 = f.to_native(datetime.datetime(1984, 7, 31)) - result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) - result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) - result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) - - self.assertEqual('1984-07-31T00:00:00', result_1) - self.assertEqual('1984-07-31T04:31:00', result_2) - self.assertEqual('1984-07-31T04:31:59', result_3) - self.assertEqual('1984-07-31T04:31:59.000200', result_4) - - def test_to_native_custom_format(self): - """ - Make sure to_native() returns correct custom format. - """ - f = serializers.DateTimeField(format="%Y - %H:%M") - - result_1 = f.to_native(datetime.datetime(1984, 7, 31)) - result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) - result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) - result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) - - self.assertEqual('1984 - 00:00', result_1) - self.assertEqual('1984 - 04:31', result_2) - self.assertEqual('1984 - 04:31', result_3) - self.assertEqual('1984 - 04:31', result_4) - - def test_to_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DateTimeField(required=False) - self.assertEqual(None, f.to_native(None)) - - -class TimeFieldTest(TestCase): - """ - Tests for the TimeField from_native() and to_native() behavior - """ - - def test_from_native_string(self): - """ - Make sure from_native() accepts default iso input formats. - """ - f = serializers.TimeField() - result_1 = f.from_native('04:31') - result_2 = f.from_native('04:31:59') - result_3 = f.from_native('04:31:59.000200') - - self.assertEqual(datetime.time(4, 31), result_1) - self.assertEqual(datetime.time(4, 31, 59), result_2) - self.assertEqual(datetime.time(4, 31, 59, 200), result_3) - - def test_from_native_datetime_time(self): - """ - Make sure from_native() accepts a datetime.time instance. - """ - f = serializers.TimeField() - result_1 = f.from_native(datetime.time(4, 31)) - result_2 = f.from_native(datetime.time(4, 31, 59)) - result_3 = f.from_native(datetime.time(4, 31, 59, 200)) - - self.assertEqual(result_1, datetime.time(4, 31)) - self.assertEqual(result_2, datetime.time(4, 31, 59)) - self.assertEqual(result_3, datetime.time(4, 31, 59, 200)) - - def test_from_native_custom_format(self): - """ - Make sure from_native() accepts custom input formats. - """ - f = serializers.TimeField(input_formats=['%H -- %M']) - result = f.from_native('04 -- 31') - - self.assertEqual(datetime.time(4, 31), result) - - def test_from_native_invalid_default_on_custom_format(self): - """ - Make sure from_native() don't accept default formats if custom format is preset - """ - f = serializers.TimeField(input_formats=['%H -- %M']) - - try: - f.from_native('04:31:59') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_empty(self): - """ - Make sure from_native() returns None on empty param. - """ - f = serializers.TimeField() - result = f.from_native('') - - self.assertEqual(result, None) - - def test_from_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.TimeField() - result = f.from_native(None) - - self.assertEqual(result, None) - - def test_from_native_invalid_time(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid time. - """ - f = serializers.TimeField() - - try: - f.from_native('04:61:59') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " - "hh:mm[:ss[.uuuuuu]]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_invalid_format(self): - """ - Make sure from_native() raises a ValidationError on passing an invalid format. - """ - f = serializers.TimeField() - - try: - f.from_native('04 -- 31') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " - "hh:mm[:ss[.uuuuuu]]"]) - else: - self.fail("ValidationError was not properly raised") - - def test_to_native(self): - """ - Make sure to_native() returns time object as default. - """ - f = serializers.TimeField() - result_1 = f.to_native(datetime.time(4, 31)) - result_2 = f.to_native(datetime.time(4, 31, 59)) - result_3 = f.to_native(datetime.time(4, 31, 59, 200)) - - self.assertEqual(datetime.time(4, 31), result_1) - self.assertEqual(datetime.time(4, 31, 59), result_2) - self.assertEqual(datetime.time(4, 31, 59, 200), result_3) - - def test_to_native_iso(self): - """ - Make sure to_native() with format='iso-8601' returns iso formatted time. - """ - f = serializers.TimeField(format='iso-8601') - result_1 = f.to_native(datetime.time(4, 31)) - result_2 = f.to_native(datetime.time(4, 31, 59)) - result_3 = f.to_native(datetime.time(4, 31, 59, 200)) - - self.assertEqual('04:31:00', result_1) - self.assertEqual('04:31:59', result_2) - self.assertEqual('04:31:59.000200', result_3) - - def test_to_native_custom_format(self): - """ - Make sure to_native() returns correct custom format. - """ - f = serializers.TimeField(format="%H - %S [%f]") - result_1 = f.to_native(datetime.time(4, 31)) - result_2 = f.to_native(datetime.time(4, 31, 59)) - result_3 = f.to_native(datetime.time(4, 31, 59, 200)) - - self.assertEqual('04 - 00 [000000]', result_1) - self.assertEqual('04 - 59 [000000]', result_2) - self.assertEqual('04 - 59 [000200]', result_3) - - -class DecimalFieldTest(TestCase): - """ - Tests for the DecimalField from_native() and to_native() behavior - """ - - def test_from_native_string(self): - """ - Make sure from_native() accepts string values - """ - f = serializers.DecimalField() - result_1 = f.from_native('9000') - result_2 = f.from_native('1.00000001') - - self.assertEqual(Decimal('9000'), result_1) - self.assertEqual(Decimal('1.00000001'), result_2) - - def test_from_native_invalid_string(self): - """ - Make sure from_native() raises ValidationError on passing invalid string - """ - f = serializers.DecimalField() - - try: - f.from_native('123.45.6') - except validators.ValidationError as e: - self.assertEqual(e.messages, ["Enter a number."]) - else: - self.fail("ValidationError was not properly raised") - - def test_from_native_integer(self): - """ - Make sure from_native() accepts integer values - """ - f = serializers.DecimalField() - result = f.from_native(9000) - - self.assertEqual(Decimal('9000'), result) - - def test_from_native_float(self): - """ - Make sure from_native() accepts float values - """ - f = serializers.DecimalField() - result = f.from_native(1.00000001) - - self.assertEqual(Decimal('1.00000001'), result) - - def test_from_native_empty(self): - """ - Make sure from_native() returns None on empty param. - """ - f = serializers.DecimalField() - result = f.from_native('') - - self.assertEqual(result, None) - - def test_from_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DecimalField() - result = f.from_native(None) - - self.assertEqual(result, None) - - def test_to_native(self): - """ - Make sure to_native() returns Decimal as string. - """ - f = serializers.DecimalField() - - result_1 = f.to_native(Decimal('9000')) - result_2 = f.to_native(Decimal('1.00000001')) - - self.assertEqual(Decimal('9000'), result_1) - self.assertEqual(Decimal('1.00000001'), result_2) - - def test_to_native_none(self): - """ - Make sure from_native() returns None on None param. - """ - f = serializers.DecimalField(required=False) - self.assertEqual(None, f.to_native(None)) - - def test_valid_serialization(self): - """ - Make sure the serializer works correctly - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(max_value=9010, - min_value=9000, - max_digits=6, - decimal_places=2) - - self.assertTrue(DecimalSerializer(data={'decimal_field': '9001'}).is_valid()) - self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.2'}).is_valid()) - self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.23'}).is_valid()) - - self.assertFalse(DecimalSerializer(data={'decimal_field': '8000'}).is_valid()) - self.assertFalse(DecimalSerializer(data={'decimal_field': '9900'}).is_valid()) - self.assertFalse(DecimalSerializer(data={'decimal_field': '9001.234'}).is_valid()) - - def test_raise_max_value(self): - """ - Make sure max_value violations raises ValidationError - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(max_value=100) - - s = DecimalSerializer(data={'decimal_field': '123'}) - - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is less than or equal to 100.']}) - - def test_raise_min_value(self): - """ - Make sure min_value violations raises ValidationError - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(min_value=100) - - s = DecimalSerializer(data={'decimal_field': '99'}) - - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is greater than or equal to 100.']}) - - def test_raise_max_digits(self): - """ - Make sure max_digits violations raises ValidationError - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(max_digits=5) - - s = DecimalSerializer(data={'decimal_field': '123.456'}) - - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 5 digits in total.']}) - - def test_raise_max_decimal_places(self): - """ - Make sure max_decimal_places violations raises ValidationError - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(decimal_places=3) - - s = DecimalSerializer(data={'decimal_field': '123.4567'}) - - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 3 decimal places.']}) - - def test_raise_max_whole_digits(self): - """ - Make sure max_whole_digits violations raises ValidationError - """ - class DecimalSerializer(serializers.Serializer): - decimal_field = serializers.DecimalField(max_digits=4, decimal_places=3) - - s = DecimalSerializer(data={'decimal_field': '12345.6'}) - - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 4 digits in total.']}) - - -class ChoiceFieldTests(TestCase): - """ - Tests for the ChoiceField options generator - """ - def test_choices_required(self): - """ - Make sure proper choices are rendered if field is required - """ - f = serializers.ChoiceField(required=True, choices=SAMPLE_CHOICES) - self.assertEqual(f.choices, SAMPLE_CHOICES) - - def test_choices_not_required(self): - """ - Make sure proper choices (plus blank) are rendered if the field isn't required - """ - f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES) - self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES) - - def test_blank_choice_display(self): - blank = 'No Preference' - f = serializers.ChoiceField( - required=False, - choices=SAMPLE_CHOICES, - blank_display_value=blank, - ) - self.assertEqual(f.choices, [('', blank)] + SAMPLE_CHOICES) - - def test_invalid_choice_model(self): - s = ChoiceFieldModelSerializer(data={'choice': 'wrong_value'}) - self.assertFalse(s.is_valid()) - self.assertEqual(s.errors, {'choice': ['Select a valid choice. wrong_value is not one of the available choices.']}) - self.assertEqual(s.data['choice'], '') - - def test_empty_choice_model(self): - """ - Test that the 'empty' value is correctly passed and used depending on - the 'null' property on the model field. - """ - s = ChoiceFieldModelSerializer(data={'choice': ''}) - self.assertTrue(s.is_valid()) - self.assertEqual(s.data['choice'], '') - - s = ChoiceFieldModelWithNullSerializer(data={'choice': ''}) - self.assertTrue(s.is_valid()) - self.assertEqual(s.data['choice'], None) - - def test_from_native_empty(self): - """ - Make sure from_native() returns an empty string on empty param by default. - """ - f = serializers.ChoiceField(choices=SAMPLE_CHOICES) - self.assertEqual(f.from_native(''), '') - self.assertEqual(f.from_native(None), '') - - def test_from_native_empty_override(self): - """ - Make sure you can override from_native() behavior regarding empty values. - """ - f = serializers.ChoiceField(choices=SAMPLE_CHOICES, empty=None) - self.assertEqual(f.from_native(''), None) - self.assertEqual(f.from_native(None), None) - - def test_metadata_choices(self): - """ - Make sure proper choices are included in the field's metadata. - """ - choices = [{'value': v, 'display_name': n} for v, n in SAMPLE_CHOICES] - f = serializers.ChoiceField(choices=SAMPLE_CHOICES) - self.assertEqual(f.metadata()['choices'], choices) - - def test_metadata_choices_not_required(self): - """ - Make sure proper choices are included in the field's metadata. - """ - choices = [{'value': v, 'display_name': n} - for v, n in models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES] - f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES) - self.assertEqual(f.metadata()['choices'], choices) - - -class EmailFieldTests(TestCase): - """ - Tests for EmailField attribute values - """ - - class EmailFieldModel(RESTFrameworkModel): - email_field = models.EmailField(blank=True) - - class EmailFieldWithGivenMaxLengthModel(RESTFrameworkModel): - email_field = models.EmailField(max_length=150, blank=True) - - def test_default_model_value(self): - class EmailFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.EmailFieldModel - - serializer = EmailFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 75) - - def test_given_model_value(self): - class EmailFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.EmailFieldWithGivenMaxLengthModel - - serializer = EmailFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 150) - - def test_given_serializer_value(self): - class EmailFieldSerializer(serializers.ModelSerializer): - email_field = serializers.EmailField(source='email_field', max_length=20, required=False) - - class Meta: - model = self.EmailFieldModel - - serializer = EmailFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 20) - - -class SlugFieldTests(TestCase): - """ - Tests for SlugField attribute values - """ - - class SlugFieldModel(RESTFrameworkModel): - slug_field = models.SlugField(blank=True) - - class SlugFieldWithGivenMaxLengthModel(RESTFrameworkModel): - slug_field = models.SlugField(max_length=84, blank=True) - - def test_default_model_value(self): - class SlugFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.SlugFieldModel - - serializer = SlugFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50) - - def test_given_model_value(self): - class SlugFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.SlugFieldWithGivenMaxLengthModel - - serializer = SlugFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84) - - def test_given_serializer_value(self): - class SlugFieldSerializer(serializers.ModelSerializer): - slug_field = serializers.SlugField(source='slug_field', - max_length=20, required=False) - - class Meta: - model = self.SlugFieldModel - - serializer = SlugFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['slug_field'], - 'max_length'), 20) - - def test_invalid_slug(self): - """ - Make sure an invalid slug raises ValidationError - """ - class SlugFieldSerializer(serializers.ModelSerializer): - slug_field = serializers.SlugField(source='slug_field', max_length=20, required=True) +# class DateFieldTest(TestCase): +# """ +# Tests for the DateFieldTest from_native() and to_native() behavior +# """ + +# def test_from_native_string(self): +# """ +# Make sure from_native() accepts default iso input formats. +# """ +# f = serializers.DateField() +# result_1 = f.from_native('1984-07-31') + +# self.assertEqual(datetime.date(1984, 7, 31), result_1) + +# def test_from_native_datetime_date(self): +# """ +# Make sure from_native() accepts a datetime.date instance. +# """ +# f = serializers.DateField() +# result_1 = f.from_native(datetime.date(1984, 7, 31)) + +# self.assertEqual(result_1, datetime.date(1984, 7, 31)) + +# def test_from_native_custom_format(self): +# """ +# Make sure from_native() accepts custom input formats. +# """ +# f = serializers.DateField(input_formats=['%Y -- %d']) +# result = f.from_native('1984 -- 31') + +# self.assertEqual(datetime.date(1984, 1, 31), result) + +# def test_from_native_invalid_default_on_custom_format(self): +# """ +# Make sure from_native() don't accept default formats if custom format is preset +# """ +# f = serializers.DateField(input_formats=['%Y -- %d']) + +# try: +# f.from_native('1984-07-31') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY -- DD"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_empty(self): +# """ +# Make sure from_native() returns None on empty param. +# """ +# f = serializers.DateField() +# result = f.from_native('') + +# self.assertEqual(result, None) + +# def test_from_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DateField() +# result = f.from_native(None) + +# self.assertEqual(result, None) + +# def test_from_native_invalid_date(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid date. +# """ +# f = serializers.DateField() + +# try: +# f.from_native('1984-13-31') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_invalid_format(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid format. +# """ +# f = serializers.DateField() + +# try: +# f.from_native('1984 -- 31') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Date has wrong format. Use one of these formats instead: YYYY[-MM[-DD]]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_to_native(self): +# """ +# Make sure to_native() returns datetime as default. +# """ +# f = serializers.DateField() + +# result_1 = f.to_native(datetime.date(1984, 7, 31)) + +# self.assertEqual(datetime.date(1984, 7, 31), result_1) + +# def test_to_native_iso(self): +# """ +# Make sure to_native() with 'iso-8601' returns iso formated date. +# """ +# f = serializers.DateField(format='iso-8601') + +# result_1 = f.to_native(datetime.date(1984, 7, 31)) + +# self.assertEqual('1984-07-31', result_1) + +# def test_to_native_custom_format(self): +# """ +# Make sure to_native() returns correct custom format. +# """ +# f = serializers.DateField(format="%Y - %m.%d") + +# result_1 = f.to_native(datetime.date(1984, 7, 31)) + +# self.assertEqual('1984 - 07.31', result_1) + +# def test_to_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DateField(required=False) +# self.assertEqual(None, f.to_native(None)) + + +# class DateTimeFieldTest(TestCase): +# """ +# Tests for the DateTimeField from_native() and to_native() behavior +# """ + +# def test_from_native_string(self): +# """ +# Make sure from_native() accepts default iso input formats. +# """ +# f = serializers.DateTimeField() +# result_1 = f.from_native('1984-07-31 04:31') +# result_2 = f.from_native('1984-07-31 04:31:59') +# result_3 = f.from_native('1984-07-31 04:31:59.000200') + +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_1) +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_2) +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_3) + +# def test_from_native_datetime_datetime(self): +# """ +# Make sure from_native() accepts a datetime.datetime instance. +# """ +# f = serializers.DateTimeField() +# result_1 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31)) +# result_2 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) +# result_3 = f.from_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) + +# self.assertEqual(result_1, datetime.datetime(1984, 7, 31, 4, 31)) +# self.assertEqual(result_2, datetime.datetime(1984, 7, 31, 4, 31, 59)) +# self.assertEqual(result_3, datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) + +# def test_from_native_custom_format(self): +# """ +# Make sure from_native() accepts custom input formats. +# """ +# f = serializers.DateTimeField(input_formats=['%Y -- %H:%M']) +# result = f.from_native('1984 -- 04:59') + +# self.assertEqual(datetime.datetime(1984, 1, 1, 4, 59), result) + +# def test_from_native_invalid_default_on_custom_format(self): +# """ +# Make sure from_native() don't accept default formats if custom format is preset +# """ +# f = serializers.DateTimeField(input_formats=['%Y -- %H:%M']) + +# try: +# f.from_native('1984-07-31 04:31:59') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: YYYY -- hh:mm"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_empty(self): +# """ +# Make sure from_native() returns None on empty param. +# """ +# f = serializers.DateTimeField() +# result = f.from_native('') + +# self.assertEqual(result, None) + +# def test_from_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DateTimeField() +# result = f.from_native(None) + +# self.assertEqual(result, None) + +# def test_from_native_invalid_datetime(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid datetime. +# """ +# f = serializers.DateTimeField() + +# try: +# f.from_native('04:61:59') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " +# "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_invalid_format(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid format. +# """ +# f = serializers.DateTimeField() + +# try: +# f.from_native('04 -- 31') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Datetime has wrong format. Use one of these formats instead: " +# "YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_to_native(self): +# """ +# Make sure to_native() returns isoformat as default. +# """ +# f = serializers.DateTimeField() + +# result_1 = f.to_native(datetime.datetime(1984, 7, 31)) +# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) +# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) +# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) + +# self.assertEqual(datetime.datetime(1984, 7, 31), result_1) +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31), result_2) +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59), result_3) +# self.assertEqual(datetime.datetime(1984, 7, 31, 4, 31, 59, 200), result_4) + +# def test_to_native_iso(self): +# """ +# Make sure to_native() with format=iso-8601 returns iso formatted datetime. +# """ +# f = serializers.DateTimeField(format='iso-8601') + +# result_1 = f.to_native(datetime.datetime(1984, 7, 31)) +# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) +# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) +# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) + +# self.assertEqual('1984-07-31T00:00:00', result_1) +# self.assertEqual('1984-07-31T04:31:00', result_2) +# self.assertEqual('1984-07-31T04:31:59', result_3) +# self.assertEqual('1984-07-31T04:31:59.000200', result_4) + +# def test_to_native_custom_format(self): +# """ +# Make sure to_native() returns correct custom format. +# """ +# f = serializers.DateTimeField(format="%Y - %H:%M") + +# result_1 = f.to_native(datetime.datetime(1984, 7, 31)) +# result_2 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31)) +# result_3 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59)) +# result_4 = f.to_native(datetime.datetime(1984, 7, 31, 4, 31, 59, 200)) + +# self.assertEqual('1984 - 00:00', result_1) +# self.assertEqual('1984 - 04:31', result_2) +# self.assertEqual('1984 - 04:31', result_3) +# self.assertEqual('1984 - 04:31', result_4) + +# def test_to_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DateTimeField(required=False) +# self.assertEqual(None, f.to_native(None)) + + +# class TimeFieldTest(TestCase): +# """ +# Tests for the TimeField from_native() and to_native() behavior +# """ + +# def test_from_native_string(self): +# """ +# Make sure from_native() accepts default iso input formats. +# """ +# f = serializers.TimeField() +# result_1 = f.from_native('04:31') +# result_2 = f.from_native('04:31:59') +# result_3 = f.from_native('04:31:59.000200') + +# self.assertEqual(datetime.time(4, 31), result_1) +# self.assertEqual(datetime.time(4, 31, 59), result_2) +# self.assertEqual(datetime.time(4, 31, 59, 200), result_3) + +# def test_from_native_datetime_time(self): +# """ +# Make sure from_native() accepts a datetime.time instance. +# """ +# f = serializers.TimeField() +# result_1 = f.from_native(datetime.time(4, 31)) +# result_2 = f.from_native(datetime.time(4, 31, 59)) +# result_3 = f.from_native(datetime.time(4, 31, 59, 200)) + +# self.assertEqual(result_1, datetime.time(4, 31)) +# self.assertEqual(result_2, datetime.time(4, 31, 59)) +# self.assertEqual(result_3, datetime.time(4, 31, 59, 200)) + +# def test_from_native_custom_format(self): +# """ +# Make sure from_native() accepts custom input formats. +# """ +# f = serializers.TimeField(input_formats=['%H -- %M']) +# result = f.from_native('04 -- 31') + +# self.assertEqual(datetime.time(4, 31), result) + +# def test_from_native_invalid_default_on_custom_format(self): +# """ +# Make sure from_native() don't accept default formats if custom format is preset +# """ +# f = serializers.TimeField(input_formats=['%H -- %M']) + +# try: +# f.from_native('04:31:59') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: hh -- mm"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_empty(self): +# """ +# Make sure from_native() returns None on empty param. +# """ +# f = serializers.TimeField() +# result = f.from_native('') + +# self.assertEqual(result, None) + +# def test_from_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.TimeField() +# result = f.from_native(None) + +# self.assertEqual(result, None) + +# def test_from_native_invalid_time(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid time. +# """ +# f = serializers.TimeField() + +# try: +# f.from_native('04:61:59') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " +# "hh:mm[:ss[.uuuuuu]]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_invalid_format(self): +# """ +# Make sure from_native() raises a ValidationError on passing an invalid format. +# """ +# f = serializers.TimeField() + +# try: +# f.from_native('04 -- 31') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Time has wrong format. Use one of these formats instead: " +# "hh:mm[:ss[.uuuuuu]]"]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_to_native(self): +# """ +# Make sure to_native() returns time object as default. +# """ +# f = serializers.TimeField() +# result_1 = f.to_native(datetime.time(4, 31)) +# result_2 = f.to_native(datetime.time(4, 31, 59)) +# result_3 = f.to_native(datetime.time(4, 31, 59, 200)) + +# self.assertEqual(datetime.time(4, 31), result_1) +# self.assertEqual(datetime.time(4, 31, 59), result_2) +# self.assertEqual(datetime.time(4, 31, 59, 200), result_3) + +# def test_to_native_iso(self): +# """ +# Make sure to_native() with format='iso-8601' returns iso formatted time. +# """ +# f = serializers.TimeField(format='iso-8601') +# result_1 = f.to_native(datetime.time(4, 31)) +# result_2 = f.to_native(datetime.time(4, 31, 59)) +# result_3 = f.to_native(datetime.time(4, 31, 59, 200)) + +# self.assertEqual('04:31:00', result_1) +# self.assertEqual('04:31:59', result_2) +# self.assertEqual('04:31:59.000200', result_3) + +# def test_to_native_custom_format(self): +# """ +# Make sure to_native() returns correct custom format. +# """ +# f = serializers.TimeField(format="%H - %S [%f]") +# result_1 = f.to_native(datetime.time(4, 31)) +# result_2 = f.to_native(datetime.time(4, 31, 59)) +# result_3 = f.to_native(datetime.time(4, 31, 59, 200)) + +# self.assertEqual('04 - 00 [000000]', result_1) +# self.assertEqual('04 - 59 [000000]', result_2) +# self.assertEqual('04 - 59 [000200]', result_3) + + +# class DecimalFieldTest(TestCase): +# """ +# Tests for the DecimalField from_native() and to_native() behavior +# """ + +# def test_from_native_string(self): +# """ +# Make sure from_native() accepts string values +# """ +# f = serializers.DecimalField() +# result_1 = f.from_native('9000') +# result_2 = f.from_native('1.00000001') + +# self.assertEqual(Decimal('9000'), result_1) +# self.assertEqual(Decimal('1.00000001'), result_2) + +# def test_from_native_invalid_string(self): +# """ +# Make sure from_native() raises ValidationError on passing invalid string +# """ +# f = serializers.DecimalField() + +# try: +# f.from_native('123.45.6') +# except validators.ValidationError as e: +# self.assertEqual(e.messages, ["Enter a number."]) +# else: +# self.fail("ValidationError was not properly raised") + +# def test_from_native_integer(self): +# """ +# Make sure from_native() accepts integer values +# """ +# f = serializers.DecimalField() +# result = f.from_native(9000) + +# self.assertEqual(Decimal('9000'), result) + +# def test_from_native_float(self): +# """ +# Make sure from_native() accepts float values +# """ +# f = serializers.DecimalField() +# result = f.from_native(1.00000001) + +# self.assertEqual(Decimal('1.00000001'), result) + +# def test_from_native_empty(self): +# """ +# Make sure from_native() returns None on empty param. +# """ +# f = serializers.DecimalField() +# result = f.from_native('') + +# self.assertEqual(result, None) + +# def test_from_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DecimalField() +# result = f.from_native(None) + +# self.assertEqual(result, None) + +# def test_to_native(self): +# """ +# Make sure to_native() returns Decimal as string. +# """ +# f = serializers.DecimalField() + +# result_1 = f.to_native(Decimal('9000')) +# result_2 = f.to_native(Decimal('1.00000001')) + +# self.assertEqual(Decimal('9000'), result_1) +# self.assertEqual(Decimal('1.00000001'), result_2) + +# def test_to_native_none(self): +# """ +# Make sure from_native() returns None on None param. +# """ +# f = serializers.DecimalField(required=False) +# self.assertEqual(None, f.to_native(None)) + +# def test_valid_serialization(self): +# """ +# Make sure the serializer works correctly +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(max_value=9010, +# min_value=9000, +# max_digits=6, +# decimal_places=2) + +# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001'}).is_valid()) +# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.2'}).is_valid()) +# self.assertTrue(DecimalSerializer(data={'decimal_field': '9001.23'}).is_valid()) + +# self.assertFalse(DecimalSerializer(data={'decimal_field': '8000'}).is_valid()) +# self.assertFalse(DecimalSerializer(data={'decimal_field': '9900'}).is_valid()) +# self.assertFalse(DecimalSerializer(data={'decimal_field': '9001.234'}).is_valid()) + +# def test_raise_max_value(self): +# """ +# Make sure max_value violations raises ValidationError +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(max_value=100) + +# s = DecimalSerializer(data={'decimal_field': '123'}) + +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is less than or equal to 100.']}) + +# def test_raise_min_value(self): +# """ +# Make sure min_value violations raises ValidationError +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(min_value=100) + +# s = DecimalSerializer(data={'decimal_field': '99'}) + +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'decimal_field': ['Ensure this value is greater than or equal to 100.']}) + +# def test_raise_max_digits(self): +# """ +# Make sure max_digits violations raises ValidationError +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(max_digits=5) + +# s = DecimalSerializer(data={'decimal_field': '123.456'}) + +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 5 digits in total.']}) + +# def test_raise_max_decimal_places(self): +# """ +# Make sure max_decimal_places violations raises ValidationError +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(decimal_places=3) + +# s = DecimalSerializer(data={'decimal_field': '123.4567'}) + +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 3 decimal places.']}) + +# def test_raise_max_whole_digits(self): +# """ +# Make sure max_whole_digits violations raises ValidationError +# """ +# class DecimalSerializer(serializers.Serializer): +# decimal_field = serializers.DecimalField(max_digits=4, decimal_places=3) + +# s = DecimalSerializer(data={'decimal_field': '12345.6'}) + +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'decimal_field': ['Ensure that there are no more than 4 digits in total.']}) + + +# class ChoiceFieldTests(TestCase): +# """ +# Tests for the ChoiceField options generator +# """ +# def test_choices_required(self): +# """ +# Make sure proper choices are rendered if field is required +# """ +# f = serializers.ChoiceField(required=True, choices=SAMPLE_CHOICES) +# self.assertEqual(f.choices, SAMPLE_CHOICES) + +# def test_choices_not_required(self): +# """ +# Make sure proper choices (plus blank) are rendered if the field isn't required +# """ +# f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES) +# self.assertEqual(f.choices, models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES) + +# def test_blank_choice_display(self): +# blank = 'No Preference' +# f = serializers.ChoiceField( +# required=False, +# choices=SAMPLE_CHOICES, +# blank_display_value=blank, +# ) +# self.assertEqual(f.choices, [('', blank)] + SAMPLE_CHOICES) + +# def test_invalid_choice_model(self): +# s = ChoiceFieldModelSerializer(data={'choice': 'wrong_value'}) +# self.assertFalse(s.is_valid()) +# self.assertEqual(s.errors, {'choice': ['Select a valid choice. wrong_value is not one of the available choices.']}) +# self.assertEqual(s.data['choice'], '') + +# def test_empty_choice_model(self): +# """ +# Test that the 'empty' value is correctly passed and used depending on +# the 'null' property on the model field. +# """ +# s = ChoiceFieldModelSerializer(data={'choice': ''}) +# self.assertTrue(s.is_valid()) +# self.assertEqual(s.data['choice'], '') + +# s = ChoiceFieldModelWithNullSerializer(data={'choice': ''}) +# self.assertTrue(s.is_valid()) +# self.assertEqual(s.data['choice'], None) + +# def test_from_native_empty(self): +# """ +# Make sure from_native() returns an empty string on empty param by default. +# """ +# f = serializers.ChoiceField(choices=SAMPLE_CHOICES) +# self.assertEqual(f.from_native(''), '') +# self.assertEqual(f.from_native(None), '') + +# def test_from_native_empty_override(self): +# """ +# Make sure you can override from_native() behavior regarding empty values. +# """ +# f = serializers.ChoiceField(choices=SAMPLE_CHOICES, empty=None) +# self.assertEqual(f.from_native(''), None) +# self.assertEqual(f.from_native(None), None) + +# def test_metadata_choices(self): +# """ +# Make sure proper choices are included in the field's metadata. +# """ +# choices = [{'value': v, 'display_name': n} for v, n in SAMPLE_CHOICES] +# f = serializers.ChoiceField(choices=SAMPLE_CHOICES) +# self.assertEqual(f.metadata()['choices'], choices) + +# def test_metadata_choices_not_required(self): +# """ +# Make sure proper choices are included in the field's metadata. +# """ +# choices = [{'value': v, 'display_name': n} +# for v, n in models.fields.BLANK_CHOICE_DASH + SAMPLE_CHOICES] +# f = serializers.ChoiceField(required=False, choices=SAMPLE_CHOICES) +# self.assertEqual(f.metadata()['choices'], choices) + + +# class EmailFieldTests(TestCase): +# """ +# Tests for EmailField attribute values +# """ + +# class EmailFieldModel(RESTFrameworkModel): +# email_field = models.EmailField(blank=True) + +# class EmailFieldWithGivenMaxLengthModel(RESTFrameworkModel): +# email_field = models.EmailField(max_length=150, blank=True) + +# def test_default_model_value(self): +# class EmailFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.EmailFieldModel + +# serializer = EmailFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 75) + +# def test_given_model_value(self): +# class EmailFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.EmailFieldWithGivenMaxLengthModel + +# serializer = EmailFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 150) + +# def test_given_serializer_value(self): +# class EmailFieldSerializer(serializers.ModelSerializer): +# email_field = serializers.EmailField(source='email_field', max_length=20, required=False) + +# class Meta: +# model = self.EmailFieldModel + +# serializer = EmailFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['email_field'], 'max_length'), 20) + + +# class SlugFieldTests(TestCase): +# """ +# Tests for SlugField attribute values +# """ + +# class SlugFieldModel(RESTFrameworkModel): +# slug_field = models.SlugField(blank=True) + +# class SlugFieldWithGivenMaxLengthModel(RESTFrameworkModel): +# slug_field = models.SlugField(max_length=84, blank=True) + +# def test_default_model_value(self): +# class SlugFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.SlugFieldModel + +# serializer = SlugFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 50) + +# def test_given_model_value(self): +# class SlugFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.SlugFieldWithGivenMaxLengthModel + +# serializer = SlugFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['slug_field'], 'max_length'), 84) + +# def test_given_serializer_value(self): +# class SlugFieldSerializer(serializers.ModelSerializer): +# slug_field = serializers.SlugField(source='slug_field', +# max_length=20, required=False) + +# class Meta: +# model = self.SlugFieldModel + +# serializer = SlugFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['slug_field'], +# 'max_length'), 20) + +# def test_invalid_slug(self): +# """ +# Make sure an invalid slug raises ValidationError +# """ +# class SlugFieldSerializer(serializers.ModelSerializer): +# slug_field = serializers.SlugField(source='slug_field', max_length=20, required=True) - class Meta: - model = self.SlugFieldModel +# class Meta: +# model = self.SlugFieldModel - s = SlugFieldSerializer(data={'slug_field': 'a b'}) +# s = SlugFieldSerializer(data={'slug_field': 'a b'}) - self.assertEqual(s.is_valid(), False) - self.assertEqual(s.errors, {'slug_field': ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]}) +# self.assertEqual(s.is_valid(), False) +# self.assertEqual(s.errors, {'slug_field': ["Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."]}) -class URLFieldTests(TestCase): - """ - Tests for URLField attribute values. +# class URLFieldTests(TestCase): +# """ +# Tests for URLField attribute values. - (Includes test for #1210, checking that validators can be overridden.) - """ +# (Includes test for #1210, checking that validators can be overridden.) +# """ - class URLFieldModel(RESTFrameworkModel): - url_field = models.URLField(blank=True) +# class URLFieldModel(RESTFrameworkModel): +# url_field = models.URLField(blank=True) - class URLFieldWithGivenMaxLengthModel(RESTFrameworkModel): - url_field = models.URLField(max_length=128, blank=True) +# class URLFieldWithGivenMaxLengthModel(RESTFrameworkModel): +# url_field = models.URLField(max_length=128, blank=True) - def test_default_model_value(self): - class URLFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.URLFieldModel +# def test_default_model_value(self): +# class URLFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.URLFieldModel - serializer = URLFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], - 'max_length'), 200) +# serializer = URLFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['url_field'], +# 'max_length'), 200) - def test_given_model_value(self): - class URLFieldSerializer(serializers.ModelSerializer): - class Meta: - model = self.URLFieldWithGivenMaxLengthModel +# def test_given_model_value(self): +# class URLFieldSerializer(serializers.ModelSerializer): +# class Meta: +# model = self.URLFieldWithGivenMaxLengthModel - serializer = URLFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], - 'max_length'), 128) +# serializer = URLFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['url_field'], +# 'max_length'), 128) - def test_given_serializer_value(self): - class URLFieldSerializer(serializers.ModelSerializer): - url_field = serializers.URLField(source='url_field', - max_length=20, required=False) +# def test_given_serializer_value(self): +# class URLFieldSerializer(serializers.ModelSerializer): +# url_field = serializers.URLField(source='url_field', +# max_length=20, required=False) - class Meta: - model = self.URLFieldWithGivenMaxLengthModel +# class Meta: +# model = self.URLFieldWithGivenMaxLengthModel - serializer = URLFieldSerializer(data={}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(getattr(serializer.fields['url_field'], - 'max_length'), 20) +# serializer = URLFieldSerializer(data={}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(getattr(serializer.fields['url_field'], +# 'max_length'), 20) - def test_validators_can_be_overridden(self): - url_field = serializers.URLField(validators=[]) - validators = url_field.validators - self.assertEqual([], validators, 'Passing `validators` kwarg should have overridden default validators') +# def test_validators_can_be_overridden(self): +# url_field = serializers.URLField(validators=[]) +# validators = url_field.validators +# self.assertEqual([], validators, 'Passing `validators` kwarg should have overridden default validators') -class FieldMetadata(TestCase): - def setUp(self): - self.required_field = serializers.Field() - self.required_field.label = uuid4().hex - self.required_field.required = True +# class FieldMetadata(TestCase): +# def setUp(self): +# self.required_field = serializers.Field() +# self.required_field.label = uuid4().hex +# self.required_field.required = True - self.optional_field = serializers.Field() - self.optional_field.label = uuid4().hex - self.optional_field.required = False +# self.optional_field = serializers.Field() +# self.optional_field.label = uuid4().hex +# self.optional_field.required = False - def test_required(self): - self.assertEqual(self.required_field.metadata()['required'], True) +# def test_required(self): +# self.assertEqual(self.required_field.metadata()['required'], True) - def test_optional(self): - self.assertEqual(self.optional_field.metadata()['required'], False) +# def test_optional(self): +# self.assertEqual(self.optional_field.metadata()['required'], False) - def test_label(self): - for field in (self.required_field, self.optional_field): - self.assertEqual(field.metadata()['label'], field.label) +# def test_label(self): +# for field in (self.required_field, self.optional_field): +# self.assertEqual(field.metadata()['label'], field.label) -class FieldCallableDefault(TestCase): - def setUp(self): - self.simple_callable = lambda: 'foo bar' +# class FieldCallableDefault(TestCase): +# def setUp(self): +# self.simple_callable = lambda: 'foo bar' - def test_default_can_be_simple_callable(self): - """ - Ensure that the 'default' argument can also be a simple callable. - """ - field = serializers.WritableField(default=self.simple_callable) - into = {} - field.field_from_native({}, {}, 'field', into) - self.assertEqual(into, {'field': 'foo bar'}) +# def test_default_can_be_simple_callable(self): +# """ +# Ensure that the 'default' argument can also be a simple callable. +# """ +# field = serializers.WritableField(default=self.simple_callable) +# into = {} +# field.field_from_native({}, {}, 'field', into) +# self.assertEqual(into, {'field': 'foo bar'}) -class CustomIntegerField(TestCase): - """ - Test that custom fields apply min_value and max_value constraints - """ - def test_custom_fields_can_be_validated_for_value(self): +# class CustomIntegerField(TestCase): +# """ +# Test that custom fields apply min_value and max_value constraints +# """ +# def test_custom_fields_can_be_validated_for_value(self): - class MoneyField(models.PositiveIntegerField): - pass +# class MoneyField(models.PositiveIntegerField): +# pass - class EntryModel(models.Model): - bank = MoneyField(validators=[validators.MaxValueValidator(100)]) +# class EntryModel(models.Model): +# bank = MoneyField(validators=[validators.MaxValueValidator(100)]) - class EntrySerializer(serializers.ModelSerializer): - class Meta: - model = EntryModel +# class EntrySerializer(serializers.ModelSerializer): +# class Meta: +# model = EntryModel - entry = EntryModel(bank=1) +# entry = EntryModel(bank=1) - serializer = EntrySerializer(entry, data={"bank": 11}) - self.assertTrue(serializer.is_valid()) +# serializer = EntrySerializer(entry, data={"bank": 11}) +# self.assertTrue(serializer.is_valid()) - serializer = EntrySerializer(entry, data={"bank": -1}) - self.assertFalse(serializer.is_valid()) +# serializer = EntrySerializer(entry, data={"bank": -1}) +# self.assertFalse(serializer.is_valid()) - serializer = EntrySerializer(entry, data={"bank": 101}) - self.assertFalse(serializer.is_valid()) +# serializer = EntrySerializer(entry, data={"bank": 101}) +# self.assertFalse(serializer.is_valid()) -class BooleanField(TestCase): - """ - Tests for BooleanField - """ - def test_boolean_required(self): - class BooleanRequiredSerializer(serializers.Serializer): - bool_field = serializers.BooleanField(required=True) +# class BooleanField(TestCase): +# """ +# Tests for BooleanField +# """ +# def test_boolean_required(self): +# class BooleanRequiredSerializer(serializers.Serializer): +# bool_field = serializers.BooleanField(required=True) - self.assertFalse(BooleanRequiredSerializer(data={}).is_valid()) +# self.assertFalse(BooleanRequiredSerializer(data={}).is_valid()) -class SerializerMethodFieldTest(TestCase): - """ - Tests for the SerializerMethodField field_to_native() behavior - """ - class SerializerTest(serializers.Serializer): - def get_my_test(self, obj): - return obj.my_test[0:5] +# class SerializerMethodFieldTest(TestCase): +# """ +# Tests for the SerializerMethodField field_to_native() behavior +# """ +# class SerializerTest(serializers.Serializer): +# def get_my_test(self, obj): +# return obj.my_test[0:5] - class Example(): - my_test = 'Hey, this is a test !' +# class Example(): +# my_test = 'Hey, this is a test !' - def test_field_to_native(self): - s = serializers.SerializerMethodField('get_my_test') - s.initialize(self.SerializerTest(), 'name') - result = s.field_to_native(self.Example(), None) - self.assertEqual(result, 'Hey, ') +# def test_field_to_native(self): +# s = serializers.SerializerMethodField('get_my_test') +# s.initialize(self.SerializerTest(), 'name') +# result = s.field_to_native(self.Example(), None) +# self.assertEqual(result, 'Hey, ') diff --git a/tests/test_files.py b/tests/test_files.py index de4f71d1a..a5613fcbe 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -1,92 +1,92 @@ -from __future__ import unicode_literals -from django.test import TestCase -from django.utils import six -from rest_framework import serializers -from rest_framework.compat import BytesIO -import datetime +# from __future__ import unicode_literals +# from django.test import TestCase +# from django.utils import six +# from rest_framework import serializers +# from rest_framework.compat import BytesIO +# import datetime -class UploadedFile(object): - def __init__(self, file=None, created=None): - self.file = file - self.created = created or datetime.datetime.now() +# class UploadedFile(object): +# def __init__(self, file=None, created=None): +# self.file = file +# self.created = created or datetime.datetime.now() -class UploadedFileSerializer(serializers.Serializer): - file = serializers.FileField(required=False) - created = serializers.DateTimeField() +# class UploadedFileSerializer(serializers.Serializer): +# file = serializers.FileField(required=False) +# created = serializers.DateTimeField() - def restore_object(self, attrs, instance=None): - if instance: - instance.file = attrs['file'] - instance.created = attrs['created'] - return instance - return UploadedFile(**attrs) +# def restore_object(self, attrs, instance=None): +# if instance: +# instance.file = attrs['file'] +# instance.created = attrs['created'] +# return instance +# return UploadedFile(**attrs) -class FileSerializerTests(TestCase): - def test_create(self): - now = datetime.datetime.now() - file = BytesIO(six.b('stuff')) - file.name = 'stuff.txt' - file.size = len(file.getvalue()) - serializer = UploadedFileSerializer(data={'created': now}, files={'file': file}) - uploaded_file = UploadedFile(file=file, created=now) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.object.created, uploaded_file.created) - self.assertEqual(serializer.object.file, uploaded_file.file) - self.assertFalse(serializer.object is uploaded_file) +# class FileSerializerTests(TestCase): +# def test_create(self): +# now = datetime.datetime.now() +# file = BytesIO(six.b('stuff')) +# file.name = 'stuff.txt' +# file.size = len(file.getvalue()) +# serializer = UploadedFileSerializer(data={'created': now}, files={'file': file}) +# uploaded_file = UploadedFile(file=file, created=now) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.object.created, uploaded_file.created) +# self.assertEqual(serializer.object.file, uploaded_file.file) +# self.assertFalse(serializer.object is uploaded_file) - def test_creation_failure(self): - """ - Passing files=None should result in an ValidationError +# def test_creation_failure(self): +# """ +# Passing files=None should result in an ValidationError - Regression test for: - https://github.com/tomchristie/django-rest-framework/issues/542 - """ - now = datetime.datetime.now() +# Regression test for: +# https://github.com/tomchristie/django-rest-framework/issues/542 +# """ +# now = datetime.datetime.now() - serializer = UploadedFileSerializer(data={'created': now}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.object.created, now) - self.assertIsNone(serializer.object.file) +# serializer = UploadedFileSerializer(data={'created': now}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.object.created, now) +# self.assertIsNone(serializer.object.file) - def test_remove_with_empty_string(self): - """ - Passing empty string as data should cause file to be removed +# def test_remove_with_empty_string(self): +# """ +# Passing empty string as data should cause file to be removed - Test for: - https://github.com/tomchristie/django-rest-framework/issues/937 - """ - now = datetime.datetime.now() - file = BytesIO(six.b('stuff')) - file.name = 'stuff.txt' - file.size = len(file.getvalue()) +# Test for: +# https://github.com/tomchristie/django-rest-framework/issues/937 +# """ +# now = datetime.datetime.now() +# file = BytesIO(six.b('stuff')) +# file.name = 'stuff.txt' +# file.size = len(file.getvalue()) - uploaded_file = UploadedFile(file=file, created=now) +# uploaded_file = UploadedFile(file=file, created=now) - serializer = UploadedFileSerializer(instance=uploaded_file, data={'created': now, 'file': ''}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.object.created, uploaded_file.created) - self.assertIsNone(serializer.object.file) +# serializer = UploadedFileSerializer(instance=uploaded_file, data={'created': now, 'file': ''}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.object.created, uploaded_file.created) +# self.assertIsNone(serializer.object.file) - def test_validation_error_with_non_file(self): - """ - Passing non-files should raise a validation error. - """ - now = datetime.datetime.now() - errmsg = 'No file was submitted. Check the encoding type on the form.' +# def test_validation_error_with_non_file(self): +# """ +# Passing non-files should raise a validation error. +# """ +# now = datetime.datetime.now() +# errmsg = 'No file was submitted. Check the encoding type on the form.' - serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'file': [errmsg]}) +# serializer = UploadedFileSerializer(data={'created': now, 'file': 'abc'}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'file': [errmsg]}) - def test_validation_with_no_data(self): - """ - Validation should still function when no data dictionary is provided. - """ - uploaded_file = BytesIO(six.b('stuff')) - uploaded_file.name = 'stuff.txt' - uploaded_file.size = len(uploaded_file.getvalue()) - serializer = UploadedFileSerializer(files={'file': uploaded_file}) - self.assertFalse(serializer.is_valid()) +# def test_validation_with_no_data(self): +# """ +# Validation should still function when no data dictionary is provided. +# """ +# uploaded_file = BytesIO(six.b('stuff')) +# uploaded_file.name = 'stuff.txt' +# uploaded_file.size = len(uploaded_file.getvalue()) +# serializer = UploadedFileSerializer(files={'file': uploaded_file}) +# self.assertFalse(serializer.is_valid()) diff --git a/tests/test_genericrelations.py b/tests/test_genericrelations.py index 95295eaa8..a87ea3fdd 100644 --- a/tests/test_genericrelations.py +++ b/tests/test_genericrelations.py @@ -1,151 +1,151 @@ -from __future__ import unicode_literals -from django.contrib.contenttypes.models import ContentType -from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey -from django.db import models -from django.test import TestCase -from rest_framework import serializers -from rest_framework.compat import python_2_unicode_compatible +# from __future__ import unicode_literals +# from django.contrib.contenttypes.models import ContentType +# from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey +# from django.db import models +# from django.test import TestCase +# from rest_framework import serializers +# from rest_framework.compat import python_2_unicode_compatible -@python_2_unicode_compatible -class Tag(models.Model): - """ - Tags have a descriptive slug, and are attached to an arbitrary object. - """ - tag = models.SlugField() - content_type = models.ForeignKey(ContentType) - object_id = models.PositiveIntegerField() - tagged_item = GenericForeignKey('content_type', 'object_id') +# @python_2_unicode_compatible +# class Tag(models.Model): +# """ +# Tags have a descriptive slug, and are attached to an arbitrary object. +# """ +# tag = models.SlugField() +# content_type = models.ForeignKey(ContentType) +# object_id = models.PositiveIntegerField() +# tagged_item = GenericForeignKey('content_type', 'object_id') - def __str__(self): - return self.tag +# def __str__(self): +# return self.tag -@python_2_unicode_compatible -class Bookmark(models.Model): - """ - A URL bookmark that may have multiple tags attached. - """ - url = models.URLField() - tags = GenericRelation(Tag) +# @python_2_unicode_compatible +# class Bookmark(models.Model): +# """ +# A URL bookmark that may have multiple tags attached. +# """ +# url = models.URLField() +# tags = GenericRelation(Tag) - def __str__(self): - return 'Bookmark: %s' % self.url +# def __str__(self): +# return 'Bookmark: %s' % self.url -@python_2_unicode_compatible -class Note(models.Model): - """ - A textual note that may have multiple tags attached. - """ - text = models.TextField() - tags = GenericRelation(Tag) +# @python_2_unicode_compatible +# class Note(models.Model): +# """ +# A textual note that may have multiple tags attached. +# """ +# text = models.TextField() +# tags = GenericRelation(Tag) - def __str__(self): - return 'Note: %s' % self.text +# def __str__(self): +# return 'Note: %s' % self.text -class TestGenericRelations(TestCase): - def setUp(self): - self.bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/') - Tag.objects.create(tagged_item=self.bookmark, tag='django') - Tag.objects.create(tagged_item=self.bookmark, tag='python') - self.note = Note.objects.create(text='Remember the milk') - Tag.objects.create(tagged_item=self.note, tag='reminder') +# class TestGenericRelations(TestCase): +# def setUp(self): +# self.bookmark = Bookmark.objects.create(url='https://www.djangoproject.com/') +# Tag.objects.create(tagged_item=self.bookmark, tag='django') +# Tag.objects.create(tagged_item=self.bookmark, tag='python') +# self.note = Note.objects.create(text='Remember the milk') +# Tag.objects.create(tagged_item=self.note, tag='reminder') - def test_generic_relation(self): - """ - Test a relationship that spans a GenericRelation field. - IE. A reverse generic relationship. - """ +# def test_generic_relation(self): +# """ +# Test a relationship that spans a GenericRelation field. +# IE. A reverse generic relationship. +# """ - class BookmarkSerializer(serializers.ModelSerializer): - tags = serializers.RelatedField(many=True) +# class BookmarkSerializer(serializers.ModelSerializer): +# tags = serializers.RelatedField(many=True) - class Meta: - model = Bookmark - exclude = ('id',) +# class Meta: +# model = Bookmark +# exclude = ('id',) - serializer = BookmarkSerializer(self.bookmark) - expected = { - 'tags': ['django', 'python'], - 'url': 'https://www.djangoproject.com/' - } - self.assertEqual(serializer.data, expected) +# serializer = BookmarkSerializer(self.bookmark) +# expected = { +# 'tags': ['django', 'python'], +# 'url': 'https://www.djangoproject.com/' +# } +# self.assertEqual(serializer.data, expected) - def test_generic_nested_relation(self): - """ - Test saving a GenericRelation field via a nested serializer. - """ +# def test_generic_nested_relation(self): +# """ +# Test saving a GenericRelation field via a nested serializer. +# """ - class TagSerializer(serializers.ModelSerializer): - class Meta: - model = Tag - exclude = ('content_type', 'object_id') +# class TagSerializer(serializers.ModelSerializer): +# class Meta: +# model = Tag +# exclude = ('content_type', 'object_id') - class BookmarkSerializer(serializers.ModelSerializer): - tags = TagSerializer(many=True) +# class BookmarkSerializer(serializers.ModelSerializer): +# tags = TagSerializer(many=True) - class Meta: - model = Bookmark - exclude = ('id',) +# class Meta: +# model = Bookmark +# exclude = ('id',) - data = { - 'url': 'https://docs.djangoproject.com/', - 'tags': [ - {'tag': 'contenttypes'}, - {'tag': 'genericrelations'}, - ] - } - serializer = BookmarkSerializer(data=data) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.object.tags.count(), 2) +# data = { +# 'url': 'https://docs.djangoproject.com/', +# 'tags': [ +# {'tag': 'contenttypes'}, +# {'tag': 'genericrelations'}, +# ] +# } +# serializer = BookmarkSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.object.tags.count(), 2) - def test_generic_fk(self): - """ - Test a relationship that spans a GenericForeignKey field. - IE. A forward generic relationship. - """ +# def test_generic_fk(self): +# """ +# Test a relationship that spans a GenericForeignKey field. +# IE. A forward generic relationship. +# """ - class TagSerializer(serializers.ModelSerializer): - tagged_item = serializers.RelatedField() +# class TagSerializer(serializers.ModelSerializer): +# tagged_item = serializers.RelatedField() - class Meta: - model = Tag - exclude = ('id', 'content_type', 'object_id') +# class Meta: +# model = Tag +# exclude = ('id', 'content_type', 'object_id') - serializer = TagSerializer(Tag.objects.all(), many=True) - expected = [ - { - 'tag': 'django', - 'tagged_item': 'Bookmark: https://www.djangoproject.com/' - }, - { - 'tag': 'python', - 'tagged_item': 'Bookmark: https://www.djangoproject.com/' - }, - { - 'tag': 'reminder', - 'tagged_item': 'Note: Remember the milk' - } - ] - self.assertEqual(serializer.data, expected) +# serializer = TagSerializer(Tag.objects.all(), many=True) +# expected = [ +# { +# 'tag': 'django', +# 'tagged_item': 'Bookmark: https://www.djangoproject.com/' +# }, +# { +# 'tag': 'python', +# 'tagged_item': 'Bookmark: https://www.djangoproject.com/' +# }, +# { +# 'tag': 'reminder', +# 'tagged_item': 'Note: Remember the milk' +# } +# ] +# self.assertEqual(serializer.data, expected) - def test_restore_object_generic_fk(self): - """ - Ensure an object with a generic foreign key can be restored. - """ +# def test_restore_object_generic_fk(self): +# """ +# Ensure an object with a generic foreign key can be restored. +# """ - class TagSerializer(serializers.ModelSerializer): - class Meta: - model = Tag - exclude = ('content_type', 'object_id') +# class TagSerializer(serializers.ModelSerializer): +# class Meta: +# model = Tag +# exclude = ('content_type', 'object_id') - serializer = TagSerializer() +# serializer = TagSerializer() - bookmark = Bookmark(url='http://example.com') - attrs = {'tagged_item': bookmark, 'tag': 'example'} +# bookmark = Bookmark(url='http://example.com') +# attrs = {'tagged_item': bookmark, 'tag': 'example'} - tag = serializer.restore_object(attrs) - self.assertEqual(tag.tagged_item, bookmark) +# tag = serializer.restore_object(attrs) +# self.assertEqual(tag.tagged_item, bookmark) diff --git a/tests/test_generics.py b/tests/test_generics.py index f50d53e99..55f361b24 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -33,13 +33,9 @@ class InstanceView(generics.RetrieveUpdateDestroyAPIView): """ Example description for OPTIONS. """ - queryset = BasicModel.objects.all() + queryset = BasicModel.objects.exclude(text='filtered out') serializer_class = BasicSerializer - def get_queryset(self): - queryset = super(InstanceView, self).get_queryset() - return queryset.exclude(text='filtered out') - class FKInstanceView(generics.RetrieveUpdateDestroyAPIView): """ @@ -50,11 +46,11 @@ class FKInstanceView(generics.RetrieveUpdateDestroyAPIView): class SlugSerializer(serializers.ModelSerializer): - slug = serializers.Field() # read only + slug = serializers.Field(read_only=True) class Meta: model = SlugBasedModel - exclude = ('id',) + fields = ('text', 'slug') class SlugBasedInstanceView(InstanceView): @@ -125,46 +121,46 @@ class TestRootView(TestCase): self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) self.assertEqual(response.data, {"detail": "Method 'DELETE' not allowed."}) - def test_options_root_view(self): - """ - OPTIONS requests to ListCreateAPIView should return metadata - """ - request = factory.options('/') - with self.assertNumQueries(0): - response = self.view(request).render() - expected = { - 'parses': [ - 'application/json', - 'application/x-www-form-urlencoded', - 'multipart/form-data' - ], - 'renders': [ - 'application/json', - 'text/html' - ], - 'name': 'Root', - 'description': 'Example description for OPTIONS.', - 'actions': { - 'POST': { - 'text': { - 'max_length': 100, - 'read_only': False, - 'required': True, - 'type': 'string', - "label": "Text comes here", - "help_text": "Text description." - }, - 'id': { - 'read_only': True, - 'required': False, - 'type': 'integer', - 'label': 'ID', - }, - } - } - } - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, expected) + # def test_options_root_view(self): + # """ + # OPTIONS requests to ListCreateAPIView should return metadata + # """ + # request = factory.options('/') + # with self.assertNumQueries(0): + # response = self.view(request).render() + # expected = { + # 'parses': [ + # 'application/json', + # 'application/x-www-form-urlencoded', + # 'multipart/form-data' + # ], + # 'renders': [ + # 'application/json', + # 'text/html' + # ], + # 'name': 'Root', + # 'description': 'Example description for OPTIONS.', + # 'actions': { + # 'POST': { + # 'text': { + # 'max_length': 100, + # 'read_only': False, + # 'required': True, + # 'type': 'string', + # "label": "Text comes here", + # "help_text": "Text description." + # }, + # 'id': { + # 'read_only': True, + # 'required': False, + # 'type': 'integer', + # 'label': 'ID', + # }, + # } + # } + # } + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertEqual(response.data, expected) def test_post_cannot_set_id(self): """ @@ -223,10 +219,10 @@ class TestInstanceView(TestCase): """ data = {'text': 'foobar'} request = factory.put('/1', data, format='json') - with self.assertNumQueries(2): + with self.assertNumQueries(3): response = self.view(request, pk='1').render() self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, {'id': 1, 'text': 'foobar'}) + self.assertEqual(dict(response.data), {'id': 1, 'text': 'foobar'}) updated = self.objects.get(id=1) self.assertEqual(updated.text, 'foobar') @@ -237,7 +233,7 @@ class TestInstanceView(TestCase): data = {'text': 'foobar'} request = factory.patch('/1', data, format='json') - with self.assertNumQueries(2): + with self.assertNumQueries(3): response = self.view(request, pk=1).render() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {'id': 1, 'text': 'foobar'}) @@ -256,88 +252,88 @@ class TestInstanceView(TestCase): ids = [obj.id for obj in self.objects.all()] self.assertEqual(ids, [2, 3]) - def test_options_instance_view(self): - """ - OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata - """ - request = factory.options('/1') - with self.assertNumQueries(1): - response = self.view(request, pk=1).render() - expected = { - 'parses': [ - 'application/json', - 'application/x-www-form-urlencoded', - 'multipart/form-data' - ], - 'renders': [ - 'application/json', - 'text/html' - ], - 'name': 'Instance', - 'description': 'Example description for OPTIONS.', - 'actions': { - 'PUT': { - 'text': { - 'max_length': 100, - 'read_only': False, - 'required': True, - 'type': 'string', - 'label': 'Text comes here', - 'help_text': 'Text description.' - }, - 'id': { - 'read_only': True, - 'required': False, - 'type': 'integer', - 'label': 'ID', - }, - } - } - } - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, expected) + # def test_options_instance_view(self): + # """ + # OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata + # """ + # request = factory.options('/1') + # with self.assertNumQueries(1): + # response = self.view(request, pk=1).render() + # expected = { + # 'parses': [ + # 'application/json', + # 'application/x-www-form-urlencoded', + # 'multipart/form-data' + # ], + # 'renders': [ + # 'application/json', + # 'text/html' + # ], + # 'name': 'Instance', + # 'description': 'Example description for OPTIONS.', + # 'actions': { + # 'PUT': { + # 'text': { + # 'max_length': 100, + # 'read_only': False, + # 'required': True, + # 'type': 'string', + # 'label': 'Text comes here', + # 'help_text': 'Text description.' + # }, + # 'id': { + # 'read_only': True, + # 'required': False, + # 'type': 'integer', + # 'label': 'ID', + # }, + # } + # } + # } + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertEqual(response.data, expected) - def test_options_before_instance_create(self): - """ - OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata - before the instance has been created - """ - request = factory.options('/999') - with self.assertNumQueries(1): - response = self.view(request, pk=999).render() - expected = { - 'parses': [ - 'application/json', - 'application/x-www-form-urlencoded', - 'multipart/form-data' - ], - 'renders': [ - 'application/json', - 'text/html' - ], - 'name': 'Instance', - 'description': 'Example description for OPTIONS.', - 'actions': { - 'PUT': { - 'text': { - 'max_length': 100, - 'read_only': False, - 'required': True, - 'type': 'string', - 'label': 'Text comes here', - 'help_text': 'Text description.' - }, - 'id': { - 'read_only': True, - 'required': False, - 'type': 'integer', - 'label': 'ID', - }, - } - } - } - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, expected) + # def test_options_before_instance_create(self): + # """ + # OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata + # before the instance has been created + # """ + # request = factory.options('/999') + # with self.assertNumQueries(1): + # response = self.view(request, pk=999).render() + # expected = { + # 'parses': [ + # 'application/json', + # 'application/x-www-form-urlencoded', + # 'multipart/form-data' + # ], + # 'renders': [ + # 'application/json', + # 'text/html' + # ], + # 'name': 'Instance', + # 'description': 'Example description for OPTIONS.', + # 'actions': { + # 'PUT': { + # 'text': { + # 'max_length': 100, + # 'read_only': False, + # 'required': True, + # 'type': 'string', + # 'label': 'Text comes here', + # 'help_text': 'Text description.' + # }, + # 'id': { + # 'read_only': True, + # 'required': False, + # 'type': 'integer', + # 'label': 'ID', + # }, + # } + # } + # } + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertEqual(response.data, expected) def test_get_instance_view_incorrect_arg(self): """ @@ -355,7 +351,7 @@ class TestInstanceView(TestCase): """ data = {'id': 999, 'text': 'foobar'} request = factory.put('/1', data, format='json') - with self.assertNumQueries(2): + with self.assertNumQueries(3): response = self.view(request, pk=1).render() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {'id': 1, 'text': 'foobar'}) @@ -370,7 +366,7 @@ class TestInstanceView(TestCase): self.objects.get(id=1).delete() data = {'text': 'foobar'} request = factory.put('/1', data, format='json') - with self.assertNumQueries(3): + with self.assertNumQueries(2): response = self.view(request, pk=1).render() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, {'id': 1, 'text': 'foobar'}) @@ -396,7 +392,7 @@ class TestInstanceView(TestCase): data = {'text': 'foobar'} # pk fields can not be created on demand, only the database can set the pk for a new object request = factory.put('/5', data, format='json') - with self.assertNumQueries(3): + with self.assertNumQueries(2): response = self.view(request, pk=5).render() self.assertEqual(response.status_code, status.HTTP_201_CREATED) new_obj = self.objects.get(pk=5) @@ -446,52 +442,52 @@ class TestFKInstanceView(TestCase): ] self.view = FKInstanceView.as_view() - def test_options_root_view(self): - """ - OPTIONS requests to ListCreateAPIView should return metadata - """ - request = factory.options('/999') - with self.assertNumQueries(1): - response = self.view(request, pk=999).render() - expected = { - 'name': 'Fk Instance', - 'description': 'FK: example description for OPTIONS.', - 'renders': [ - 'application/json', - 'text/html' - ], - 'parses': [ - 'application/json', - 'application/x-www-form-urlencoded', - 'multipart/form-data' - ], - 'actions': { - 'PUT': { - 'id': { - 'type': 'integer', - 'required': False, - 'read_only': True, - 'label': 'ID' - }, - 'name': { - 'type': 'string', - 'required': True, - 'read_only': False, - 'label': 'name', - 'max_length': 100 - }, - 'target': { - 'type': 'field', - 'required': True, - 'read_only': False, - 'label': 'Target', - 'help_text': 'Target' - } - } - } - } - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, expected) + # def test_options_root_view(self): + # """ + # OPTIONS requests to ListCreateAPIView should return metadata + # """ + # request = factory.options('/999') + # with self.assertNumQueries(1): + # response = self.view(request, pk=999).render() + # expected = { + # 'name': 'Fk Instance', + # 'description': 'FK: example description for OPTIONS.', + # 'renders': [ + # 'application/json', + # 'text/html' + # ], + # 'parses': [ + # 'application/json', + # 'application/x-www-form-urlencoded', + # 'multipart/form-data' + # ], + # 'actions': { + # 'PUT': { + # 'id': { + # 'type': 'integer', + # 'required': False, + # 'read_only': True, + # 'label': 'ID' + # }, + # 'name': { + # 'type': 'string', + # 'required': True, + # 'read_only': False, + # 'label': 'name', + # 'max_length': 100 + # }, + # 'target': { + # 'type': 'field', + # 'required': True, + # 'read_only': False, + # 'label': 'Target', + # 'help_text': 'Target' + # } + # } + # } + # } + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertEqual(response.data, expected) class TestOverriddenGetObject(TestCase): diff --git a/tests/test_hyperlinkedserializers.py b/tests/test_hyperlinkedserializers.py index 0e8c1ed46..ff3663dd3 100644 --- a/tests/test_hyperlinkedserializers.py +++ b/tests/test_hyperlinkedserializers.py @@ -1,406 +1,406 @@ -from __future__ import unicode_literals -import json -from django.test import TestCase -from rest_framework import generics, status, serializers -from django.conf.urls import patterns, url -from rest_framework.settings import api_settings -from rest_framework.test import APIRequestFactory -from tests.models import ( - Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, - Album, Photo, OptionalRelationModel -) +# from __future__ import unicode_literals +# import json +# from django.test import TestCase +# from rest_framework import generics, status, serializers +# from django.conf.urls import patterns, url +# from rest_framework.settings import api_settings +# from rest_framework.test import APIRequestFactory +# from tests.models import ( +# Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, +# Album, Photo, OptionalRelationModel +# ) -factory = APIRequestFactory() +# factory = APIRequestFactory() -class BlogPostCommentSerializer(serializers.ModelSerializer): - url = serializers.HyperlinkedIdentityField(view_name='blogpostcomment-detail') - text = serializers.CharField() - blog_post_url = serializers.HyperlinkedRelatedField(source='blog_post', view_name='blogpost-detail') +# class BlogPostCommentSerializer(serializers.ModelSerializer): +# url = serializers.HyperlinkedIdentityField(view_name='blogpostcomment-detail') +# text = serializers.CharField() +# blog_post_url = serializers.HyperlinkedRelatedField(source='blog_post', view_name='blogpost-detail') - class Meta: - model = BlogPostComment - fields = ('text', 'blog_post_url', 'url') +# class Meta: +# model = BlogPostComment +# fields = ('text', 'blog_post_url', 'url') -class PhotoSerializer(serializers.Serializer): - description = serializers.CharField() - album_url = serializers.HyperlinkedRelatedField(source='album', view_name='album-detail', queryset=Album.objects.all(), lookup_field='title') +# class PhotoSerializer(serializers.Serializer): +# description = serializers.CharField() +# album_url = serializers.HyperlinkedRelatedField(source='album', view_name='album-detail', queryset=Album.objects.all(), lookup_field='title') - def restore_object(self, attrs, instance=None): - return Photo(**attrs) +# def restore_object(self, attrs, instance=None): +# return Photo(**attrs) -class AlbumSerializer(serializers.ModelSerializer): - url = serializers.HyperlinkedIdentityField(view_name='album-detail', lookup_field='title') +# class AlbumSerializer(serializers.ModelSerializer): +# url = serializers.HyperlinkedIdentityField(view_name='album-detail', lookup_field='title') - class Meta: - model = Album - fields = ('title', 'url') +# class Meta: +# model = Album +# fields = ('title', 'url') -class BasicSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = BasicModel +# class BasicSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = BasicModel -class AnchorSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = Anchor +# class AnchorSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = Anchor -class ManyToManySerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ManyToManyModel +# class ManyToManySerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ManyToManyModel -class BlogPostSerializer(serializers.ModelSerializer): - class Meta: - model = BlogPost +# class BlogPostSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlogPost -class OptionalRelationSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = OptionalRelationModel +# class OptionalRelationSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = OptionalRelationModel -class BasicList(generics.ListCreateAPIView): - queryset = BasicModel.objects.all() - serializer_class = BasicSerializer +# class BasicList(generics.ListCreateAPIView): +# queryset = BasicModel.objects.all() +# serializer_class = BasicSerializer -class BasicDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = BasicModel.objects.all() - serializer_class = BasicSerializer +# class BasicDetail(generics.RetrieveUpdateDestroyAPIView): +# queryset = BasicModel.objects.all() +# serializer_class = BasicSerializer -class AnchorDetail(generics.RetrieveAPIView): - queryset = Anchor.objects.all() - serializer_class = AnchorSerializer +# class AnchorDetail(generics.RetrieveAPIView): +# queryset = Anchor.objects.all() +# serializer_class = AnchorSerializer -class ManyToManyList(generics.ListAPIView): - queryset = ManyToManyModel.objects.all() - serializer_class = ManyToManySerializer +# class ManyToManyList(generics.ListAPIView): +# queryset = ManyToManyModel.objects.all() +# serializer_class = ManyToManySerializer -class ManyToManyDetail(generics.RetrieveAPIView): - queryset = ManyToManyModel.objects.all() - serializer_class = ManyToManySerializer +# class ManyToManyDetail(generics.RetrieveAPIView): +# queryset = ManyToManyModel.objects.all() +# serializer_class = ManyToManySerializer -class BlogPostCommentListCreate(generics.ListCreateAPIView): - queryset = BlogPostComment.objects.all() - serializer_class = BlogPostCommentSerializer +# class BlogPostCommentListCreate(generics.ListCreateAPIView): +# queryset = BlogPostComment.objects.all() +# serializer_class = BlogPostCommentSerializer -class BlogPostCommentDetail(generics.RetrieveAPIView): - queryset = BlogPostComment.objects.all() - serializer_class = BlogPostCommentSerializer +# class BlogPostCommentDetail(generics.RetrieveAPIView): +# queryset = BlogPostComment.objects.all() +# serializer_class = BlogPostCommentSerializer -class BlogPostDetail(generics.RetrieveAPIView): - queryset = BlogPost.objects.all() - serializer_class = BlogPostSerializer +# class BlogPostDetail(generics.RetrieveAPIView): +# queryset = BlogPost.objects.all() +# serializer_class = BlogPostSerializer -class PhotoListCreate(generics.ListCreateAPIView): - queryset = Photo.objects.all() - serializer_class = PhotoSerializer +# class PhotoListCreate(generics.ListCreateAPIView): +# queryset = Photo.objects.all() +# serializer_class = PhotoSerializer -class AlbumDetail(generics.RetrieveAPIView): - queryset = Album.objects.all() - serializer_class = AlbumSerializer - lookup_field = 'title' +# class AlbumDetail(generics.RetrieveAPIView): +# queryset = Album.objects.all() +# serializer_class = AlbumSerializer +# lookup_field = 'title' -class OptionalRelationDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = OptionalRelationModel.objects.all() - serializer_class = OptionalRelationSerializer +# class OptionalRelationDetail(generics.RetrieveUpdateDestroyAPIView): +# queryset = OptionalRelationModel.objects.all() +# serializer_class = OptionalRelationSerializer -urlpatterns = patterns( - '', - url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'), - url(r'^basic/(?P\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'), - url(r'^anchor/(?P\d+)/$', AnchorDetail.as_view(), name='anchor-detail'), - url(r'^manytomany/$', ManyToManyList.as_view(), name='manytomanymodel-list'), - url(r'^manytomany/(?P\d+)/$', ManyToManyDetail.as_view(), name='manytomanymodel-detail'), - url(r'^posts/(?P\d+)/$', BlogPostDetail.as_view(), name='blogpost-detail'), - url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list'), - url(r'^comments/(?P\d+)/$', BlogPostCommentDetail.as_view(), name='blogpostcomment-detail'), - url(r'^albums/(?P\w[\w-]*)/$', AlbumDetail.as_view(), name='album-detail'), - url(r'^photos/$', PhotoListCreate.as_view(), name='photo-list'), - url(r'^optionalrelation/(?P<pk>\d+)/$', OptionalRelationDetail.as_view(), name='optionalrelationmodel-detail'), -) - - -class TestBasicHyperlinkedView(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create 3 BasicModel instances. - """ - items = ['foo', 'bar', 'baz'] - for item in items: - BasicModel(text=item).save() - self.objects = BasicModel.objects - self.data = [ - {'url': 'http://testserver/basic/%d/' % obj.id, 'text': obj.text} - for obj in self.objects.all() - ] - self.list_view = BasicList.as_view() - self.detail_view = BasicDetail.as_view() - - def test_get_list_view(self): - """ - GET requests to ListCreateAPIView should return list of objects. - """ - request = factory.get('/basic/') - response = self.list_view(request).render() - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data) - - def test_get_detail_view(self): - """ - GET requests to ListCreateAPIView should return list of objects. - """ - request = factory.get('/basic/1') - response = self.detail_view(request, pk=1).render() - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data[0]) - - -class TestManyToManyHyperlinkedView(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create 3 BasicModel instances. - """ - items = ['foo', 'bar', 'baz'] - anchors = [] - for item in items: - anchor = Anchor(text=item) - anchor.save() - anchors.append(anchor) - - manytomany = ManyToManyModel() - manytomany.save() - manytomany.rel.add(*anchors) - - self.data = [{ - 'url': 'http://testserver/manytomany/1/', - 'rel': [ - 'http://testserver/anchor/1/', - 'http://testserver/anchor/2/', - 'http://testserver/anchor/3/', - ] - }] - self.list_view = ManyToManyList.as_view() - self.detail_view = ManyToManyDetail.as_view() - - def test_get_list_view(self): - """ - GET requests to ListCreateAPIView should return list of objects. - """ - request = factory.get('/manytomany/') - response = self.list_view(request) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data) - - def test_get_detail_view(self): - """ - GET requests to ListCreateAPIView should return list of objects. - """ - request = factory.get('/manytomany/1/') - response = self.detail_view(request, pk=1) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data[0]) - - -class TestHyperlinkedIdentityFieldLookup(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create 3 Album instances. - """ - titles = ['foo', 'bar', 'baz'] - for title in titles: - album = Album(title=title) - album.save() - self.detail_view = AlbumDetail.as_view() - self.data = { - 'foo': {'title': 'foo', 'url': 'http://testserver/albums/foo/'}, - 'bar': {'title': 'bar', 'url': 'http://testserver/albums/bar/'}, - 'baz': {'title': 'baz', 'url': 'http://testserver/albums/baz/'} - } - - def test_lookup_field(self): - """ - GET requests to AlbumDetail view should return serialized Albums - with a url field keyed by `title`. - """ - for album in Album.objects.all(): - request = factory.get('/albums/{0}/'.format(album.title)) - response = self.detail_view(request, title=album.title) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data[album.title]) - - -class TestCreateWithForeignKeys(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create a blog post - """ - self.post = BlogPost.objects.create(title="Test post") - self.create_view = BlogPostCommentListCreate.as_view() - - def test_create_comment(self): - - data = { - 'text': 'A test comment', - 'blog_post_url': 'http://testserver/posts/1/' - } - - request = factory.post('/comments/', data=data) - response = self.create_view(request) - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertEqual(response['Location'], 'http://testserver/comments/1/') - self.assertEqual(self.post.blogpostcomment_set.count(), 1) - self.assertEqual(self.post.blogpostcomment_set.all()[0].text, 'A test comment') - - -class TestCreateWithForeignKeysAndCustomSlug(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create an Album - """ - self.post = Album.objects.create(title='test-album') - self.list_create_view = PhotoListCreate.as_view() - - def test_create_photo(self): - - data = { - 'description': 'A test photo', - 'album_url': 'http://testserver/albums/test-album/' - } - - request = factory.post('/photos/', data=data) - response = self.list_create_view(request) - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertNotIn('Location', response, msg='Location should only be included if there is a "url" field on the serializer') - self.assertEqual(self.post.photo_set.count(), 1) - self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo') - - -class TestOptionalRelationHyperlinkedView(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - """ - Create 1 OptionalRelationModel instances. - """ - OptionalRelationModel().save() - self.objects = OptionalRelationModel.objects - self.detail_view = OptionalRelationDetail.as_view() - self.data = {"url": "http://testserver/optionalrelation/1/", "other": None} - - def test_get_detail_view(self): - """ - GET requests to RetrieveAPIView with optional relations should return None - for non existing relations. - """ - request = factory.get('/optionalrelationmodel-detail/1') - response = self.detail_view(request, pk=1) - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data, self.data) - - def test_put_detail_view(self): - """ - PUT requests to RetrieveUpdateDestroyAPIView with optional relations - should accept None for non existing relations. - """ - response = self.client.put('/optionalrelation/1/', - data=json.dumps(self.data), - content_type='application/json') - self.assertEqual(response.status_code, status.HTTP_200_OK) - - -class TestOverriddenURLField(TestCase): - def setUp(self): - class OverriddenURLSerializer(serializers.HyperlinkedModelSerializer): - url = serializers.SerializerMethodField('get_url') - - class Meta: - model = BlogPost - fields = ('title', 'url') - - def get_url(self, obj): - return 'foo bar' - - self.Serializer = OverriddenURLSerializer - self.obj = BlogPost.objects.create(title='New blog post') - - def test_overridden_url_field(self): - """ - The 'url' field should respect overriding. - Regression test for #936. - """ - serializer = self.Serializer(self.obj) - self.assertEqual( - serializer.data, - {'title': 'New blog post', 'url': 'foo bar'} - ) - - -class TestURLFieldNameBySettings(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - self.saved_url_field_name = api_settings.URL_FIELD_NAME - api_settings.URL_FIELD_NAME = 'global_url_field' - - class Serializer(serializers.HyperlinkedModelSerializer): - - class Meta: - model = BlogPost - fields = ('title', api_settings.URL_FIELD_NAME) - - self.Serializer = Serializer - self.obj = BlogPost.objects.create(title="New blog post") - - def tearDown(self): - api_settings.URL_FIELD_NAME = self.saved_url_field_name - - def test_overridden_url_field_name(self): - request = factory.get('/posts/') - serializer = self.Serializer(self.obj, context={'request': request}) - self.assertIn(api_settings.URL_FIELD_NAME, serializer.data) - - -class TestURLFieldNameByOptions(TestCase): - urls = 'tests.test_hyperlinkedserializers' - - def setUp(self): - class Serializer(serializers.HyperlinkedModelSerializer): - - class Meta: - model = BlogPost - fields = ('title', 'serializer_url_field') - url_field_name = 'serializer_url_field' - - self.Serializer = Serializer - self.obj = BlogPost.objects.create(title="New blog post") - - def test_overridden_url_field_name(self): - request = factory.get('/posts/') - serializer = self.Serializer(self.obj, context={'request': request}) - self.assertIn(self.Serializer.Meta.url_field_name, serializer.data) +# urlpatterns = patterns( +# '', +# url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'), +# url(r'^basic/(?P<pk>\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'), +# url(r'^anchor/(?P<pk>\d+)/$', AnchorDetail.as_view(), name='anchor-detail'), +# url(r'^manytomany/$', ManyToManyList.as_view(), name='manytomanymodel-list'), +# url(r'^manytomany/(?P<pk>\d+)/$', ManyToManyDetail.as_view(), name='manytomanymodel-detail'), +# url(r'^posts/(?P<pk>\d+)/$', BlogPostDetail.as_view(), name='blogpost-detail'), +# url(r'^comments/$', BlogPostCommentListCreate.as_view(), name='blogpostcomment-list'), +# url(r'^comments/(?P<pk>\d+)/$', BlogPostCommentDetail.as_view(), name='blogpostcomment-detail'), +# url(r'^albums/(?P<title>\w[\w-]*)/$', AlbumDetail.as_view(), name='album-detail'), +# url(r'^photos/$', PhotoListCreate.as_view(), name='photo-list'), +# url(r'^optionalrelation/(?P<pk>\d+)/$', OptionalRelationDetail.as_view(), name='optionalrelationmodel-detail'), +# ) + + +# class TestBasicHyperlinkedView(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create 3 BasicModel instances. +# """ +# items = ['foo', 'bar', 'baz'] +# for item in items: +# BasicModel(text=item).save() +# self.objects = BasicModel.objects +# self.data = [ +# {'url': 'http://testserver/basic/%d/' % obj.id, 'text': obj.text} +# for obj in self.objects.all() +# ] +# self.list_view = BasicList.as_view() +# self.detail_view = BasicDetail.as_view() + +# def test_get_list_view(self): +# """ +# GET requests to ListCreateAPIView should return list of objects. +# """ +# request = factory.get('/basic/') +# response = self.list_view(request).render() +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data) + +# def test_get_detail_view(self): +# """ +# GET requests to ListCreateAPIView should return list of objects. +# """ +# request = factory.get('/basic/1') +# response = self.detail_view(request, pk=1).render() +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data[0]) + + +# class TestManyToManyHyperlinkedView(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create 3 BasicModel instances. +# """ +# items = ['foo', 'bar', 'baz'] +# anchors = [] +# for item in items: +# anchor = Anchor(text=item) +# anchor.save() +# anchors.append(anchor) + +# manytomany = ManyToManyModel() +# manytomany.save() +# manytomany.rel.add(*anchors) + +# self.data = [{ +# 'url': 'http://testserver/manytomany/1/', +# 'rel': [ +# 'http://testserver/anchor/1/', +# 'http://testserver/anchor/2/', +# 'http://testserver/anchor/3/', +# ] +# }] +# self.list_view = ManyToManyList.as_view() +# self.detail_view = ManyToManyDetail.as_view() + +# def test_get_list_view(self): +# """ +# GET requests to ListCreateAPIView should return list of objects. +# """ +# request = factory.get('/manytomany/') +# response = self.list_view(request) +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data) + +# def test_get_detail_view(self): +# """ +# GET requests to ListCreateAPIView should return list of objects. +# """ +# request = factory.get('/manytomany/1/') +# response = self.detail_view(request, pk=1) +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data[0]) + + +# class TestHyperlinkedIdentityFieldLookup(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create 3 Album instances. +# """ +# titles = ['foo', 'bar', 'baz'] +# for title in titles: +# album = Album(title=title) +# album.save() +# self.detail_view = AlbumDetail.as_view() +# self.data = { +# 'foo': {'title': 'foo', 'url': 'http://testserver/albums/foo/'}, +# 'bar': {'title': 'bar', 'url': 'http://testserver/albums/bar/'}, +# 'baz': {'title': 'baz', 'url': 'http://testserver/albums/baz/'} +# } + +# def test_lookup_field(self): +# """ +# GET requests to AlbumDetail view should return serialized Albums +# with a url field keyed by `title`. +# """ +# for album in Album.objects.all(): +# request = factory.get('/albums/{0}/'.format(album.title)) +# response = self.detail_view(request, title=album.title) +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data[album.title]) + + +# class TestCreateWithForeignKeys(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create a blog post +# """ +# self.post = BlogPost.objects.create(title="Test post") +# self.create_view = BlogPostCommentListCreate.as_view() + +# def test_create_comment(self): + +# data = { +# 'text': 'A test comment', +# 'blog_post_url': 'http://testserver/posts/1/' +# } + +# request = factory.post('/comments/', data=data) +# response = self.create_view(request) +# self.assertEqual(response.status_code, status.HTTP_201_CREATED) +# self.assertEqual(response['Location'], 'http://testserver/comments/1/') +# self.assertEqual(self.post.blogpostcomment_set.count(), 1) +# self.assertEqual(self.post.blogpostcomment_set.all()[0].text, 'A test comment') + + +# class TestCreateWithForeignKeysAndCustomSlug(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create an Album +# """ +# self.post = Album.objects.create(title='test-album') +# self.list_create_view = PhotoListCreate.as_view() + +# def test_create_photo(self): + +# data = { +# 'description': 'A test photo', +# 'album_url': 'http://testserver/albums/test-album/' +# } + +# request = factory.post('/photos/', data=data) +# response = self.list_create_view(request) +# self.assertEqual(response.status_code, status.HTTP_201_CREATED) +# self.assertNotIn('Location', response, msg='Location should only be included if there is a "url" field on the serializer') +# self.assertEqual(self.post.photo_set.count(), 1) +# self.assertEqual(self.post.photo_set.all()[0].description, 'A test photo') + + +# class TestOptionalRelationHyperlinkedView(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# """ +# Create 1 OptionalRelationModel instances. +# """ +# OptionalRelationModel().save() +# self.objects = OptionalRelationModel.objects +# self.detail_view = OptionalRelationDetail.as_view() +# self.data = {"url": "http://testserver/optionalrelation/1/", "other": None} + +# def test_get_detail_view(self): +# """ +# GET requests to RetrieveAPIView with optional relations should return None +# for non existing relations. +# """ +# request = factory.get('/optionalrelationmodel-detail/1') +# response = self.detail_view(request, pk=1) +# self.assertEqual(response.status_code, status.HTTP_200_OK) +# self.assertEqual(response.data, self.data) + +# def test_put_detail_view(self): +# """ +# PUT requests to RetrieveUpdateDestroyAPIView with optional relations +# should accept None for non existing relations. +# """ +# response = self.client.put('/optionalrelation/1/', +# data=json.dumps(self.data), +# content_type='application/json') +# self.assertEqual(response.status_code, status.HTTP_200_OK) + + +# class TestOverriddenURLField(TestCase): +# def setUp(self): +# class OverriddenURLSerializer(serializers.HyperlinkedModelSerializer): +# url = serializers.SerializerMethodField('get_url') + +# class Meta: +# model = BlogPost +# fields = ('title', 'url') + +# def get_url(self, obj): +# return 'foo bar' + +# self.Serializer = OverriddenURLSerializer +# self.obj = BlogPost.objects.create(title='New blog post') + +# def test_overridden_url_field(self): +# """ +# The 'url' field should respect overriding. +# Regression test for #936. +# """ +# serializer = self.Serializer(self.obj) +# self.assertEqual( +# serializer.data, +# {'title': 'New blog post', 'url': 'foo bar'} +# ) + + +# class TestURLFieldNameBySettings(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# self.saved_url_field_name = api_settings.URL_FIELD_NAME +# api_settings.URL_FIELD_NAME = 'global_url_field' + +# class Serializer(serializers.HyperlinkedModelSerializer): + +# class Meta: +# model = BlogPost +# fields = ('title', api_settings.URL_FIELD_NAME) + +# self.Serializer = Serializer +# self.obj = BlogPost.objects.create(title="New blog post") + +# def tearDown(self): +# api_settings.URL_FIELD_NAME = self.saved_url_field_name + +# def test_overridden_url_field_name(self): +# request = factory.get('/posts/') +# serializer = self.Serializer(self.obj, context={'request': request}) +# self.assertIn(api_settings.URL_FIELD_NAME, serializer.data) + + +# class TestURLFieldNameByOptions(TestCase): +# urls = 'tests.test_hyperlinkedserializers' + +# def setUp(self): +# class Serializer(serializers.HyperlinkedModelSerializer): + +# class Meta: +# model = BlogPost +# fields = ('title', 'serializer_url_field') +# url_field_name = 'serializer_url_field' + +# self.Serializer = Serializer +# self.obj = BlogPost.objects.create(title="New blog post") + +# def test_overridden_url_field_name(self): +# request = factory.get('/posts/') +# serializer = self.Serializer(self.obj, context={'request': request}) +# self.assertIn(self.Serializer.Meta.url_field_name, serializer.data) diff --git a/tests/test_nullable_fields.py b/tests/test_nullable_fields.py index 8d0c84bb0..9843182ad 100644 --- a/tests/test_nullable_fields.py +++ b/tests/test_nullable_fields.py @@ -1,39 +1,39 @@ -from django.core.urlresolvers import reverse +# from django.core.urlresolvers import reverse -from django.conf.urls import patterns, url -from rest_framework import serializers, generics -from rest_framework.test import APITestCase -from tests.models import NullableForeignKeySource +# from django.conf.urls import patterns, url +# from rest_framework import serializers, generics +# from rest_framework.test import APITestCase +# from tests.models import NullableForeignKeySource -class NullableFKSourceSerializer(serializers.ModelSerializer): - class Meta: - model = NullableForeignKeySource +# class NullableFKSourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = NullableForeignKeySource -class NullableFKSourceDetail(generics.RetrieveUpdateDestroyAPIView): - queryset = NullableForeignKeySource.objects.all() - serializer_class = NullableFKSourceSerializer +# class NullableFKSourceDetail(generics.RetrieveUpdateDestroyAPIView): +# queryset = NullableForeignKeySource.objects.all() +# serializer_class = NullableFKSourceSerializer -urlpatterns = patterns( - '', - url(r'^objects/(?P<pk>\d+)/$', NullableFKSourceDetail.as_view(), name='object-detail'), -) +# urlpatterns = patterns( +# '', +# url(r'^objects/(?P<pk>\d+)/$', NullableFKSourceDetail.as_view(), name='object-detail'), +# ) -class NullableForeignKeyTests(APITestCase): - """ - DRF should be able to handle nullable foreign keys when a test - Client POST/PUT request is made with its own serialized object. - """ - urls = 'tests.test_nullable_fields' +# class NullableForeignKeyTests(APITestCase): +# """ +# DRF should be able to handle nullable foreign keys when a test +# Client POST/PUT request is made with its own serialized object. +# """ +# urls = 'tests.test_nullable_fields' - def test_updating_object_with_null_fk(self): - obj = NullableForeignKeySource(name='example', target=None) - obj.save() - serialized_data = NullableFKSourceSerializer(obj).data +# def test_updating_object_with_null_fk(self): +# obj = NullableForeignKeySource(name='example', target=None) +# obj.save() +# serialized_data = NullableFKSourceSerializer(obj).data - response = self.client.put(reverse('object-detail', args=[obj.pk]), serialized_data) +# response = self.client.put(reverse('object-detail', args=[obj.pk]), serialized_data) - self.assertEqual(response.data, serialized_data) +# self.assertEqual(response.data, serialized_data) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 8f9e0005e..2e56d9709 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -391,10 +391,10 @@ class CustomField(serializers.Field): class BasicModelSerializer(serializers.Serializer): text = CustomField() - def __init__(self, *args, **kwargs): - super(BasicModelSerializer, self).__init__(*args, **kwargs) + def to_native(self, value): if 'view' not in self.context: - raise RuntimeError("context isn't getting passed into serializer init") + raise RuntimeError("context isn't getting passed into serializer") + return super(BasicSerializer, self).to_native(value) class TestContextPassedToCustomField(TestCase): @@ -423,7 +423,7 @@ class LinksSerializer(serializers.Serializer): class CustomPaginationSerializer(pagination.BasePaginationSerializer): links = LinksSerializer(source='*') # Takes the page object as the source - total_results = serializers.Field(source='paginator.count') + total_results = serializers.ReadOnlyField(source='paginator.count') results_field = 'objects' diff --git a/tests/test_permissions.py b/tests/test_permissions.py index b90ba4f19..d5568c551 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -108,59 +108,59 @@ class ModelPermissionsIntegrationTests(TestCase): response = instance_view(request, pk='2') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - def test_options_permitted(self): - request = factory.options( - '/', - HTTP_AUTHORIZATION=self.permitted_credentials - ) - response = root_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertIn('actions', response.data) - self.assertEqual(list(response.data['actions'].keys()), ['POST']) + # def test_options_permitted(self): + # request = factory.options( + # '/', + # HTTP_AUTHORIZATION=self.permitted_credentials + # ) + # response = root_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertIn('actions', response.data) + # self.assertEqual(list(response.data['actions'].keys()), ['POST']) - request = factory.options( - '/1', - HTTP_AUTHORIZATION=self.permitted_credentials - ) - response = instance_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertIn('actions', response.data) - self.assertEqual(list(response.data['actions'].keys()), ['PUT']) + # request = factory.options( + # '/1', + # HTTP_AUTHORIZATION=self.permitted_credentials + # ) + # response = instance_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertIn('actions', response.data) + # self.assertEqual(list(response.data['actions'].keys()), ['PUT']) - def test_options_disallowed(self): - request = factory.options( - '/', - HTTP_AUTHORIZATION=self.disallowed_credentials - ) - response = root_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotIn('actions', response.data) + # def test_options_disallowed(self): + # request = factory.options( + # '/', + # HTTP_AUTHORIZATION=self.disallowed_credentials + # ) + # response = root_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertNotIn('actions', response.data) - request = factory.options( - '/1', - HTTP_AUTHORIZATION=self.disallowed_credentials - ) - response = instance_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotIn('actions', response.data) + # request = factory.options( + # '/1', + # HTTP_AUTHORIZATION=self.disallowed_credentials + # ) + # response = instance_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertNotIn('actions', response.data) - def test_options_updateonly(self): - request = factory.options( - '/', - HTTP_AUTHORIZATION=self.updateonly_credentials - ) - response = root_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertNotIn('actions', response.data) + # def test_options_updateonly(self): + # request = factory.options( + # '/', + # HTTP_AUTHORIZATION=self.updateonly_credentials + # ) + # response = root_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertNotIn('actions', response.data) - request = factory.options( - '/1', - HTTP_AUTHORIZATION=self.updateonly_credentials - ) - response = instance_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertIn('actions', response.data) - self.assertEqual(list(response.data['actions'].keys()), ['PUT']) + # request = factory.options( + # '/1', + # HTTP_AUTHORIZATION=self.updateonly_credentials + # ) + # response = instance_view(request, pk='1') + # self.assertEqual(response.status_code, status.HTTP_200_OK) + # self.assertIn('actions', response.data) + # self.assertEqual(list(response.data['actions'].keys()), ['PUT']) class BasicPermModel(models.Model): diff --git a/tests/test_relations.py b/tests/test_relations.py index bc1db69fc..a30b12e68 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -1,149 +1,149 @@ -""" -General tests for relational fields. -""" -from __future__ import unicode_literals -from django import get_version -from django.db import models -from django.test import TestCase -from django.utils import unittest -from rest_framework import serializers -from tests.models import BlogPost +# """ +# General tests for relational fields. +# """ +# from __future__ import unicode_literals +# from django import get_version +# from django.db import models +# from django.test import TestCase +# from django.utils import unittest +# from rest_framework import serializers +# from tests.models import BlogPost -class NullModel(models.Model): - pass +# class NullModel(models.Model): +# pass -class FieldTests(TestCase): - def test_pk_related_field_with_empty_string(self): - """ - Regression test for #446 +# class FieldTests(TestCase): +# def test_pk_related_field_with_empty_string(self): +# """ +# Regression test for #446 - https://github.com/tomchristie/django-rest-framework/issues/446 - """ - field = serializers.PrimaryKeyRelatedField(queryset=NullModel.objects.all()) - self.assertRaises(serializers.ValidationError, field.from_native, '') - self.assertRaises(serializers.ValidationError, field.from_native, []) +# https://github.com/tomchristie/django-rest-framework/issues/446 +# """ +# field = serializers.PrimaryKeyRelatedField(queryset=NullModel.objects.all()) +# self.assertRaises(serializers.ValidationError, field.from_native, '') +# self.assertRaises(serializers.ValidationError, field.from_native, []) - def test_hyperlinked_related_field_with_empty_string(self): - field = serializers.HyperlinkedRelatedField(queryset=NullModel.objects.all(), view_name='') - self.assertRaises(serializers.ValidationError, field.from_native, '') - self.assertRaises(serializers.ValidationError, field.from_native, []) +# def test_hyperlinked_related_field_with_empty_string(self): +# field = serializers.HyperlinkedRelatedField(queryset=NullModel.objects.all(), view_name='') +# self.assertRaises(serializers.ValidationError, field.from_native, '') +# self.assertRaises(serializers.ValidationError, field.from_native, []) - def test_slug_related_field_with_empty_string(self): - field = serializers.SlugRelatedField(queryset=NullModel.objects.all(), slug_field='pk') - self.assertRaises(serializers.ValidationError, field.from_native, '') - self.assertRaises(serializers.ValidationError, field.from_native, []) +# def test_slug_related_field_with_empty_string(self): +# field = serializers.SlugRelatedField(queryset=NullModel.objects.all(), slug_field='pk') +# self.assertRaises(serializers.ValidationError, field.from_native, '') +# self.assertRaises(serializers.ValidationError, field.from_native, []) -class TestManyRelatedMixin(TestCase): - def test_missing_many_to_many_related_field(self): - ''' - Regression test for #632 +# class TestManyRelatedMixin(TestCase): +# def test_missing_many_to_many_related_field(self): +# ''' +# Regression test for #632 - https://github.com/tomchristie/django-rest-framework/pull/632 - ''' - field = serializers.RelatedField(many=True, read_only=False) +# https://github.com/tomchristie/django-rest-framework/pull/632 +# ''' +# field = serializers.RelatedField(many=True, read_only=False) - into = {} - field.field_from_native({}, None, 'field_name', into) - self.assertEqual(into['field_name'], []) +# into = {} +# field.field_from_native({}, None, 'field_name', into) +# self.assertEqual(into['field_name'], []) -# Regression tests for #694 (`source` attribute on related fields) +# # Regression tests for #694 (`source` attribute on related fields) -class RelatedFieldSourceTests(TestCase): - def test_related_manager_source(self): - """ - Relational fields should be able to use manager-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.RelatedField(many=True, source='get_blogposts_manager') +# class RelatedFieldSourceTests(TestCase): +# def test_related_manager_source(self): +# """ +# Relational fields should be able to use manager-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.RelatedField(many=True, source='get_blogposts_manager') - class ClassWithManagerMethod(object): - def get_blogposts_manager(self): - return BlogPost.objects +# class ClassWithManagerMethod(object): +# def get_blogposts_manager(self): +# return BlogPost.objects - obj = ClassWithManagerMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['BlogPost object']) +# obj = ClassWithManagerMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['BlogPost object']) - def test_related_queryset_source(self): - """ - Relational fields should be able to use queryset-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.RelatedField(many=True, source='get_blogposts_queryset') +# def test_related_queryset_source(self): +# """ +# Relational fields should be able to use queryset-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.RelatedField(many=True, source='get_blogposts_queryset') - class ClassWithQuerysetMethod(object): - def get_blogposts_queryset(self): - return BlogPost.objects.all() +# class ClassWithQuerysetMethod(object): +# def get_blogposts_queryset(self): +# return BlogPost.objects.all() - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['BlogPost object']) +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['BlogPost object']) - def test_dotted_source(self): - """ - Source argument should support dotted.source notation. - """ - BlogPost.objects.create(title='blah') - field = serializers.RelatedField(many=True, source='a.b.c') +# def test_dotted_source(self): +# """ +# Source argument should support dotted.source notation. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.RelatedField(many=True, source='a.b.c') - class ClassWithQuerysetMethod(object): - a = { - 'b': { - 'c': BlogPost.objects.all() - } - } +# class ClassWithQuerysetMethod(object): +# a = { +# 'b': { +# 'c': BlogPost.objects.all() +# } +# } - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['BlogPost object']) +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['BlogPost object']) - # Regression for #1129 - def test_exception_for_incorect_fk(self): - """ - Check that the exception message are correct if the source field - doesn't exist. - """ - from tests.models import ManyToManySource +# # Regression for #1129 +# def test_exception_for_incorect_fk(self): +# """ +# Check that the exception message are correct if the source field +# doesn't exist. +# """ +# from tests.models import ManyToManySource - class Meta: - model = ManyToManySource +# class Meta: +# model = ManyToManySource - attrs = { - 'name': serializers.SlugRelatedField( - slug_field='name', source='banzai'), - 'Meta': Meta, - } +# attrs = { +# 'name': serializers.SlugRelatedField( +# slug_field='name', source='banzai'), +# 'Meta': Meta, +# } - TestSerializer = type( - str('TestSerializer'), - (serializers.ModelSerializer,), - attrs - ) - with self.assertRaises(AttributeError): - TestSerializer(data={'name': 'foo'}) +# TestSerializer = type( +# str('TestSerializer'), +# (serializers.ModelSerializer,), +# attrs +# ) +# with self.assertRaises(AttributeError): +# TestSerializer(data={'name': 'foo'}) -@unittest.skipIf(get_version() < '1.6.0', 'Upstream behaviour changed in v1.6') -class RelatedFieldChoicesTests(TestCase): - """ - Tests for #1408 "Web browseable API doesn't have blank option on drop down list box" - https://github.com/tomchristie/django-rest-framework/issues/1408 - """ - def test_blank_option_is_added_to_choice_if_required_equals_false(self): - """ +# @unittest.skipIf(get_version() < '1.6.0', 'Upstream behaviour changed in v1.6') +# class RelatedFieldChoicesTests(TestCase): +# """ +# Tests for #1408 "Web browseable API doesn't have blank option on drop down list box" +# https://github.com/tomchristie/django-rest-framework/issues/1408 +# """ +# def test_blank_option_is_added_to_choice_if_required_equals_false(self): +# """ - """ - post = BlogPost(title="Checking blank option is added") - post.save() +# """ +# post = BlogPost(title="Checking blank option is added") +# post.save() - queryset = BlogPost.objects.all() - field = serializers.RelatedField(required=False, queryset=queryset) +# queryset = BlogPost.objects.all() +# field = serializers.RelatedField(required=False, queryset=queryset) - choice_count = BlogPost.objects.count() - widget_count = len(field.widget.choices) +# choice_count = BlogPost.objects.count() +# widget_count = len(field.widget.choices) - self.assertEqual(widget_count, choice_count + 1, 'BLANK_CHOICE_DASH option should have been added') +# self.assertEqual(widget_count, choice_count + 1, 'BLANK_CHOICE_DASH option should have been added') diff --git a/tests/test_relations_hyperlink.py b/tests/test_relations_hyperlink.py index 0c8eb2544..315d1abf8 100644 --- a/tests/test_relations_hyperlink.py +++ b/tests/test_relations_hyperlink.py @@ -1,525 +1,525 @@ -from __future__ import unicode_literals -from django.conf.urls import patterns, url -from django.test import TestCase -from rest_framework import serializers -from rest_framework.test import APIRequestFactory -from tests.models import ( - BlogPost, - ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, - NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource -) - -factory = APIRequestFactory() -request = factory.get('/') # Just to ensure we have a request in the serializer context - - -def dummy_view(request, pk): - pass - -urlpatterns = patterns( - '', - url(r'^dummyurl/(?P<pk>[0-9]+)/$', dummy_view, name='dummy-url'), - url(r'^manytomanysource/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanysource-detail'), - url(r'^manytomanytarget/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanytarget-detail'), - url(r'^foreignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeysource-detail'), - url(r'^foreignkeytarget/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeytarget-detail'), - url(r'^nullableforeignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableforeignkeysource-detail'), - url(r'^onetoonetarget/(?P<pk>[0-9]+)/$', dummy_view, name='onetoonetarget-detail'), - url(r'^nullableonetoonesource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableonetoonesource-detail'), -) - - -# ManyToMany -class ManyToManyTargetSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ManyToManyTarget - fields = ('url', 'name', 'sources') - - -class ManyToManySourceSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ManyToManySource - fields = ('url', 'name', 'targets') - - -# ForeignKey -class ForeignKeyTargetSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ForeignKeyTarget - fields = ('url', 'name', 'sources') - - -class ForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ForeignKeySource - fields = ('url', 'name', 'target') - - -# Nullable ForeignKey -class NullableForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = NullableForeignKeySource - fields = ('url', 'name', 'target') - - -# Nullable OneToOne -class NullableOneToOneTargetSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = OneToOneTarget - fields = ('url', 'name', 'nullable_source') - - -# TODO: Add test that .data cannot be accessed prior to .is_valid - -class HyperlinkedManyToManyTests(TestCase): - urls = 'tests.test_relations_hyperlink' - - def setUp(self): - for idx in range(1, 4): - target = ManyToManyTarget(name='target-%d' % idx) - target.save() - source = ManyToManySource(name='source-%d' % idx) - source.save() - for target in ManyToManyTarget.objects.all(): - source.targets.add(target) - - def test_many_to_many_retrieve(self): - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']}, - {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, - {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_retrieve(self): - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']} - ] - self.assertEqual(serializer.data, expected) - - def test_many_to_many_update(self): - data = {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} - instance = ManyToManySource.objects.get(pk=1) - serializer = ManyToManySourceSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure source 1 is updated, and everything else is as expected - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}, - {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, - {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_update(self): - data = {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']} - instance = ManyToManyTarget.objects.get(pk=1) - serializer = ManyToManyTargetSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure target 1 is updated, and everything else is as expected - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']}, - {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']} - - ] - self.assertEqual(serializer.data, expected) - - def test_many_to_many_create(self): - data = {'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']} - serializer = ManyToManySourceSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is added, and everything else is as expected - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']}, - {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, - {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}, - {'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_create(self): - data = {'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']} - serializer = ManyToManyTargetSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-4') - - # Ensure target 4 is added, and everything else is as expected - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']}, - {'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']} - ] - self.assertEqual(serializer.data, expected) - - -class HyperlinkedForeignKeyTests(TestCase): - urls = 'tests.test_relations_hyperlink' - - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - new_target = ForeignKeyTarget(name='target-2') - new_target.save() - for idx in range(1, 4): - source = ForeignKeySource(name='source-%d' % idx, target=target) - source.save() - - def test_foreign_key_retrieve(self): - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_foreign_key_retrieve(self): - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']}, - {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update(self): - data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'}, - {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_incorrect_type(self): - data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 2} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected url string, received int.']}) - - def test_reverse_foreign_key_update(self): - data = {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']} - instance = ForeignKeyTarget.objects.get(pk=2) - serializer = ForeignKeyTargetSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - # We shouldn't have saved anything to the db yet since save - # hasn't been called. - queryset = ForeignKeyTarget.objects.all() - new_serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']}, - {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(new_serializer.data, expected) - - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure target 2 is update, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']}, - {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create(self): - data = {'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'} - serializer = ForeignKeySourceSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 1 is updated, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'}, - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_foreign_key_create(self): - data = {'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']} - serializer = ForeignKeyTargetSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-3') - - # Ensure target 4 is added, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']}, - {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, - {'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_invalid_null(self): - data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': None} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['This field is required.']}) - - -class HyperlinkedNullableForeignKeyTests(TestCase): - urls = 'tests.test_relations_hyperlink' - - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - for idx in range(1, 4): - if idx == 3: - target = None - source = NullableForeignKeySource(name='source-%d' % idx, target=target) - source.save() - - def test_foreign_key_retrieve_with_null(self): - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create_with_valid_null(self): - data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, - {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': ''} - expected_data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, expected_data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, - {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_valid_null(self): - data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}, - {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': ''} - expected_data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, expected_data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}, - {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, - {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, - ] - self.assertEqual(serializer.data, expected) - - # reverse foreign keys MUST be read_only - # In the general case they do not provide .remove() or .clear() - # and cannot be arbitrarily set. - - # def test_reverse_foreign_key_update(self): - # data = {'id': 1, 'name': 'target-1', 'sources': [1]} - # instance = ForeignKeyTarget.objects.get(pk=1) - # serializer = ForeignKeyTargetSerializer(instance, data=data) - # self.assertTrue(serializer.is_valid()) - # self.assertEqual(serializer.data, data) - # serializer.save() - - # # Ensure target 1 is updated, and everything else is as expected - # queryset = ForeignKeyTarget.objects.all() - # serializer = ForeignKeyTargetSerializer(queryset, many=True) - # expected = [ - # {'id': 1, 'name': 'target-1', 'sources': [1]}, - # {'id': 2, 'name': 'target-2', 'sources': []}, - # ] - # self.assertEqual(serializer.data, expected) - - -class HyperlinkedNullableOneToOneTests(TestCase): - urls = 'tests.test_relations_hyperlink' - - def setUp(self): - target = OneToOneTarget(name='target-1') - target.save() - new_target = OneToOneTarget(name='target-2') - new_target.save() - source = NullableOneToOneSource(name='source-1', target=target) - source.save() - - def test_reverse_foreign_key_retrieve_with_null(self): - queryset = OneToOneTarget.objects.all() - serializer = NullableOneToOneTargetSerializer(queryset, many=True, context={'request': request}) - expected = [ - {'url': 'http://testserver/onetoonetarget/1/', 'name': 'target-1', 'nullable_source': 'http://testserver/nullableonetoonesource/1/'}, - {'url': 'http://testserver/onetoonetarget/2/', 'name': 'target-2', 'nullable_source': None}, - ] - self.assertEqual(serializer.data, expected) - - -# Regression tests for #694 (`source` attribute on related fields) - -class HyperlinkedRelatedFieldSourceTests(TestCase): - urls = 'tests.test_relations_hyperlink' - - def test_related_manager_source(self): - """ - Relational fields should be able to use manager-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.HyperlinkedRelatedField( - many=True, - source='get_blogposts_manager', - view_name='dummy-url', - ) - field.context = {'request': request} - - class ClassWithManagerMethod(object): - def get_blogposts_manager(self): - return BlogPost.objects - - obj = ClassWithManagerMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['http://testserver/dummyurl/1/']) - - def test_related_queryset_source(self): - """ - Relational fields should be able to use queryset-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.HyperlinkedRelatedField( - many=True, - source='get_blogposts_queryset', - view_name='dummy-url', - ) - field.context = {'request': request} - - class ClassWithQuerysetMethod(object): - def get_blogposts_queryset(self): - return BlogPost.objects.all() - - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['http://testserver/dummyurl/1/']) - - def test_dotted_source(self): - """ - Source argument should support dotted.source notation. - """ - BlogPost.objects.create(title='blah') - field = serializers.HyperlinkedRelatedField( - many=True, - source='a.b.c', - view_name='dummy-url', - ) - field.context = {'request': request} - - class ClassWithQuerysetMethod(object): - a = { - 'b': { - 'c': BlogPost.objects.all() - } - } - - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, ['http://testserver/dummyurl/1/']) +# from __future__ import unicode_literals +# from django.conf.urls import patterns, url +# from django.test import TestCase +# from rest_framework import serializers +# from rest_framework.test import APIRequestFactory +# from tests.models import ( +# BlogPost, +# ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, +# NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource +# ) + +# factory = APIRequestFactory() +# request = factory.get('/') # Just to ensure we have a request in the serializer context + + +# def dummy_view(request, pk): +# pass + +# urlpatterns = patterns( +# '', +# url(r'^dummyurl/(?P<pk>[0-9]+)/$', dummy_view, name='dummy-url'), +# url(r'^manytomanysource/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanysource-detail'), +# url(r'^manytomanytarget/(?P<pk>[0-9]+)/$', dummy_view, name='manytomanytarget-detail'), +# url(r'^foreignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeysource-detail'), +# url(r'^foreignkeytarget/(?P<pk>[0-9]+)/$', dummy_view, name='foreignkeytarget-detail'), +# url(r'^nullableforeignkeysource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableforeignkeysource-detail'), +# url(r'^onetoonetarget/(?P<pk>[0-9]+)/$', dummy_view, name='onetoonetarget-detail'), +# url(r'^nullableonetoonesource/(?P<pk>[0-9]+)/$', dummy_view, name='nullableonetoonesource-detail'), +# ) + + +# # ManyToMany +# class ManyToManyTargetSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ManyToManyTarget +# fields = ('url', 'name', 'sources') + + +# class ManyToManySourceSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ManyToManySource +# fields = ('url', 'name', 'targets') + + +# # ForeignKey +# class ForeignKeyTargetSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ForeignKeyTarget +# fields = ('url', 'name', 'sources') + + +# class ForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ForeignKeySource +# fields = ('url', 'name', 'target') + + +# # Nullable ForeignKey +# class NullableForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = NullableForeignKeySource +# fields = ('url', 'name', 'target') + + +# # Nullable OneToOne +# class NullableOneToOneTargetSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = OneToOneTarget +# fields = ('url', 'name', 'nullable_source') + + +# # TODO: Add test that .data cannot be accessed prior to .is_valid + +# class HyperlinkedManyToManyTests(TestCase): +# urls = 'tests.test_relations_hyperlink' + +# def setUp(self): +# for idx in range(1, 4): +# target = ManyToManyTarget(name='target-%d' % idx) +# target.save() +# source = ManyToManySource(name='source-%d' % idx) +# source.save() +# for target in ManyToManyTarget.objects.all(): +# source.targets.add(target) + +# def test_many_to_many_retrieve(self): +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']}, +# {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, +# {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_retrieve(self): +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_many_to_many_update(self): +# data = {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} +# instance = ManyToManySource.objects.get(pk=1) +# serializer = ManyToManySourceSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}, +# {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, +# {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_update(self): +# data = {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']} +# instance = ManyToManyTarget.objects.get(pk=1) +# serializer = ManyToManyTargetSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure target 1 is updated, and everything else is as expected +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/']}, +# {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']} + +# ] +# self.assertEqual(serializer.data, expected) + +# def test_many_to_many_create(self): +# data = {'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']} +# serializer = ManyToManySourceSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is added, and everything else is as expected +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanysource/1/', 'name': 'source-1', 'targets': ['http://testserver/manytomanytarget/1/']}, +# {'url': 'http://testserver/manytomanysource/2/', 'name': 'source-2', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/']}, +# {'url': 'http://testserver/manytomanysource/3/', 'name': 'source-3', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/2/', 'http://testserver/manytomanytarget/3/']}, +# {'url': 'http://testserver/manytomanysource/4/', 'name': 'source-4', 'targets': ['http://testserver/manytomanytarget/1/', 'http://testserver/manytomanytarget/3/']} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_create(self): +# data = {'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']} +# serializer = ManyToManyTargetSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-4') + +# # Ensure target 4 is added, and everything else is as expected +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/manytomanytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/manytomanysource/2/', 'http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/manytomanysource/3/']}, +# {'url': 'http://testserver/manytomanytarget/4/', 'name': 'target-4', 'sources': ['http://testserver/manytomanysource/1/', 'http://testserver/manytomanysource/3/']} +# ] +# self.assertEqual(serializer.data, expected) + + +# class HyperlinkedForeignKeyTests(TestCase): +# urls = 'tests.test_relations_hyperlink' + +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# new_target = ForeignKeyTarget(name='target-2') +# new_target.save() +# for idx in range(1, 4): +# source = ForeignKeySource(name='source-%d' % idx, target=target) +# source.save() + +# def test_foreign_key_retrieve(self): +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_foreign_key_retrieve(self): +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']}, +# {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update(self): +# data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/2/'}, +# {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_incorrect_type(self): +# data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 2} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected url string, received int.']}) + +# def test_reverse_foreign_key_update(self): +# data = {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']} +# instance = ForeignKeyTarget.objects.get(pk=2) +# serializer = ForeignKeyTargetSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# # We shouldn't have saved anything to the db yet since save +# # hasn't been called. +# queryset = ForeignKeyTarget.objects.all() +# new_serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/2/', 'http://testserver/foreignkeysource/3/']}, +# {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(new_serializer.data, expected) + +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure target 2 is update, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']}, +# {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create(self): +# data = {'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'} +# serializer = ForeignKeySourceSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/3/', 'name': 'source-3', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/foreignkeysource/4/', 'name': 'source-4', 'target': 'http://testserver/foreignkeytarget/2/'}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_foreign_key_create(self): +# data = {'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']} +# serializer = ForeignKeyTargetSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-3') + +# # Ensure target 4 is added, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/foreignkeytarget/1/', 'name': 'target-1', 'sources': ['http://testserver/foreignkeysource/2/']}, +# {'url': 'http://testserver/foreignkeytarget/2/', 'name': 'target-2', 'sources': []}, +# {'url': 'http://testserver/foreignkeytarget/3/', 'name': 'target-3', 'sources': ['http://testserver/foreignkeysource/1/', 'http://testserver/foreignkeysource/3/']}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_invalid_null(self): +# data = {'url': 'http://testserver/foreignkeysource/1/', 'name': 'source-1', 'target': None} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data, context={'request': request}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['This field is required.']}) + + +# class HyperlinkedNullableForeignKeyTests(TestCase): +# urls = 'tests.test_relations_hyperlink' + +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# for idx in range(1, 4): +# if idx == 3: +# target = None +# source = NullableForeignKeySource(name='source-%d' % idx, target=target) +# source.save() + +# def test_foreign_key_retrieve_with_null(self): +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create_with_valid_null(self): +# data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, +# {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': ''} +# expected_data = {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, expected_data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, +# {'url': 'http://testserver/nullableforeignkeysource/4/', 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_valid_null(self): +# data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}, +# {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': ''} +# expected_data = {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data, context={'request': request}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, expected_data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/nullableforeignkeysource/1/', 'name': 'source-1', 'target': None}, +# {'url': 'http://testserver/nullableforeignkeysource/2/', 'name': 'source-2', 'target': 'http://testserver/foreignkeytarget/1/'}, +# {'url': 'http://testserver/nullableforeignkeysource/3/', 'name': 'source-3', 'target': None}, +# ] +# self.assertEqual(serializer.data, expected) + +# # reverse foreign keys MUST be read_only +# # In the general case they do not provide .remove() or .clear() +# # and cannot be arbitrarily set. + +# # def test_reverse_foreign_key_update(self): +# # data = {'id': 1, 'name': 'target-1', 'sources': [1]} +# # instance = ForeignKeyTarget.objects.get(pk=1) +# # serializer = ForeignKeyTargetSerializer(instance, data=data) +# # self.assertTrue(serializer.is_valid()) +# # self.assertEqual(serializer.data, data) +# # serializer.save() + +# # # Ensure target 1 is updated, and everything else is as expected +# # queryset = ForeignKeyTarget.objects.all() +# # serializer = ForeignKeyTargetSerializer(queryset, many=True) +# # expected = [ +# # {'id': 1, 'name': 'target-1', 'sources': [1]}, +# # {'id': 2, 'name': 'target-2', 'sources': []}, +# # ] +# # self.assertEqual(serializer.data, expected) + + +# class HyperlinkedNullableOneToOneTests(TestCase): +# urls = 'tests.test_relations_hyperlink' + +# def setUp(self): +# target = OneToOneTarget(name='target-1') +# target.save() +# new_target = OneToOneTarget(name='target-2') +# new_target.save() +# source = NullableOneToOneSource(name='source-1', target=target) +# source.save() + +# def test_reverse_foreign_key_retrieve_with_null(self): +# queryset = OneToOneTarget.objects.all() +# serializer = NullableOneToOneTargetSerializer(queryset, many=True, context={'request': request}) +# expected = [ +# {'url': 'http://testserver/onetoonetarget/1/', 'name': 'target-1', 'nullable_source': 'http://testserver/nullableonetoonesource/1/'}, +# {'url': 'http://testserver/onetoonetarget/2/', 'name': 'target-2', 'nullable_source': None}, +# ] +# self.assertEqual(serializer.data, expected) + + +# # Regression tests for #694 (`source` attribute on related fields) + +# class HyperlinkedRelatedFieldSourceTests(TestCase): +# urls = 'tests.test_relations_hyperlink' + +# def test_related_manager_source(self): +# """ +# Relational fields should be able to use manager-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.HyperlinkedRelatedField( +# many=True, +# source='get_blogposts_manager', +# view_name='dummy-url', +# ) +# field.context = {'request': request} + +# class ClassWithManagerMethod(object): +# def get_blogposts_manager(self): +# return BlogPost.objects + +# obj = ClassWithManagerMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['http://testserver/dummyurl/1/']) + +# def test_related_queryset_source(self): +# """ +# Relational fields should be able to use queryset-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.HyperlinkedRelatedField( +# many=True, +# source='get_blogposts_queryset', +# view_name='dummy-url', +# ) +# field.context = {'request': request} + +# class ClassWithQuerysetMethod(object): +# def get_blogposts_queryset(self): +# return BlogPost.objects.all() + +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['http://testserver/dummyurl/1/']) + +# def test_dotted_source(self): +# """ +# Source argument should support dotted.source notation. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.HyperlinkedRelatedField( +# many=True, +# source='a.b.c', +# view_name='dummy-url', +# ) +# field.context = {'request': request} + +# class ClassWithQuerysetMethod(object): +# a = { +# 'b': { +# 'c': BlogPost.objects.all() +# } +# } + +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, ['http://testserver/dummyurl/1/']) diff --git a/tests/test_relations_nested.py b/tests/test_relations_nested.py index 4d9da4896..4a99fee99 100644 --- a/tests/test_relations_nested.py +++ b/tests/test_relations_nested.py @@ -1,326 +1,326 @@ -from __future__ import unicode_literals -from django.db import models -from django.test import TestCase -from rest_framework import serializers +# from __future__ import unicode_literals +# from django.db import models +# from django.test import TestCase +# from rest_framework import serializers -from .models import OneToOneTarget +# from .models import OneToOneTarget -class OneToOneSource(models.Model): - name = models.CharField(max_length=100) - target = models.OneToOneField(OneToOneTarget, related_name='source', - null=True, blank=True) +# class OneToOneSource(models.Model): +# name = models.CharField(max_length=100) +# target = models.OneToOneField(OneToOneTarget, related_name='source', +# null=True, blank=True) -class OneToManyTarget(models.Model): - name = models.CharField(max_length=100) +# class OneToManyTarget(models.Model): +# name = models.CharField(max_length=100) -class OneToManySource(models.Model): - name = models.CharField(max_length=100) - target = models.ForeignKey(OneToManyTarget, related_name='sources') +# class OneToManySource(models.Model): +# name = models.CharField(max_length=100) +# target = models.ForeignKey(OneToManyTarget, related_name='sources') -class ReverseNestedOneToOneTests(TestCase): - def setUp(self): - class OneToOneSourceSerializer(serializers.ModelSerializer): - class Meta: - model = OneToOneSource - fields = ('id', 'name') +# class ReverseNestedOneToOneTests(TestCase): +# def setUp(self): +# class OneToOneSourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = OneToOneSource +# fields = ('id', 'name') - class OneToOneTargetSerializer(serializers.ModelSerializer): - source = OneToOneSourceSerializer() +# class OneToOneTargetSerializer(serializers.ModelSerializer): +# source = OneToOneSourceSerializer() - class Meta: - model = OneToOneTarget - fields = ('id', 'name', 'source') +# class Meta: +# model = OneToOneTarget +# fields = ('id', 'name', 'source') - self.Serializer = OneToOneTargetSerializer +# self.Serializer = OneToOneTargetSerializer - for idx in range(1, 4): - target = OneToOneTarget(name='target-%d' % idx) - target.save() - source = OneToOneSource(name='source-%d' % idx, target=target) - source.save() +# for idx in range(1, 4): +# target = OneToOneTarget(name='target-%d' % idx) +# target.save() +# source = OneToOneSource(name='source-%d' % idx, target=target) +# source.save() - def test_one_to_one_retrieve(self): - queryset = OneToOneTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, - {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, - {'id': 3, 'name': 'target-3', 'source': {'id': 3, 'name': 'source-3'}} - ] - self.assertEqual(serializer.data, expected) +# def test_one_to_one_retrieve(self): +# queryset = OneToOneTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, +# {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, +# {'id': 3, 'name': 'target-3', 'source': {'id': 3, 'name': 'source-3'}} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_one_create(self): - data = {'id': 4, 'name': 'target-4', 'source': {'id': 4, 'name': 'source-4'}} - serializer = self.Serializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-4') +# def test_one_to_one_create(self): +# data = {'id': 4, 'name': 'target-4', 'source': {'id': 4, 'name': 'source-4'}} +# serializer = self.Serializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-4') - # Ensure (target 4, target_source 4, source 4) are added, and - # everything else is as expected. - queryset = OneToOneTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, - {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, - {'id': 3, 'name': 'target-3', 'source': {'id': 3, 'name': 'source-3'}}, - {'id': 4, 'name': 'target-4', 'source': {'id': 4, 'name': 'source-4'}} - ] - self.assertEqual(serializer.data, expected) +# # Ensure (target 4, target_source 4, source 4) are added, and +# # everything else is as expected. +# queryset = OneToOneTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, +# {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, +# {'id': 3, 'name': 'target-3', 'source': {'id': 3, 'name': 'source-3'}}, +# {'id': 4, 'name': 'target-4', 'source': {'id': 4, 'name': 'source-4'}} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_one_create_with_invalid_data(self): - data = {'id': 4, 'name': 'target-4', 'source': {'id': 4}} - serializer = self.Serializer(data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'source': [{'name': ['This field is required.']}]}) +# def test_one_to_one_create_with_invalid_data(self): +# data = {'id': 4, 'name': 'target-4', 'source': {'id': 4}} +# serializer = self.Serializer(data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'source': [{'name': ['This field is required.']}]}) - def test_one_to_one_update(self): - data = {'id': 3, 'name': 'target-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}} - instance = OneToOneTarget.objects.get(pk=3) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-3-updated') +# def test_one_to_one_update(self): +# data = {'id': 3, 'name': 'target-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}} +# instance = OneToOneTarget.objects.get(pk=3) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-3-updated') - # Ensure (target 3, target_source 3, source 3) are updated, - # and everything else is as expected. - queryset = OneToOneTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, - {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, - {'id': 3, 'name': 'target-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}} - ] - self.assertEqual(serializer.data, expected) +# # Ensure (target 3, target_source 3, source 3) are updated, +# # and everything else is as expected. +# queryset = OneToOneTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, +# {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, +# {'id': 3, 'name': 'target-3-updated', 'source': {'id': 3, 'name': 'source-3-updated'}} +# ] +# self.assertEqual(serializer.data, expected) -class ForwardNestedOneToOneTests(TestCase): - def setUp(self): - class OneToOneTargetSerializer(serializers.ModelSerializer): - class Meta: - model = OneToOneTarget - fields = ('id', 'name') +# class ForwardNestedOneToOneTests(TestCase): +# def setUp(self): +# class OneToOneTargetSerializer(serializers.ModelSerializer): +# class Meta: +# model = OneToOneTarget +# fields = ('id', 'name') - class OneToOneSourceSerializer(serializers.ModelSerializer): - target = OneToOneTargetSerializer() +# class OneToOneSourceSerializer(serializers.ModelSerializer): +# target = OneToOneTargetSerializer() - class Meta: - model = OneToOneSource - fields = ('id', 'name', 'target') +# class Meta: +# model = OneToOneSource +# fields = ('id', 'name', 'target') - self.Serializer = OneToOneSourceSerializer +# self.Serializer = OneToOneSourceSerializer - for idx in range(1, 4): - target = OneToOneTarget(name='target-%d' % idx) - target.save() - source = OneToOneSource(name='source-%d' % idx, target=target) - source.save() +# for idx in range(1, 4): +# target = OneToOneTarget(name='target-%d' % idx) +# target.save() +# source = OneToOneSource(name='source-%d' % idx, target=target) +# source.save() - def test_one_to_one_retrieve(self): - queryset = OneToOneSource.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, - {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, - {'id': 3, 'name': 'source-3', 'target': {'id': 3, 'name': 'target-3'}} - ] - self.assertEqual(serializer.data, expected) +# def test_one_to_one_retrieve(self): +# queryset = OneToOneSource.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, +# {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, +# {'id': 3, 'name': 'source-3', 'target': {'id': 3, 'name': 'target-3'}} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_one_create(self): - data = {'id': 4, 'name': 'source-4', 'target': {'id': 4, 'name': 'target-4'}} - serializer = self.Serializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') +# def test_one_to_one_create(self): +# data = {'id': 4, 'name': 'source-4', 'target': {'id': 4, 'name': 'target-4'}} +# serializer = self.Serializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') - # Ensure (target 4, target_source 4, source 4) are added, and - # everything else is as expected. - queryset = OneToOneSource.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, - {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, - {'id': 3, 'name': 'source-3', 'target': {'id': 3, 'name': 'target-3'}}, - {'id': 4, 'name': 'source-4', 'target': {'id': 4, 'name': 'target-4'}} - ] - self.assertEqual(serializer.data, expected) +# # Ensure (target 4, target_source 4, source 4) are added, and +# # everything else is as expected. +# queryset = OneToOneSource.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, +# {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, +# {'id': 3, 'name': 'source-3', 'target': {'id': 3, 'name': 'target-3'}}, +# {'id': 4, 'name': 'source-4', 'target': {'id': 4, 'name': 'target-4'}} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_one_create_with_invalid_data(self): - data = {'id': 4, 'name': 'source-4', 'target': {'id': 4}} - serializer = self.Serializer(data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': [{'name': ['This field is required.']}]}) +# def test_one_to_one_create_with_invalid_data(self): +# data = {'id': 4, 'name': 'source-4', 'target': {'id': 4}} +# serializer = self.Serializer(data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': [{'name': ['This field is required.']}]}) - def test_one_to_one_update(self): - data = {'id': 3, 'name': 'source-3-updated', 'target': {'id': 3, 'name': 'target-3-updated'}} - instance = OneToOneSource.objects.get(pk=3) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-3-updated') +# def test_one_to_one_update(self): +# data = {'id': 3, 'name': 'source-3-updated', 'target': {'id': 3, 'name': 'target-3-updated'}} +# instance = OneToOneSource.objects.get(pk=3) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-3-updated') - # Ensure (target 3, target_source 3, source 3) are updated, - # and everything else is as expected. - queryset = OneToOneSource.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, - {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, - {'id': 3, 'name': 'source-3-updated', 'target': {'id': 3, 'name': 'target-3-updated'}} - ] - self.assertEqual(serializer.data, expected) +# # Ensure (target 3, target_source 3, source 3) are updated, +# # and everything else is as expected. +# queryset = OneToOneSource.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, +# {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, +# {'id': 3, 'name': 'source-3-updated', 'target': {'id': 3, 'name': 'target-3-updated'}} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_one_update_to_null(self): - data = {'id': 3, 'name': 'source-3-updated', 'target': None} - instance = OneToOneSource.objects.get(pk=3) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() +# def test_one_to_one_update_to_null(self): +# data = {'id': 3, 'name': 'source-3-updated', 'target': None} +# instance = OneToOneSource.objects.get(pk=3) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-3-updated') - self.assertEqual(obj.target, None) +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-3-updated') +# self.assertEqual(obj.target, None) - queryset = OneToOneSource.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, - {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, - {'id': 3, 'name': 'source-3-updated', 'target': None} - ] - self.assertEqual(serializer.data, expected) +# queryset = OneToOneSource.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': {'id': 1, 'name': 'target-1'}}, +# {'id': 2, 'name': 'source-2', 'target': {'id': 2, 'name': 'target-2'}}, +# {'id': 3, 'name': 'source-3-updated', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) - # TODO: Nullable 1-1 tests - # def test_one_to_one_delete(self): - # data = {'id': 3, 'name': 'target-3', 'target_source': None} - # instance = OneToOneTarget.objects.get(pk=3) - # serializer = self.Serializer(instance, data=data) - # self.assertTrue(serializer.is_valid()) - # serializer.save() +# # TODO: Nullable 1-1 tests +# # def test_one_to_one_delete(self): +# # data = {'id': 3, 'name': 'target-3', 'target_source': None} +# # instance = OneToOneTarget.objects.get(pk=3) +# # serializer = self.Serializer(instance, data=data) +# # self.assertTrue(serializer.is_valid()) +# # serializer.save() - # # Ensure (target_source 3, source 3) are deleted, - # # and everything else is as expected. - # queryset = OneToOneTarget.objects.all() - # serializer = self.Serializer(queryset) - # expected = [ - # {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, - # {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, - # {'id': 3, 'name': 'target-3', 'source': None} - # ] - # self.assertEqual(serializer.data, expected) +# # # Ensure (target_source 3, source 3) are deleted, +# # # and everything else is as expected. +# # queryset = OneToOneTarget.objects.all() +# # serializer = self.Serializer(queryset) +# # expected = [ +# # {'id': 1, 'name': 'target-1', 'source': {'id': 1, 'name': 'source-1'}}, +# # {'id': 2, 'name': 'target-2', 'source': {'id': 2, 'name': 'source-2'}}, +# # {'id': 3, 'name': 'target-3', 'source': None} +# # ] +# # self.assertEqual(serializer.data, expected) -class ReverseNestedOneToManyTests(TestCase): - def setUp(self): - class OneToManySourceSerializer(serializers.ModelSerializer): - class Meta: - model = OneToManySource - fields = ('id', 'name') +# class ReverseNestedOneToManyTests(TestCase): +# def setUp(self): +# class OneToManySourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = OneToManySource +# fields = ('id', 'name') - class OneToManyTargetSerializer(serializers.ModelSerializer): - sources = OneToManySourceSerializer(many=True, allow_add_remove=True) +# class OneToManyTargetSerializer(serializers.ModelSerializer): +# sources = OneToManySourceSerializer(many=True, allow_add_remove=True) - class Meta: - model = OneToManyTarget - fields = ('id', 'name', 'sources') +# class Meta: +# model = OneToManyTarget +# fields = ('id', 'name', 'sources') - self.Serializer = OneToManyTargetSerializer +# self.Serializer = OneToManyTargetSerializer - target = OneToManyTarget(name='target-1') - target.save() - for idx in range(1, 4): - source = OneToManySource(name='source-%d' % idx, target=target) - source.save() +# target = OneToManyTarget(name='target-1') +# target.save() +# for idx in range(1, 4): +# source = OneToManySource(name='source-%d' % idx, target=target) +# source.save() - def test_one_to_many_retrieve(self): - queryset = OneToManyTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}]}, - ] - self.assertEqual(serializer.data, expected) +# def test_one_to_many_retrieve(self): +# queryset = OneToManyTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}]}, +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_many_create(self): - data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}, - {'id': 4, 'name': 'source-4'}]} - instance = OneToManyTarget.objects.get(pk=1) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-1') +# def test_one_to_many_create(self): +# data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}, +# {'id': 4, 'name': 'source-4'}]} +# instance = OneToManyTarget.objects.get(pk=1) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-1') - # Ensure source 4 is added, and everything else is as - # expected. - queryset = OneToManyTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}, - {'id': 4, 'name': 'source-4'}]} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 4 is added, and everything else is as +# # expected. +# queryset = OneToManyTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}, +# {'id': 4, 'name': 'source-4'}]} +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_many_create_with_invalid_data(self): - data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}, - {'id': 4}]} - serializer = self.Serializer(data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'sources': [{}, {}, {}, {'name': ['This field is required.']}]}) +# def test_one_to_many_create_with_invalid_data(self): +# data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}, +# {'id': 4}]} +# serializer = self.Serializer(data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'sources': [{}, {}, {}, {'name': ['This field is required.']}]}) - def test_one_to_many_update(self): - data = {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}]} - instance = OneToManyTarget.objects.get(pk=1) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-1-updated') +# def test_one_to_many_update(self): +# data = {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}]} +# instance = OneToManyTarget.objects.get(pk=1) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-1-updated') - # Ensure (target 1, source 1) are updated, - # and everything else is as expected. - queryset = OneToManyTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'}, - {'id': 2, 'name': 'source-2'}, - {'id': 3, 'name': 'source-3'}]} +# # Ensure (target 1, source 1) are updated, +# # and everything else is as expected. +# queryset = OneToManyTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1-updated', 'sources': [{'id': 1, 'name': 'source-1-updated'}, +# {'id': 2, 'name': 'source-2'}, +# {'id': 3, 'name': 'source-3'}]} - ] - self.assertEqual(serializer.data, expected) +# ] +# self.assertEqual(serializer.data, expected) - def test_one_to_many_delete(self): - data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 3, 'name': 'source-3'}]} - instance = OneToManyTarget.objects.get(pk=1) - serializer = self.Serializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - serializer.save() +# def test_one_to_many_delete(self): +# data = {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 3, 'name': 'source-3'}]} +# instance = OneToManyTarget.objects.get(pk=1) +# serializer = self.Serializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# serializer.save() - # Ensure source 2 is deleted, and everything else is as - # expected. - queryset = OneToManyTarget.objects.all() - serializer = self.Serializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, - {'id': 3, 'name': 'source-3'}]} +# # Ensure source 2 is deleted, and everything else is as +# # expected. +# queryset = OneToManyTarget.objects.all() +# serializer = self.Serializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [{'id': 1, 'name': 'source-1'}, +# {'id': 3, 'name': 'source-3'}]} - ] - self.assertEqual(serializer.data, expected) +# ] +# self.assertEqual(serializer.data, expected) diff --git a/tests/test_relations_pk.py b/tests/test_relations_pk.py index e3f836ed6..031a79b32 100644 --- a/tests/test_relations_pk.py +++ b/tests/test_relations_pk.py @@ -1,551 +1,551 @@ -from __future__ import unicode_literals -from django.db import models -from django.test import TestCase -from django.utils import six -from rest_framework import serializers -from tests.models import ( - BlogPost, ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, - NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource, -) - - -# ManyToMany -class ManyToManyTargetSerializer(serializers.ModelSerializer): - class Meta: - model = ManyToManyTarget - fields = ('id', 'name', 'sources') - - -class ManyToManySourceSerializer(serializers.ModelSerializer): - class Meta: - model = ManyToManySource - fields = ('id', 'name', 'targets') - - -# ForeignKey -class ForeignKeyTargetSerializer(serializers.ModelSerializer): - class Meta: - model = ForeignKeyTarget - fields = ('id', 'name', 'sources') - - -class ForeignKeySourceSerializer(serializers.ModelSerializer): - class Meta: - model = ForeignKeySource - fields = ('id', 'name', 'target') - - -# Nullable ForeignKey -class NullableForeignKeySourceSerializer(serializers.ModelSerializer): - class Meta: - model = NullableForeignKeySource - fields = ('id', 'name', 'target') - - -# Nullable OneToOne -class NullableOneToOneTargetSerializer(serializers.ModelSerializer): - class Meta: - model = OneToOneTarget - fields = ('id', 'name', 'nullable_source') - - -# TODO: Add test that .data cannot be accessed prior to .is_valid - -class PKManyToManyTests(TestCase): - def setUp(self): - for idx in range(1, 4): - target = ManyToManyTarget(name='target-%d' % idx) - target.save() - source = ManyToManySource(name='source-%d' % idx) - source.save() - for target in ManyToManyTarget.objects.all(): - source.targets.add(target) - - def test_many_to_many_retrieve(self): - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'targets': [1]}, - {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, - {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_retrieve(self): - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, - {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, - {'id': 3, 'name': 'target-3', 'sources': [3]} - ] - self.assertEqual(serializer.data, expected) - - def test_many_to_many_update(self): - data = {'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]} - instance = ManyToManySource.objects.get(pk=1) - serializer = ManyToManySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure source 1 is updated, and everything else is as expected - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]}, - {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, - {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_update(self): - data = {'id': 1, 'name': 'target-1', 'sources': [1]} - instance = ManyToManyTarget.objects.get(pk=1) - serializer = ManyToManyTargetSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure target 1 is updated, and everything else is as expected - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [1]}, - {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, - {'id': 3, 'name': 'target-3', 'sources': [3]} - ] - self.assertEqual(serializer.data, expected) - - def test_many_to_many_create(self): - data = {'id': 4, 'name': 'source-4', 'targets': [1, 3]} - serializer = ManyToManySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is added, and everything else is as expected - queryset = ManyToManySource.objects.all() - serializer = ManyToManySourceSerializer(queryset, many=True) - self.assertFalse(serializer.fields['targets'].read_only) - expected = [ - {'id': 1, 'name': 'source-1', 'targets': [1]}, - {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, - {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]}, - {'id': 4, 'name': 'source-4', 'targets': [1, 3]}, - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_many_to_many_create(self): - data = {'id': 4, 'name': 'target-4', 'sources': [1, 3]} - serializer = ManyToManyTargetSerializer(data=data) - self.assertFalse(serializer.fields['sources'].read_only) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-4') - - # Ensure target 4 is added, and everything else is as expected - queryset = ManyToManyTarget.objects.all() - serializer = ManyToManyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, - {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, - {'id': 3, 'name': 'target-3', 'sources': [3]}, - {'id': 4, 'name': 'target-4', 'sources': [1, 3]} - ] - self.assertEqual(serializer.data, expected) - - -class PKForeignKeyTests(TestCase): - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - new_target = ForeignKeyTarget(name='target-2') - new_target.save() - for idx in range(1, 4): - source = ForeignKeySource(name='source-%d' % idx, target=target) - source.save() - - def test_foreign_key_retrieve(self): - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 1}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': 1} - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_foreign_key_retrieve(self): - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, - {'id': 2, 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update(self): - data = {'id': 1, 'name': 'source-1', 'target': 2} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 2}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': 1} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_incorrect_type(self): - data = {'id': 1, 'name': 'source-1', 'target': 'foo'} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected pk value, received %s.' % six.text_type.__name__]}) - - def test_reverse_foreign_key_update(self): - data = {'id': 2, 'name': 'target-2', 'sources': [1, 3]} - instance = ForeignKeyTarget.objects.get(pk=2) - serializer = ForeignKeyTargetSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - # We shouldn't have saved anything to the db yet since save - # hasn't been called. - queryset = ForeignKeyTarget.objects.all() - new_serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, - {'id': 2, 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(new_serializer.data, expected) - - serializer.save() - self.assertEqual(serializer.data, data) - - # Ensure target 2 is update, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [2]}, - {'id': 2, 'name': 'target-2', 'sources': [1, 3]}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create(self): - data = {'id': 4, 'name': 'source-4', 'target': 2} - serializer = ForeignKeySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is added, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 1}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': 1}, - {'id': 4, 'name': 'source-4', 'target': 2}, - ] - self.assertEqual(serializer.data, expected) - - def test_reverse_foreign_key_create(self): - data = {'id': 3, 'name': 'target-3', 'sources': [1, 3]} - serializer = ForeignKeyTargetSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-3') - - # Ensure target 3 is added, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': [2]}, - {'id': 2, 'name': 'target-2', 'sources': []}, - {'id': 3, 'name': 'target-3', 'sources': [1, 3]}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_invalid_null(self): - data = {'id': 1, 'name': 'source-1', 'target': None} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['This field is required.']}) - - def test_foreign_key_with_empty(self): - """ - Regression test for #1072 - - https://github.com/tomchristie/django-rest-framework/issues/1072 - """ - serializer = NullableForeignKeySourceSerializer() - self.assertEqual(serializer.data['target'], None) - - -class PKNullableForeignKeyTests(TestCase): - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - for idx in range(1, 4): - if idx == 3: - target = None - source = NullableForeignKeySource(name='source-%d' % idx, target=target) - source.save() - - def test_foreign_key_retrieve_with_null(self): - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 1}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': None}, - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create_with_valid_null(self): - data = {'id': 4, 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 1}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': None}, - {'id': 4, 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_create_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'id': 4, 'name': 'source-4', 'target': ''} - expected_data = {'id': 4, 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, expected_data) - self.assertEqual(obj.name, 'source-4') - - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 1}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': None}, - {'id': 4, 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_valid_null(self): - data = {'id': 1, 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': None}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - def test_foreign_key_update_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'id': 1, 'name': 'source-1', 'target': ''} - expected_data = {'id': 1, 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, expected_data) - serializer.save() - - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': None}, - {'id': 2, 'name': 'source-2', 'target': 1}, - {'id': 3, 'name': 'source-3', 'target': None} - ] - self.assertEqual(serializer.data, expected) - - # reverse foreign keys MUST be read_only - # In the general case they do not provide .remove() or .clear() - # and cannot be arbitrarily set. - - # def test_reverse_foreign_key_update(self): - # data = {'id': 1, 'name': 'target-1', 'sources': [1]} - # instance = ForeignKeyTarget.objects.get(pk=1) - # serializer = ForeignKeyTargetSerializer(instance, data=data) - # self.assertTrue(serializer.is_valid()) - # self.assertEqual(serializer.data, data) - # serializer.save() - - # # Ensure target 1 is updated, and everything else is as expected - # queryset = ForeignKeyTarget.objects.all() - # serializer = ForeignKeyTargetSerializer(queryset, many=True) - # expected = [ - # {'id': 1, 'name': 'target-1', 'sources': [1]}, - # {'id': 2, 'name': 'target-2', 'sources': []}, - # ] - # self.assertEqual(serializer.data, expected) - - -class PKNullableOneToOneTests(TestCase): - def setUp(self): - target = OneToOneTarget(name='target-1') - target.save() - new_target = OneToOneTarget(name='target-2') - new_target.save() - source = NullableOneToOneSource(name='source-1', target=new_target) - source.save() - - def test_reverse_foreign_key_retrieve_with_null(self): - queryset = OneToOneTarget.objects.all() - serializer = NullableOneToOneTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'nullable_source': None}, - {'id': 2, 'name': 'target-2', 'nullable_source': 1}, - ] - self.assertEqual(serializer.data, expected) - - -# The below models and tests ensure that serializer fields corresponding -# to a ManyToManyField field with a user-specified ``through`` model are -# set to read only - - -class ManyToManyThroughTarget(models.Model): - name = models.CharField(max_length=100) - - -class ManyToManyThrough(models.Model): - source = models.ForeignKey('ManyToManyThroughSource') - target = models.ForeignKey(ManyToManyThroughTarget) - - -class ManyToManyThroughSource(models.Model): - name = models.CharField(max_length=100) - targets = models.ManyToManyField(ManyToManyThroughTarget, - related_name='sources', - through='ManyToManyThrough') - - -class ManyToManyThroughTargetSerializer(serializers.ModelSerializer): - class Meta: - model = ManyToManyThroughTarget - fields = ('id', 'name', 'sources') - - -class ManyToManyThroughSourceSerializer(serializers.ModelSerializer): - class Meta: - model = ManyToManyThroughSource - fields = ('id', 'name', 'targets') - - -class PKManyToManyThroughTests(TestCase): - def setUp(self): - self.source = ManyToManyThroughSource.objects.create( - name='through-source-1') - self.target = ManyToManyThroughTarget.objects.create( - name='through-target-1') - - def test_many_to_many_create(self): - data = {'id': 2, 'name': 'source-2', 'targets': [self.target.pk]} - serializer = ManyToManyThroughSourceSerializer(data=data) - self.assertTrue(serializer.fields['targets'].read_only) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(obj.name, 'source-2') - self.assertEqual(obj.targets.count(), 0) - - def test_many_to_many_reverse_create(self): - data = {'id': 2, 'name': 'target-2', 'sources': [self.source.pk]} - serializer = ManyToManyThroughTargetSerializer(data=data) - self.assertTrue(serializer.fields['sources'].read_only) - self.assertTrue(serializer.is_valid()) - serializer.save() - obj = serializer.save() - self.assertEqual(obj.name, 'target-2') - self.assertEqual(obj.sources.count(), 0) - - -# Regression tests for #694 (`source` attribute on related fields) - - -class PrimaryKeyRelatedFieldSourceTests(TestCase): - def test_related_manager_source(self): - """ - Relational fields should be able to use manager-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_manager') - - class ClassWithManagerMethod(object): - def get_blogposts_manager(self): - return BlogPost.objects - - obj = ClassWithManagerMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, [1]) - - def test_related_queryset_source(self): - """ - Relational fields should be able to use queryset-returning methods as their source. - """ - BlogPost.objects.create(title='blah') - field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_queryset') - - class ClassWithQuerysetMethod(object): - def get_blogposts_queryset(self): - return BlogPost.objects.all() - - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, [1]) - - def test_dotted_source(self): - """ - Source argument should support dotted.source notation. - """ - BlogPost.objects.create(title='blah') - field = serializers.PrimaryKeyRelatedField(many=True, source='a.b.c') - - class ClassWithQuerysetMethod(object): - a = { - 'b': { - 'c': BlogPost.objects.all() - } - } - - obj = ClassWithQuerysetMethod() - value = field.field_to_native(obj, 'field_name') - self.assertEqual(value, [1]) +# from __future__ import unicode_literals +# from django.db import models +# from django.test import TestCase +# from django.utils import six +# from rest_framework import serializers +# from tests.models import ( +# BlogPost, ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, +# NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource, +# ) + + +# # ManyToMany +# class ManyToManyTargetSerializer(serializers.ModelSerializer): +# class Meta: +# model = ManyToManyTarget +# fields = ('id', 'name', 'sources') + + +# class ManyToManySourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = ManyToManySource +# fields = ('id', 'name', 'targets') + + +# # ForeignKey +# class ForeignKeyTargetSerializer(serializers.ModelSerializer): +# class Meta: +# model = ForeignKeyTarget +# fields = ('id', 'name', 'sources') + + +# class ForeignKeySourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = ForeignKeySource +# fields = ('id', 'name', 'target') + + +# # Nullable ForeignKey +# class NullableForeignKeySourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = NullableForeignKeySource +# fields = ('id', 'name', 'target') + + +# # Nullable OneToOne +# class NullableOneToOneTargetSerializer(serializers.ModelSerializer): +# class Meta: +# model = OneToOneTarget +# fields = ('id', 'name', 'nullable_source') + + +# # TODO: Add test that .data cannot be accessed prior to .is_valid + +# class PKManyToManyTests(TestCase): +# def setUp(self): +# for idx in range(1, 4): +# target = ManyToManyTarget(name='target-%d' % idx) +# target.save() +# source = ManyToManySource(name='source-%d' % idx) +# source.save() +# for target in ManyToManyTarget.objects.all(): +# source.targets.add(target) + +# def test_many_to_many_retrieve(self): +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'targets': [1]}, +# {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, +# {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_retrieve(self): +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, +# {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, +# {'id': 3, 'name': 'target-3', 'sources': [3]} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_many_to_many_update(self): +# data = {'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]} +# instance = ManyToManySource.objects.get(pk=1) +# serializer = ManyToManySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'targets': [1, 2, 3]}, +# {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, +# {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_update(self): +# data = {'id': 1, 'name': 'target-1', 'sources': [1]} +# instance = ManyToManyTarget.objects.get(pk=1) +# serializer = ManyToManyTargetSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure target 1 is updated, and everything else is as expected +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [1]}, +# {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, +# {'id': 3, 'name': 'target-3', 'sources': [3]} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_many_to_many_create(self): +# data = {'id': 4, 'name': 'source-4', 'targets': [1, 3]} +# serializer = ManyToManySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is added, and everything else is as expected +# queryset = ManyToManySource.objects.all() +# serializer = ManyToManySourceSerializer(queryset, many=True) +# self.assertFalse(serializer.fields['targets'].read_only) +# expected = [ +# {'id': 1, 'name': 'source-1', 'targets': [1]}, +# {'id': 2, 'name': 'source-2', 'targets': [1, 2]}, +# {'id': 3, 'name': 'source-3', 'targets': [1, 2, 3]}, +# {'id': 4, 'name': 'source-4', 'targets': [1, 3]}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_many_to_many_create(self): +# data = {'id': 4, 'name': 'target-4', 'sources': [1, 3]} +# serializer = ManyToManyTargetSerializer(data=data) +# self.assertFalse(serializer.fields['sources'].read_only) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-4') + +# # Ensure target 4 is added, and everything else is as expected +# queryset = ManyToManyTarget.objects.all() +# serializer = ManyToManyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, +# {'id': 2, 'name': 'target-2', 'sources': [2, 3]}, +# {'id': 3, 'name': 'target-3', 'sources': [3]}, +# {'id': 4, 'name': 'target-4', 'sources': [1, 3]} +# ] +# self.assertEqual(serializer.data, expected) + + +# class PKForeignKeyTests(TestCase): +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# new_target = ForeignKeyTarget(name='target-2') +# new_target.save() +# for idx in range(1, 4): +# source = ForeignKeySource(name='source-%d' % idx, target=target) +# source.save() + +# def test_foreign_key_retrieve(self): +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 1}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': 1} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_foreign_key_retrieve(self): +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update(self): +# data = {'id': 1, 'name': 'source-1', 'target': 2} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 2}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': 1} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_incorrect_type(self): +# data = {'id': 1, 'name': 'source-1', 'target': 'foo'} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['Incorrect type. Expected pk value, received %s.' % six.text_type.__name__]}) + +# def test_reverse_foreign_key_update(self): +# data = {'id': 2, 'name': 'target-2', 'sources': [1, 3]} +# instance = ForeignKeyTarget.objects.get(pk=2) +# serializer = ForeignKeyTargetSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# # We shouldn't have saved anything to the db yet since save +# # hasn't been called. +# queryset = ForeignKeyTarget.objects.all() +# new_serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [1, 2, 3]}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(new_serializer.data, expected) + +# serializer.save() +# self.assertEqual(serializer.data, data) + +# # Ensure target 2 is update, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [2]}, +# {'id': 2, 'name': 'target-2', 'sources': [1, 3]}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create(self): +# data = {'id': 4, 'name': 'source-4', 'target': 2} +# serializer = ForeignKeySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is added, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 1}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': 1}, +# {'id': 4, 'name': 'source-4', 'target': 2}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_reverse_foreign_key_create(self): +# data = {'id': 3, 'name': 'target-3', 'sources': [1, 3]} +# serializer = ForeignKeyTargetSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-3') + +# # Ensure target 3 is added, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': [2]}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# {'id': 3, 'name': 'target-3', 'sources': [1, 3]}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_invalid_null(self): +# data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['This field is required.']}) + +# def test_foreign_key_with_empty(self): +# """ +# Regression test for #1072 + +# https://github.com/tomchristie/django-rest-framework/issues/1072 +# """ +# serializer = NullableForeignKeySourceSerializer() +# self.assertEqual(serializer.data['target'], None) + + +# class PKNullableForeignKeyTests(TestCase): +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# for idx in range(1, 4): +# if idx == 3: +# target = None +# source = NullableForeignKeySource(name='source-%d' % idx, target=target) +# source.save() + +# def test_foreign_key_retrieve_with_null(self): +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 1}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create_with_valid_null(self): +# data = {'id': 4, 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 1}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# {'id': 4, 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_create_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'id': 4, 'name': 'source-4', 'target': ''} +# expected_data = {'id': 4, 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, expected_data) +# self.assertEqual(obj.name, 'source-4') + +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 1}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# {'id': 4, 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_valid_null(self): +# data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': None}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# def test_foreign_key_update_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'id': 1, 'name': 'source-1', 'target': ''} +# expected_data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, expected_data) +# serializer.save() + +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': None}, +# {'id': 2, 'name': 'source-2', 'target': 1}, +# {'id': 3, 'name': 'source-3', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) + +# # reverse foreign keys MUST be read_only +# # In the general case they do not provide .remove() or .clear() +# # and cannot be arbitrarily set. + +# # def test_reverse_foreign_key_update(self): +# # data = {'id': 1, 'name': 'target-1', 'sources': [1]} +# # instance = ForeignKeyTarget.objects.get(pk=1) +# # serializer = ForeignKeyTargetSerializer(instance, data=data) +# # self.assertTrue(serializer.is_valid()) +# # self.assertEqual(serializer.data, data) +# # serializer.save() + +# # # Ensure target 1 is updated, and everything else is as expected +# # queryset = ForeignKeyTarget.objects.all() +# # serializer = ForeignKeyTargetSerializer(queryset, many=True) +# # expected = [ +# # {'id': 1, 'name': 'target-1', 'sources': [1]}, +# # {'id': 2, 'name': 'target-2', 'sources': []}, +# # ] +# # self.assertEqual(serializer.data, expected) + + +# class PKNullableOneToOneTests(TestCase): +# def setUp(self): +# target = OneToOneTarget(name='target-1') +# target.save() +# new_target = OneToOneTarget(name='target-2') +# new_target.save() +# source = NullableOneToOneSource(name='source-1', target=new_target) +# source.save() + +# def test_reverse_foreign_key_retrieve_with_null(self): +# queryset = OneToOneTarget.objects.all() +# serializer = NullableOneToOneTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'nullable_source': None}, +# {'id': 2, 'name': 'target-2', 'nullable_source': 1}, +# ] +# self.assertEqual(serializer.data, expected) + + +# # The below models and tests ensure that serializer fields corresponding +# # to a ManyToManyField field with a user-specified ``through`` model are +# # set to read only + + +# class ManyToManyThroughTarget(models.Model): +# name = models.CharField(max_length=100) + + +# class ManyToManyThrough(models.Model): +# source = models.ForeignKey('ManyToManyThroughSource') +# target = models.ForeignKey(ManyToManyThroughTarget) + + +# class ManyToManyThroughSource(models.Model): +# name = models.CharField(max_length=100) +# targets = models.ManyToManyField(ManyToManyThroughTarget, +# related_name='sources', +# through='ManyToManyThrough') + + +# class ManyToManyThroughTargetSerializer(serializers.ModelSerializer): +# class Meta: +# model = ManyToManyThroughTarget +# fields = ('id', 'name', 'sources') + + +# class ManyToManyThroughSourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = ManyToManyThroughSource +# fields = ('id', 'name', 'targets') + + +# class PKManyToManyThroughTests(TestCase): +# def setUp(self): +# self.source = ManyToManyThroughSource.objects.create( +# name='through-source-1') +# self.target = ManyToManyThroughTarget.objects.create( +# name='through-target-1') + +# def test_many_to_many_create(self): +# data = {'id': 2, 'name': 'source-2', 'targets': [self.target.pk]} +# serializer = ManyToManyThroughSourceSerializer(data=data) +# self.assertTrue(serializer.fields['targets'].read_only) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(obj.name, 'source-2') +# self.assertEqual(obj.targets.count(), 0) + +# def test_many_to_many_reverse_create(self): +# data = {'id': 2, 'name': 'target-2', 'sources': [self.source.pk]} +# serializer = ManyToManyThroughTargetSerializer(data=data) +# self.assertTrue(serializer.fields['sources'].read_only) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# obj = serializer.save() +# self.assertEqual(obj.name, 'target-2') +# self.assertEqual(obj.sources.count(), 0) + + +# # Regression tests for #694 (`source` attribute on related fields) + + +# class PrimaryKeyRelatedFieldSourceTests(TestCase): +# def test_related_manager_source(self): +# """ +# Relational fields should be able to use manager-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_manager') + +# class ClassWithManagerMethod(object): +# def get_blogposts_manager(self): +# return BlogPost.objects + +# obj = ClassWithManagerMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, [1]) + +# def test_related_queryset_source(self): +# """ +# Relational fields should be able to use queryset-returning methods as their source. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.PrimaryKeyRelatedField(many=True, source='get_blogposts_queryset') + +# class ClassWithQuerysetMethod(object): +# def get_blogposts_queryset(self): +# return BlogPost.objects.all() + +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, [1]) + +# def test_dotted_source(self): +# """ +# Source argument should support dotted.source notation. +# """ +# BlogPost.objects.create(title='blah') +# field = serializers.PrimaryKeyRelatedField(many=True, source='a.b.c') + +# class ClassWithQuerysetMethod(object): +# a = { +# 'b': { +# 'c': BlogPost.objects.all() +# } +# } + +# obj = ClassWithQuerysetMethod() +# value = field.field_to_native(obj, 'field_name') +# self.assertEqual(value, [1]) diff --git a/tests/test_relations_slug.py b/tests/test_relations_slug.py index 97ebf23a1..f7a59a957 100644 --- a/tests/test_relations_slug.py +++ b/tests/test_relations_slug.py @@ -1,257 +1,257 @@ -from django.test import TestCase -from rest_framework import serializers -from tests.models import NullableForeignKeySource, ForeignKeySource, ForeignKeyTarget +# from django.test import TestCase +# from rest_framework import serializers +# from tests.models import NullableForeignKeySource, ForeignKeySource, ForeignKeyTarget -class ForeignKeyTargetSerializer(serializers.ModelSerializer): - sources = serializers.SlugRelatedField(many=True, slug_field='name') +# class ForeignKeyTargetSerializer(serializers.ModelSerializer): +# sources = serializers.SlugRelatedField(many=True, slug_field='name') - class Meta: - model = ForeignKeyTarget +# class Meta: +# model = ForeignKeyTarget -class ForeignKeySourceSerializer(serializers.ModelSerializer): - target = serializers.SlugRelatedField(slug_field='name') +# class ForeignKeySourceSerializer(serializers.ModelSerializer): +# target = serializers.SlugRelatedField(slug_field='name') - class Meta: - model = ForeignKeySource +# class Meta: +# model = ForeignKeySource -class NullableForeignKeySourceSerializer(serializers.ModelSerializer): - target = serializers.SlugRelatedField(slug_field='name', required=False) +# class NullableForeignKeySourceSerializer(serializers.ModelSerializer): +# target = serializers.SlugRelatedField(slug_field='name', required=False) - class Meta: - model = NullableForeignKeySource +# class Meta: +# model = NullableForeignKeySource -# TODO: M2M Tests, FKTests (Non-nullable), One2One -class SlugForeignKeyTests(TestCase): - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - new_target = ForeignKeyTarget(name='target-2') - new_target.save() - for idx in range(1, 4): - source = ForeignKeySource(name='source-%d' % idx, target=target) - source.save() +# # TODO: M2M Tests, FKTests (Non-nullable), One2One +# class SlugForeignKeyTests(TestCase): +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# new_target = ForeignKeyTarget(name='target-2') +# new_target.save() +# for idx in range(1, 4): +# source = ForeignKeySource(name='source-%d' % idx, target=target) +# source.save() - def test_foreign_key_retrieve(self): - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-1'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': 'target-1'} - ] - self.assertEqual(serializer.data, expected) +# def test_foreign_key_retrieve(self): +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-1'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': 'target-1'} +# ] +# self.assertEqual(serializer.data, expected) - def test_reverse_foreign_key_retrieve(self): - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': ['source-1', 'source-2', 'source-3']}, - {'id': 2, 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(serializer.data, expected) +# def test_reverse_foreign_key_retrieve(self): +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': ['source-1', 'source-2', 'source-3']}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_update(self): - data = {'id': 1, 'name': 'source-1', 'target': 'target-2'} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() +# def test_foreign_key_update(self): +# data = {'id': 1, 'name': 'source-1', 'target': 'target-2'} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() - # Ensure source 1 is updated, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-2'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': 'target-1'} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 1 is updated, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-2'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': 'target-1'} +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_update_incorrect_type(self): - data = {'id': 1, 'name': 'source-1', 'target': 123} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['Object with name=123 does not exist.']}) +# def test_foreign_key_update_incorrect_type(self): +# data = {'id': 1, 'name': 'source-1', 'target': 123} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['Object with name=123 does not exist.']}) - def test_reverse_foreign_key_update(self): - data = {'id': 2, 'name': 'target-2', 'sources': ['source-1', 'source-3']} - instance = ForeignKeyTarget.objects.get(pk=2) - serializer = ForeignKeyTargetSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - # We shouldn't have saved anything to the db yet since save - # hasn't been called. - queryset = ForeignKeyTarget.objects.all() - new_serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': ['source-1', 'source-2', 'source-3']}, - {'id': 2, 'name': 'target-2', 'sources': []}, - ] - self.assertEqual(new_serializer.data, expected) +# def test_reverse_foreign_key_update(self): +# data = {'id': 2, 'name': 'target-2', 'sources': ['source-1', 'source-3']} +# instance = ForeignKeyTarget.objects.get(pk=2) +# serializer = ForeignKeyTargetSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# # We shouldn't have saved anything to the db yet since save +# # hasn't been called. +# queryset = ForeignKeyTarget.objects.all() +# new_serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': ['source-1', 'source-2', 'source-3']}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# ] +# self.assertEqual(new_serializer.data, expected) - serializer.save() - self.assertEqual(serializer.data, data) +# serializer.save() +# self.assertEqual(serializer.data, data) - # Ensure target 2 is update, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': ['source-2']}, - {'id': 2, 'name': 'target-2', 'sources': ['source-1', 'source-3']}, - ] - self.assertEqual(serializer.data, expected) +# # Ensure target 2 is update, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': ['source-2']}, +# {'id': 2, 'name': 'target-2', 'sources': ['source-1', 'source-3']}, +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_create(self): - data = {'id': 4, 'name': 'source-4', 'target': 'target-2'} - serializer = ForeignKeySourceSerializer(data=data) - serializer.is_valid() - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') +# def test_foreign_key_create(self): +# data = {'id': 4, 'name': 'source-4', 'target': 'target-2'} +# serializer = ForeignKeySourceSerializer(data=data) +# serializer.is_valid() +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') - # Ensure source 4 is added, and everything else is as expected - queryset = ForeignKeySource.objects.all() - serializer = ForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-1'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': 'target-1'}, - {'id': 4, 'name': 'source-4', 'target': 'target-2'}, - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 4 is added, and everything else is as expected +# queryset = ForeignKeySource.objects.all() +# serializer = ForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-1'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': 'target-1'}, +# {'id': 4, 'name': 'source-4', 'target': 'target-2'}, +# ] +# self.assertEqual(serializer.data, expected) - def test_reverse_foreign_key_create(self): - data = {'id': 3, 'name': 'target-3', 'sources': ['source-1', 'source-3']} - serializer = ForeignKeyTargetSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'target-3') +# def test_reverse_foreign_key_create(self): +# data = {'id': 3, 'name': 'target-3', 'sources': ['source-1', 'source-3']} +# serializer = ForeignKeyTargetSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'target-3') - # Ensure target 3 is added, and everything else is as expected - queryset = ForeignKeyTarget.objects.all() - serializer = ForeignKeyTargetSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'target-1', 'sources': ['source-2']}, - {'id': 2, 'name': 'target-2', 'sources': []}, - {'id': 3, 'name': 'target-3', 'sources': ['source-1', 'source-3']}, - ] - self.assertEqual(serializer.data, expected) +# # Ensure target 3 is added, and everything else is as expected +# queryset = ForeignKeyTarget.objects.all() +# serializer = ForeignKeyTargetSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'target-1', 'sources': ['source-2']}, +# {'id': 2, 'name': 'target-2', 'sources': []}, +# {'id': 3, 'name': 'target-3', 'sources': ['source-1', 'source-3']}, +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_update_with_invalid_null(self): - data = {'id': 1, 'name': 'source-1', 'target': None} - instance = ForeignKeySource.objects.get(pk=1) - serializer = ForeignKeySourceSerializer(instance, data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'target': ['This field is required.']}) +# def test_foreign_key_update_with_invalid_null(self): +# data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = ForeignKeySource.objects.get(pk=1) +# serializer = ForeignKeySourceSerializer(instance, data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'target': ['This field is required.']}) -class SlugNullableForeignKeyTests(TestCase): - def setUp(self): - target = ForeignKeyTarget(name='target-1') - target.save() - for idx in range(1, 4): - if idx == 3: - target = None - source = NullableForeignKeySource(name='source-%d' % idx, target=target) - source.save() +# class SlugNullableForeignKeyTests(TestCase): +# def setUp(self): +# target = ForeignKeyTarget(name='target-1') +# target.save() +# for idx in range(1, 4): +# if idx == 3: +# target = None +# source = NullableForeignKeySource(name='source-%d' % idx, target=target) +# source.save() - def test_foreign_key_retrieve_with_null(self): - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-1'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': None}, - ] - self.assertEqual(serializer.data, expected) +# def test_foreign_key_retrieve_with_null(self): +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-1'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_create_with_valid_null(self): - data = {'id': 4, 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, data) - self.assertEqual(obj.name, 'source-4') +# def test_foreign_key_create_with_valid_null(self): +# data = {'id': 4, 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, data) +# self.assertEqual(obj.name, 'source-4') - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-1'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': None}, - {'id': 4, 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-1'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# {'id': 4, 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_create_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'id': 4, 'name': 'source-4', 'target': ''} - expected_data = {'id': 4, 'name': 'source-4', 'target': None} - serializer = NullableForeignKeySourceSerializer(data=data) - self.assertTrue(serializer.is_valid()) - obj = serializer.save() - self.assertEqual(serializer.data, expected_data) - self.assertEqual(obj.name, 'source-4') +# def test_foreign_key_create_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'id': 4, 'name': 'source-4', 'target': ''} +# expected_data = {'id': 4, 'name': 'source-4', 'target': None} +# serializer = NullableForeignKeySourceSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# obj = serializer.save() +# self.assertEqual(serializer.data, expected_data) +# self.assertEqual(obj.name, 'source-4') - # Ensure source 4 is created, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': 'target-1'}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': None}, - {'id': 4, 'name': 'source-4', 'target': None} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 4 is created, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': 'target-1'}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': None}, +# {'id': 4, 'name': 'source-4', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_update_with_valid_null(self): - data = {'id': 1, 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - serializer.save() +# def test_foreign_key_update_with_valid_null(self): +# data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) +# serializer.save() - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': None}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': None} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': None}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) - def test_foreign_key_update_with_valid_emptystring(self): - """ - The emptystring should be interpreted as null in the context - of relationships. - """ - data = {'id': 1, 'name': 'source-1', 'target': ''} - expected_data = {'id': 1, 'name': 'source-1', 'target': None} - instance = NullableForeignKeySource.objects.get(pk=1) - serializer = NullableForeignKeySourceSerializer(instance, data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, expected_data) - serializer.save() +# def test_foreign_key_update_with_valid_emptystring(self): +# """ +# The emptystring should be interpreted as null in the context +# of relationships. +# """ +# data = {'id': 1, 'name': 'source-1', 'target': ''} +# expected_data = {'id': 1, 'name': 'source-1', 'target': None} +# instance = NullableForeignKeySource.objects.get(pk=1) +# serializer = NullableForeignKeySourceSerializer(instance, data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, expected_data) +# serializer.save() - # Ensure source 1 is updated, and everything else is as expected - queryset = NullableForeignKeySource.objects.all() - serializer = NullableForeignKeySourceSerializer(queryset, many=True) - expected = [ - {'id': 1, 'name': 'source-1', 'target': None}, - {'id': 2, 'name': 'source-2', 'target': 'target-1'}, - {'id': 3, 'name': 'source-3', 'target': None} - ] - self.assertEqual(serializer.data, expected) +# # Ensure source 1 is updated, and everything else is as expected +# queryset = NullableForeignKeySource.objects.all() +# serializer = NullableForeignKeySourceSerializer(queryset, many=True) +# expected = [ +# {'id': 1, 'name': 'source-1', 'target': None}, +# {'id': 2, 'name': 'source-2', 'target': 'target-1'}, +# {'id': 3, 'name': 'source-3', 'target': None} +# ] +# self.assertEqual(serializer.data, expected) From c1036c17533a3091401ff90f825571f0e6125eca Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 3 Sep 2014 16:34:09 +0100 Subject: [PATCH 024/100] More test passing --- rest_framework/relations.py | 56 +- tests/test_serializer.py | 3748 +++++++++++++------------- tests/test_serializer_bulk_update.py | 484 ++-- tests/test_serializer_empty.py | 22 +- tests/test_serializer_import.py | 32 +- tests/test_serializer_nested.py | 586 ++-- tests/test_serializers.py | 50 +- 7 files changed, 2516 insertions(+), 2462 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 0b01394a8..661a1249a 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -1,6 +1,7 @@ from rest_framework.fields import Field +from rest_framework.reverse import reverse from django.core.exceptions import ObjectDoesNotExist -from django.core.urlresolvers import resolve, get_script_prefix +from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch from rest_framework.compat import urlparse @@ -100,11 +101,64 @@ class HyperlinkedIdentityField(RelatedField): lookup_field = 'pk' def __init__(self, **kwargs): + kwargs['read_only'] = True self.view_name = kwargs.pop('view_name') self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) super(HyperlinkedIdentityField, self).__init__(**kwargs) + def get_attribute(self, instance): + return instance + + def to_primative(self, value): + request = self.context.get('request', None) + format = self.context.get('format', None) + + assert request is not None, ( + "`HyperlinkedIdentityField` requires the request in the serializer" + " context. Add `context={'request': request}` when instantiating " + "the serializer." + ) + + # By default use whatever format is given for the current context + # unless the target is a different type to the source. + # + # Eg. Consider a HyperlinkedIdentityField pointing from a json + # representation to an html property of that representation... + # + # '/snippets/1/' should link to '/snippets/1/highlight/' + # ...but... + # '/snippets/1/.json' should link to '/snippets/1/highlight/.html' + if format and self.format and self.format != format: + format = self.format + + # Return the hyperlink, or error if incorrectly configured. + try: + return self.get_url(value, self.view_name, request, format) + except NoReverseMatch: + msg = ( + 'Could not resolve URL for hyperlinked relationship using ' + 'view name "%s". You may have failed to include the related ' + 'model in your API, or incorrectly configured the ' + '`lookup_field` attribute on this field.' + ) + raise Exception(msg % self.view_name) + + def get_url(self, obj, view_name, request, format): + """ + Given an object, return the URL that hyperlinks to the object. + + May raise a `NoReverseMatch` if the `view_name` and `lookup_field` + attributes are not configured to correctly match the URL conf. + """ + # Unsaved objects will not yet have a valid URL. + if obj.pk is None: + return None + + lookup_value = getattr(obj, self.lookup_field) + kwargs = {self.lookup_url_kwarg: lookup_value} + return reverse(view_name, kwargs=kwargs, request=request, format=format) + class SlugRelatedField(RelatedField): def __init__(self, **kwargs): diff --git a/tests/test_serializer.py b/tests/test_serializer.py index fa5cafcfb..b0eb4e270 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -1,2004 +1,2004 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals -from django.db import models -from django.db.models.fields import BLANK_CHOICE_DASH -from django.test import TestCase -from django.utils import unittest -from django.utils.datastructures import MultiValueDict -from django.utils.translation import ugettext_lazy as _ -from rest_framework import serializers, fields, relations -from tests.models import ( - HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel, - BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, - DefaultValueModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, - RESTFrameworkModel, ForeignKeySource -) -from tests.models import BasicModelSerializer -import datetime -import pickle -try: - import PIL -except: - PIL = None - - -if PIL is not None: - class AMOAFModel(RESTFrameworkModel): - char_field = models.CharField(max_length=1024, blank=True) - comma_separated_integer_field = models.CommaSeparatedIntegerField(max_length=1024, blank=True) - decimal_field = models.DecimalField(max_digits=64, decimal_places=32, blank=True) - email_field = models.EmailField(max_length=1024, blank=True) - file_field = models.FileField(upload_to='test', max_length=1024, blank=True) - image_field = models.ImageField(upload_to='test', max_length=1024, blank=True) - slug_field = models.SlugField(max_length=1024, blank=True) - url_field = models.URLField(max_length=1024, blank=True) - nullable_char_field = models.CharField(max_length=1024, blank=True, null=True) - - class DVOAFModel(RESTFrameworkModel): - positive_integer_field = models.PositiveIntegerField(blank=True) - positive_small_integer_field = models.PositiveSmallIntegerField(blank=True) - email_field = models.EmailField(blank=True) - file_field = models.FileField(upload_to='test', blank=True) - image_field = models.ImageField(upload_to='test', blank=True) - slug_field = models.SlugField(blank=True) - url_field = models.URLField(blank=True) +# # -*- coding: utf-8 -*- +# from __future__ import unicode_literals +# from django.db import models +# from django.db.models.fields import BLANK_CHOICE_DASH +# from django.test import TestCase +# from django.utils import unittest +# from django.utils.datastructures import MultiValueDict +# from django.utils.translation import ugettext_lazy as _ +# from rest_framework import serializers, fields, relations +# from tests.models import ( +# HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel, +# BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel, +# DefaultValueModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo, +# RESTFrameworkModel, ForeignKeySource +# ) +# from tests.models import BasicModelSerializer +# import datetime +# import pickle +# try: +# import PIL +# except: +# PIL = None + + +# if PIL is not None: +# class AMOAFModel(RESTFrameworkModel): +# char_field = models.CharField(max_length=1024, blank=True) +# comma_separated_integer_field = models.CommaSeparatedIntegerField(max_length=1024, blank=True) +# decimal_field = models.DecimalField(max_digits=64, decimal_places=32, blank=True) +# email_field = models.EmailField(max_length=1024, blank=True) +# file_field = models.FileField(upload_to='test', max_length=1024, blank=True) +# image_field = models.ImageField(upload_to='test', max_length=1024, blank=True) +# slug_field = models.SlugField(max_length=1024, blank=True) +# url_field = models.URLField(max_length=1024, blank=True) +# nullable_char_field = models.CharField(max_length=1024, blank=True, null=True) + +# class DVOAFModel(RESTFrameworkModel): +# positive_integer_field = models.PositiveIntegerField(blank=True) +# positive_small_integer_field = models.PositiveSmallIntegerField(blank=True) +# email_field = models.EmailField(blank=True) +# file_field = models.FileField(upload_to='test', blank=True) +# image_field = models.ImageField(upload_to='test', blank=True) +# slug_field = models.SlugField(blank=True) +# url_field = models.URLField(blank=True) -class SubComment(object): - def __init__(self, sub_comment): - self.sub_comment = sub_comment +# class SubComment(object): +# def __init__(self, sub_comment): +# self.sub_comment = sub_comment -class Comment(object): - def __init__(self, email, content, created): - self.email = email - self.content = content - self.created = created or datetime.datetime.now() +# class Comment(object): +# def __init__(self, email, content, created): +# self.email = email +# self.content = content +# self.created = created or datetime.datetime.now() - def __eq__(self, other): - return all([getattr(self, attr) == getattr(other, attr) - for attr in ('email', 'content', 'created')]) +# def __eq__(self, other): +# return all([getattr(self, attr) == getattr(other, attr) +# for attr in ('email', 'content', 'created')]) - def get_sub_comment(self): - sub_comment = SubComment('And Merry Christmas!') - return sub_comment +# def get_sub_comment(self): +# sub_comment = SubComment('And Merry Christmas!') +# return sub_comment -class CommentSerializer(serializers.Serializer): - email = serializers.EmailField() - content = serializers.CharField(max_length=1000) - created = serializers.DateTimeField() - sub_comment = serializers.Field(source='get_sub_comment.sub_comment') +# class CommentSerializer(serializers.Serializer): +# email = serializers.EmailField() +# content = serializers.CharField(max_length=1000) +# created = serializers.DateTimeField() +# sub_comment = serializers.Field(source='get_sub_comment.sub_comment') - def restore_object(self, data, instance=None): - if instance is None: - return Comment(**data) - for key, val in data.items(): - setattr(instance, key, val) - return instance +# def restore_object(self, data, instance=None): +# if instance is None: +# return Comment(**data) +# for key, val in data.items(): +# setattr(instance, key, val) +# return instance -class NamesSerializer(serializers.Serializer): - first = serializers.CharField() - last = serializers.CharField(required=False, default='') - initials = serializers.CharField(required=False, default='') +# class NamesSerializer(serializers.Serializer): +# first = serializers.CharField() +# last = serializers.CharField(required=False, default='') +# initials = serializers.CharField(required=False, default='') -class PersonIdentifierSerializer(serializers.Serializer): - ssn = serializers.CharField() - names = NamesSerializer(source='names', required=False) +# class PersonIdentifierSerializer(serializers.Serializer): +# ssn = serializers.CharField() +# names = NamesSerializer(source='names', required=False) -class BookSerializer(serializers.ModelSerializer): - isbn = serializers.RegexField(regex=r'^[0-9]{13}$', error_messages={'invalid': 'isbn has to be exact 13 numbers'}) +# class BookSerializer(serializers.ModelSerializer): +# isbn = serializers.RegexField(regex=r'^[0-9]{13}$', error_messages={'invalid': 'isbn has to be exact 13 numbers'}) - class Meta: - model = Book +# class Meta: +# model = Book -class ActionItemSerializer(serializers.ModelSerializer): +# class ActionItemSerializer(serializers.ModelSerializer): - class Meta: - model = ActionItem +# class Meta: +# model = ActionItem -class ActionItemSerializerOptionalFields(serializers.ModelSerializer): - """ - Intended to test that fields with `required=False` are excluded from validation. - """ - title = serializers.CharField(required=False) +# class ActionItemSerializerOptionalFields(serializers.ModelSerializer): +# """ +# Intended to test that fields with `required=False` are excluded from validation. +# """ +# title = serializers.CharField(required=False) - class Meta: - model = ActionItem - fields = ('title',) +# class Meta: +# model = ActionItem +# fields = ('title',) -class ActionItemSerializerCustomRestore(serializers.ModelSerializer): +# class ActionItemSerializerCustomRestore(serializers.ModelSerializer): - class Meta: - model = ActionItem +# class Meta: +# model = ActionItem - def restore_object(self, data, instance=None): - if instance is None: - return ActionItem(**data) - for key, val in data.items(): - setattr(instance, key, val) - return instance +# def restore_object(self, data, instance=None): +# if instance is None: +# return ActionItem(**data) +# for key, val in data.items(): +# setattr(instance, key, val) +# return instance -class PersonSerializer(serializers.ModelSerializer): - info = serializers.Field(source='info') +# class PersonSerializer(serializers.ModelSerializer): +# info = serializers.Field(source='info') - class Meta: - model = Person - fields = ('name', 'age', 'info') - read_only_fields = ('age',) +# class Meta: +# model = Person +# fields = ('name', 'age', 'info') +# read_only_fields = ('age',) -class NestedSerializer(serializers.Serializer): - info = serializers.Field() +# class NestedSerializer(serializers.Serializer): +# info = serializers.Field() -class ModelSerializerWithNestedSerializer(serializers.ModelSerializer): - nested = NestedSerializer(source='*') +# class ModelSerializerWithNestedSerializer(serializers.ModelSerializer): +# nested = NestedSerializer(source='*') - class Meta: - model = Person +# class Meta: +# model = Person -class NestedSerializerWithRenamedField(serializers.Serializer): - renamed_info = serializers.Field(source='info') +# class NestedSerializerWithRenamedField(serializers.Serializer): +# renamed_info = serializers.Field(source='info') -class ModelSerializerWithNestedSerializerWithRenamedField(serializers.ModelSerializer): - nested = NestedSerializerWithRenamedField(source='*') +# class ModelSerializerWithNestedSerializerWithRenamedField(serializers.ModelSerializer): +# nested = NestedSerializerWithRenamedField(source='*') - class Meta: - model = Person +# class Meta: +# model = Person -class PersonSerializerInvalidReadOnly(serializers.ModelSerializer): - """ - Testing for #652. - """ - info = serializers.Field(source='info') +# class PersonSerializerInvalidReadOnly(serializers.ModelSerializer): +# """ +# Testing for #652. +# """ +# info = serializers.Field(source='info') - class Meta: - model = Person - fields = ('name', 'age', 'info') - read_only_fields = ('age', 'info') +# class Meta: +# model = Person +# fields = ('name', 'age', 'info') +# read_only_fields = ('age', 'info') -class AlbumsSerializer(serializers.ModelSerializer): +# class AlbumsSerializer(serializers.ModelSerializer): - class Meta: - model = Album - fields = ['title', 'ref'] # lists are also valid options +# class Meta: +# model = Album +# fields = ['title', 'ref'] # lists are also valid options -class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer): - class Meta: - model = HasPositiveIntegerAsChoice - fields = ['some_integer'] +# class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer): +# class Meta: +# model = HasPositiveIntegerAsChoice +# fields = ['some_integer'] -class ForeignKeySourceSerializer(serializers.ModelSerializer): - class Meta: - model = ForeignKeySource +# class ForeignKeySourceSerializer(serializers.ModelSerializer): +# class Meta: +# model = ForeignKeySource -class HyperlinkedForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): - class Meta: - model = ForeignKeySource +# class HyperlinkedForeignKeySourceSerializer(serializers.HyperlinkedModelSerializer): +# class Meta: +# model = ForeignKeySource -class BasicTests(TestCase): - def setUp(self): - self.comment = Comment( - 'tom@example.com', - 'Happy new year!', - datetime.datetime(2012, 1, 1) - ) - self.actionitem = ActionItem(title='Some to do item',) - self.data = { - 'email': 'tom@example.com', - 'content': 'Happy new year!', - 'created': datetime.datetime(2012, 1, 1), - 'sub_comment': 'This wont change' - } - self.expected = { - 'email': 'tom@example.com', - 'content': 'Happy new year!', - 'created': datetime.datetime(2012, 1, 1), - 'sub_comment': 'And Merry Christmas!' - } - self.person_data = {'name': 'dwight', 'age': 35} - self.person = Person(**self.person_data) - self.person.save() - - def test_empty(self): - serializer = CommentSerializer() - expected = { - 'email': '', - 'content': '', - 'created': None - } - self.assertEqual(serializer.data, expected) - - def test_retrieve(self): - serializer = CommentSerializer(self.comment) - self.assertEqual(serializer.data, self.expected) - - def test_create(self): - serializer = CommentSerializer(data=self.data) - expected = self.comment - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected) - self.assertFalse(serializer.object is expected) - self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!') - - def test_create_nested(self): - """Test a serializer with nested data.""" - names = {'first': 'John', 'last': 'Doe', 'initials': 'jd'} - data = {'ssn': '1234567890', 'names': names} - serializer = PersonIdentifierSerializer(data=data) - - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) - self.assertFalse(serializer.object is data) - self.assertEqual(serializer.data['names'], names) - - def test_create_partial_nested(self): - """Test a serializer with nested data which has missing fields.""" - names = {'first': 'John'} - data = {'ssn': '1234567890', 'names': names} - serializer = PersonIdentifierSerializer(data=data) - - expected_names = {'first': 'John', 'last': '', 'initials': ''} - data['names'] = expected_names - - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) - self.assertFalse(serializer.object is expected_names) - self.assertEqual(serializer.data['names'], expected_names) - - def test_null_nested(self): - """Test a serializer with a nonexistent nested field""" - data = {'ssn': '1234567890'} - serializer = PersonIdentifierSerializer(data=data) - - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) - self.assertFalse(serializer.object is data) - expected = {'ssn': '1234567890', 'names': None} - self.assertEqual(serializer.data, expected) - - def test_update(self): - serializer = CommentSerializer(self.comment, data=self.data) - expected = self.comment - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected) - self.assertTrue(serializer.object is expected) - self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!') - - def test_partial_update(self): - msg = 'Merry New Year!' - partial_data = {'content': msg} - serializer = CommentSerializer(self.comment, data=partial_data) - self.assertEqual(serializer.is_valid(), False) - serializer = CommentSerializer(self.comment, data=partial_data, partial=True) - expected = self.comment - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected) - self.assertTrue(serializer.object is expected) - self.assertEqual(serializer.data['content'], msg) - - def test_model_fields_as_expected(self): - """ - Make sure that the fields returned are the same as defined - in the Meta data - """ - serializer = PersonSerializer(self.person) - self.assertEqual( - set(serializer.data.keys()), - set(['name', 'age', 'info']) - ) - - def test_field_with_dictionary(self): - """ - Make sure that dictionaries from fields are left intact - """ - serializer = PersonSerializer(self.person) - expected = self.person_data - self.assertEqual(serializer.data['info'], expected) - - def test_read_only_fields(self): - """ - Attempting to update fields set as read_only should have no effect. - """ - serializer = PersonSerializer(self.person, data={'name': 'dwight', 'age': 99}) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(serializer.errors, {}) - # Assert age is unchanged (35) - self.assertEqual(instance.age, self.person_data['age']) - - def test_invalid_read_only_fields(self): - """ - Regression test for #652. - """ - self.assertRaises(AssertionError, PersonSerializerInvalidReadOnly, []) - - def test_serializer_data_is_cleared_on_save(self): - """ - Check _data attribute is cleared on `save()` - - Regression test for #1116 - — id field is not populated if `data` is accessed prior to `save()` - """ - serializer = ActionItemSerializer(self.actionitem) - self.assertIsNone(serializer.data.get('id', None), 'New instance. `id` should not be set.') - serializer.save() - self.assertIsNotNone(serializer.data.get('id', None), 'Model is saved. `id` should be set.') - - def test_fields_marked_as_not_required_are_excluded_from_validation(self): - """ - Check that fields with `required=False` are included in list of exclusions. - """ - serializer = ActionItemSerializerOptionalFields(self.actionitem) - exclusions = serializer.get_validation_exclusions() - self.assertTrue('title' in exclusions, '`title` field was marked `required=False` and should be excluded') - - -class DictStyleSerializer(serializers.Serializer): - """ - Note that we don't have any `restore_object` method, so the default - case of simply returning a dict will apply. - """ - email = serializers.EmailField() - - -class DictStyleSerializerTests(TestCase): - def test_dict_style_deserialize(self): - """ - Ensure serializers can deserialize into a dict. - """ - data = {'email': 'foo@example.com'} - serializer = DictStyleSerializer(data=data) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, data) - - def test_dict_style_serialize(self): - """ - Ensure serializers can serialize dict objects. - """ - data = {'email': 'foo@example.com'} - serializer = DictStyleSerializer(data) - self.assertEqual(serializer.data, data) - - -class ValidationTests(TestCase): - def setUp(self): - self.comment = Comment( - 'tom@example.com', - 'Happy new year!', - datetime.datetime(2012, 1, 1) - ) - self.data = { - 'email': 'tom@example.com', - 'content': 'x' * 1001, - 'created': datetime.datetime(2012, 1, 1) - } - self.actionitem = ActionItem(title='Some to do item',) - - def test_create(self): - serializer = CommentSerializer(data=self.data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'content': ['Ensure this value has at most 1000 characters (it has 1001).']}) - - def test_update(self): - serializer = CommentSerializer(self.comment, data=self.data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'content': ['Ensure this value has at most 1000 characters (it has 1001).']}) - - def test_update_missing_field(self): - data = { - 'content': 'xxx', - 'created': datetime.datetime(2012, 1, 1) - } - serializer = CommentSerializer(self.comment, data=data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'email': ['This field is required.']}) - - def test_missing_bool_with_default(self): - """Make sure that a boolean value with a 'False' value is not - mistaken for not having a default.""" - data = { - 'title': 'Some action item', - # No 'done' value. - } - serializer = ActionItemSerializer(self.actionitem, data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.errors, {}) - - def test_cross_field_validation(self): - - class CommentSerializerWithCrossFieldValidator(CommentSerializer): - - def validate(self, attrs): - if attrs["email"] not in attrs["content"]: - raise serializers.ValidationError("Email address not in content") - return attrs - - data = { - 'email': 'tom@example.com', - 'content': 'A comment from tom@example.com', - 'created': datetime.datetime(2012, 1, 1) - } - - serializer = CommentSerializerWithCrossFieldValidator(data=data) - self.assertTrue(serializer.is_valid()) - - data['content'] = 'A comment from foo@bar.com' - - serializer = CommentSerializerWithCrossFieldValidator(data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'non_field_errors': ['Email address not in content']}) - - def test_null_is_true_fields(self): - """ - Omitting a value for null-field should validate. - """ - serializer = PersonSerializer(data={'name': 'marko'}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.errors, {}) - - def test_modelserializer_max_length_exceeded(self): - data = { - 'title': 'x' * 201, - } - serializer = ActionItemSerializer(data=data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'title': ['Ensure this value has at most 200 characters (it has 201).']}) - - def test_modelserializer_max_length_exceeded_with_custom_restore(self): - """ - When overriding ModelSerializer.restore_object, validation tests should still apply. - Regression test for #623. - - https://github.com/tomchristie/django-rest-framework/pull/623 - """ - data = { - 'title': 'x' * 201, - } - serializer = ActionItemSerializerCustomRestore(data=data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'title': ['Ensure this value has at most 200 characters (it has 201).']}) - - def test_default_modelfield_max_length_exceeded(self): - data = { - 'title': 'Testing "info" field...', - 'info': 'x' * 13, - } - serializer = ActionItemSerializer(data=data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']}) - - def test_datetime_validation_failure(self): - """ - Test DateTimeField validation errors on non-str values. - Regression test for #669. - - https://github.com/tomchristie/django-rest-framework/issues/669 - """ - data = self.data - data['created'] = 0 - - serializer = CommentSerializer(data=data) - self.assertEqual(serializer.is_valid(), False) - - self.assertIn('created', serializer.errors) - - def test_missing_model_field_exception_msg(self): - """ - Assert that a meaningful exception message is outputted when the model - field is missing (e.g. when mistyping ``model``). - """ - class BrokenModelSerializer(serializers.ModelSerializer): - class Meta: - fields = ['some_field'] - - try: - BrokenModelSerializer() - except AssertionError as e: - self.assertEqual(e.args[0], "Serializer class 'BrokenModelSerializer' is missing 'model' Meta option") - except: - self.fail('Wrong exception type thrown.') - - def test_writable_star_source_on_nested_serializer(self): - """ - Assert that a nested serializer instantiated with source='*' correctly - expands the data into the outer serializer. - """ - serializer = ModelSerializerWithNestedSerializer(data={ - 'name': 'marko', - 'nested': {'info': 'hi'}}, - ) - self.assertEqual(serializer.is_valid(), True) - - def test_writable_star_source_on_nested_serializer_with_parent_object(self): - class TitleSerializer(serializers.Serializer): - title = serializers.WritableField(source='title') - - class AlbumSerializer(serializers.ModelSerializer): - nested = TitleSerializer(source='*') - - class Meta: - model = Album - fields = ('nested',) - - class PhotoSerializer(serializers.ModelSerializer): - album = AlbumSerializer(source='album') - - class Meta: - model = Photo - fields = ('album', ) - - photo = Photo(album=Album()) - - data = {'album': {'nested': {'title': 'test'}}} - - serializer = PhotoSerializer(photo, data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data, data) - - def test_writable_star_source_with_inner_source_fields(self): - """ - Tests that a serializer with source="*" correctly expands the - it's fields into the outer serializer even if they have their - own 'source' parameters. - """ - - serializer = ModelSerializerWithNestedSerializerWithRenamedField(data={ - 'name': 'marko', - 'nested': {'renamed_info': 'hi'}}, - ) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.errors, {}) - - -class CustomValidationTests(TestCase): - class CommentSerializerWithFieldValidator(CommentSerializer): - - def validate_email(self, attrs, source): - attrs[source] - return attrs - - def validate_content(self, attrs, source): - value = attrs[source] - if "test" not in value: - raise serializers.ValidationError("Test not in value") - return attrs - - def test_field_validation(self): - data = { - 'email': 'tom@example.com', - 'content': 'A test comment', - 'created': datetime.datetime(2012, 1, 1) - } - - serializer = self.CommentSerializerWithFieldValidator(data=data) - self.assertTrue(serializer.is_valid()) - - data['content'] = 'This should not validate' - - serializer = self.CommentSerializerWithFieldValidator(data=data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'content': ['Test not in value']}) - - def test_missing_data(self): - """ - Make sure that validate_content isn't called if the field is missing - """ - incomplete_data = { - 'email': 'tom@example.com', - 'created': datetime.datetime(2012, 1, 1) - } - serializer = self.CommentSerializerWithFieldValidator(data=incomplete_data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'content': ['This field is required.']}) - - def test_wrong_data(self): - """ - Make sure that validate_content isn't called if the field input is wrong - """ - wrong_data = { - 'email': 'not an email', - 'content': 'A test comment', - 'created': datetime.datetime(2012, 1, 1) - } - serializer = self.CommentSerializerWithFieldValidator(data=wrong_data) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']}) - - def test_partial_update(self): - """ - Make sure that validate_email isn't called when partial=True and email - isn't found in data. - """ - initial_data = { - 'email': 'tom@example.com', - 'content': 'A test comment', - 'created': datetime.datetime(2012, 1, 1) - } - - serializer = self.CommentSerializerWithFieldValidator(data=initial_data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.object - - new_content = 'An *updated* test comment' - partial_data = { - 'content': new_content - } - - serializer = self.CommentSerializerWithFieldValidator(instance=instance, - data=partial_data, - partial=True) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.object - self.assertEqual(instance.content, new_content) - - -class PositiveIntegerAsChoiceTests(TestCase): - def test_positive_integer_in_json_is_correctly_parsed(self): - data = {'some_integer': 1} - serializer = PositiveIntegerAsChoiceSerializer(data=data) - self.assertEqual(serializer.is_valid(), True) - - -class ModelValidationTests(TestCase): - def test_validate_unique(self): - """ - Just check if serializers.ModelSerializer handles unique checks via .full_clean() - """ - serializer = AlbumsSerializer(data={'title': 'a', 'ref': '1'}) - serializer.is_valid() - serializer.save() - second_serializer = AlbumsSerializer(data={'title': 'a'}) - self.assertFalse(second_serializer.is_valid()) - self.assertEqual(second_serializer.errors, {'title': ['Album with this Title already exists.']}) - third_serializer = AlbumsSerializer(data=[{'title': 'b', 'ref': '1'}, {'title': 'c'}], many=True) - self.assertFalse(third_serializer.is_valid()) - self.assertEqual(third_serializer.errors, [{'ref': ['Album with this Ref already exists.']}, {}]) - - def test_foreign_key_is_null_with_partial(self): - """ - Test ModelSerializer validation with partial=True - - Specifically test that a null foreign key does not pass validation - """ - album = Album(title='test') - album.save() - - class PhotoSerializer(serializers.ModelSerializer): - class Meta: - model = Photo - - photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk}) - self.assertTrue(photo_serializer.is_valid()) - photo = photo_serializer.save() - - # Updating only the album (foreign key) - photo_serializer = PhotoSerializer(instance=photo, data={'album': ''}, partial=True) - self.assertFalse(photo_serializer.is_valid()) - self.assertTrue('album' in photo_serializer.errors) - self.assertEqual(photo_serializer.errors['album'], [photo_serializer.error_messages['required']]) - - def test_foreign_key_with_partial(self): - """ - Test ModelSerializer validation with partial=True - - Specifically test foreign key validation. - """ - - album = Album(title='test') - album.save() - - class PhotoSerializer(serializers.ModelSerializer): - class Meta: - model = Photo - - photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk}) - self.assertTrue(photo_serializer.is_valid()) - photo = photo_serializer.save() - - # Updating only the album (foreign key) - photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True) - self.assertTrue(photo_serializer.is_valid()) - self.assertTrue(photo_serializer.save()) - - # Updating only the description - photo_serializer = PhotoSerializer(instance=photo, - data={'description': 'new'}, - partial=True) - - self.assertTrue(photo_serializer.is_valid()) - self.assertTrue(photo_serializer.save()) - - -class RegexValidationTest(TestCase): - def test_create_failed(self): - serializer = BookSerializer(data={'isbn': '1234567890'}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) - - serializer = BookSerializer(data={'isbn': '12345678901234'}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) - - serializer = BookSerializer(data={'isbn': 'abcdefghijklm'}) - self.assertFalse(serializer.is_valid()) - self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) - - def test_create_success(self): - serializer = BookSerializer(data={'isbn': '1234567890123'}) - self.assertTrue(serializer.is_valid()) - - -class MetadataTests(TestCase): - def test_empty(self): - serializer = CommentSerializer() - expected = { - 'email': serializers.CharField, - 'content': serializers.CharField, - 'created': serializers.DateTimeField - } - for field_name, field in expected.items(): - self.assertTrue(isinstance(serializer.data.fields[field_name], field)) - - -class ManyToManyTests(TestCase): - def setUp(self): - class ManyToManySerializer(serializers.ModelSerializer): - class Meta: - model = ManyToManyModel - - self.serializer_class = ManyToManySerializer - - # An anchor instance to use for the relationship - self.anchor = Anchor() - self.anchor.save() - - # A model instance with a many to many relationship to the anchor - self.instance = ManyToManyModel() - self.instance.save() - self.instance.rel.add(self.anchor) - - # A serialized representation of the model instance - self.data = {'id': 1, 'rel': [self.anchor.id]} - - def test_retrieve(self): - """ - Serialize an instance of a model with a ManyToMany relationship. - """ - serializer = self.serializer_class(instance=self.instance) - expected = self.data - self.assertEqual(serializer.data, expected) - - def test_create(self): - """ - Create an instance of a model with a ManyToMany relationship. - """ - data = {'rel': [self.anchor.id]} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ManyToManyModel.objects.all()), 2) - self.assertEqual(instance.pk, 2) - self.assertEqual(list(instance.rel.all()), [self.anchor]) - - def test_update(self): - """ - Update an instance of a model with a ManyToMany relationship. - """ - new_anchor = Anchor() - new_anchor.save() - data = {'rel': [self.anchor.id, new_anchor.id]} - serializer = self.serializer_class(self.instance, data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ManyToManyModel.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(list(instance.rel.all()), [self.anchor, new_anchor]) - - def test_create_empty_relationship(self): - """ - Create an instance of a model with a ManyToMany relationship, - containing no items. - """ - data = {'rel': []} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ManyToManyModel.objects.all()), 2) - self.assertEqual(instance.pk, 2) - self.assertEqual(list(instance.rel.all()), []) - - def test_update_empty_relationship(self): - """ - Update an instance of a model with a ManyToMany relationship, - containing no items. - """ - new_anchor = Anchor() - new_anchor.save() - data = {'rel': []} - serializer = self.serializer_class(self.instance, data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ManyToManyModel.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(list(instance.rel.all()), []) - - def test_create_empty_relationship_flat_data(self): - """ - Create an instance of a model with a ManyToMany relationship, - containing no items, using a representation that does not support - lists (eg form data). - """ - data = MultiValueDict() - data.setlist('rel', ['']) - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ManyToManyModel.objects.all()), 2) - self.assertEqual(instance.pk, 2) - self.assertEqual(list(instance.rel.all()), []) - - -class ReadOnlyManyToManyTests(TestCase): - def setUp(self): - class ReadOnlyManyToManySerializer(serializers.ModelSerializer): - rel = serializers.RelatedField(many=True, read_only=True) - - class Meta: - model = ReadOnlyManyToManyModel - - self.serializer_class = ReadOnlyManyToManySerializer - - # An anchor instance to use for the relationship - self.anchor = Anchor() - self.anchor.save() - - # A model instance with a many to many relationship to the anchor - self.instance = ReadOnlyManyToManyModel() - self.instance.save() - self.instance.rel.add(self.anchor) - - # A serialized representation of the model instance - self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'} - - def test_update(self): - """ - Attempt to update an instance of a model with a ManyToMany - relationship. Not updated due to read_only=True - """ - new_anchor = Anchor() - new_anchor.save() - data = {'rel': [self.anchor.id, new_anchor.id]} - serializer = self.serializer_class(self.instance, data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1) - self.assertEqual(instance.pk, 1) - # rel is still as original (1 entry) - self.assertEqual(list(instance.rel.all()), [self.anchor]) - - def test_update_without_relationship(self): - """ - Attempt to update an instance of a model where many to ManyToMany - relationship is not supplied. Not updated due to read_only=True - """ - new_anchor = Anchor() - new_anchor.save() - data = {} - serializer = self.serializer_class(self.instance, data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1) - self.assertEqual(instance.pk, 1) - # rel is still as original (1 entry) - self.assertEqual(list(instance.rel.all()), [self.anchor]) - - -class DefaultValueTests(TestCase): - def setUp(self): - class DefaultValueSerializer(serializers.ModelSerializer): - class Meta: - model = DefaultValueModel - - self.serializer_class = DefaultValueSerializer - self.objects = DefaultValueModel.objects - - def test_create_using_default(self): - data = {} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(self.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(instance.text, 'foobar') - - def test_create_overriding_default(self): - data = {'text': 'overridden'} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(self.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(instance.text, 'overridden') - - def test_partial_update_default(self): - """ Regression test for issue #532 """ - data = {'text': 'overridden'} - serializer = self.serializer_class(data=data, partial=True) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - - data = {'extra': 'extra_value'} - serializer = self.serializer_class(instance=instance, data=data, partial=True) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - - self.assertEqual(instance.extra, 'extra_value') - self.assertEqual(instance.text, 'overridden') - - -class WritableFieldDefaultValueTests(TestCase): - - def setUp(self): - self.expected = {'default': 'value'} - self.create_field = fields.WritableField - - def test_get_default_value_with_noncallable(self): - field = self.create_field(default=self.expected) - got = field.get_default_value() - self.assertEqual(got, self.expected) - - def test_get_default_value_with_callable(self): - field = self.create_field(default=lambda: self.expected) - got = field.get_default_value() - self.assertEqual(got, self.expected) - - def test_get_default_value_when_not_required(self): - field = self.create_field(default=self.expected, required=False) - got = field.get_default_value() - self.assertEqual(got, self.expected) - - def test_get_default_value_returns_None(self): - field = self.create_field() - got = field.get_default_value() - self.assertIsNone(got) - - def test_get_default_value_returns_non_True_values(self): - values = [None, '', False, 0, [], (), {}] # values that assumed as 'False' in the 'if' clause - for expected in values: - field = self.create_field(default=expected) - got = field.get_default_value() - self.assertEqual(got, expected) - - -class RelatedFieldDefaultValueTests(WritableFieldDefaultValueTests): - - def setUp(self): - self.expected = {'foo': 'bar'} - self.create_field = relations.RelatedField - - def test_get_default_value_returns_empty_list(self): - field = self.create_field(many=True) - got = field.get_default_value() - self.assertListEqual(got, []) - - def test_get_default_value_returns_expected(self): - expected = [1, 2, 3] - field = self.create_field(many=True, default=expected) - got = field.get_default_value() - self.assertListEqual(got, expected) - - -class CallableDefaultValueTests(TestCase): - def setUp(self): - class CallableDefaultValueSerializer(serializers.ModelSerializer): - class Meta: - model = CallableDefaultValueModel - - self.serializer_class = CallableDefaultValueSerializer - self.objects = CallableDefaultValueModel.objects - - def test_create_using_default(self): - data = {} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(self.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(instance.text, 'foobar') - - def test_create_overriding_default(self): - data = {'text': 'overridden'} - serializer = self.serializer_class(data=data) - self.assertEqual(serializer.is_valid(), True) - instance = serializer.save() - self.assertEqual(len(self.objects.all()), 1) - self.assertEqual(instance.pk, 1) - self.assertEqual(instance.text, 'overridden') - - -class ManyRelatedTests(TestCase): - def test_reverse_relations(self): - post = BlogPost.objects.create(title="Test blog post") - post.blogpostcomment_set.create(text="I hate this blog post") - post.blogpostcomment_set.create(text="I love this blog post") - - class BlogPostCommentSerializer(serializers.Serializer): - text = serializers.CharField() - - class BlogPostSerializer(serializers.Serializer): - title = serializers.CharField() - comments = BlogPostCommentSerializer(source='blogpostcomment_set') - - serializer = BlogPostSerializer(instance=post) - expected = { - 'title': 'Test blog post', - 'comments': [ - {'text': 'I hate this blog post'}, - {'text': 'I love this blog post'} - ] - } - - self.assertEqual(serializer.data, expected) - - def test_include_reverse_relations(self): - post = BlogPost.objects.create(title="Test blog post") - post.blogpostcomment_set.create(text="I hate this blog post") - post.blogpostcomment_set.create(text="I love this blog post") - - class BlogPostSerializer(serializers.ModelSerializer): - class Meta: - model = BlogPost - fields = ('id', 'title', 'blogpostcomment_set') - - serializer = BlogPostSerializer(instance=post) - expected = { - 'id': 1, 'title': 'Test blog post', 'blogpostcomment_set': [1, 2] - } - self.assertEqual(serializer.data, expected) - - def test_depth_include_reverse_relations(self): - post = BlogPost.objects.create(title="Test blog post") - post.blogpostcomment_set.create(text="I hate this blog post") - post.blogpostcomment_set.create(text="I love this blog post") - - class BlogPostSerializer(serializers.ModelSerializer): - class Meta: - model = BlogPost - fields = ('id', 'title', 'blogpostcomment_set') - depth = 1 - - serializer = BlogPostSerializer(instance=post) - expected = { - 'id': 1, 'title': 'Test blog post', - 'blogpostcomment_set': [ - {'id': 1, 'text': 'I hate this blog post', 'blog_post': 1}, - {'id': 2, 'text': 'I love this blog post', 'blog_post': 1} - ] - } - self.assertEqual(serializer.data, expected) - - def test_callable_source(self): - post = BlogPost.objects.create(title="Test blog post") - post.blogpostcomment_set.create(text="I love this blog post") - - class BlogPostCommentSerializer(serializers.Serializer): - text = serializers.CharField() - - class BlogPostSerializer(serializers.Serializer): - title = serializers.CharField() - first_comment = BlogPostCommentSerializer(source='get_first_comment') - - serializer = BlogPostSerializer(post) - - expected = { - 'title': 'Test blog post', - 'first_comment': {'text': 'I love this blog post'} - } - self.assertEqual(serializer.data, expected) - - -class RelatedTraversalTest(TestCase): - def test_nested_traversal(self): - """ - Source argument should support dotted.source notation. - """ - user = Person.objects.create(name="django") - post = BlogPost.objects.create(title="Test blog post", writer=user) - post.blogpostcomment_set.create(text="I love this blog post") +# class BasicTests(TestCase): +# def setUp(self): +# self.comment = Comment( +# 'tom@example.com', +# 'Happy new year!', +# datetime.datetime(2012, 1, 1) +# ) +# self.actionitem = ActionItem(title='Some to do item',) +# self.data = { +# 'email': 'tom@example.com', +# 'content': 'Happy new year!', +# 'created': datetime.datetime(2012, 1, 1), +# 'sub_comment': 'This wont change' +# } +# self.expected = { +# 'email': 'tom@example.com', +# 'content': 'Happy new year!', +# 'created': datetime.datetime(2012, 1, 1), +# 'sub_comment': 'And Merry Christmas!' +# } +# self.person_data = {'name': 'dwight', 'age': 35} +# self.person = Person(**self.person_data) +# self.person.save() + +# def test_empty(self): +# serializer = CommentSerializer() +# expected = { +# 'email': '', +# 'content': '', +# 'created': None +# } +# self.assertEqual(serializer.data, expected) + +# def test_retrieve(self): +# serializer = CommentSerializer(self.comment) +# self.assertEqual(serializer.data, self.expected) + +# def test_create(self): +# serializer = CommentSerializer(data=self.data) +# expected = self.comment +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected) +# self.assertFalse(serializer.object is expected) +# self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!') + +# def test_create_nested(self): +# """Test a serializer with nested data.""" +# names = {'first': 'John', 'last': 'Doe', 'initials': 'jd'} +# data = {'ssn': '1234567890', 'names': names} +# serializer = PersonIdentifierSerializer(data=data) + +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) +# self.assertFalse(serializer.object is data) +# self.assertEqual(serializer.data['names'], names) + +# def test_create_partial_nested(self): +# """Test a serializer with nested data which has missing fields.""" +# names = {'first': 'John'} +# data = {'ssn': '1234567890', 'names': names} +# serializer = PersonIdentifierSerializer(data=data) + +# expected_names = {'first': 'John', 'last': '', 'initials': ''} +# data['names'] = expected_names + +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) +# self.assertFalse(serializer.object is expected_names) +# self.assertEqual(serializer.data['names'], expected_names) + +# def test_null_nested(self): +# """Test a serializer with a nonexistent nested field""" +# data = {'ssn': '1234567890'} +# serializer = PersonIdentifierSerializer(data=data) + +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) +# self.assertFalse(serializer.object is data) +# expected = {'ssn': '1234567890', 'names': None} +# self.assertEqual(serializer.data, expected) + +# def test_update(self): +# serializer = CommentSerializer(self.comment, data=self.data) +# expected = self.comment +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected) +# self.assertTrue(serializer.object is expected) +# self.assertEqual(serializer.data['sub_comment'], 'And Merry Christmas!') + +# def test_partial_update(self): +# msg = 'Merry New Year!' +# partial_data = {'content': msg} +# serializer = CommentSerializer(self.comment, data=partial_data) +# self.assertEqual(serializer.is_valid(), False) +# serializer = CommentSerializer(self.comment, data=partial_data, partial=True) +# expected = self.comment +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected) +# self.assertTrue(serializer.object is expected) +# self.assertEqual(serializer.data['content'], msg) + +# def test_model_fields_as_expected(self): +# """ +# Make sure that the fields returned are the same as defined +# in the Meta data +# """ +# serializer = PersonSerializer(self.person) +# self.assertEqual( +# set(serializer.data.keys()), +# set(['name', 'age', 'info']) +# ) + +# def test_field_with_dictionary(self): +# """ +# Make sure that dictionaries from fields are left intact +# """ +# serializer = PersonSerializer(self.person) +# expected = self.person_data +# self.assertEqual(serializer.data['info'], expected) + +# def test_read_only_fields(self): +# """ +# Attempting to update fields set as read_only should have no effect. +# """ +# serializer = PersonSerializer(self.person, data={'name': 'dwight', 'age': 99}) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(serializer.errors, {}) +# # Assert age is unchanged (35) +# self.assertEqual(instance.age, self.person_data['age']) + +# def test_invalid_read_only_fields(self): +# """ +# Regression test for #652. +# """ +# self.assertRaises(AssertionError, PersonSerializerInvalidReadOnly, []) + +# def test_serializer_data_is_cleared_on_save(self): +# """ +# Check _data attribute is cleared on `save()` + +# Regression test for #1116 +# — id field is not populated if `data` is accessed prior to `save()` +# """ +# serializer = ActionItemSerializer(self.actionitem) +# self.assertIsNone(serializer.data.get('id', None), 'New instance. `id` should not be set.') +# serializer.save() +# self.assertIsNotNone(serializer.data.get('id', None), 'Model is saved. `id` should be set.') + +# def test_fields_marked_as_not_required_are_excluded_from_validation(self): +# """ +# Check that fields with `required=False` are included in list of exclusions. +# """ +# serializer = ActionItemSerializerOptionalFields(self.actionitem) +# exclusions = serializer.get_validation_exclusions() +# self.assertTrue('title' in exclusions, '`title` field was marked `required=False` and should be excluded') + + +# class DictStyleSerializer(serializers.Serializer): +# """ +# Note that we don't have any `restore_object` method, so the default +# case of simply returning a dict will apply. +# """ +# email = serializers.EmailField() + + +# class DictStyleSerializerTests(TestCase): +# def test_dict_style_deserialize(self): +# """ +# Ensure serializers can deserialize into a dict. +# """ +# data = {'email': 'foo@example.com'} +# serializer = DictStyleSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, data) + +# def test_dict_style_serialize(self): +# """ +# Ensure serializers can serialize dict objects. +# """ +# data = {'email': 'foo@example.com'} +# serializer = DictStyleSerializer(data) +# self.assertEqual(serializer.data, data) + + +# class ValidationTests(TestCase): +# def setUp(self): +# self.comment = Comment( +# 'tom@example.com', +# 'Happy new year!', +# datetime.datetime(2012, 1, 1) +# ) +# self.data = { +# 'email': 'tom@example.com', +# 'content': 'x' * 1001, +# 'created': datetime.datetime(2012, 1, 1) +# } +# self.actionitem = ActionItem(title='Some to do item',) + +# def test_create(self): +# serializer = CommentSerializer(data=self.data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'content': ['Ensure this value has at most 1000 characters (it has 1001).']}) + +# def test_update(self): +# serializer = CommentSerializer(self.comment, data=self.data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'content': ['Ensure this value has at most 1000 characters (it has 1001).']}) + +# def test_update_missing_field(self): +# data = { +# 'content': 'xxx', +# 'created': datetime.datetime(2012, 1, 1) +# } +# serializer = CommentSerializer(self.comment, data=data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'email': ['This field is required.']}) + +# def test_missing_bool_with_default(self): +# """Make sure that a boolean value with a 'False' value is not +# mistaken for not having a default.""" +# data = { +# 'title': 'Some action item', +# # No 'done' value. +# } +# serializer = ActionItemSerializer(self.actionitem, data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.errors, {}) + +# def test_cross_field_validation(self): + +# class CommentSerializerWithCrossFieldValidator(CommentSerializer): + +# def validate(self, attrs): +# if attrs["email"] not in attrs["content"]: +# raise serializers.ValidationError("Email address not in content") +# return attrs + +# data = { +# 'email': 'tom@example.com', +# 'content': 'A comment from tom@example.com', +# 'created': datetime.datetime(2012, 1, 1) +# } + +# serializer = CommentSerializerWithCrossFieldValidator(data=data) +# self.assertTrue(serializer.is_valid()) + +# data['content'] = 'A comment from foo@bar.com' + +# serializer = CommentSerializerWithCrossFieldValidator(data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'non_field_errors': ['Email address not in content']}) + +# def test_null_is_true_fields(self): +# """ +# Omitting a value for null-field should validate. +# """ +# serializer = PersonSerializer(data={'name': 'marko'}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.errors, {}) + +# def test_modelserializer_max_length_exceeded(self): +# data = { +# 'title': 'x' * 201, +# } +# serializer = ActionItemSerializer(data=data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'title': ['Ensure this value has at most 200 characters (it has 201).']}) + +# def test_modelserializer_max_length_exceeded_with_custom_restore(self): +# """ +# When overriding ModelSerializer.restore_object, validation tests should still apply. +# Regression test for #623. + +# https://github.com/tomchristie/django-rest-framework/pull/623 +# """ +# data = { +# 'title': 'x' * 201, +# } +# serializer = ActionItemSerializerCustomRestore(data=data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'title': ['Ensure this value has at most 200 characters (it has 201).']}) + +# def test_default_modelfield_max_length_exceeded(self): +# data = { +# 'title': 'Testing "info" field...', +# 'info': 'x' * 13, +# } +# serializer = ActionItemSerializer(data=data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']}) + +# def test_datetime_validation_failure(self): +# """ +# Test DateTimeField validation errors on non-str values. +# Regression test for #669. + +# https://github.com/tomchristie/django-rest-framework/issues/669 +# """ +# data = self.data +# data['created'] = 0 + +# serializer = CommentSerializer(data=data) +# self.assertEqual(serializer.is_valid(), False) + +# self.assertIn('created', serializer.errors) + +# def test_missing_model_field_exception_msg(self): +# """ +# Assert that a meaningful exception message is outputted when the model +# field is missing (e.g. when mistyping ``model``). +# """ +# class BrokenModelSerializer(serializers.ModelSerializer): +# class Meta: +# fields = ['some_field'] + +# try: +# BrokenModelSerializer() +# except AssertionError as e: +# self.assertEqual(e.args[0], "Serializer class 'BrokenModelSerializer' is missing 'model' Meta option") +# except: +# self.fail('Wrong exception type thrown.') + +# def test_writable_star_source_on_nested_serializer(self): +# """ +# Assert that a nested serializer instantiated with source='*' correctly +# expands the data into the outer serializer. +# """ +# serializer = ModelSerializerWithNestedSerializer(data={ +# 'name': 'marko', +# 'nested': {'info': 'hi'}}, +# ) +# self.assertEqual(serializer.is_valid(), True) + +# def test_writable_star_source_on_nested_serializer_with_parent_object(self): +# class TitleSerializer(serializers.Serializer): +# title = serializers.WritableField(source='title') + +# class AlbumSerializer(serializers.ModelSerializer): +# nested = TitleSerializer(source='*') + +# class Meta: +# model = Album +# fields = ('nested',) + +# class PhotoSerializer(serializers.ModelSerializer): +# album = AlbumSerializer(source='album') + +# class Meta: +# model = Photo +# fields = ('album', ) + +# photo = Photo(album=Album()) + +# data = {'album': {'nested': {'title': 'test'}}} + +# serializer = PhotoSerializer(photo, data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data, data) + +# def test_writable_star_source_with_inner_source_fields(self): +# """ +# Tests that a serializer with source="*" correctly expands the +# it's fields into the outer serializer even if they have their +# own 'source' parameters. +# """ + +# serializer = ModelSerializerWithNestedSerializerWithRenamedField(data={ +# 'name': 'marko', +# 'nested': {'renamed_info': 'hi'}}, +# ) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.errors, {}) + + +# class CustomValidationTests(TestCase): +# class CommentSerializerWithFieldValidator(CommentSerializer): + +# def validate_email(self, attrs, source): +# attrs[source] +# return attrs + +# def validate_content(self, attrs, source): +# value = attrs[source] +# if "test" not in value: +# raise serializers.ValidationError("Test not in value") +# return attrs + +# def test_field_validation(self): +# data = { +# 'email': 'tom@example.com', +# 'content': 'A test comment', +# 'created': datetime.datetime(2012, 1, 1) +# } + +# serializer = self.CommentSerializerWithFieldValidator(data=data) +# self.assertTrue(serializer.is_valid()) + +# data['content'] = 'This should not validate' + +# serializer = self.CommentSerializerWithFieldValidator(data=data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'content': ['Test not in value']}) + +# def test_missing_data(self): +# """ +# Make sure that validate_content isn't called if the field is missing +# """ +# incomplete_data = { +# 'email': 'tom@example.com', +# 'created': datetime.datetime(2012, 1, 1) +# } +# serializer = self.CommentSerializerWithFieldValidator(data=incomplete_data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'content': ['This field is required.']}) + +# def test_wrong_data(self): +# """ +# Make sure that validate_content isn't called if the field input is wrong +# """ +# wrong_data = { +# 'email': 'not an email', +# 'content': 'A test comment', +# 'created': datetime.datetime(2012, 1, 1) +# } +# serializer = self.CommentSerializerWithFieldValidator(data=wrong_data) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'email': ['Enter a valid email address.']}) + +# def test_partial_update(self): +# """ +# Make sure that validate_email isn't called when partial=True and email +# isn't found in data. +# """ +# initial_data = { +# 'email': 'tom@example.com', +# 'content': 'A test comment', +# 'created': datetime.datetime(2012, 1, 1) +# } + +# serializer = self.CommentSerializerWithFieldValidator(data=initial_data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.object + +# new_content = 'An *updated* test comment' +# partial_data = { +# 'content': new_content +# } + +# serializer = self.CommentSerializerWithFieldValidator(instance=instance, +# data=partial_data, +# partial=True) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.object +# self.assertEqual(instance.content, new_content) + + +# class PositiveIntegerAsChoiceTests(TestCase): +# def test_positive_integer_in_json_is_correctly_parsed(self): +# data = {'some_integer': 1} +# serializer = PositiveIntegerAsChoiceSerializer(data=data) +# self.assertEqual(serializer.is_valid(), True) + + +# class ModelValidationTests(TestCase): +# def test_validate_unique(self): +# """ +# Just check if serializers.ModelSerializer handles unique checks via .full_clean() +# """ +# serializer = AlbumsSerializer(data={'title': 'a', 'ref': '1'}) +# serializer.is_valid() +# serializer.save() +# second_serializer = AlbumsSerializer(data={'title': 'a'}) +# self.assertFalse(second_serializer.is_valid()) +# self.assertEqual(second_serializer.errors, {'title': ['Album with this Title already exists.']}) +# third_serializer = AlbumsSerializer(data=[{'title': 'b', 'ref': '1'}, {'title': 'c'}], many=True) +# self.assertFalse(third_serializer.is_valid()) +# self.assertEqual(third_serializer.errors, [{'ref': ['Album with this Ref already exists.']}, {}]) + +# def test_foreign_key_is_null_with_partial(self): +# """ +# Test ModelSerializer validation with partial=True + +# Specifically test that a null foreign key does not pass validation +# """ +# album = Album(title='test') +# album.save() + +# class PhotoSerializer(serializers.ModelSerializer): +# class Meta: +# model = Photo + +# photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk}) +# self.assertTrue(photo_serializer.is_valid()) +# photo = photo_serializer.save() + +# # Updating only the album (foreign key) +# photo_serializer = PhotoSerializer(instance=photo, data={'album': ''}, partial=True) +# self.assertFalse(photo_serializer.is_valid()) +# self.assertTrue('album' in photo_serializer.errors) +# self.assertEqual(photo_serializer.errors['album'], [photo_serializer.error_messages['required']]) + +# def test_foreign_key_with_partial(self): +# """ +# Test ModelSerializer validation with partial=True + +# Specifically test foreign key validation. +# """ + +# album = Album(title='test') +# album.save() + +# class PhotoSerializer(serializers.ModelSerializer): +# class Meta: +# model = Photo + +# photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk}) +# self.assertTrue(photo_serializer.is_valid()) +# photo = photo_serializer.save() + +# # Updating only the album (foreign key) +# photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True) +# self.assertTrue(photo_serializer.is_valid()) +# self.assertTrue(photo_serializer.save()) + +# # Updating only the description +# photo_serializer = PhotoSerializer(instance=photo, +# data={'description': 'new'}, +# partial=True) + +# self.assertTrue(photo_serializer.is_valid()) +# self.assertTrue(photo_serializer.save()) + + +# class RegexValidationTest(TestCase): +# def test_create_failed(self): +# serializer = BookSerializer(data={'isbn': '1234567890'}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) + +# serializer = BookSerializer(data={'isbn': '12345678901234'}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) + +# serializer = BookSerializer(data={'isbn': 'abcdefghijklm'}) +# self.assertFalse(serializer.is_valid()) +# self.assertEqual(serializer.errors, {'isbn': ['isbn has to be exact 13 numbers']}) + +# def test_create_success(self): +# serializer = BookSerializer(data={'isbn': '1234567890123'}) +# self.assertTrue(serializer.is_valid()) + + +# class MetadataTests(TestCase): +# def test_empty(self): +# serializer = CommentSerializer() +# expected = { +# 'email': serializers.CharField, +# 'content': serializers.CharField, +# 'created': serializers.DateTimeField +# } +# for field_name, field in expected.items(): +# self.assertTrue(isinstance(serializer.data.fields[field_name], field)) + + +# class ManyToManyTests(TestCase): +# def setUp(self): +# class ManyToManySerializer(serializers.ModelSerializer): +# class Meta: +# model = ManyToManyModel + +# self.serializer_class = ManyToManySerializer + +# # An anchor instance to use for the relationship +# self.anchor = Anchor() +# self.anchor.save() + +# # A model instance with a many to many relationship to the anchor +# self.instance = ManyToManyModel() +# self.instance.save() +# self.instance.rel.add(self.anchor) + +# # A serialized representation of the model instance +# self.data = {'id': 1, 'rel': [self.anchor.id]} + +# def test_retrieve(self): +# """ +# Serialize an instance of a model with a ManyToMany relationship. +# """ +# serializer = self.serializer_class(instance=self.instance) +# expected = self.data +# self.assertEqual(serializer.data, expected) + +# def test_create(self): +# """ +# Create an instance of a model with a ManyToMany relationship. +# """ +# data = {'rel': [self.anchor.id]} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ManyToManyModel.objects.all()), 2) +# self.assertEqual(instance.pk, 2) +# self.assertEqual(list(instance.rel.all()), [self.anchor]) + +# def test_update(self): +# """ +# Update an instance of a model with a ManyToMany relationship. +# """ +# new_anchor = Anchor() +# new_anchor.save() +# data = {'rel': [self.anchor.id, new_anchor.id]} +# serializer = self.serializer_class(self.instance, data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ManyToManyModel.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(list(instance.rel.all()), [self.anchor, new_anchor]) + +# def test_create_empty_relationship(self): +# """ +# Create an instance of a model with a ManyToMany relationship, +# containing no items. +# """ +# data = {'rel': []} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ManyToManyModel.objects.all()), 2) +# self.assertEqual(instance.pk, 2) +# self.assertEqual(list(instance.rel.all()), []) + +# def test_update_empty_relationship(self): +# """ +# Update an instance of a model with a ManyToMany relationship, +# containing no items. +# """ +# new_anchor = Anchor() +# new_anchor.save() +# data = {'rel': []} +# serializer = self.serializer_class(self.instance, data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ManyToManyModel.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(list(instance.rel.all()), []) + +# def test_create_empty_relationship_flat_data(self): +# """ +# Create an instance of a model with a ManyToMany relationship, +# containing no items, using a representation that does not support +# lists (eg form data). +# """ +# data = MultiValueDict() +# data.setlist('rel', ['']) +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ManyToManyModel.objects.all()), 2) +# self.assertEqual(instance.pk, 2) +# self.assertEqual(list(instance.rel.all()), []) + + +# class ReadOnlyManyToManyTests(TestCase): +# def setUp(self): +# class ReadOnlyManyToManySerializer(serializers.ModelSerializer): +# rel = serializers.RelatedField(many=True, read_only=True) + +# class Meta: +# model = ReadOnlyManyToManyModel + +# self.serializer_class = ReadOnlyManyToManySerializer + +# # An anchor instance to use for the relationship +# self.anchor = Anchor() +# self.anchor.save() + +# # A model instance with a many to many relationship to the anchor +# self.instance = ReadOnlyManyToManyModel() +# self.instance.save() +# self.instance.rel.add(self.anchor) + +# # A serialized representation of the model instance +# self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'} + +# def test_update(self): +# """ +# Attempt to update an instance of a model with a ManyToMany +# relationship. Not updated due to read_only=True +# """ +# new_anchor = Anchor() +# new_anchor.save() +# data = {'rel': [self.anchor.id, new_anchor.id]} +# serializer = self.serializer_class(self.instance, data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# # rel is still as original (1 entry) +# self.assertEqual(list(instance.rel.all()), [self.anchor]) + +# def test_update_without_relationship(self): +# """ +# Attempt to update an instance of a model where many to ManyToMany +# relationship is not supplied. Not updated due to read_only=True +# """ +# new_anchor = Anchor() +# new_anchor.save() +# data = {} +# serializer = self.serializer_class(self.instance, data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# # rel is still as original (1 entry) +# self.assertEqual(list(instance.rel.all()), [self.anchor]) + + +# class DefaultValueTests(TestCase): +# def setUp(self): +# class DefaultValueSerializer(serializers.ModelSerializer): +# class Meta: +# model = DefaultValueModel + +# self.serializer_class = DefaultValueSerializer +# self.objects = DefaultValueModel.objects + +# def test_create_using_default(self): +# data = {} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(self.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(instance.text, 'foobar') + +# def test_create_overriding_default(self): +# data = {'text': 'overridden'} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(self.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(instance.text, 'overridden') + +# def test_partial_update_default(self): +# """ Regression test for issue #532 """ +# data = {'text': 'overridden'} +# serializer = self.serializer_class(data=data, partial=True) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() + +# data = {'extra': 'extra_value'} +# serializer = self.serializer_class(instance=instance, data=data, partial=True) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() + +# self.assertEqual(instance.extra, 'extra_value') +# self.assertEqual(instance.text, 'overridden') + + +# class WritableFieldDefaultValueTests(TestCase): + +# def setUp(self): +# self.expected = {'default': 'value'} +# self.create_field = fields.WritableField + +# def test_get_default_value_with_noncallable(self): +# field = self.create_field(default=self.expected) +# got = field.get_default_value() +# self.assertEqual(got, self.expected) + +# def test_get_default_value_with_callable(self): +# field = self.create_field(default=lambda: self.expected) +# got = field.get_default_value() +# self.assertEqual(got, self.expected) + +# def test_get_default_value_when_not_required(self): +# field = self.create_field(default=self.expected, required=False) +# got = field.get_default_value() +# self.assertEqual(got, self.expected) + +# def test_get_default_value_returns_None(self): +# field = self.create_field() +# got = field.get_default_value() +# self.assertIsNone(got) + +# def test_get_default_value_returns_non_True_values(self): +# values = [None, '', False, 0, [], (), {}] # values that assumed as 'False' in the 'if' clause +# for expected in values: +# field = self.create_field(default=expected) +# got = field.get_default_value() +# self.assertEqual(got, expected) + + +# class RelatedFieldDefaultValueTests(WritableFieldDefaultValueTests): + +# def setUp(self): +# self.expected = {'foo': 'bar'} +# self.create_field = relations.RelatedField + +# def test_get_default_value_returns_empty_list(self): +# field = self.create_field(many=True) +# got = field.get_default_value() +# self.assertListEqual(got, []) + +# def test_get_default_value_returns_expected(self): +# expected = [1, 2, 3] +# field = self.create_field(many=True, default=expected) +# got = field.get_default_value() +# self.assertListEqual(got, expected) + + +# class CallableDefaultValueTests(TestCase): +# def setUp(self): +# class CallableDefaultValueSerializer(serializers.ModelSerializer): +# class Meta: +# model = CallableDefaultValueModel + +# self.serializer_class = CallableDefaultValueSerializer +# self.objects = CallableDefaultValueModel.objects + +# def test_create_using_default(self): +# data = {} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(self.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(instance.text, 'foobar') + +# def test_create_overriding_default(self): +# data = {'text': 'overridden'} +# serializer = self.serializer_class(data=data) +# self.assertEqual(serializer.is_valid(), True) +# instance = serializer.save() +# self.assertEqual(len(self.objects.all()), 1) +# self.assertEqual(instance.pk, 1) +# self.assertEqual(instance.text, 'overridden') + + +# class ManyRelatedTests(TestCase): +# def test_reverse_relations(self): +# post = BlogPost.objects.create(title="Test blog post") +# post.blogpostcomment_set.create(text="I hate this blog post") +# post.blogpostcomment_set.create(text="I love this blog post") + +# class BlogPostCommentSerializer(serializers.Serializer): +# text = serializers.CharField() + +# class BlogPostSerializer(serializers.Serializer): +# title = serializers.CharField() +# comments = BlogPostCommentSerializer(source='blogpostcomment_set') + +# serializer = BlogPostSerializer(instance=post) +# expected = { +# 'title': 'Test blog post', +# 'comments': [ +# {'text': 'I hate this blog post'}, +# {'text': 'I love this blog post'} +# ] +# } + +# self.assertEqual(serializer.data, expected) + +# def test_include_reverse_relations(self): +# post = BlogPost.objects.create(title="Test blog post") +# post.blogpostcomment_set.create(text="I hate this blog post") +# post.blogpostcomment_set.create(text="I love this blog post") + +# class BlogPostSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlogPost +# fields = ('id', 'title', 'blogpostcomment_set') + +# serializer = BlogPostSerializer(instance=post) +# expected = { +# 'id': 1, 'title': 'Test blog post', 'blogpostcomment_set': [1, 2] +# } +# self.assertEqual(serializer.data, expected) + +# def test_depth_include_reverse_relations(self): +# post = BlogPost.objects.create(title="Test blog post") +# post.blogpostcomment_set.create(text="I hate this blog post") +# post.blogpostcomment_set.create(text="I love this blog post") + +# class BlogPostSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlogPost +# fields = ('id', 'title', 'blogpostcomment_set') +# depth = 1 + +# serializer = BlogPostSerializer(instance=post) +# expected = { +# 'id': 1, 'title': 'Test blog post', +# 'blogpostcomment_set': [ +# {'id': 1, 'text': 'I hate this blog post', 'blog_post': 1}, +# {'id': 2, 'text': 'I love this blog post', 'blog_post': 1} +# ] +# } +# self.assertEqual(serializer.data, expected) + +# def test_callable_source(self): +# post = BlogPost.objects.create(title="Test blog post") +# post.blogpostcomment_set.create(text="I love this blog post") + +# class BlogPostCommentSerializer(serializers.Serializer): +# text = serializers.CharField() + +# class BlogPostSerializer(serializers.Serializer): +# title = serializers.CharField() +# first_comment = BlogPostCommentSerializer(source='get_first_comment') + +# serializer = BlogPostSerializer(post) + +# expected = { +# 'title': 'Test blog post', +# 'first_comment': {'text': 'I love this blog post'} +# } +# self.assertEqual(serializer.data, expected) + + +# class RelatedTraversalTest(TestCase): +# def test_nested_traversal(self): +# """ +# Source argument should support dotted.source notation. +# """ +# user = Person.objects.create(name="django") +# post = BlogPost.objects.create(title="Test blog post", writer=user) +# post.blogpostcomment_set.create(text="I love this blog post") - class PersonSerializer(serializers.ModelSerializer): - class Meta: - model = Person - fields = ("name", "age") +# class PersonSerializer(serializers.ModelSerializer): +# class Meta: +# model = Person +# fields = ("name", "age") - class BlogPostCommentSerializer(serializers.ModelSerializer): - class Meta: - model = BlogPostComment - fields = ("text", "post_owner") +# class BlogPostCommentSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlogPostComment +# fields = ("text", "post_owner") - text = serializers.CharField() - post_owner = PersonSerializer(source='blog_post.writer') +# text = serializers.CharField() +# post_owner = PersonSerializer(source='blog_post.writer') - class BlogPostSerializer(serializers.Serializer): - title = serializers.CharField() - comments = BlogPostCommentSerializer(source='blogpostcomment_set') +# class BlogPostSerializer(serializers.Serializer): +# title = serializers.CharField() +# comments = BlogPostCommentSerializer(source='blogpostcomment_set') - serializer = BlogPostSerializer(instance=post) +# serializer = BlogPostSerializer(instance=post) - expected = { - 'title': 'Test blog post', - 'comments': [{ - 'text': 'I love this blog post', - 'post_owner': { - "name": "django", - "age": None - } - }] - } - - self.assertEqual(serializer.data, expected) +# expected = { +# 'title': 'Test blog post', +# 'comments': [{ +# 'text': 'I love this blog post', +# 'post_owner': { +# "name": "django", +# "age": None +# } +# }] +# } + +# self.assertEqual(serializer.data, expected) - def test_nested_traversal_with_none(self): - """ - If a component of the dotted.source is None, return None for the field. - """ - from tests.models import NullableForeignKeySource - instance = NullableForeignKeySource.objects.create(name='Source with null FK') - - class NullableSourceSerializer(serializers.Serializer): - target_name = serializers.Field(source='target.name') - - serializer = NullableSourceSerializer(instance=instance) - - expected = { - 'target_name': None, - } - - self.assertEqual(serializer.data, expected) - - -class SerializerMethodFieldTests(TestCase): - def setUp(self): - - class BoopSerializer(serializers.Serializer): - beep = serializers.SerializerMethodField('get_beep') - boop = serializers.Field() - boop_count = serializers.SerializerMethodField('get_boop_count') - - def get_beep(self, obj): - return 'hello!' - - def get_boop_count(self, obj): - return len(obj.boop) - - self.serializer_class = BoopSerializer - - def test_serializer_method_field(self): - - class MyModel(object): - boop = ['a', 'b', 'c'] - - source_data = MyModel() - - serializer = self.serializer_class(source_data) - - expected = { - 'beep': 'hello!', - 'boop': ['a', 'b', 'c'], - 'boop_count': 3, - } - - self.assertEqual(serializer.data, expected) - - -# Test for issue #324 -class BlankFieldTests(TestCase): - def setUp(self): - - class BlankFieldModelSerializer(serializers.ModelSerializer): - class Meta: - model = BlankFieldModel - - class BlankFieldSerializer(serializers.Serializer): - title = serializers.CharField(required=False) - - class NotBlankFieldModelSerializer(serializers.ModelSerializer): - class Meta: - model = BasicModel - - class NotBlankFieldSerializer(serializers.Serializer): - title = serializers.CharField() - - self.model_serializer_class = BlankFieldModelSerializer - self.serializer_class = BlankFieldSerializer - self.not_blank_model_serializer_class = NotBlankFieldModelSerializer - self.not_blank_serializer_class = NotBlankFieldSerializer - self.data = {'title': ''} - - def test_create_blank_field(self): - serializer = self.serializer_class(data=self.data) - self.assertEqual(serializer.is_valid(), True) - - def test_create_model_blank_field(self): - serializer = self.model_serializer_class(data=self.data) - self.assertEqual(serializer.is_valid(), True) - - def test_create_model_null_field(self): - serializer = self.model_serializer_class(data={'title': None}) - self.assertEqual(serializer.is_valid(), True) - serializer.save() - self.assertIsNot(serializer.object.pk, None) - self.assertEqual(serializer.object.title, '') - - def test_create_not_blank_field(self): - """ - Test to ensure blank data in a field not marked as blank=True - is considered invalid in a non-model serializer - """ - serializer = self.not_blank_serializer_class(data=self.data) - self.assertEqual(serializer.is_valid(), False) - - def test_create_model_not_blank_field(self): - """ - Test to ensure blank data in a field not marked as blank=True - is considered invalid in a model serializer - """ - serializer = self.not_blank_model_serializer_class(data=self.data) - self.assertEqual(serializer.is_valid(), False) - - def test_create_model_empty_field(self): - serializer = self.model_serializer_class(data={}) - self.assertEqual(serializer.is_valid(), True) - - def test_create_model_null_field_save(self): - """ - Regression test for #1330. - - https://github.com/tomchristie/django-rest-framework/pull/1330 - """ - serializer = self.model_serializer_class(data={'title': None}) - self.assertEqual(serializer.is_valid(), True) - - try: - serializer.save() - except Exception: - self.fail('Exception raised on save() after validation passes') - - -# Test for issue #460 -class SerializerPickleTests(TestCase): - """ - Test pickleability of the output of Serializers - """ - def test_pickle_simple_model_serializer_data(self): - """ - Test simple serializer - """ - pickle.dumps(PersonSerializer(Person(name="Methusela", age=969)).data) - - def test_pickle_inner_serializer(self): - """ - Test pickling a serializer whose resulting .data (a SortedDictWithMetadata) will - have unpickleable meta data--in order to make sure metadata doesn't get pulled into the pickle. - See DictWithMetadata.__getstate__ - """ - class InnerPersonSerializer(serializers.ModelSerializer): - class Meta: - model = Person - fields = ('name', 'age') - pickle.dumps(InnerPersonSerializer(Person(name="Noah", age=950)).data, 0) - - def test_getstate_method_should_not_return_none(self): - """ - Regression test for #645. - """ - data = serializers.DictWithMetadata({1: 1}) - self.assertEqual(data.__getstate__(), serializers.SortedDict({1: 1})) - - def test_serializer_data_is_pickleable(self): - """ - Another regression test for #645. - """ - data = serializers.SortedDictWithMetadata({1: 1}) - repr(pickle.loads(pickle.dumps(data, 0))) - - -# test for issue #725 -class SeveralChoicesModel(models.Model): - color = models.CharField( - max_length=10, - choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue')], - blank=False - ) - drink = models.CharField( - max_length=10, - choices=[('beer', 'Beer'), ('wine', 'Wine'), ('cider', 'Cider')], - blank=False, - default='beer' - ) - os = models.CharField( - max_length=10, - choices=[('linux', 'Linux'), ('osx', 'OSX'), ('windows', 'Windows')], - blank=True - ) - music_genre = models.CharField( - max_length=10, - choices=[('rock', 'Rock'), ('metal', 'Metal'), ('grunge', 'Grunge')], - blank=True, - default='metal' - ) - - -class SerializerChoiceFields(TestCase): - - def setUp(self): - super(SerializerChoiceFields, self).setUp() - - class SeveralChoicesSerializer(serializers.ModelSerializer): - class Meta: - model = SeveralChoicesModel - fields = ('color', 'drink', 'os', 'music_genre') - - self.several_choices_serializer = SeveralChoicesSerializer - - def test_choices_blank_false_not_default(self): - serializer = self.several_choices_serializer() - self.assertEqual( - serializer.fields['color'].choices, - [('red', 'Red'), ('green', 'Green'), ('blue', 'Blue')] - ) - - def test_choices_blank_false_with_default(self): - serializer = self.several_choices_serializer() - self.assertEqual( - serializer.fields['drink'].choices, - [('beer', 'Beer'), ('wine', 'Wine'), ('cider', 'Cider')] - ) - - def test_choices_blank_true_not_default(self): - serializer = self.several_choices_serializer() - self.assertEqual( - serializer.fields['os'].choices, - BLANK_CHOICE_DASH + [('linux', 'Linux'), ('osx', 'OSX'), ('windows', 'Windows')] - ) - - def test_choices_blank_true_with_default(self): - serializer = self.several_choices_serializer() - self.assertEqual( - serializer.fields['music_genre'].choices, - BLANK_CHOICE_DASH + [('rock', 'Rock'), ('metal', 'Metal'), ('grunge', 'Grunge')] - ) - - -# Regression tests for #675 -class Ticket(models.Model): - assigned = models.ForeignKey( - Person, related_name='assigned_tickets') - reviewer = models.ForeignKey( - Person, blank=True, null=True, related_name='reviewed_tickets') - - -class SerializerRelatedChoicesTest(TestCase): - - def setUp(self): - super(SerializerRelatedChoicesTest, self).setUp() - - class RelatedChoicesSerializer(serializers.ModelSerializer): - class Meta: - model = Ticket - fields = ('assigned', 'reviewer') - - self.related_fields_serializer = RelatedChoicesSerializer - - def test_empty_queryset_required(self): - serializer = self.related_fields_serializer() - self.assertEqual(serializer.fields['assigned'].queryset.count(), 0) - self.assertEqual( - [x for x in serializer.fields['assigned'].widget.choices], - [] - ) - - def test_empty_queryset_not_required(self): - serializer = self.related_fields_serializer() - self.assertEqual(serializer.fields['reviewer'].queryset.count(), 0) - self.assertEqual( - [x for x in serializer.fields['reviewer'].widget.choices], - [('', '---------')] - ) - - def test_with_some_persons_required(self): - Person.objects.create(name="Lionel Messi") - Person.objects.create(name="Xavi Hernandez") - serializer = self.related_fields_serializer() - self.assertEqual(serializer.fields['assigned'].queryset.count(), 2) - self.assertEqual( - [x for x in serializer.fields['assigned'].widget.choices], - [(1, 'Person object - 1'), (2, 'Person object - 2')] - ) - - def test_with_some_persons_not_required(self): - Person.objects.create(name="Lionel Messi") - Person.objects.create(name="Xavi Hernandez") - serializer = self.related_fields_serializer() - self.assertEqual(serializer.fields['reviewer'].queryset.count(), 2) - self.assertEqual( - [x for x in serializer.fields['reviewer'].widget.choices], - [('', '---------'), (1, 'Person object - 1'), (2, 'Person object - 2')] - ) +# def test_nested_traversal_with_none(self): +# """ +# If a component of the dotted.source is None, return None for the field. +# """ +# from tests.models import NullableForeignKeySource +# instance = NullableForeignKeySource.objects.create(name='Source with null FK') + +# class NullableSourceSerializer(serializers.Serializer): +# target_name = serializers.Field(source='target.name') + +# serializer = NullableSourceSerializer(instance=instance) + +# expected = { +# 'target_name': None, +# } + +# self.assertEqual(serializer.data, expected) + + +# class SerializerMethodFieldTests(TestCase): +# def setUp(self): + +# class BoopSerializer(serializers.Serializer): +# beep = serializers.SerializerMethodField('get_beep') +# boop = serializers.Field() +# boop_count = serializers.SerializerMethodField('get_boop_count') + +# def get_beep(self, obj): +# return 'hello!' + +# def get_boop_count(self, obj): +# return len(obj.boop) + +# self.serializer_class = BoopSerializer + +# def test_serializer_method_field(self): + +# class MyModel(object): +# boop = ['a', 'b', 'c'] + +# source_data = MyModel() + +# serializer = self.serializer_class(source_data) + +# expected = { +# 'beep': 'hello!', +# 'boop': ['a', 'b', 'c'], +# 'boop_count': 3, +# } + +# self.assertEqual(serializer.data, expected) + + +# # Test for issue #324 +# class BlankFieldTests(TestCase): +# def setUp(self): + +# class BlankFieldModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlankFieldModel + +# class BlankFieldSerializer(serializers.Serializer): +# title = serializers.CharField(required=False) + +# class NotBlankFieldModelSerializer(serializers.ModelSerializer): +# class Meta: +# model = BasicModel + +# class NotBlankFieldSerializer(serializers.Serializer): +# title = serializers.CharField() + +# self.model_serializer_class = BlankFieldModelSerializer +# self.serializer_class = BlankFieldSerializer +# self.not_blank_model_serializer_class = NotBlankFieldModelSerializer +# self.not_blank_serializer_class = NotBlankFieldSerializer +# self.data = {'title': ''} + +# def test_create_blank_field(self): +# serializer = self.serializer_class(data=self.data) +# self.assertEqual(serializer.is_valid(), True) + +# def test_create_model_blank_field(self): +# serializer = self.model_serializer_class(data=self.data) +# self.assertEqual(serializer.is_valid(), True) + +# def test_create_model_null_field(self): +# serializer = self.model_serializer_class(data={'title': None}) +# self.assertEqual(serializer.is_valid(), True) +# serializer.save() +# self.assertIsNot(serializer.object.pk, None) +# self.assertEqual(serializer.object.title, '') + +# def test_create_not_blank_field(self): +# """ +# Test to ensure blank data in a field not marked as blank=True +# is considered invalid in a non-model serializer +# """ +# serializer = self.not_blank_serializer_class(data=self.data) +# self.assertEqual(serializer.is_valid(), False) + +# def test_create_model_not_blank_field(self): +# """ +# Test to ensure blank data in a field not marked as blank=True +# is considered invalid in a model serializer +# """ +# serializer = self.not_blank_model_serializer_class(data=self.data) +# self.assertEqual(serializer.is_valid(), False) + +# def test_create_model_empty_field(self): +# serializer = self.model_serializer_class(data={}) +# self.assertEqual(serializer.is_valid(), True) + +# def test_create_model_null_field_save(self): +# """ +# Regression test for #1330. + +# https://github.com/tomchristie/django-rest-framework/pull/1330 +# """ +# serializer = self.model_serializer_class(data={'title': None}) +# self.assertEqual(serializer.is_valid(), True) + +# try: +# serializer.save() +# except Exception: +# self.fail('Exception raised on save() after validation passes') + + +# # Test for issue #460 +# class SerializerPickleTests(TestCase): +# """ +# Test pickleability of the output of Serializers +# """ +# def test_pickle_simple_model_serializer_data(self): +# """ +# Test simple serializer +# """ +# pickle.dumps(PersonSerializer(Person(name="Methusela", age=969)).data) + +# def test_pickle_inner_serializer(self): +# """ +# Test pickling a serializer whose resulting .data (a SortedDictWithMetadata) will +# have unpickleable meta data--in order to make sure metadata doesn't get pulled into the pickle. +# See DictWithMetadata.__getstate__ +# """ +# class InnerPersonSerializer(serializers.ModelSerializer): +# class Meta: +# model = Person +# fields = ('name', 'age') +# pickle.dumps(InnerPersonSerializer(Person(name="Noah", age=950)).data, 0) + +# def test_getstate_method_should_not_return_none(self): +# """ +# Regression test for #645. +# """ +# data = serializers.DictWithMetadata({1: 1}) +# self.assertEqual(data.__getstate__(), serializers.SortedDict({1: 1})) + +# def test_serializer_data_is_pickleable(self): +# """ +# Another regression test for #645. +# """ +# data = serializers.SortedDictWithMetadata({1: 1}) +# repr(pickle.loads(pickle.dumps(data, 0))) + + +# # test for issue #725 +# class SeveralChoicesModel(models.Model): +# color = models.CharField( +# max_length=10, +# choices=[('red', 'Red'), ('green', 'Green'), ('blue', 'Blue')], +# blank=False +# ) +# drink = models.CharField( +# max_length=10, +# choices=[('beer', 'Beer'), ('wine', 'Wine'), ('cider', 'Cider')], +# blank=False, +# default='beer' +# ) +# os = models.CharField( +# max_length=10, +# choices=[('linux', 'Linux'), ('osx', 'OSX'), ('windows', 'Windows')], +# blank=True +# ) +# music_genre = models.CharField( +# max_length=10, +# choices=[('rock', 'Rock'), ('metal', 'Metal'), ('grunge', 'Grunge')], +# blank=True, +# default='metal' +# ) + + +# class SerializerChoiceFields(TestCase): + +# def setUp(self): +# super(SerializerChoiceFields, self).setUp() + +# class SeveralChoicesSerializer(serializers.ModelSerializer): +# class Meta: +# model = SeveralChoicesModel +# fields = ('color', 'drink', 'os', 'music_genre') + +# self.several_choices_serializer = SeveralChoicesSerializer + +# def test_choices_blank_false_not_default(self): +# serializer = self.several_choices_serializer() +# self.assertEqual( +# serializer.fields['color'].choices, +# [('red', 'Red'), ('green', 'Green'), ('blue', 'Blue')] +# ) + +# def test_choices_blank_false_with_default(self): +# serializer = self.several_choices_serializer() +# self.assertEqual( +# serializer.fields['drink'].choices, +# [('beer', 'Beer'), ('wine', 'Wine'), ('cider', 'Cider')] +# ) + +# def test_choices_blank_true_not_default(self): +# serializer = self.several_choices_serializer() +# self.assertEqual( +# serializer.fields['os'].choices, +# BLANK_CHOICE_DASH + [('linux', 'Linux'), ('osx', 'OSX'), ('windows', 'Windows')] +# ) + +# def test_choices_blank_true_with_default(self): +# serializer = self.several_choices_serializer() +# self.assertEqual( +# serializer.fields['music_genre'].choices, +# BLANK_CHOICE_DASH + [('rock', 'Rock'), ('metal', 'Metal'), ('grunge', 'Grunge')] +# ) + + +# # Regression tests for #675 +# class Ticket(models.Model): +# assigned = models.ForeignKey( +# Person, related_name='assigned_tickets') +# reviewer = models.ForeignKey( +# Person, blank=True, null=True, related_name='reviewed_tickets') + + +# class SerializerRelatedChoicesTest(TestCase): + +# def setUp(self): +# super(SerializerRelatedChoicesTest, self).setUp() + +# class RelatedChoicesSerializer(serializers.ModelSerializer): +# class Meta: +# model = Ticket +# fields = ('assigned', 'reviewer') + +# self.related_fields_serializer = RelatedChoicesSerializer + +# def test_empty_queryset_required(self): +# serializer = self.related_fields_serializer() +# self.assertEqual(serializer.fields['assigned'].queryset.count(), 0) +# self.assertEqual( +# [x for x in serializer.fields['assigned'].widget.choices], +# [] +# ) + +# def test_empty_queryset_not_required(self): +# serializer = self.related_fields_serializer() +# self.assertEqual(serializer.fields['reviewer'].queryset.count(), 0) +# self.assertEqual( +# [x for x in serializer.fields['reviewer'].widget.choices], +# [('', '---------')] +# ) + +# def test_with_some_persons_required(self): +# Person.objects.create(name="Lionel Messi") +# Person.objects.create(name="Xavi Hernandez") +# serializer = self.related_fields_serializer() +# self.assertEqual(serializer.fields['assigned'].queryset.count(), 2) +# self.assertEqual( +# [x for x in serializer.fields['assigned'].widget.choices], +# [(1, 'Person object - 1'), (2, 'Person object - 2')] +# ) + +# def test_with_some_persons_not_required(self): +# Person.objects.create(name="Lionel Messi") +# Person.objects.create(name="Xavi Hernandez") +# serializer = self.related_fields_serializer() +# self.assertEqual(serializer.fields['reviewer'].queryset.count(), 2) +# self.assertEqual( +# [x for x in serializer.fields['reviewer'].widget.choices], +# [('', '---------'), (1, 'Person object - 1'), (2, 'Person object - 2')] +# ) -class DepthTest(TestCase): - def test_implicit_nesting(self): - - writer = Person.objects.create(name="django", age=1) - post = BlogPost.objects.create(title="Test blog post", writer=writer) - comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post) - - class BlogPostCommentSerializer(serializers.ModelSerializer): - class Meta: - model = BlogPostComment - depth = 2 - - serializer = BlogPostCommentSerializer(instance=comment) - expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post', - 'writer': {'id': 1, 'name': 'django', 'age': 1}}} - - self.assertEqual(serializer.data, expected) - - def test_explicit_nesting(self): - writer = Person.objects.create(name="django", age=1) - post = BlogPost.objects.create(title="Test blog post", writer=writer) - comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post) - - class PersonSerializer(serializers.ModelSerializer): - class Meta: - model = Person - - class BlogPostSerializer(serializers.ModelSerializer): - writer = PersonSerializer() - - class Meta: - model = BlogPost - - class BlogPostCommentSerializer(serializers.ModelSerializer): - blog_post = BlogPostSerializer() - - class Meta: - model = BlogPostComment +# class DepthTest(TestCase): +# def test_implicit_nesting(self): + +# writer = Person.objects.create(name="django", age=1) +# post = BlogPost.objects.create(title="Test blog post", writer=writer) +# comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post) + +# class BlogPostCommentSerializer(serializers.ModelSerializer): +# class Meta: +# model = BlogPostComment +# depth = 2 + +# serializer = BlogPostCommentSerializer(instance=comment) +# expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post', +# 'writer': {'id': 1, 'name': 'django', 'age': 1}}} + +# self.assertEqual(serializer.data, expected) + +# def test_explicit_nesting(self): +# writer = Person.objects.create(name="django", age=1) +# post = BlogPost.objects.create(title="Test blog post", writer=writer) +# comment = BlogPostComment.objects.create(text="Test blog post comment", blog_post=post) + +# class PersonSerializer(serializers.ModelSerializer): +# class Meta: +# model = Person + +# class BlogPostSerializer(serializers.ModelSerializer): +# writer = PersonSerializer() + +# class Meta: +# model = BlogPost + +# class BlogPostCommentSerializer(serializers.ModelSerializer): +# blog_post = BlogPostSerializer() + +# class Meta: +# model = BlogPostComment - serializer = BlogPostCommentSerializer(instance=comment) - expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post', - 'writer': {'id': 1, 'name': 'django', 'age': 1}}} +# serializer = BlogPostCommentSerializer(instance=comment) +# expected = {'id': 1, 'text': 'Test blog post comment', 'blog_post': {'id': 1, 'title': 'Test blog post', +# 'writer': {'id': 1, 'name': 'django', 'age': 1}}} - self.assertEqual(serializer.data, expected) +# self.assertEqual(serializer.data, expected) -class NestedSerializerContextTests(TestCase): +# class NestedSerializerContextTests(TestCase): - def test_nested_serializer_context(self): - """ - Regression for #497 - - https://github.com/tomchristie/django-rest-framework/issues/497 - """ - class PhotoSerializer(serializers.ModelSerializer): - class Meta: - model = Photo - fields = ("description", "callable") +# def test_nested_serializer_context(self): +# """ +# Regression for #497 + +# https://github.com/tomchristie/django-rest-framework/issues/497 +# """ +# class PhotoSerializer(serializers.ModelSerializer): +# class Meta: +# model = Photo +# fields = ("description", "callable") - callable = serializers.SerializerMethodField('_callable') +# callable = serializers.SerializerMethodField('_callable') - def _callable(self, instance): - if 'context_item' not in self.context: - raise RuntimeError("context isn't getting passed into 2nd level nested serializer") - return "success" - - class AlbumSerializer(serializers.ModelSerializer): - class Meta: - model = Album - fields = ("photo_set", "callable") +# def _callable(self, instance): +# if 'context_item' not in self.context: +# raise RuntimeError("context isn't getting passed into 2nd level nested serializer") +# return "success" + +# class AlbumSerializer(serializers.ModelSerializer): +# class Meta: +# model = Album +# fields = ("photo_set", "callable") - photo_set = PhotoSerializer(source="photo_set", many=True) - callable = serializers.SerializerMethodField("_callable") +# photo_set = PhotoSerializer(source="photo_set", many=True) +# callable = serializers.SerializerMethodField("_callable") - def _callable(self, instance): - if 'context_item' not in self.context: - raise RuntimeError("context isn't getting passed into 1st level nested serializer") - return "success" +# def _callable(self, instance): +# if 'context_item' not in self.context: +# raise RuntimeError("context isn't getting passed into 1st level nested serializer") +# return "success" - class AlbumCollection(object): - albums = None +# class AlbumCollection(object): +# albums = None - class AlbumCollectionSerializer(serializers.Serializer): - albums = AlbumSerializer(source="albums", many=True) +# class AlbumCollectionSerializer(serializers.Serializer): +# albums = AlbumSerializer(source="albums", many=True) - album1 = Album.objects.create(title="album 1") - album2 = Album.objects.create(title="album 2") - Photo.objects.create(description="Bigfoot", album=album1) - Photo.objects.create(description="Unicorn", album=album1) - Photo.objects.create(description="Yeti", album=album2) - Photo.objects.create(description="Sasquatch", album=album2) - album_collection = AlbumCollection() - album_collection.albums = [album1, album2] +# album1 = Album.objects.create(title="album 1") +# album2 = Album.objects.create(title="album 2") +# Photo.objects.create(description="Bigfoot", album=album1) +# Photo.objects.create(description="Unicorn", album=album1) +# Photo.objects.create(description="Yeti", album=album2) +# Photo.objects.create(description="Sasquatch", album=album2) +# album_collection = AlbumCollection() +# album_collection.albums = [album1, album2] - # This will raise RuntimeError if context doesn't get passed correctly to the nested Serializers - AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data +# # This will raise RuntimeError if context doesn't get passed correctly to the nested Serializers +# AlbumCollectionSerializer(album_collection, context={'context_item': 'album context'}).data -class DeserializeListTestCase(TestCase): +# class DeserializeListTestCase(TestCase): - def setUp(self): - self.data = { - 'email': 'nobody@nowhere.com', - 'content': 'This is some test content', - 'created': datetime.datetime(2013, 3, 7), - } +# def setUp(self): +# self.data = { +# 'email': 'nobody@nowhere.com', +# 'content': 'This is some test content', +# 'created': datetime.datetime(2013, 3, 7), +# } - def test_no_errors(self): - data = [self.data.copy() for x in range(0, 3)] - serializer = CommentSerializer(data=data, many=True) - self.assertTrue(serializer.is_valid()) - self.assertTrue(isinstance(serializer.object, list)) - self.assertTrue( - all((isinstance(item, Comment) for item in serializer.object)) - ) +# def test_no_errors(self): +# data = [self.data.copy() for x in range(0, 3)] +# serializer = CommentSerializer(data=data, many=True) +# self.assertTrue(serializer.is_valid()) +# self.assertTrue(isinstance(serializer.object, list)) +# self.assertTrue( +# all((isinstance(item, Comment) for item in serializer.object)) +# ) - def test_errors_return_as_list(self): - invalid_item = self.data.copy() - invalid_item['email'] = '' - data = [self.data.copy(), invalid_item, self.data.copy()] +# def test_errors_return_as_list(self): +# invalid_item = self.data.copy() +# invalid_item['email'] = '' +# data = [self.data.copy(), invalid_item, self.data.copy()] - serializer = CommentSerializer(data=data, many=True) - self.assertFalse(serializer.is_valid()) - expected = [{}, {'email': ['This field is required.']}, {}] - self.assertEqual(serializer.errors, expected) +# serializer = CommentSerializer(data=data, many=True) +# self.assertFalse(serializer.is_valid()) +# expected = [{}, {'email': ['This field is required.']}, {}] +# self.assertEqual(serializer.errors, expected) -# Test for issue 747 +# # Test for issue 747 -class LazyStringModel(object): - def __init__(self, lazystring): - self.lazystring = lazystring +# class LazyStringModel(object): +# def __init__(self, lazystring): +# self.lazystring = lazystring -class LazyStringSerializer(serializers.Serializer): - lazystring = serializers.Field() - - def restore_object(self, attrs, instance=None): - if instance is not None: - instance.lazystring = attrs.get('lazystring', instance.lazystring) - return instance - return LazyStringModel(**attrs) - - -class LazyStringsTestCase(TestCase): - def setUp(self): - self.model = LazyStringModel(lazystring=_('lazystring')) - - def test_lazy_strings_are_translated(self): - serializer = LazyStringSerializer(self.model) - self.assertEqual(type(serializer.data['lazystring']), - type('lazystring')) - - -# Test for issue #467 - -class FieldLabelTest(TestCase): - def setUp(self): - self.serializer_class = BasicModelSerializer - - def test_label_from_model(self): - """ - Validates that label and help_text are correctly copied from the model class. - """ - serializer = self.serializer_class() - text_field = serializer.fields['text'] - - self.assertEqual('Text comes here', text_field.label) - self.assertEqual('Text description.', text_field.help_text) - - def test_field_ctor(self): - """ - This is check that ctor supports both label and help_text. - """ - self.assertEqual('Label', fields.Field(label='Label', help_text='Help').label) - self.assertEqual('Help', fields.CharField(label='Label', help_text='Help').help_text) - self.assertEqual('Label', relations.HyperlinkedRelatedField(view_name='fake', label='Label', help_text='Help', many=True).label) - - -# Test for issue #961 - -class ManyFieldHelpTextTest(TestCase): - def test_help_text_no_hold_down_control_msg(self): - """ - Validate that help_text doesn't contain the 'Hold down "Control" ...' - message that Django appends to choice fields. - """ - rel_field = fields.Field(help_text=ManyToManyModel._meta.get_field('rel').help_text) - self.assertEqual('Some help text.', rel_field.help_text) - - -class AttributeMappingOnAutogeneratedRelatedFields(TestCase): - - def test_primary_key_related_field(self): - serializer = ForeignKeySourceSerializer() - self.assertEqual(serializer.fields['target'].help_text, 'Target') - self.assertEqual(serializer.fields['target'].label, 'Target') - - def test_hyperlinked_related_field(self): - serializer = HyperlinkedForeignKeySourceSerializer() - self.assertEqual(serializer.fields['target'].help_text, 'Target') - self.assertEqual(serializer.fields['target'].label, 'Target') - - -@unittest.skipUnless(PIL is not None, 'PIL is not installed') -class AttributeMappingOnAutogeneratedFieldsTests(TestCase): - - def setUp(self): - - class AMOAFSerializer(serializers.ModelSerializer): - class Meta: - model = AMOAFModel - - self.serializer_class = AMOAFSerializer - self.fields_attributes = { - 'char_field': [ - ('max_length', 1024), - ], - 'comma_separated_integer_field': [ - ('max_length', 1024), - ], - 'decimal_field': [ - ('max_digits', 64), - ('decimal_places', 32), - ], - 'email_field': [ - ('max_length', 1024), - ], - 'file_field': [ - ('max_length', 1024), - ], - 'image_field': [ - ('max_length', 1024), - ], - 'slug_field': [ - ('max_length', 1024), - ], - 'url_field': [ - ('max_length', 1024), - ], - 'nullable_char_field': [ - ('max_length', 1024), - ('allow_none', True), - ], - } - - def field_test(self, field): - serializer = self.serializer_class(data={}) - self.assertEqual(serializer.is_valid(), True) - - for attribute in self.fields_attributes[field]: - self.assertEqual( - getattr(serializer.fields[field], attribute[0]), - attribute[1] - ) - - def test_char_field(self): - self.field_test('char_field') - - def test_comma_separated_integer_field(self): - self.field_test('comma_separated_integer_field') - - def test_decimal_field(self): - self.field_test('decimal_field') - - def test_email_field(self): - self.field_test('email_field') - - def test_file_field(self): - self.field_test('file_field') - - def test_image_field(self): - self.field_test('image_field') - - def test_slug_field(self): - self.field_test('slug_field') - - def test_url_field(self): - self.field_test('url_field') - - def test_nullable_char_field(self): - self.field_test('nullable_char_field') - - -@unittest.skipUnless(PIL is not None, 'PIL is not installed') -class DefaultValuesOnAutogeneratedFieldsTests(TestCase): - - def setUp(self): - - class DVOAFSerializer(serializers.ModelSerializer): - class Meta: - model = DVOAFModel - - self.serializer_class = DVOAFSerializer - self.fields_attributes = { - 'positive_integer_field': [ - ('min_value', 0), - ], - 'positive_small_integer_field': [ - ('min_value', 0), - ], - 'email_field': [ - ('max_length', 75), - ], - 'file_field': [ - ('max_length', 100), - ], - 'image_field': [ - ('max_length', 100), - ], - 'slug_field': [ - ('max_length', 50), - ], - 'url_field': [ - ('max_length', 200), - ], - } - - def field_test(self, field): - serializer = self.serializer_class(data={}) - self.assertEqual(serializer.is_valid(), True) - - for attribute in self.fields_attributes[field]: - self.assertEqual( - getattr(serializer.fields[field], attribute[0]), - attribute[1] - ) +# class LazyStringSerializer(serializers.Serializer): +# lazystring = serializers.Field() + +# def restore_object(self, attrs, instance=None): +# if instance is not None: +# instance.lazystring = attrs.get('lazystring', instance.lazystring) +# return instance +# return LazyStringModel(**attrs) + + +# class LazyStringsTestCase(TestCase): +# def setUp(self): +# self.model = LazyStringModel(lazystring=_('lazystring')) + +# def test_lazy_strings_are_translated(self): +# serializer = LazyStringSerializer(self.model) +# self.assertEqual(type(serializer.data['lazystring']), +# type('lazystring')) + + +# # Test for issue #467 + +# class FieldLabelTest(TestCase): +# def setUp(self): +# self.serializer_class = BasicModelSerializer + +# def test_label_from_model(self): +# """ +# Validates that label and help_text are correctly copied from the model class. +# """ +# serializer = self.serializer_class() +# text_field = serializer.fields['text'] + +# self.assertEqual('Text comes here', text_field.label) +# self.assertEqual('Text description.', text_field.help_text) + +# def test_field_ctor(self): +# """ +# This is check that ctor supports both label and help_text. +# """ +# self.assertEqual('Label', fields.Field(label='Label', help_text='Help').label) +# self.assertEqual('Help', fields.CharField(label='Label', help_text='Help').help_text) +# self.assertEqual('Label', relations.HyperlinkedRelatedField(view_name='fake', label='Label', help_text='Help', many=True).label) + + +# # Test for issue #961 + +# class ManyFieldHelpTextTest(TestCase): +# def test_help_text_no_hold_down_control_msg(self): +# """ +# Validate that help_text doesn't contain the 'Hold down "Control" ...' +# message that Django appends to choice fields. +# """ +# rel_field = fields.Field(help_text=ManyToManyModel._meta.get_field('rel').help_text) +# self.assertEqual('Some help text.', rel_field.help_text) + + +# class AttributeMappingOnAutogeneratedRelatedFields(TestCase): + +# def test_primary_key_related_field(self): +# serializer = ForeignKeySourceSerializer() +# self.assertEqual(serializer.fields['target'].help_text, 'Target') +# self.assertEqual(serializer.fields['target'].label, 'Target') + +# def test_hyperlinked_related_field(self): +# serializer = HyperlinkedForeignKeySourceSerializer() +# self.assertEqual(serializer.fields['target'].help_text, 'Target') +# self.assertEqual(serializer.fields['target'].label, 'Target') + + +# @unittest.skipUnless(PIL is not None, 'PIL is not installed') +# class AttributeMappingOnAutogeneratedFieldsTests(TestCase): + +# def setUp(self): + +# class AMOAFSerializer(serializers.ModelSerializer): +# class Meta: +# model = AMOAFModel + +# self.serializer_class = AMOAFSerializer +# self.fields_attributes = { +# 'char_field': [ +# ('max_length', 1024), +# ], +# 'comma_separated_integer_field': [ +# ('max_length', 1024), +# ], +# 'decimal_field': [ +# ('max_digits', 64), +# ('decimal_places', 32), +# ], +# 'email_field': [ +# ('max_length', 1024), +# ], +# 'file_field': [ +# ('max_length', 1024), +# ], +# 'image_field': [ +# ('max_length', 1024), +# ], +# 'slug_field': [ +# ('max_length', 1024), +# ], +# 'url_field': [ +# ('max_length', 1024), +# ], +# 'nullable_char_field': [ +# ('max_length', 1024), +# ('allow_none', True), +# ], +# } + +# def field_test(self, field): +# serializer = self.serializer_class(data={}) +# self.assertEqual(serializer.is_valid(), True) + +# for attribute in self.fields_attributes[field]: +# self.assertEqual( +# getattr(serializer.fields[field], attribute[0]), +# attribute[1] +# ) + +# def test_char_field(self): +# self.field_test('char_field') + +# def test_comma_separated_integer_field(self): +# self.field_test('comma_separated_integer_field') + +# def test_decimal_field(self): +# self.field_test('decimal_field') + +# def test_email_field(self): +# self.field_test('email_field') + +# def test_file_field(self): +# self.field_test('file_field') + +# def test_image_field(self): +# self.field_test('image_field') + +# def test_slug_field(self): +# self.field_test('slug_field') + +# def test_url_field(self): +# self.field_test('url_field') + +# def test_nullable_char_field(self): +# self.field_test('nullable_char_field') + + +# @unittest.skipUnless(PIL is not None, 'PIL is not installed') +# class DefaultValuesOnAutogeneratedFieldsTests(TestCase): + +# def setUp(self): + +# class DVOAFSerializer(serializers.ModelSerializer): +# class Meta: +# model = DVOAFModel + +# self.serializer_class = DVOAFSerializer +# self.fields_attributes = { +# 'positive_integer_field': [ +# ('min_value', 0), +# ], +# 'positive_small_integer_field': [ +# ('min_value', 0), +# ], +# 'email_field': [ +# ('max_length', 75), +# ], +# 'file_field': [ +# ('max_length', 100), +# ], +# 'image_field': [ +# ('max_length', 100), +# ], +# 'slug_field': [ +# ('max_length', 50), +# ], +# 'url_field': [ +# ('max_length', 200), +# ], +# } + +# def field_test(self, field): +# serializer = self.serializer_class(data={}) +# self.assertEqual(serializer.is_valid(), True) + +# for attribute in self.fields_attributes[field]: +# self.assertEqual( +# getattr(serializer.fields[field], attribute[0]), +# attribute[1] +# ) - def test_positive_integer_field(self): - self.field_test('positive_integer_field') +# def test_positive_integer_field(self): +# self.field_test('positive_integer_field') - def test_positive_small_integer_field(self): - self.field_test('positive_small_integer_field') +# def test_positive_small_integer_field(self): +# self.field_test('positive_small_integer_field') - def test_email_field(self): - self.field_test('email_field') +# def test_email_field(self): +# self.field_test('email_field') - def test_file_field(self): - self.field_test('file_field') +# def test_file_field(self): +# self.field_test('file_field') - def test_image_field(self): - self.field_test('image_field') +# def test_image_field(self): +# self.field_test('image_field') - def test_slug_field(self): - self.field_test('slug_field') +# def test_slug_field(self): +# self.field_test('slug_field') - def test_url_field(self): - self.field_test('url_field') +# def test_url_field(self): +# self.field_test('url_field') -class MetadataSerializer(serializers.Serializer): - field1 = serializers.CharField(max_length=3, required=True) - field2 = serializers.CharField(max_length=10, required=False) +# class MetadataSerializer(serializers.Serializer): +# field1 = serializers.CharField(max_length=3, required=True) +# field2 = serializers.CharField(max_length=10, required=False) -class MetadataSerializerTestCase(TestCase): - def setUp(self): - self.serializer = MetadataSerializer() +# class MetadataSerializerTestCase(TestCase): +# def setUp(self): +# self.serializer = MetadataSerializer() - def test_serializer_metadata(self): - metadata = self.serializer.metadata() - expected = { - 'field1': { - 'required': True, - 'max_length': 3, - 'type': 'string', - 'read_only': False - }, - 'field2': { - 'required': False, - 'max_length': 10, - 'type': 'string', - 'read_only': False - } - } - self.assertEqual(expected, metadata) +# def test_serializer_metadata(self): +# metadata = self.serializer.metadata() +# expected = { +# 'field1': { +# 'required': True, +# 'max_length': 3, +# 'type': 'string', +# 'read_only': False +# }, +# 'field2': { +# 'required': False, +# 'max_length': 10, +# 'type': 'string', +# 'read_only': False +# } +# } +# self.assertEqual(expected, metadata) -# Regression test for #840 +# # Regression test for #840 -class SimpleModel(models.Model): - text = models.CharField(max_length=100) +# class SimpleModel(models.Model): +# text = models.CharField(max_length=100) - -class SimpleModelSerializer(serializers.ModelSerializer): - text = serializers.CharField() - other = serializers.CharField() - - class Meta: - model = SimpleModel - - def validate_other(self, attrs, source): - del attrs['other'] - return attrs + +# class SimpleModelSerializer(serializers.ModelSerializer): +# text = serializers.CharField() +# other = serializers.CharField() + +# class Meta: +# model = SimpleModel + +# def validate_other(self, attrs, source): +# del attrs['other'] +# return attrs -class FieldValidationRemovingAttr(TestCase): - def test_removing_non_model_field_in_validation(self): - """ - Removing an attr during field valiation should ensure that it is not - passed through when restoring the object. +# class FieldValidationRemovingAttr(TestCase): +# def test_removing_non_model_field_in_validation(self): +# """ +# Removing an attr during field valiation should ensure that it is not +# passed through when restoring the object. - This allows additional non-model fields to be supported. +# This allows additional non-model fields to be supported. - Regression test for #840. - """ - serializer = SimpleModelSerializer(data={'text': 'foo', 'other': 'bar'}) - self.assertTrue(serializer.is_valid()) - serializer.save() - self.assertEqual(serializer.object.text, 'foo') +# Regression test for #840. +# """ +# serializer = SimpleModelSerializer(data={'text': 'foo', 'other': 'bar'}) +# self.assertTrue(serializer.is_valid()) +# serializer.save() +# self.assertEqual(serializer.object.text, 'foo') -# Regression test for #878 +# # Regression test for #878 -class SimpleTargetModel(models.Model): - text = models.CharField(max_length=100) +# class SimpleTargetModel(models.Model): +# text = models.CharField(max_length=100) -class SimplePKSourceModelSerializer(serializers.Serializer): - targets = serializers.PrimaryKeyRelatedField(queryset=SimpleTargetModel.objects.all(), many=True) - text = serializers.CharField() +# class SimplePKSourceModelSerializer(serializers.Serializer): +# targets = serializers.PrimaryKeyRelatedField(queryset=SimpleTargetModel.objects.all(), many=True) +# text = serializers.CharField() -class SimpleSlugSourceModelSerializer(serializers.Serializer): - targets = serializers.SlugRelatedField(queryset=SimpleTargetModel.objects.all(), many=True, slug_field='pk') - text = serializers.CharField() +# class SimpleSlugSourceModelSerializer(serializers.Serializer): +# targets = serializers.SlugRelatedField(queryset=SimpleTargetModel.objects.all(), many=True, slug_field='pk') +# text = serializers.CharField() -class SerializerSupportsManyRelationships(TestCase): - def setUp(self): - SimpleTargetModel.objects.create(text='foo') - SimpleTargetModel.objects.create(text='bar') +# class SerializerSupportsManyRelationships(TestCase): +# def setUp(self): +# SimpleTargetModel.objects.create(text='foo') +# SimpleTargetModel.objects.create(text='bar') - def test_serializer_supports_pk_many_relationships(self): - """ - Regression test for #878. +# def test_serializer_supports_pk_many_relationships(self): +# """ +# Regression test for #878. - Note that pk behavior has a different code path to usual cases, - for performance reasons. - """ - serializer = SimplePKSourceModelSerializer(data={'text': 'foo', 'targets': [1, 2]}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, {'text': 'foo', 'targets': [1, 2]}) +# Note that pk behavior has a different code path to usual cases, +# for performance reasons. +# """ +# serializer = SimplePKSourceModelSerializer(data={'text': 'foo', 'targets': [1, 2]}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, {'text': 'foo', 'targets': [1, 2]}) - def test_serializer_supports_slug_many_relationships(self): - """ - Regression test for #878. - """ - serializer = SimpleSlugSourceModelSerializer(data={'text': 'foo', 'targets': [1, 2]}) - self.assertTrue(serializer.is_valid()) - self.assertEqual(serializer.data, {'text': 'foo', 'targets': [1, 2]}) +# def test_serializer_supports_slug_many_relationships(self): +# """ +# Regression test for #878. +# """ +# serializer = SimpleSlugSourceModelSerializer(data={'text': 'foo', 'targets': [1, 2]}) +# self.assertTrue(serializer.is_valid()) +# self.assertEqual(serializer.data, {'text': 'foo', 'targets': [1, 2]}) -class TransformMethodsSerializer(serializers.Serializer): - a = serializers.CharField() - b_renamed = serializers.CharField(source='b') - - def transform_a(self, obj, value): - return value.lower() - - def transform_b_renamed(self, obj, value): - if value is not None: - return 'and ' + value +# class TransformMethodsSerializer(serializers.Serializer): +# a = serializers.CharField() +# b_renamed = serializers.CharField(source='b') + +# def transform_a(self, obj, value): +# return value.lower() + +# def transform_b_renamed(self, obj, value): +# if value is not None: +# return 'and ' + value -class TestSerializerTransformMethods(TestCase): - def setUp(self): - self.s = TransformMethodsSerializer() +# class TestSerializerTransformMethods(TestCase): +# def setUp(self): +# self.s = TransformMethodsSerializer() - def test_transform_methods(self): - self.assertEqual( - self.s.to_native({'a': 'GREEN EGGS', 'b': 'HAM'}), - { - 'a': 'green eggs', - 'b_renamed': 'and HAM', - } - ) +# def test_transform_methods(self): +# self.assertEqual( +# self.s.to_native({'a': 'GREEN EGGS', 'b': 'HAM'}), +# { +# 'a': 'green eggs', +# 'b_renamed': 'and HAM', +# } +# ) - def test_missing_fields(self): - self.assertEqual( - self.s.to_native({'a': 'GREEN EGGS'}), - { - 'a': 'green eggs', - 'b_renamed': None, - } - ) +# def test_missing_fields(self): +# self.assertEqual( +# self.s.to_native({'a': 'GREEN EGGS'}), +# { +# 'a': 'green eggs', +# 'b_renamed': None, +# } +# ) -class DefaultTrueBooleanModel(models.Model): - cat = models.BooleanField(default=True) - dog = models.BooleanField(default=False) +# class DefaultTrueBooleanModel(models.Model): +# cat = models.BooleanField(default=True) +# dog = models.BooleanField(default=False) -class SerializerDefaultTrueBoolean(TestCase): +# class SerializerDefaultTrueBoolean(TestCase): - def setUp(self): - super(SerializerDefaultTrueBoolean, self).setUp() - - class DefaultTrueBooleanSerializer(serializers.ModelSerializer): - class Meta: - model = DefaultTrueBooleanModel - fields = ('cat', 'dog') - - self.default_true_boolean_serializer = DefaultTrueBooleanSerializer - - def test_enabled_as_false(self): - serializer = self.default_true_boolean_serializer(data={'cat': False, - 'dog': False}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data['cat'], False) - self.assertEqual(serializer.data['dog'], False) +# def setUp(self): +# super(SerializerDefaultTrueBoolean, self).setUp() + +# class DefaultTrueBooleanSerializer(serializers.ModelSerializer): +# class Meta: +# model = DefaultTrueBooleanModel +# fields = ('cat', 'dog') + +# self.default_true_boolean_serializer = DefaultTrueBooleanSerializer + +# def test_enabled_as_false(self): +# serializer = self.default_true_boolean_serializer(data={'cat': False, +# 'dog': False}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data['cat'], False) +# self.assertEqual(serializer.data['dog'], False) - def test_enabled_as_true(self): - serializer = self.default_true_boolean_serializer(data={'cat': True, - 'dog': True}) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data['cat'], True) - self.assertEqual(serializer.data['dog'], True) - - def test_enabled_partial(self): - serializer = self.default_true_boolean_serializer(data={'cat': False}, - partial=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data['cat'], False) - self.assertEqual(serializer.data['dog'], False) - - -class BoolenFieldTypeTest(TestCase): - ''' - Ensure the various Boolean based model fields are rendered as the proper - field type - - ''' - - def setUp(self): - ''' - Setup an ActionItemSerializer for BooleanTesting - ''' - data = { - 'title': 'b' * 201, - } - self.serializer = ActionItemSerializer(data=data) - - def test_booleanfield_type(self): - ''' - Test that BooleanField is infered from models.BooleanField - ''' - bfield = self.serializer.get_fields()['done'] - self.assertEqual(type(bfield), fields.BooleanField) - - def test_nullbooleanfield_type(self): - ''' - Test that BooleanField is infered from models.NullBooleanField - - https://groups.google.com/forum/#!topic/django-rest-framework/D9mXEftpuQ8 - ''' - bfield = self.serializer.get_fields()['started'] - self.assertEqual(type(bfield), fields.BooleanField) +# def test_enabled_as_true(self): +# serializer = self.default_true_boolean_serializer(data={'cat': True, +# 'dog': True}) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data['cat'], True) +# self.assertEqual(serializer.data['dog'], True) + +# def test_enabled_partial(self): +# serializer = self.default_true_boolean_serializer(data={'cat': False}, +# partial=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data['cat'], False) +# self.assertEqual(serializer.data['dog'], False) + + +# class BoolenFieldTypeTest(TestCase): +# ''' +# Ensure the various Boolean based model fields are rendered as the proper +# field type + +# ''' + +# def setUp(self): +# ''' +# Setup an ActionItemSerializer for BooleanTesting +# ''' +# data = { +# 'title': 'b' * 201, +# } +# self.serializer = ActionItemSerializer(data=data) + +# def test_booleanfield_type(self): +# ''' +# Test that BooleanField is infered from models.BooleanField +# ''' +# bfield = self.serializer.get_fields()['done'] +# self.assertEqual(type(bfield), fields.BooleanField) + +# def test_nullbooleanfield_type(self): +# ''' +# Test that BooleanField is infered from models.NullBooleanField + +# https://groups.google.com/forum/#!topic/django-rest-framework/D9mXEftpuQ8 +# ''' +# bfield = self.serializer.get_fields()['started'] +# self.assertEqual(type(bfield), fields.BooleanField) diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index 67a8ed0dc..3341ce590 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -1,278 +1,278 @@ -""" -Tests to cover bulk create and update using serializers. -""" -from __future__ import unicode_literals -from django.test import TestCase -from rest_framework import serializers +# """ +# Tests to cover bulk create and update using serializers. +# """ +# from __future__ import unicode_literals +# from django.test import TestCase +# from rest_framework import serializers -class BulkCreateSerializerTests(TestCase): - """ - Creating multiple instances using serializers. - """ +# class BulkCreateSerializerTests(TestCase): +# """ +# Creating multiple instances using serializers. +# """ - def setUp(self): - class BookSerializer(serializers.Serializer): - id = serializers.IntegerField() - title = serializers.CharField(max_length=100) - author = serializers.CharField(max_length=100) +# def setUp(self): +# class BookSerializer(serializers.Serializer): +# id = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# author = serializers.CharField(max_length=100) - self.BookSerializer = BookSerializer +# self.BookSerializer = BookSerializer - def test_bulk_create_success(self): - """ - Correct bulk update serialization should return the input data. - """ +# def test_bulk_create_success(self): +# """ +# Correct bulk update serialization should return the input data. +# """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 2, - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 2, +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) - def test_bulk_create_errors(self): - """ - Correct bulk update serialization should return the input data. - """ +# def test_bulk_create_errors(self): +# """ +# Correct bulk update serialization should return the input data. +# """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 'foo', - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {}, - {'id': ['Enter a whole number.']} - ] +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 'foo', +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {}, +# {'id': ['Enter a whole number.']} +# ] - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) - def test_invalid_list_datatype(self): - """ - Data containing list of incorrect data type should return errors. - """ - data = ['foo', 'bar', 'baz'] - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) +# def test_invalid_list_datatype(self): +# """ +# Data containing list of incorrect data type should return errors. +# """ +# data = ['foo', 'bar', 'baz'] +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) - expected_errors = [ - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']}, - {'non_field_errors': ['Invalid data']} - ] +# expected_errors = [ +# {'non_field_errors': ['Invalid data']}, +# {'non_field_errors': ['Invalid data']}, +# {'non_field_errors': ['Invalid data']} +# ] - self.assertEqual(serializer.errors, expected_errors) +# self.assertEqual(serializer.errors, expected_errors) - def test_invalid_single_datatype(self): - """ - Data containing a single incorrect data type should return errors. - """ - data = 123 - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) +# def test_invalid_single_datatype(self): +# """ +# Data containing a single incorrect data type should return errors. +# """ +# data = 123 +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items.']} +# expected_errors = {'non_field_errors': ['Expected a list of items.']} - self.assertEqual(serializer.errors, expected_errors) +# self.assertEqual(serializer.errors, expected_errors) - def test_invalid_single_object(self): - """ - Data containing only a single object, instead of a list of objects - should return errors. - """ - data = { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - } - serializer = self.BookSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) +# def test_invalid_single_object(self): +# """ +# Data containing only a single object, instead of a list of objects +# should return errors. +# """ +# data = { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# } +# serializer = self.BookSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) - expected_errors = {'non_field_errors': ['Expected a list of items.']} +# expected_errors = {'non_field_errors': ['Expected a list of items.']} - self.assertEqual(serializer.errors, expected_errors) +# self.assertEqual(serializer.errors, expected_errors) -class BulkUpdateSerializerTests(TestCase): - """ - Updating multiple instances using serializers. - """ +# class BulkUpdateSerializerTests(TestCase): +# """ +# Updating multiple instances using serializers. +# """ - def setUp(self): - class Book(object): - """ - A data type that can be persisted to a mock storage backend - with `.save()` and `.delete()`. - """ - object_map = {} +# def setUp(self): +# class Book(object): +# """ +# A data type that can be persisted to a mock storage backend +# with `.save()` and `.delete()`. +# """ +# object_map = {} - def __init__(self, id, title, author): - self.id = id - self.title = title - self.author = author +# def __init__(self, id, title, author): +# self.id = id +# self.title = title +# self.author = author - def save(self): - Book.object_map[self.id] = self +# def save(self): +# Book.object_map[self.id] = self - def delete(self): - del Book.object_map[self.id] +# def delete(self): +# del Book.object_map[self.id] - class BookSerializer(serializers.Serializer): - id = serializers.IntegerField() - title = serializers.CharField(max_length=100) - author = serializers.CharField(max_length=100) +# class BookSerializer(serializers.Serializer): +# id = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# author = serializers.CharField(max_length=100) - def restore_object(self, attrs, instance=None): - if instance: - instance.id = attrs['id'] - instance.title = attrs['title'] - instance.author = attrs['author'] - return instance - return Book(**attrs) +# def restore_object(self, attrs, instance=None): +# if instance: +# instance.id = attrs['id'] +# instance.title = attrs['title'] +# instance.author = attrs['author'] +# return instance +# return Book(**attrs) - self.Book = Book - self.BookSerializer = BookSerializer +# self.Book = Book +# self.BookSerializer = BookSerializer - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 1, - 'title': 'If this is a man', - 'author': 'Primo Levi' - }, { - 'id': 2, - 'title': 'The wind-up bird chronicle', - 'author': 'Haruki Murakami' - } - ] +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 1, +# 'title': 'If this is a man', +# 'author': 'Primo Levi' +# }, { +# 'id': 2, +# 'title': 'The wind-up bird chronicle', +# 'author': 'Haruki Murakami' +# } +# ] - for item in data: - book = Book(item['id'], item['title'], item['author']) - book.save() +# for item in data: +# book = Book(item['id'], item['title'], item['author']) +# book.save() - def books(self): - """ - Return all the objects in the mock storage backend. - """ - return self.Book.object_map.values() +# def books(self): +# """ +# Return all the objects in the mock storage backend. +# """ +# return self.Book.object_map.values() - def test_bulk_update_success(self): - """ - Correct bulk update serialization should return the input data. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 2, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data, data) - serializer.save() - new_data = self.BookSerializer(self.books(), many=True).data +# def test_bulk_update_success(self): +# """ +# Correct bulk update serialization should return the input data. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 2, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data, data) +# serializer.save() +# new_data = self.BookSerializer(self.books(), many=True).data - self.assertEqual(data, new_data) +# self.assertEqual(data, new_data) - def test_bulk_update_and_create(self): - """ - Bulk update serialization may also include created items. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 3, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.data, data) - serializer.save() - new_data = self.BookSerializer(self.books(), many=True).data - self.assertEqual(data, new_data) +# def test_bulk_update_and_create(self): +# """ +# Bulk update serialization may also include created items. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 3, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.data, data) +# serializer.save() +# new_data = self.BookSerializer(self.books(), many=True).data +# self.assertEqual(data, new_data) - def test_bulk_update_invalid_create(self): - """ - Bulk update serialization without allow_add_remove may not create items. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 3, - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} - ] - serializer = self.BookSerializer(self.books(), data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# def test_bulk_update_invalid_create(self): +# """ +# Bulk update serialization without allow_add_remove may not create items. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 3, +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {'non_field_errors': ['Cannot create a new item, only existing items may be updated.']} +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) - def test_bulk_update_error(self): - """ - Incorrect bulk update serialization should return error data. - """ - data = [ - { - 'id': 0, - 'title': 'The electric kool-aid acid test', - 'author': 'Tom Wolfe' - }, { - 'id': 'foo', - 'title': 'Kafka on the shore', - 'author': 'Haruki Murakami' - } - ] - expected_errors = [ - {}, - {'id': ['Enter a whole number.']} - ] - serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# def test_bulk_update_error(self): +# """ +# Incorrect bulk update serialization should return error data. +# """ +# data = [ +# { +# 'id': 0, +# 'title': 'The electric kool-aid acid test', +# 'author': 'Tom Wolfe' +# }, { +# 'id': 'foo', +# 'title': 'Kafka on the shore', +# 'author': 'Haruki Murakami' +# } +# ] +# expected_errors = [ +# {}, +# {'id': ['Enter a whole number.']} +# ] +# serializer = self.BookSerializer(self.books(), data=data, many=True, allow_add_remove=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) diff --git a/tests/test_serializer_empty.py b/tests/test_serializer_empty.py index 30cff3615..d0006ad38 100644 --- a/tests/test_serializer_empty.py +++ b/tests/test_serializer_empty.py @@ -1,15 +1,15 @@ -from django.test import TestCase -from rest_framework import serializers +# from django.test import TestCase +# from rest_framework import serializers -class EmptySerializerTestCase(TestCase): - def test_empty_serializer(self): - class FooBarSerializer(serializers.Serializer): - foo = serializers.IntegerField() - bar = serializers.SerializerMethodField('get_bar') +# class EmptySerializerTestCase(TestCase): +# def test_empty_serializer(self): +# class FooBarSerializer(serializers.Serializer): +# foo = serializers.IntegerField() +# bar = serializers.SerializerMethodField('get_bar') - def get_bar(self, obj): - return 'bar' +# def get_bar(self, obj): +# return 'bar' - serializer = FooBarSerializer() - self.assertEquals(serializer.data, {'foo': 0}) +# serializer = FooBarSerializer() +# self.assertEquals(serializer.data, {'foo': 0}) diff --git a/tests/test_serializer_import.py b/tests/test_serializer_import.py index 3b8ff4b3c..d029c3c55 100644 --- a/tests/test_serializer_import.py +++ b/tests/test_serializer_import.py @@ -1,19 +1,19 @@ -from django.test import TestCase +# from django.test import TestCase -from rest_framework import serializers -from tests.accounts.serializers import AccountSerializer +# from rest_framework import serializers +# from tests.accounts.serializers import AccountSerializer -class ImportingModelSerializerTests(TestCase): - """ - In some situations like, GH #1225, it is possible, especially in - testing, to import a serializer who's related models have not yet - been resolved by Django. `AccountSerializer` is an example of such - a serializer (imported at the top of this file). - """ - def test_import_model_serializer(self): - """ - The serializer at the top of this file should have been - imported successfully, and we should be able to instantiate it. - """ - self.assertIsInstance(AccountSerializer(), serializers.ModelSerializer) +# class ImportingModelSerializerTests(TestCase): +# """ +# In some situations like, GH #1225, it is possible, especially in +# testing, to import a serializer who's related models have not yet +# been resolved by Django. `AccountSerializer` is an example of such +# a serializer (imported at the top of this file). +# """ +# def test_import_model_serializer(self): +# """ +# The serializer at the top of this file should have been +# imported successfully, and we should be able to instantiate it. +# """ +# self.assertIsInstance(AccountSerializer(), serializers.ModelSerializer) diff --git a/tests/test_serializer_nested.py b/tests/test_serializer_nested.py index c09c24db2..b0f64ca77 100644 --- a/tests/test_serializer_nested.py +++ b/tests/test_serializer_nested.py @@ -1,349 +1,349 @@ -""" -Tests to cover nested serializers. +# """ +# Tests to cover nested serializers. -Doesn't cover model serializers. -""" -from __future__ import unicode_literals -from django.test import TestCase -from rest_framework import serializers -from . import models +# Doesn't cover model serializers. +# """ +# from __future__ import unicode_literals +# from django.test import TestCase +# from rest_framework import serializers +# from . import models -class WritableNestedSerializerBasicTests(TestCase): - """ - Tests for deserializing nested entities. - Basic tests that use serializers that simply restore to dicts. - """ +# class WritableNestedSerializerBasicTests(TestCase): +# """ +# Tests for deserializing nested entities. +# Basic tests that use serializers that simply restore to dicts. +# """ - def setUp(self): - class TrackSerializer(serializers.Serializer): - order = serializers.IntegerField() - title = serializers.CharField(max_length=100) - duration = serializers.IntegerField() +# def setUp(self): +# class TrackSerializer(serializers.Serializer): +# order = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# duration = serializers.IntegerField() - class AlbumSerializer(serializers.Serializer): - album_name = serializers.CharField(max_length=100) - artist = serializers.CharField(max_length=100) - tracks = TrackSerializer(many=True) +# class AlbumSerializer(serializers.Serializer): +# album_name = serializers.CharField(max_length=100) +# artist = serializers.CharField(max_length=100) +# tracks = TrackSerializer(many=True) - self.AlbumSerializer = AlbumSerializer +# self.AlbumSerializer = AlbumSerializer - def test_nested_validation_success(self): - """ - Correct nested serialization should return the input data. - """ +# def test_nested_validation_success(self): +# """ +# Correct nested serialization should return the input data. +# """ - data = { - 'album_name': 'Discovery', - 'artist': 'Daft Punk', - 'tracks': [ - {'order': 1, 'title': 'One More Time', 'duration': 235}, - {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, - {'order': 3, 'title': 'Digital Love', 'duration': 239} - ] - } +# data = { +# 'album_name': 'Discovery', +# 'artist': 'Daft Punk', +# 'tracks': [ +# {'order': 1, 'title': 'One More Time', 'duration': 235}, +# {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, +# {'order': 3, 'title': 'Digital Love', 'duration': 239} +# ] +# } - serializer = self.AlbumSerializer(data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, data) +# serializer = self.AlbumSerializer(data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, data) - def test_nested_validation_error(self): - """ - Incorrect nested serialization should return appropriate error data. - """ +# def test_nested_validation_error(self): +# """ +# Incorrect nested serialization should return appropriate error data. +# """ - data = { - 'album_name': 'Discovery', - 'artist': 'Daft Punk', - 'tracks': [ - {'order': 1, 'title': 'One More Time', 'duration': 235}, - {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, - {'order': 3, 'title': 'Digital Love', 'duration': 'foobar'} - ] - } - expected_errors = { - 'tracks': [ - {}, - {}, - {'duration': ['Enter a whole number.']} - ] - } +# data = { +# 'album_name': 'Discovery', +# 'artist': 'Daft Punk', +# 'tracks': [ +# {'order': 1, 'title': 'One More Time', 'duration': 235}, +# {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, +# {'order': 3, 'title': 'Digital Love', 'duration': 'foobar'} +# ] +# } +# expected_errors = { +# 'tracks': [ +# {}, +# {}, +# {'duration': ['Enter a whole number.']} +# ] +# } - serializer = self.AlbumSerializer(data=data) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# serializer = self.AlbumSerializer(data=data) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) - def test_many_nested_validation_error(self): - """ - Incorrect nested serialization should return appropriate error data - when multiple entities are being deserialized. - """ +# def test_many_nested_validation_error(self): +# """ +# Incorrect nested serialization should return appropriate error data +# when multiple entities are being deserialized. +# """ - data = [ - { - 'album_name': 'Russian Red', - 'artist': 'I Love Your Glasses', - 'tracks': [ - {'order': 1, 'title': 'Cigarettes', 'duration': 121}, - {'order': 2, 'title': 'No Past Land', 'duration': 198}, - {'order': 3, 'title': 'They Don\'t Believe', 'duration': 191} - ] - }, - { - 'album_name': 'Discovery', - 'artist': 'Daft Punk', - 'tracks': [ - {'order': 1, 'title': 'One More Time', 'duration': 235}, - {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, - {'order': 3, 'title': 'Digital Love', 'duration': 'foobar'} - ] - } - ] - expected_errors = [ - {}, - { - 'tracks': [ - {}, - {}, - {'duration': ['Enter a whole number.']} - ] - } - ] +# data = [ +# { +# 'album_name': 'Russian Red', +# 'artist': 'I Love Your Glasses', +# 'tracks': [ +# {'order': 1, 'title': 'Cigarettes', 'duration': 121}, +# {'order': 2, 'title': 'No Past Land', 'duration': 198}, +# {'order': 3, 'title': 'They Don\'t Believe', 'duration': 191} +# ] +# }, +# { +# 'album_name': 'Discovery', +# 'artist': 'Daft Punk', +# 'tracks': [ +# {'order': 1, 'title': 'One More Time', 'duration': 235}, +# {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, +# {'order': 3, 'title': 'Digital Love', 'duration': 'foobar'} +# ] +# } +# ] +# expected_errors = [ +# {}, +# { +# 'tracks': [ +# {}, +# {}, +# {'duration': ['Enter a whole number.']} +# ] +# } +# ] - serializer = self.AlbumSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), False) - self.assertEqual(serializer.errors, expected_errors) +# serializer = self.AlbumSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), False) +# self.assertEqual(serializer.errors, expected_errors) -class WritableNestedSerializerObjectTests(TestCase): - """ - Tests for deserializing nested entities. - These tests use serializers that restore to concrete objects. - """ +# class WritableNestedSerializerObjectTests(TestCase): +# """ +# Tests for deserializing nested entities. +# These tests use serializers that restore to concrete objects. +# """ - def setUp(self): - # Couple of concrete objects that we're going to deserialize into - class Track(object): - def __init__(self, order, title, duration): - self.order, self.title, self.duration = order, title, duration +# def setUp(self): +# # Couple of concrete objects that we're going to deserialize into +# class Track(object): +# def __init__(self, order, title, duration): +# self.order, self.title, self.duration = order, title, duration - def __eq__(self, other): - return ( - self.order == other.order and - self.title == other.title and - self.duration == other.duration - ) +# def __eq__(self, other): +# return ( +# self.order == other.order and +# self.title == other.title and +# self.duration == other.duration +# ) - class Album(object): - def __init__(self, album_name, artist, tracks): - self.album_name, self.artist, self.tracks = album_name, artist, tracks +# class Album(object): +# def __init__(self, album_name, artist, tracks): +# self.album_name, self.artist, self.tracks = album_name, artist, tracks - def __eq__(self, other): - return ( - self.album_name == other.album_name and - self.artist == other.artist and - self.tracks == other.tracks - ) +# def __eq__(self, other): +# return ( +# self.album_name == other.album_name and +# self.artist == other.artist and +# self.tracks == other.tracks +# ) - # And their corresponding serializers - class TrackSerializer(serializers.Serializer): - order = serializers.IntegerField() - title = serializers.CharField(max_length=100) - duration = serializers.IntegerField() +# # And their corresponding serializers +# class TrackSerializer(serializers.Serializer): +# order = serializers.IntegerField() +# title = serializers.CharField(max_length=100) +# duration = serializers.IntegerField() - def restore_object(self, attrs, instance=None): - return Track(attrs['order'], attrs['title'], attrs['duration']) +# def restore_object(self, attrs, instance=None): +# return Track(attrs['order'], attrs['title'], attrs['duration']) - class AlbumSerializer(serializers.Serializer): - album_name = serializers.CharField(max_length=100) - artist = serializers.CharField(max_length=100) - tracks = TrackSerializer(many=True) +# class AlbumSerializer(serializers.Serializer): +# album_name = serializers.CharField(max_length=100) +# artist = serializers.CharField(max_length=100) +# tracks = TrackSerializer(many=True) - def restore_object(self, attrs, instance=None): - return Album(attrs['album_name'], attrs['artist'], attrs['tracks']) +# def restore_object(self, attrs, instance=None): +# return Album(attrs['album_name'], attrs['artist'], attrs['tracks']) - self.Album, self.Track = Album, Track - self.AlbumSerializer = AlbumSerializer +# self.Album, self.Track = Album, Track +# self.AlbumSerializer = AlbumSerializer - def test_nested_validation_success(self): - """ - Correct nested serialization should return a restored object - that corresponds to the input data. - """ +# def test_nested_validation_success(self): +# """ +# Correct nested serialization should return a restored object +# that corresponds to the input data. +# """ - data = { - 'album_name': 'Discovery', - 'artist': 'Daft Punk', - 'tracks': [ - {'order': 1, 'title': 'One More Time', 'duration': 235}, - {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, - {'order': 3, 'title': 'Digital Love', 'duration': 239} - ] - } - expected_object = self.Album( - album_name='Discovery', - artist='Daft Punk', - tracks=[ - self.Track(order=1, title='One More Time', duration=235), - self.Track(order=2, title='Aerodynamic', duration=184), - self.Track(order=3, title='Digital Love', duration=239), - ] - ) +# data = { +# 'album_name': 'Discovery', +# 'artist': 'Daft Punk', +# 'tracks': [ +# {'order': 1, 'title': 'One More Time', 'duration': 235}, +# {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, +# {'order': 3, 'title': 'Digital Love', 'duration': 239} +# ] +# } +# expected_object = self.Album( +# album_name='Discovery', +# artist='Daft Punk', +# tracks=[ +# self.Track(order=1, title='One More Time', duration=235), +# self.Track(order=2, title='Aerodynamic', duration=184), +# self.Track(order=3, title='Digital Love', duration=239), +# ] +# ) - serializer = self.AlbumSerializer(data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected_object) +# serializer = self.AlbumSerializer(data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected_object) - def test_many_nested_validation_success(self): - """ - Correct nested serialization should return multiple restored objects - that corresponds to the input data when multiple objects are - being deserialized. - """ +# def test_many_nested_validation_success(self): +# """ +# Correct nested serialization should return multiple restored objects +# that corresponds to the input data when multiple objects are +# being deserialized. +# """ - data = [ - { - 'album_name': 'Russian Red', - 'artist': 'I Love Your Glasses', - 'tracks': [ - {'order': 1, 'title': 'Cigarettes', 'duration': 121}, - {'order': 2, 'title': 'No Past Land', 'duration': 198}, - {'order': 3, 'title': 'They Don\'t Believe', 'duration': 191} - ] - }, - { - 'album_name': 'Discovery', - 'artist': 'Daft Punk', - 'tracks': [ - {'order': 1, 'title': 'One More Time', 'duration': 235}, - {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, - {'order': 3, 'title': 'Digital Love', 'duration': 239} - ] - } - ] - expected_object = [ - self.Album( - album_name='Russian Red', - artist='I Love Your Glasses', - tracks=[ - self.Track(order=1, title='Cigarettes', duration=121), - self.Track(order=2, title='No Past Land', duration=198), - self.Track(order=3, title='They Don\'t Believe', duration=191), - ] - ), - self.Album( - album_name='Discovery', - artist='Daft Punk', - tracks=[ - self.Track(order=1, title='One More Time', duration=235), - self.Track(order=2, title='Aerodynamic', duration=184), - self.Track(order=3, title='Digital Love', duration=239), - ] - ) - ] +# data = [ +# { +# 'album_name': 'Russian Red', +# 'artist': 'I Love Your Glasses', +# 'tracks': [ +# {'order': 1, 'title': 'Cigarettes', 'duration': 121}, +# {'order': 2, 'title': 'No Past Land', 'duration': 198}, +# {'order': 3, 'title': 'They Don\'t Believe', 'duration': 191} +# ] +# }, +# { +# 'album_name': 'Discovery', +# 'artist': 'Daft Punk', +# 'tracks': [ +# {'order': 1, 'title': 'One More Time', 'duration': 235}, +# {'order': 2, 'title': 'Aerodynamic', 'duration': 184}, +# {'order': 3, 'title': 'Digital Love', 'duration': 239} +# ] +# } +# ] +# expected_object = [ +# self.Album( +# album_name='Russian Red', +# artist='I Love Your Glasses', +# tracks=[ +# self.Track(order=1, title='Cigarettes', duration=121), +# self.Track(order=2, title='No Past Land', duration=198), +# self.Track(order=3, title='They Don\'t Believe', duration=191), +# ] +# ), +# self.Album( +# album_name='Discovery', +# artist='Daft Punk', +# tracks=[ +# self.Track(order=1, title='One More Time', duration=235), +# self.Track(order=2, title='Aerodynamic', duration=184), +# self.Track(order=3, title='Digital Love', duration=239), +# ] +# ) +# ] - serializer = self.AlbumSerializer(data=data, many=True) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected_object) +# serializer = self.AlbumSerializer(data=data, many=True) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected_object) -class ForeignKeyNestedSerializerUpdateTests(TestCase): - def setUp(self): - class Artist(object): - def __init__(self, name): - self.name = name +# class ForeignKeyNestedSerializerUpdateTests(TestCase): +# def setUp(self): +# class Artist(object): +# def __init__(self, name): +# self.name = name - def __eq__(self, other): - return self.name == other.name +# def __eq__(self, other): +# return self.name == other.name - class Album(object): - def __init__(self, name, artist): - self.name, self.artist = name, artist +# class Album(object): +# def __init__(self, name, artist): +# self.name, self.artist = name, artist - def __eq__(self, other): - return self.name == other.name and self.artist == other.artist +# def __eq__(self, other): +# return self.name == other.name and self.artist == other.artist - class ArtistSerializer(serializers.Serializer): - name = serializers.CharField() +# class ArtistSerializer(serializers.Serializer): +# name = serializers.CharField() - def restore_object(self, attrs, instance=None): - if instance: - instance.name = attrs['name'] - else: - instance = Artist(attrs['name']) - return instance +# def restore_object(self, attrs, instance=None): +# if instance: +# instance.name = attrs['name'] +# else: +# instance = Artist(attrs['name']) +# return instance - class AlbumSerializer(serializers.Serializer): - name = serializers.CharField() - by = ArtistSerializer(source='artist') +# class AlbumSerializer(serializers.Serializer): +# name = serializers.CharField() +# by = ArtistSerializer(source='artist') - def restore_object(self, attrs, instance=None): - if instance: - instance.name = attrs['name'] - instance.artist = attrs['artist'] - else: - instance = Album(attrs['name'], attrs['artist']) - return instance +# def restore_object(self, attrs, instance=None): +# if instance: +# instance.name = attrs['name'] +# instance.artist = attrs['artist'] +# else: +# instance = Album(attrs['name'], attrs['artist']) +# return instance - self.Artist = Artist - self.Album = Album - self.AlbumSerializer = AlbumSerializer +# self.Artist = Artist +# self.Album = Album +# self.AlbumSerializer = AlbumSerializer - def test_create_via_foreign_key_with_source(self): - """ - Check that we can both *create* and *update* into objects across - ForeignKeys that have a `source` specified. - Regression test for #1170 - """ - data = { - 'name': 'Discovery', - 'by': {'name': 'Daft Punk'}, - } +# def test_create_via_foreign_key_with_source(self): +# """ +# Check that we can both *create* and *update* into objects across +# ForeignKeys that have a `source` specified. +# Regression test for #1170 +# """ +# data = { +# 'name': 'Discovery', +# 'by': {'name': 'Daft Punk'}, +# } - expected = self.Album(artist=self.Artist('Daft Punk'), name='Discovery') +# expected = self.Album(artist=self.Artist('Daft Punk'), name='Discovery') - # create - serializer = self.AlbumSerializer(data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected) +# # create +# serializer = self.AlbumSerializer(data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected) - # update - original = self.Album(artist=self.Artist('The Bats'), name='Free All the Monsters') - serializer = self.AlbumSerializer(instance=original, data=data) - self.assertEqual(serializer.is_valid(), True) - self.assertEqual(serializer.object, expected) +# # update +# original = self.Album(artist=self.Artist('The Bats'), name='Free All the Monsters') +# serializer = self.AlbumSerializer(instance=original, data=data) +# self.assertEqual(serializer.is_valid(), True) +# self.assertEqual(serializer.object, expected) -class NestedModelSerializerUpdateTests(TestCase): - def test_second_nested_level(self): - john = models.Person.objects.create(name="john") +# class NestedModelSerializerUpdateTests(TestCase): +# def test_second_nested_level(self): +# john = models.Person.objects.create(name="john") - post = john.blogpost_set.create(title="Test blog post") - post.blogpostcomment_set.create(text="I hate this blog post") - post.blogpostcomment_set.create(text="I love this blog post") +# post = john.blogpost_set.create(title="Test blog post") +# post.blogpostcomment_set.create(text="I hate this blog post") +# post.blogpostcomment_set.create(text="I love this blog post") - class BlogPostCommentSerializer(serializers.ModelSerializer): - class Meta: - model = models.BlogPostComment +# class BlogPostCommentSerializer(serializers.ModelSerializer): +# class Meta: +# model = models.BlogPostComment - class BlogPostSerializer(serializers.ModelSerializer): - comments = BlogPostCommentSerializer(many=True, source='blogpostcomment_set') +# class BlogPostSerializer(serializers.ModelSerializer): +# comments = BlogPostCommentSerializer(many=True, source='blogpostcomment_set') - class Meta: - model = models.BlogPost - fields = ('id', 'title', 'comments') +# class Meta: +# model = models.BlogPost +# fields = ('id', 'title', 'comments') - class PersonSerializer(serializers.ModelSerializer): - posts = BlogPostSerializer(many=True, source='blogpost_set') +# class PersonSerializer(serializers.ModelSerializer): +# posts = BlogPostSerializer(many=True, source='blogpost_set') - class Meta: - model = models.Person - fields = ('id', 'name', 'age', 'posts') +# class Meta: +# model = models.Person +# fields = ('id', 'name', 'age', 'posts') - serialize = PersonSerializer(instance=john) - deserialize = PersonSerializer(data=serialize.data, instance=john) - self.assertTrue(deserialize.is_valid()) +# serialize = PersonSerializer(instance=john) +# deserialize = PersonSerializer(data=serialize.data, instance=john) +# self.assertTrue(deserialize.is_valid()) - result = deserialize.object - result.save() - self.assertEqual(result.id, john.id) +# result = deserialize.object +# result.save() +# self.assertEqual(result.id, john.id) diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 31c417306..0a105e8e8 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -1,31 +1,31 @@ -from django.test import TestCase -from django.utils import six -from rest_framework.serializers import _resolve_model -from tests.models import BasicModel +# from django.test import TestCase +# from django.utils import six +# from rest_framework.serializers import _resolve_model +# from tests.models import BasicModel -class ResolveModelTests(TestCase): - """ - `_resolve_model` should return a Django model class given the - provided argument is a Django model class itself, or a properly - formatted string representation of one. - """ - def test_resolve_django_model(self): - resolved_model = _resolve_model(BasicModel) - self.assertEqual(resolved_model, BasicModel) +# class ResolveModelTests(TestCase): +# """ +# `_resolve_model` should return a Django model class given the +# provided argument is a Django model class itself, or a properly +# formatted string representation of one. +# """ +# def test_resolve_django_model(self): +# resolved_model = _resolve_model(BasicModel) +# self.assertEqual(resolved_model, BasicModel) - def test_resolve_string_representation(self): - resolved_model = _resolve_model('tests.BasicModel') - self.assertEqual(resolved_model, BasicModel) +# def test_resolve_string_representation(self): +# resolved_model = _resolve_model('tests.BasicModel') +# self.assertEqual(resolved_model, BasicModel) - def test_resolve_unicode_representation(self): - resolved_model = _resolve_model(six.text_type('tests.BasicModel')) - self.assertEqual(resolved_model, BasicModel) +# def test_resolve_unicode_representation(self): +# resolved_model = _resolve_model(six.text_type('tests.BasicModel')) +# self.assertEqual(resolved_model, BasicModel) - def test_resolve_non_django_model(self): - with self.assertRaises(ValueError): - _resolve_model(TestCase) +# def test_resolve_non_django_model(self): +# with self.assertRaises(ValueError): +# _resolve_model(TestCase) - def test_resolve_improper_string_representation(self): - with self.assertRaises(ValueError): - _resolve_model('BasicModel') +# def test_resolve_improper_string_representation(self): +# with self.assertRaises(ValueError): +# _resolve_model('BasicModel') From d934824bff21e4a11226af61efba319be227f4f0 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 5 Sep 2014 16:29:46 +0100 Subject: [PATCH 025/100] Workin on --- rest_framework/exceptions.py | 9 +++- rest_framework/fields.py | 26 +++++++++-- rest_framework/generics.py | 30 +----------- rest_framework/mixins.py | 83 +++++++++++++++++++-------------- rest_framework/serializers.py | 64 ++++++++++++++++--------- tests/test_generics.py | 41 ++-------------- tests/test_validation.py | 5 +- tests/test_write_only_fields.py | 69 ++++++++++++++------------- 8 files changed, 159 insertions(+), 168 deletions(-) diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index ad52d1726..852a08b1a 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -15,7 +15,7 @@ class APIException(Exception): Subclasses should provide `.status_code` and `.default_detail` properties. """ status_code = status.HTTP_500_INTERNAL_SERVER_ERROR - default_detail = '' + default_detail = 'A server error occured' def __init__(self, detail=None): self.detail = detail or self.default_detail @@ -29,6 +29,11 @@ class ParseError(APIException): default_detail = 'Malformed request.' +class ValidationError(APIException): + status_code = status.HTTP_400_BAD_REQUEST + default_detail = 'Invalid data in request.' + + class AuthenticationFailed(APIException): status_code = status.HTTP_401_UNAUTHORIZED default_detail = 'Incorrect authentication credentials.' @@ -54,7 +59,7 @@ class MethodNotAllowed(APIException): class NotAcceptable(APIException): status_code = status.HTTP_406_NOT_ACCEPTABLE - default_detail = "Could not satisfy the request's Accept header" + default_detail = "Could not satisfy the request Accept header" def __init__(self, detail=None, available_renderers=None): self.detail = detail or self.default_detail diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 838aa3b0c..d18551b37 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,3 +1,4 @@ +from rest_framework.exceptions import ValidationError from rest_framework.utils import html import inspect @@ -59,10 +60,6 @@ def set_value(dictionary, keys, value): dictionary[keys[-1]] = value -class ValidationError(Exception): - pass - - class SkipField(Exception): pass @@ -204,6 +201,22 @@ class Field(object): msg = self._MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) + def __new__(cls, *args, **kwargs): + instance = super(Field, cls).__new__(cls) + instance._args = args + instance._kwargs = kwargs + return instance + + def __repr__(self): + arg_string = ', '.join([repr(val) for val in self._args]) + kwarg_string = ', '.join([ + '%s=%s' % (key, repr(val)) for key, val in self._kwargs.items() + ]) + if arg_string and kwarg_string: + arg_string += ', ' + class_name = self.__class__.__name__ + return "%s(%s%s)" % (class_name, arg_string, kwarg_string) + class BooleanField(Field): MESSAGES = { @@ -308,6 +321,11 @@ class IntegerField(Field): 'invalid_integer': 'A valid integer is required.' } + def __init__(self, **kwargs): + self.max_value = kwargs.pop('max_value') + self.min_value = kwargs.pop('min_value') + super(CharField, self).__init__(**kwargs) + def to_native(self, data): try: data = int(str(data)) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 6705cbb2f..c2c59154a 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -27,7 +27,7 @@ def strict_positive_int(integer_string, cutoff=None): def get_object_or_404(queryset, *filter_args, **filter_kwargs): """ - Same as Django's standard shortcut, but make sure to raise 404 + Same as Django's standard shortcut, but make sure to also raise 404 if the filter_kwargs don't match the required types. """ try: @@ -249,34 +249,6 @@ class GenericAPIView(views.APIView): # # The are not called by GenericAPIView directly, # but are used by the mixin methods. - - def pre_save(self, obj): - """ - Placeholder method for calling before saving an object. - - May be used to set attributes on the object that are implicit - in either the request, or the url. - """ - pass - - def post_save(self, obj, created=False): - """ - Placeholder method for calling after saving an object. - """ - pass - - def pre_delete(self, obj): - """ - Placeholder method for calling before deleting an object. - """ - pass - - def post_delete(self, obj): - """ - Placeholder method for calling after deleting an object. - """ - pass - def metadata(self, request): """ Return a dictionary of metadata about the view. diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 359740cee..14a6b44b1 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -19,14 +19,10 @@ class CreateModelMixin(object): """ def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.DATA) - - if serializer.is_valid(): - self.object = serializer.save() - headers = self.get_success_headers(serializer.data) - return Response(serializer.data, status=status.HTTP_201_CREATED, - headers=headers) - - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + serializer.is_valid(raise_exception=True) + serializer.save() + headers = self.get_success_headers(serializer.data) + return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) def get_success_headers(self, data): try: @@ -40,15 +36,12 @@ class ListModelMixin(object): List a queryset. """ def list(self, request, *args, **kwargs): - self.object_list = self.filter_queryset(self.get_queryset()) - - # Switch between paginated or standard style responses - page = self.paginate_queryset(self.object_list) + instance = self.filter_queryset(self.get_queryset()) + page = self.paginate_queryset(instance) if page is not None: serializer = self.get_pagination_serializer(page) else: - serializer = self.get_serializer(self.object_list, many=True) - + serializer = self.get_serializer(instance, many=True) return Response(serializer.data) @@ -57,8 +50,8 @@ class RetrieveModelMixin(object): Retrieve a model instance. """ def retrieve(self, request, *args, **kwargs): - self.object = self.get_object() - serializer = self.get_serializer(self.object) + instance = self.get_object() + serializer = self.get_serializer(instance) return Response(serializer.data) @@ -68,22 +61,52 @@ class UpdateModelMixin(object): """ def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) - self.object = self.get_object_or_none() + instance = self.get_object() + serializer = self.get_serializer(instance, data=request.DATA, partial=partial) + serializer.is_valid(raise_exception=True) + serializer.save() + return Response(serializer.data) - serializer = self.get_serializer(self.object, data=request.DATA, partial=partial) + def partial_update(self, request, *args, **kwargs): + kwargs['partial'] = True + return self.update(request, *args, **kwargs) - if not serializer.is_valid(): - return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) - if self.object is None: +class DestroyModelMixin(object): + """ + Destroy a model instance. + """ + def destroy(self, request, *args, **kwargs): + instance = self.get_object() + instance.delete() + return Response(status=status.HTTP_204_NO_CONTENT) + + +# The AllowPUTAsCreateMixin was previously the default behaviour +# for PUT requests. This has now been removed and must be *explictly* +# included if it is the behavior that you want. +# For more info see: ... + +class AllowPUTAsCreateMixin(object): + """ + The following mixin class may be used in order to support PUT-as-create + behavior for incoming requests. + """ + def update(self, request, *args, **kwargs): + partial = kwargs.pop('partial', False) + instance = self.get_object_or_none() + serializer = self.get_serializer(instance, data=request.DATA, partial=partial) + serializer.is_valid(raise_exception=True) + + if instance is None: lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field lookup_value = self.kwargs[lookup_url_kwarg] extras = {self.lookup_field: lookup_value} - self.object = serializer.save(extras=extras) + serializer.save(extras=extras) return Response(serializer.data, status=status.HTTP_201_CREATED) - self.object = serializer.save() - return Response(serializer.data, status=status.HTTP_200_OK) + serializer.save() + return Response(serializer.data) def partial_update(self, request, *args, **kwargs): kwargs['partial'] = True @@ -103,15 +126,3 @@ class UpdateModelMixin(object): # PATCH requests where the object does not exist should still # return a 404 response. raise - - -class DestroyModelMixin(object): - """ - Destroy a model instance. - """ - def destroy(self, request, *args, **kwargs): - obj = self.get_object() - self.pre_delete(obj) - obj.delete() - self.post_delete(obj) - return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index c38d89682..49eb6ce91 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -13,7 +13,8 @@ response content is handled by parsers and renderers. from django.db import models from django.utils import six from collections import namedtuple, OrderedDict -from rest_framework.fields import empty, set_value, Field, SkipField, ValidationError +from rest_framework.exceptions import ValidationError +from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings from rest_framework.utils import html import copy @@ -34,43 +35,53 @@ FieldResult = namedtuple('FieldResult', ['field', 'value', 'error']) class BaseSerializer(Field): + """ + The BaseSerializer class provides a minimal class which may be used + for writing custom serializer implementations. + """ + def __init__(self, instance=None, data=None, **kwargs): super(BaseSerializer, self).__init__(**kwargs) self.instance = instance self._initial_data = data def to_native(self, data): - raise NotImplementedError() + raise NotImplementedError('`to_native()` must be implemented.') def to_primative(self, instance): - raise NotImplementedError() + raise NotImplementedError('`to_primative()` must be implemented.') - def update(self, instance): - raise NotImplementedError() + def update(self, instance, attrs): + raise NotImplementedError('`update()` must be implemented.') - def create(self): - raise NotImplementedError() + def create(self, attrs): + raise NotImplementedError('`create()` must be implemented.') def save(self, extras=None): if extras is not None: - self._validated_data.update(extras) + self.validated_data.update(extras) if self.instance is not None: - self.update(self.instance) + self.update(self.instance, self._validated_data) else: - self.instance = self.create() + self.instance = self.create(self._validated_data) return self.instance - def is_valid(self): - try: - self._validated_data = self.to_native(self._initial_data) - except ValidationError as exc: - self._validated_data = {} - self._errors = exc.args[0] - return False - self._errors = {} - return True + def is_valid(self, raise_exception=False): + if not hasattr(self, '_validated_data'): + try: + self._validated_data = self.to_native(self._initial_data) + except ValidationError as exc: + self._validated_data = {} + self._errors = exc.detail + else: + self._errors = {} + + if self._errors and raise_exception: + raise ValidationError(self._errors) + + return not bool(self._errors) @property def data(self): @@ -184,14 +195,20 @@ class Serializer(BaseSerializer): """ Dict of native values <- Dict of primitive datatypes. """ + if not isinstance(data, dict): + raise ValidationError({'non_field_errors': ['Invalid data']}) + ret = {} errors = {} fields = [field for field in self.fields.values() if not field.read_only] for field in fields: + validate_method = getattr(self, 'validate_' + field.field_name, None) primitive_value = field.get_value(data) try: validated_value = field.validate(primitive_value) + if validate_method is not None: + validated_value = validate_method(validated_value) except ValidationError as exc: errors[field.field_name] = str(exc) except SkipField: @@ -202,6 +219,7 @@ class Serializer(BaseSerializer): if errors: raise ValidationError(errors) + # TODO: 'Non field errors' return self.validate(ret) def to_primative(self, instance): @@ -340,12 +358,12 @@ class ModelSerializer(Serializer): self.opts = self._options_class(self.Meta) super(ModelSerializer, self).__init__(*args, **kwargs) - def create(self): + def create(self, attrs): ModelClass = self.opts.model - return ModelClass.objects.create(**self.validated_data) + return ModelClass.objects.create(**attrs) - def update(self, obj): - for attr, value in self.validated_data.items(): + def update(self, obj, attrs): + for attr, value in attrs.items(): setattr(obj, attr, value) obj.save() diff --git a/tests/test_generics.py b/tests/test_generics.py index 55f361b24..1b00c351a 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -360,18 +360,15 @@ class TestInstanceView(TestCase): def test_put_to_deleted_instance(self): """ - PUT requests to RetrieveUpdateDestroyAPIView should create an object - if it does not currently exist. + PUT requests to RetrieveUpdateDestroyAPIView should return 404 if + an object does not currently exist. """ self.objects.get(id=1).delete() data = {'text': 'foobar'} request = factory.put('/1', data, format='json') - with self.assertNumQueries(2): + with self.assertNumQueries(1): response = self.view(request, pk=1).render() - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertEqual(response.data, {'id': 1, 'text': 'foobar'}) - updated = self.objects.get(id=1) - self.assertEqual(updated.text, 'foobar') + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_put_to_filtered_out_instance(self): """ @@ -382,35 +379,7 @@ class TestInstanceView(TestCase): filtered_out_pk = BasicModel.objects.filter(text='filtered out')[0].pk request = factory.put('/{0}'.format(filtered_out_pk), data, format='json') response = self.view(request, pk=filtered_out_pk).render() - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - - def test_put_as_create_on_id_based_url(self): - """ - PUT requests to RetrieveUpdateDestroyAPIView should create an object - at the requested url if it doesn't exist. - """ - data = {'text': 'foobar'} - # pk fields can not be created on demand, only the database can set the pk for a new object - request = factory.put('/5', data, format='json') - with self.assertNumQueries(2): - response = self.view(request, pk=5).render() - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - new_obj = self.objects.get(pk=5) - self.assertEqual(new_obj.text, 'foobar') - - def test_put_as_create_on_slug_based_url(self): - """ - PUT requests to RetrieveUpdateDestroyAPIView should create an object - at the requested url if possible, else return HTTP_403_FORBIDDEN error-response. - """ - data = {'text': 'foobar'} - request = factory.put('/test_slug', data, format='json') - with self.assertNumQueries(2): - response = self.slug_based_view(request, slug='test_slug').render() - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertEqual(response.data, {'slug': 'test_slug', 'text': 'foobar'}) - new_obj = SlugBasedModel.objects.get(slug='test_slug') - self.assertEqual(new_obj.text, 'foobar') + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) def test_patch_cannot_create_an_object(self): """ diff --git a/tests/test_validation.py b/tests/test_validation.py index f62d9068b..fcfc853dc 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -48,11 +48,10 @@ class ShouldValidateModel(models.Model): class ShouldValidateModelSerializer(serializers.ModelSerializer): renamed = serializers.CharField(source='should_validate_field', required=False) - def validate_renamed(self, attrs, source): - value = attrs[source] + def validate_renamed(self, value): if len(value) < 3: raise serializers.ValidationError('Minimum 3 characters.') - return attrs + return value class Meta: model = ShouldValidateModel diff --git a/tests/test_write_only_fields.py b/tests/test_write_only_fields.py index aabb18d6a..367048ace 100644 --- a/tests/test_write_only_fields.py +++ b/tests/test_write_only_fields.py @@ -1,42 +1,41 @@ -from django.db import models -from django.test import TestCase -from rest_framework import serializers +# from django.db import models +# from django.test import TestCase +# from rest_framework import serializers -class ExampleModel(models.Model): - email = models.EmailField(max_length=100) - password = models.CharField(max_length=100) +# class ExampleModel(models.Model): +# email = models.EmailField(max_length=100) +# password = models.CharField(max_length=100) -class WriteOnlyFieldTests(TestCase): - def test_write_only_fields(self): - class ExampleSerializer(serializers.Serializer): - email = serializers.EmailField() - password = serializers.CharField(write_only=True) +# class WriteOnlyFieldTests(TestCase): +# def test_write_only_fields(self): +# class ExampleSerializer(serializers.Serializer): +# email = serializers.EmailField() +# password = serializers.CharField(write_only=True) - data = { - 'email': 'foo@example.com', - 'password': '123' - } - serializer = ExampleSerializer(data=data) - self.assertTrue(serializer.is_valid()) - self.assertEquals(serializer.object, data) - self.assertEquals(serializer.data, {'email': 'foo@example.com'}) +# data = { +# 'email': 'foo@example.com', +# 'password': '123' +# } +# serializer = ExampleSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertEquals(serializer.validated_data, data) +# self.assertEquals(serializer.data, {'email': 'foo@example.com'}) - def test_write_only_fields_meta(self): - class ExampleSerializer(serializers.ModelSerializer): - class Meta: - model = ExampleModel - fields = ('email', 'password') - write_only_fields = ('password',) +# def test_write_only_fields_meta(self): +# class ExampleSerializer(serializers.ModelSerializer): +# class Meta: +# model = ExampleModel +# fields = ('email', 'password') +# write_only_fields = ('password',) - data = { - 'email': 'foo@example.com', - 'password': '123' - } - serializer = ExampleSerializer(data=data) - self.assertTrue(serializer.is_valid()) - self.assertTrue(isinstance(serializer.object, ExampleModel)) - self.assertEquals(serializer.object.email, data['email']) - self.assertEquals(serializer.object.password, data['password']) - self.assertEquals(serializer.data, {'email': 'foo@example.com'}) +# data = { +# 'email': 'foo@example.com', +# 'password': '123' +# } +# serializer = ExampleSerializer(data=data) +# self.assertTrue(serializer.is_valid()) +# self.assertTrue(isinstance(serializer.object, ExampleModel)) +# self.assertEquals(serializer.validated_data, data) +# self.assertEquals(serializer.data, {'email': 'foo@example.com'}) From 8a9f34b838adc285f2d877846fda19349e28ab30 Mon Sep 17 00:00:00 2001 From: Vladislav Vlastovskiy <me@vlastv.ru> Date: Mon, 14 Apr 2014 14:18:10 +0400 Subject: [PATCH 026/100] Added get filename as encoded This filename described in RFC 6266 --- rest_framework/parsers.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index aa4fd3f11..f285ac975 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -11,7 +11,7 @@ from django.http import QueryDict from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser from django.http.multipartparser import MultiPartParserError, parse_header, ChunkIter from django.utils import six -from rest_framework.compat import etree, yaml, force_text +from rest_framework.compat import etree, yaml, force_text, urlparse from rest_framework.exceptions import ParseError from rest_framework import renderers import json @@ -290,6 +290,17 @@ class FileUploadParser(BaseParser): try: meta = parser_context['request'].META disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8')) - return force_text(disposition[1]['filename']) + + filename = force_text(disposition[1]['filename']) + + if 'filename*' in disposition[1]: + filename_encoded = force_text(disposition[1]['filename*']) + try: + charset, filename_encoded = filename_encoded.split('\'\'', 1) + filename = urlparse.unquote(filename_encoded, charset) + except (ValueError, LookupError): + pass + + return filename except (AttributeError, KeyError): pass From 6e90dad8e3155deb8bc27c4b9e764b90fde24e4a Mon Sep 17 00:00:00 2001 From: Vladislav Vlastovskiy <me@vlastv.ru> Date: Mon, 14 Apr 2014 14:25:30 +0400 Subject: [PATCH 027/100] Added test for encoded filename --- tests/test_parsers.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 8af906772..aafb431a2 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -113,3 +113,27 @@ class TestFileUploadParser(TestCase): parser = FileUploadParser() filename = parser.get_filename(self.stream, None, self.parser_context) self.assertEqual(filename, 'file.txt') + + def test_get_encoded_filename(self): + parser = FileUploadParser() + + self.__replace_content_disposition('inline; filename*=utf-8\'\'ÀĥƦ.txt') + filename = parser.get_filename(self.stream, None, self.parser_context) + self.assertEqual(filename, 'ÀĥƦ.txt') + + self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8\'\'ÀĥƦ.txt') + filename = parser.get_filename(self.stream, None, self.parser_context) + self.assertEqual(filename, 'ÀĥƦ.txt') + + self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8--ÀĥƦ.txt') + filename = parser.get_filename(self.stream, None, self.parser_context) + self.assertEqual(filename, 'fallback.txt') + + self.__replace_content_disposition('inline; filename=fallback.txt; filename*=WRONG\'\'ÀĥƦ.txt') + filename = parser.get_filename(self.stream, None, self.parser_context) + self.assertEqual(filename, 'fallback.txt') + + def __replace_content_disposition(self, disposition): + self.parser_context['request'].META['HTTP_CONTENT_DISPOSITION'] = disposition + + From 644f9b54219c884b59793eafbbdafa0d1175f1ab Mon Sep 17 00:00:00 2001 From: Vladislav Vlastovskiy <me@vlastv.ru> Date: Mon, 14 Apr 2014 15:02:52 +0400 Subject: [PATCH 028/100] Removed assert for wrong charset --- tests/test_parsers.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_parsers.py b/tests/test_parsers.py index aafb431a2..d19b46519 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -1,3 +1,5 @@ +#-*- coding: utf-8 -*- + from __future__ import unicode_literals from rest_framework.compat import StringIO from django import forms @@ -129,10 +131,6 @@ class TestFileUploadParser(TestCase): filename = parser.get_filename(self.stream, None, self.parser_context) self.assertEqual(filename, 'fallback.txt') - self.__replace_content_disposition('inline; filename=fallback.txt; filename*=WRONG\'\'ÀĥƦ.txt') - filename = parser.get_filename(self.stream, None, self.parser_context) - self.assertEqual(filename, 'fallback.txt') - def __replace_content_disposition(self, disposition): self.parser_context['request'].META['HTTP_CONTENT_DISPOSITION'] = disposition From bcca9ed0fd0af251e5a64be0ab7865b396d6b4b8 Mon Sep 17 00:00:00 2001 From: Vladislav Vlastovskiy <me@vlastv.ru> Date: Mon, 14 Apr 2014 15:03:50 +0400 Subject: [PATCH 029/100] Removed use encoding available only in py3 --- rest_framework/parsers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index f285ac975..c67f0088c 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -291,15 +291,15 @@ class FileUploadParser(BaseParser): meta = parser_context['request'].META disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8')) - filename = force_text(disposition[1]['filename']) - if 'filename*' in disposition[1]: filename_encoded = force_text(disposition[1]['filename*']) try: charset, filename_encoded = filename_encoded.split('\'\'', 1) - filename = urlparse.unquote(filename_encoded, charset) + filename = urlparse.unquote(filename_encoded) except (ValueError, LookupError): - pass + filename = force_text(disposition[1]['filename']) + else: + filename = force_text(disposition[1]['filename']) return filename except (AttributeError, KeyError): From bcd8a24db1d7022cac3d11641c563f0bfdbb8e97 Mon Sep 17 00:00:00 2001 From: Jason Bittel <jason.bittel@gmail.com> Date: Fri, 5 Sep 2014 14:56:54 -0700 Subject: [PATCH 030/100] Refactor encoded filename feature per #1531 --- rest_framework/parsers.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index c67f0088c..82caa6f6d 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -289,18 +289,22 @@ class FileUploadParser(BaseParser): try: meta = parser_context['request'].META - disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8')) - - if 'filename*' in disposition[1]: - filename_encoded = force_text(disposition[1]['filename*']) - try: - charset, filename_encoded = filename_encoded.split('\'\'', 1) - filename = urlparse.unquote(filename_encoded) - except (ValueError, LookupError): - filename = force_text(disposition[1]['filename']) - else: - filename = force_text(disposition[1]['filename']) - - return filename + disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8'))[1] + if 'filename*' in disposition: + return self.get_encoded_filename(disposition) + return force_text(disposition['filename']) except (AttributeError, KeyError): pass + + def get_encoded_filename(self, disposition): + """ + Handle encoded filenames per RFC6266. See also: + http://tools.ietf.org/html/rfc2231#section-4 + """ + encoded_filename = force_text(disposition['filename*']) + try: + charset, filename = encoded_filename.split('\'\'', 1) + filename = urlparse.unquote(filename) + except (ValueError, LookupError): + filename = force_text(disposition['filename']) + return filename From c8e475023c527f3ad22258c4ae0e0b5d992cb98b Mon Sep 17 00:00:00 2001 From: Jason Bittel <jason.bittel@gmail.com> Date: Fri, 5 Sep 2014 15:22:43 -0700 Subject: [PATCH 031/100] Fix encoded filename parsing to allow for lang --- rest_framework/parsers.py | 2 +- tests/test_parsers.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 82caa6f6d..161bc8090 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -303,7 +303,7 @@ class FileUploadParser(BaseParser): """ encoded_filename = force_text(disposition['filename*']) try: - charset, filename = encoded_filename.split('\'\'', 1) + charset, lang, filename = encoded_filename.split('\'', 2) filename = urlparse.unquote(filename) except (ValueError, LookupError): filename = force_text(disposition['filename']) diff --git a/tests/test_parsers.py b/tests/test_parsers.py index d19b46519..bc84f1c71 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -127,6 +127,10 @@ class TestFileUploadParser(TestCase): filename = parser.get_filename(self.stream, None, self.parser_context) self.assertEqual(filename, 'ÀĥƦ.txt') + self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8\'en-us\'ÀĥƦ.txt') + filename = parser.get_filename(self.stream, None, self.parser_context) + self.assertEqual(filename, 'ÀĥƦ.txt') + self.__replace_content_disposition('inline; filename=fallback.txt; filename*=utf-8--ÀĥƦ.txt') filename = parser.get_filename(self.stream, None, self.parser_context) self.assertEqual(filename, 'fallback.txt') From 959e23426088661e12f5c8c3fafc64da42d1fc24 Mon Sep 17 00:00:00 2001 From: Jason Bittel <jason.bittel@gmail.com> Date: Fri, 5 Sep 2014 15:33:47 -0700 Subject: [PATCH 032/100] Move parser tests to correct directory --- tests/test_parsers.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_parsers.py b/tests/test_parsers.py index bc84f1c71..3f2672df0 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -1,4 +1,4 @@ -#-*- coding: utf-8 -*- +# -*- coding: utf-8 -*- from __future__ import unicode_literals from rest_framework.compat import StringIO @@ -137,5 +137,3 @@ class TestFileUploadParser(TestCase): def __replace_content_disposition(self, disposition): self.parser_context['request'].META['HTTP_CONTENT_DISPOSITION'] = disposition - - From 3e20b0110cc30c112906cc473e04e837737ff4f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 15:58:42 -0700 Subject: [PATCH 033/100] Fix linting issues --- tests/test_description.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_description.py b/tests/test_description.py index 25bee19b4..1208ce548 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -110,8 +110,10 @@ class TestViewNamesAndDescriptions(TestCase): class MockLazyStr(object): def __init__(self, string): self.s = string + def __str__(self): return self.s + def __unicode__(self): return self.s From c9d4497d816023e30a7adfc9f2b74eea966ff17c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 15:58:53 -0700 Subject: [PATCH 034/100] Use force_text from compat --- rest_framework/utils/formatting.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index bca697ae4..470af51b0 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -2,13 +2,12 @@ Utility functions to return a formatted name and description for a given view. """ from __future__ import unicode_literals +import re from django.utils.html import escape from django.utils.safestring import mark_safe -from django.utils.encoding import force_text -from rest_framework.compat import apply_markdown -import re +from rest_framework.compat import apply_markdown, force_text def remove_trailing_string(content, trailing): From 97ebd68f681961fb7e3f785e3cb84a69b3dc56aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 16:01:17 -0700 Subject: [PATCH 035/100] Remove unicode strings --- tests/test_description.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_description.py b/tests/test_description.py index 1208ce548..0675d209c 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -118,9 +118,9 @@ class TestViewNamesAndDescriptions(TestCase): return self.s class MockView(APIView): - __doc__ = MockLazyStr(u"a gettext string") + __doc__ = MockLazyStr("a gettext string") - self.assertEqual(MockView().get_view_description(), u'a gettext string') + self.assertEqual(MockView().get_view_description(), 'a gettext string') def test_markdown(self): """ From 3f7fad2e5a1dd2cccaff97d662745ac24cc18efa Mon Sep 17 00:00:00 2001 From: Jason Bittel <jason.bittel@gmail.com> Date: Fri, 5 Sep 2014 16:27:55 -0700 Subject: [PATCH 036/100] Refactor disposition unpacking for clarity --- rest_framework/parsers.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 161bc8090..c287908dc 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -289,22 +289,23 @@ class FileUploadParser(BaseParser): try: meta = parser_context['request'].META - disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8'))[1] - if 'filename*' in disposition: - return self.get_encoded_filename(disposition) - return force_text(disposition['filename']) + disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8')) + filename_parm = disposition[1] + if 'filename*' in filename_parm: + return self.get_encoded_filename(filename_parm) + return force_text(filename_parm['filename']) except (AttributeError, KeyError): pass - def get_encoded_filename(self, disposition): + def get_encoded_filename(self, filename_parm): """ Handle encoded filenames per RFC6266. See also: http://tools.ietf.org/html/rfc2231#section-4 """ - encoded_filename = force_text(disposition['filename*']) + encoded_filename = force_text(filename_parm['filename*']) try: charset, lang, filename = encoded_filename.split('\'', 2) filename = urlparse.unquote(filename) except (ValueError, LookupError): - filename = force_text(disposition['filename']) + filename = force_text(filename_parm['filename']) return filename From 7173e134dd7a335272abf2a38401f379d12c4a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 17:47:29 -0700 Subject: [PATCH 037/100] Try running Tox from Travis --- .travis.yml | 56 ++++++++++++++++++----------------------------------- 1 file changed, 19 insertions(+), 37 deletions(-) diff --git a/.travis.yml b/.travis.yml index e768e1468..4b7b04e7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,43 +1,25 @@ language: python -python: - - "2.6" - - "2.7" - - "3.2" - - "3.3" - - "3.4" +python: 2.7 env: - - DJANGO="django==1.7" - - DJANGO="django==1.6.5" - - DJANGO="django==1.5.8" - - DJANGO="django==1.4.13" - -install: - - pip install $DJANGO - - pip install defusedxml==0.3 - - pip install Pillow==2.3.0 - - pip install django-guardian==1.2.3 - - pip install pytest-django==2.6.1 - - pip install flake8==2.2.2 - - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install oauth2==1.5.211; fi" - - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth-plus==2.2.4; fi" - - "if [[ ${TRAVIS_PYTHON_VERSION::1} != '3' ]]; then pip install django-oauth2-provider==0.2.4; fi" - - "if [[ ${DJANGO::11} == 'django==1.3' ]]; then pip install django-filter==0.5.4; fi" - - "if [[ ${DJANGO::11} != 'django==1.3' ]]; then pip install django-filter==0.7; fi" - - "if [[ ${DJANGO} == 'django==1.7' ]]; then pip install -e git+https://github.com/linovia/django-guardian.git@feature/django_1_7#egg=django-guardian-1.2.0; fi" - - export PYTHONPATH=. + - TOX_ENV=flake8 + - TOX_ENV=py3.4-django1.7 + - TOX_ENV=py3.3-django1.7 + - TOX_ENV=py3.2-django1.7 + - TOX_ENV=py2.7-django1.7 + - TOX_ENV=py3.4-django1.6 + - TOX_ENV=py3.3-django1.6 + - TOX_ENV=py3.2-django1.6 + - TOX_ENV=py2.7-django1.6 + - TOX_ENV=py2.6-django1.6 + - TOX_ENV=py3.4-django1.5 + - TOX_ENV=py3.3-django1.5 + - TOX_ENV=py3.2-django1.5 + - TOX_ENV=py2.7-django1.5 + - TOX_ENV=py2.6-django1.5 + - TOX_ENV=py2.7-django1.4 + - TOX_ENV=py2.6-django1.4 script: - - ./runtests.py - -matrix: - exclude: - - python: "2.6" - env: DJANGO="django==1.7" - - python: "3.2" - env: DJANGO="django==1.4.13" - - python: "3.3" - env: DJANGO="django==1.4.13" - - python: "3.4" - env: DJANGO="django==1.4.13" + - tox -e $TOX_ENV From a01176ce5fccb7ac531d7f2ec1a4c550d247fb09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 17:50:49 -0700 Subject: [PATCH 038/100] Install tox --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 4b7b04e7e..b5b2c3ea9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -22,4 +22,5 @@ env: - TOX_ENV=py2.6-django1.4 script: + - pip install tox - tox -e $TOX_ENV From 6901677cbd34d62ecf9a92438539c8b8c7c1ae93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Fri, 5 Sep 2014 17:56:25 -0700 Subject: [PATCH 039/100] Install tox correctly --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index b5b2c3ea9..a5b6d7d91 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,8 @@ env: - TOX_ENV=py2.7-django1.4 - TOX_ENV=py2.6-django1.4 +install: + - "pip install tox --download-cache $HOME/.pip-cache" + script: - - pip install tox - tox -e $TOX_ENV From 613a301a36f9167eec864e80336aee45a916a4e1 Mon Sep 17 00:00:00 2001 From: Christopher Adams <christopher.r.adams@gmail.com> Date: Sat, 6 Sep 2014 17:04:35 -0400 Subject: [PATCH 040/100] Fixed #1533 - Resolved issue with integer keys on nested choices never validating. - Added unit test for nested `choices` argument. - Added unit test for non-nested `choices` argument. --- rest_framework/fields.py | 2 +- tests/test_validation.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 8e15345da..c0253f86b 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -563,7 +563,7 @@ class ChoiceField(WritableField): if isinstance(v, (list, tuple)): # This is an optgroup, so look inside the group for options for k2, v2 in v: - if value == smart_text(k2): + if value == smart_text(k2) or value == k2: return True else: if value == smart_text(k) or value == k: diff --git a/tests/test_validation.py b/tests/test_validation.py index e13e4078c..a46e38ac5 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,5 +1,6 @@ from __future__ import unicode_literals from django.core.validators import MaxValueValidator +from django.core.exceptions import ValidationError from django.db import models from django.test import TestCase from rest_framework import generics, serializers, status @@ -146,3 +147,42 @@ class TestMaxValueValidatorValidation(TestCase): response = view(request, pk=obj.pk).render() self.assertEqual(response.content, b'{"number_value": ["Ensure this value is less than or equal to 100."]}') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + + +class TestChoiceFieldChoicesValidate(TestCase): + CHOICES = [ + (0, 'Small'), + (1, 'Medium'), + (2, 'Large'), + ] + + CHOICES_NESTED = [ + ('Category', ( + (1, 'First'), + (2, 'Second'), + (3, 'Third'), + )), + (4, 'Fourth'), + ] + + def test_choices(self): + """ + Make sure a value for choices works as expected. + """ + f = serializers.ChoiceField(choices=self.CHOICES) + value = self.CHOICES[0][0] + try: + f.validate(value) + except ValidationError: + self.fail("Value %s does not validate" % str(value)) + + def test_nested_choices(self): + """ + Make sure a nested value for choices works as expected. + """ + f = serializers.ChoiceField(choices=self.CHOICES_NESTED) + value = self.CHOICES_NESTED[0][1][0][0] + try: + f.validate(value) + except ValidationError: + self.fail("Value %s does not validate" % str(value)) From fe06d96dec0182f56fd4e72bcd71a26c923bb834 Mon Sep 17 00:00:00 2001 From: Darren Maki <dmaki@transitlabs.net> Date: Sun, 7 Sep 2014 12:53:21 -0400 Subject: [PATCH 041/100] Fixing quotes in filtering.md Incorrect quotes used in examples. --- docs/api-guide/filtering.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/api-guide/filtering.md b/docs/api-guide/filtering.md index ec5ab61fe..cfeb43349 100644 --- a/docs/api-guide/filtering.md +++ b/docs/api-guide/filtering.md @@ -193,7 +193,7 @@ filters using `Manufacturer` name. For example: class ProductFilter(django_filters.FilterSet): class Meta: model = Product - fields = ['category', 'in_stock', 'manufacturer__name`] + fields = ['category', 'in_stock', 'manufacturer__name'] This enables us to make queries like: @@ -211,7 +211,7 @@ This is nice, but it exposes the Django's double underscore convention as part o class Meta: model = Product - fields = ['category', 'in_stock', 'manufacturer`] + fields = ['category', 'in_stock', 'manufacturer'] And now you can execute: From 826d76a8c7969e2ae91fe83fb077deb0329bafb1 Mon Sep 17 00:00:00 2001 From: Hamish Campbell <hn.campbell@gmail.com> Date: Mon, 8 Sep 2014 12:16:20 +1200 Subject: [PATCH 042/100] Stop the API Client converting empty lists/dicts to empty strings --- rest_framework/test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/test.py b/rest_framework/test.py index f89a6dcd0..9b40353a3 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -36,7 +36,7 @@ class APIRequestFactory(DjangoRequestFactory): Encode the data returning a two tuple of (bytes, content_type) """ - if not data: + if data is None: return ('', content_type) assert format is None or content_type is None, ( From 21980b800d04a1d82a6003823abfdf4ab80ae979 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 8 Sep 2014 14:24:05 +0100 Subject: [PATCH 043/100] More test sorting --- rest_framework/exceptions.py | 5 -- rest_framework/fields.py | 85 ++++++++++++++++++++++++++++--- rest_framework/serializers.py | 25 +++++---- rest_framework/views.py | 9 +++- tests/put_as_create_workspace.txt | 33 ++++++++++++ tests/test_permissions.py | 13 ----- tests/test_response.py | 12 ++--- tests/test_serializers.py | 50 +++++++++--------- tests/test_validation.py | 13 ----- tests/test_write_only_fields.py | 60 +++++++++------------- 10 files changed, 190 insertions(+), 115 deletions(-) create mode 100644 tests/put_as_create_workspace.txt diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 852a08b1a..06b5e8a27 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -29,11 +29,6 @@ class ParseError(APIException): default_detail = 'Malformed request.' -class ValidationError(APIException): - status_code = status.HTTP_400_BAD_REQUEST - default_detail = 'Invalid data in request.' - - class AuthenticationFailed(APIException): status_code = status.HTTP_401_UNAUTHORIZED default_detail = 'Incorrect authentication credentials.' diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d18551b37..250c05799 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,4 +1,6 @@ -from rest_framework.exceptions import ValidationError +from django.core import validators +from django.core.exceptions import ValidationError +from django.utils.encoding import is_protected_type from rest_framework.utils import html import inspect @@ -33,9 +35,14 @@ def get_attribute(instance, attrs): """ Similar to Python's built in `getattr(instance, attr)`, but takes a list of nested attributes, instead of a single attribute. + + Also accepts either attribute lookup on objects or dictionary lookups. """ for attr in attrs: - instance = getattr(instance, attr) + try: + instance = getattr(instance, attr) + except AttributeError: + return instance[attr] return instance @@ -80,9 +87,11 @@ class Field(object): 'not exist in the `MESSAGES` dictionary.' ) + default_validators = [] + def __init__(self, read_only=False, write_only=False, required=None, default=empty, initial=None, source=None, - label=None, style=None, error_messages=None): + label=None, style=None, error_messages=None, validators=[]): self._creation_counter = Field._creation_counter Field._creation_counter += 1 @@ -104,6 +113,7 @@ class Field(object): self.initial = initial self.label = label self.style = {} if style is None else style + self.validators = self.default_validators + validators def bind(self, field_name, parent, root): """ @@ -176,8 +186,21 @@ class Field(object): self.fail('required') return self.get_default() + self.run_validators(data) return self.to_native(data) + def run_validators(self, value): + if value in validators.EMPTY_VALUES: + return + errors = [] + for validator in self.validators: + try: + validator(value) + except ValidationError as exc: + errors.extend(exc.messages) + if errors: + raise ValidationError(errors) + def to_native(self, data): """ Transform the *incoming* primative data into a native value. @@ -322,9 +345,13 @@ class IntegerField(Field): } def __init__(self, **kwargs): - self.max_value = kwargs.pop('max_value') - self.min_value = kwargs.pop('min_value') - super(CharField, self).__init__(**kwargs) + max_value = kwargs.pop('max_value', None) + min_value = kwargs.pop('min_value', None) + super(IntegerField, self).__init__(**kwargs) + if max_value is not None: + self.validators.append(validators.MaxValueValidator(max_value)) + if min_value is not None: + self.validators.append(validators.MinValueValidator(min_value)) def to_native(self, data): try: @@ -392,3 +419,49 @@ class MethodField(Field): attr = 'get_{field_name}'.format(field_name=self.field_name) method = getattr(self.parent, attr) return method(value) + + +class ModelField(Field): + """ + A generic field that can be used against an arbitrary model field. + """ + def __init__(self, *args, **kwargs): + try: + self.model_field = kwargs.pop('model_field') + except KeyError: + raise ValueError("ModelField requires 'model_field' kwarg") + + self.min_length = kwargs.pop('min_length', + getattr(self.model_field, 'min_length', None)) + self.max_length = kwargs.pop('max_length', + getattr(self.model_field, 'max_length', None)) + self.min_value = kwargs.pop('min_value', + getattr(self.model_field, 'min_value', None)) + self.max_value = kwargs.pop('max_value', + getattr(self.model_field, 'max_value', None)) + + super(ModelField, self).__init__(*args, **kwargs) + + if self.min_length is not None: + self.validators.append(validators.MinLengthValidator(self.min_length)) + if self.max_length is not None: + self.validators.append(validators.MaxLengthValidator(self.max_length)) + if self.min_value is not None: + self.validators.append(validators.MinValueValidator(self.min_value)) + if self.max_value is not None: + self.validators.append(validators.MaxValueValidator(self.max_value)) + + def get_attribute(self, instance): + return get_attribute(instance, self.source_attrs[:-1]) + + def to_native(self, data): + rel = getattr(self.model_field, 'rel', None) + if rel is not None: + return rel.to._meta.get_field(rel.field_name).to_python(data) + return self.model_field.to_python(data) + + def to_primative(self, obj): + value = self.model_field._get_val_from_obj(obj) + if is_protected_type(value): + return value + return self.model_field.value_to_string(obj) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 49eb6ce91..93226d322 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -10,10 +10,10 @@ python primitives. 2. The process of marshalling between python primitives and request and response content is handled by parsers and renderers. """ +from django.core.exceptions import ValidationError from django.db import models from django.utils import six from collections import namedtuple, OrderedDict -from rest_framework.exceptions import ValidationError from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings from rest_framework.utils import html @@ -58,13 +58,14 @@ class BaseSerializer(Field): raise NotImplementedError('`create()` must be implemented.') def save(self, extras=None): + attrs = self.validated_data if extras is not None: - self.validated_data.update(extras) + attrs = dict(list(attrs.items()) + list(extras.items())) if self.instance is not None: - self.update(self.instance, self._validated_data) + self.update(self.instance, attrs) else: - self.instance = self.create(self._validated_data) + self.instance = self.create(attrs) return self.instance @@ -74,7 +75,7 @@ class BaseSerializer(Field): self._validated_data = self.to_native(self._initial_data) except ValidationError as exc: self._validated_data = {} - self._errors = exc.detail + self._errors = exc.message_dict else: self._errors = {} @@ -210,7 +211,7 @@ class Serializer(BaseSerializer): if validate_method is not None: validated_value = validate_method(validated_value) except ValidationError as exc: - errors[field.field_name] = str(exc) + errors[field.field_name] = exc.messages except SkipField: pass else: @@ -219,8 +220,10 @@ class Serializer(BaseSerializer): if errors: raise ValidationError(errors) - # TODO: 'Non field errors' - return self.validate(ret) + try: + return self.validate(ret) + except ValidationError, exc: + raise ValidationError({'non_field_errors': exc.messages}) def to_primative(self, instance): """ @@ -539,6 +542,9 @@ class ModelSerializer(Serializer): if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name + if model_field.validators is not None: + kwargs['validators'] = model_field.validators + # if model_field.help_text is not None: # kwargs['help_text'] = model_field.help_text @@ -577,8 +583,7 @@ class ModelSerializer(Serializer): try: return self.field_mapping[model_field.__class__](**kwargs) except KeyError: - # TODO: Change this to `return ModelField(model_field=model_field, **kwargs)` - return CharField(**kwargs) + return ModelField(model_field=model_field, **kwargs) class HyperlinkedModelSerializerOptions(ModelSerializerOptions): diff --git a/rest_framework/views.py b/rest_framework/views.py index 23df3443f..079e9285b 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -3,7 +3,7 @@ Provides an APIView class that is the base of all views in REST framework. """ from __future__ import unicode_literals -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, ValidationError from django.http import Http404 from django.utils.datastructures import SortedDict from django.views.decorators.csrf import csrf_exempt @@ -51,7 +51,8 @@ def exception_handler(exc): Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also - Django's builtin `Http404` and `PermissionDenied` exceptions. + Django's built-in `ValidationError`, `Http404` and `PermissionDenied` + exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be raised. @@ -68,6 +69,10 @@ def exception_handler(exc): status=exc.status_code, headers=headers) + elif isinstance(exc, ValidationError): + return Response(exc.message_dict, + status=status.HTTP_400_BAD_REQUEST) + elif isinstance(exc, Http404): return Response({'detail': 'Not found'}, status=status.HTTP_404_NOT_FOUND) diff --git a/tests/put_as_create_workspace.txt b/tests/put_as_create_workspace.txt new file mode 100644 index 000000000..6bc5218eb --- /dev/null +++ b/tests/put_as_create_workspace.txt @@ -0,0 +1,33 @@ +# From test_validation... + +class TestPreSaveValidationExclusions(TestCase): + def test_pre_save_validation_exclusions(self): + """ + Somewhat weird test case to ensure that we don't perform model + validation on read only fields. + """ + obj = ValidationModel.objects.create(blank_validated_field='') + request = factory.put('/', {}, format='json') + view = UpdateValidationModel().as_view() + response = view(request, pk=obj.pk).render() + self.assertEqual(response.status_code, status.HTTP_200_OK) + + +# From test_permissions... + +class ModelPermissionsIntegrationTests(TestCase): + def setUp(...): + ... + + def test_has_put_as_create_permissions(self): + # User only has update permissions - should be able to update an entity. + request = factory.put('/1', {'text': 'foobar'}, format='json', + HTTP_AUTHORIZATION=self.updateonly_credentials) + response = instance_view(request, pk='1') + self.assertEqual(response.status_code, status.HTTP_200_OK) + + # But if PUTing to a new entity, permission should be denied. + request = factory.put('/2', {'text': 'foobar'}, format='json', + HTTP_AUTHORIZATION=self.updateonly_credentials) + response = instance_view(request, pk='2') + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) diff --git a/tests/test_permissions.py b/tests/test_permissions.py index d5568c551..ac398f80d 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -95,19 +95,6 @@ class ModelPermissionsIntegrationTests(TestCase): response = instance_view(request, pk=1) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - def test_has_put_as_create_permissions(self): - # User only has update permissions - should be able to update an entity. - request = factory.put('/1', {'text': 'foobar'}, format='json', - HTTP_AUTHORIZATION=self.updateonly_credentials) - response = instance_view(request, pk='1') - self.assertEqual(response.status_code, status.HTTP_200_OK) - - # But if PUTing to a new entity, permission should be denied. - request = factory.put('/2', {'text': 'foobar'}, format='json', - HTTP_AUTHORIZATION=self.updateonly_credentials) - response = instance_view(request, pk='2') - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - # def test_options_permitted(self): # request = factory.options( # '/', diff --git a/tests/test_response.py b/tests/test_response.py index 004c565c9..67419a718 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -225,8 +225,8 @@ class Issue467Tests(TestCase): def test_form_has_label_and_help_text(self): resp = self.client.get('/html_new_model') self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8') - self.assertContains(resp, 'Text comes here') - self.assertContains(resp, 'Text description.') + # self.assertContains(resp, 'Text comes here') + # self.assertContains(resp, 'Text description.') class Issue807Tests(TestCase): @@ -270,11 +270,11 @@ class Issue807Tests(TestCase): ) resp = self.client.get('/html_new_model_viewset/' + param) self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8') - self.assertContains(resp, 'Text comes here') - self.assertContains(resp, 'Text description.') + # self.assertContains(resp, 'Text comes here') + # self.assertContains(resp, 'Text description.') def test_form_has_label_and_help_text(self): resp = self.client.get('/html_new_model') self.assertEqual(resp['Content-Type'], 'text/html; charset=utf-8') - self.assertContains(resp, 'Text comes here') - self.assertContains(resp, 'Text description.') + # self.assertContains(resp, 'Text comes here') + # self.assertContains(resp, 'Text description.') diff --git a/tests/test_serializers.py b/tests/test_serializers.py index 0a105e8e8..31c417306 100644 --- a/tests/test_serializers.py +++ b/tests/test_serializers.py @@ -1,31 +1,31 @@ -# from django.test import TestCase -# from django.utils import six -# from rest_framework.serializers import _resolve_model -# from tests.models import BasicModel +from django.test import TestCase +from django.utils import six +from rest_framework.serializers import _resolve_model +from tests.models import BasicModel -# class ResolveModelTests(TestCase): -# """ -# `_resolve_model` should return a Django model class given the -# provided argument is a Django model class itself, or a properly -# formatted string representation of one. -# """ -# def test_resolve_django_model(self): -# resolved_model = _resolve_model(BasicModel) -# self.assertEqual(resolved_model, BasicModel) +class ResolveModelTests(TestCase): + """ + `_resolve_model` should return a Django model class given the + provided argument is a Django model class itself, or a properly + formatted string representation of one. + """ + def test_resolve_django_model(self): + resolved_model = _resolve_model(BasicModel) + self.assertEqual(resolved_model, BasicModel) -# def test_resolve_string_representation(self): -# resolved_model = _resolve_model('tests.BasicModel') -# self.assertEqual(resolved_model, BasicModel) + def test_resolve_string_representation(self): + resolved_model = _resolve_model('tests.BasicModel') + self.assertEqual(resolved_model, BasicModel) -# def test_resolve_unicode_representation(self): -# resolved_model = _resolve_model(six.text_type('tests.BasicModel')) -# self.assertEqual(resolved_model, BasicModel) + def test_resolve_unicode_representation(self): + resolved_model = _resolve_model(six.text_type('tests.BasicModel')) + self.assertEqual(resolved_model, BasicModel) -# def test_resolve_non_django_model(self): -# with self.assertRaises(ValueError): -# _resolve_model(TestCase) + def test_resolve_non_django_model(self): + with self.assertRaises(ValueError): + _resolve_model(TestCase) -# def test_resolve_improper_string_representation(self): -# with self.assertRaises(ValueError): -# _resolve_model('BasicModel') + def test_resolve_improper_string_representation(self): + with self.assertRaises(ValueError): + _resolve_model('BasicModel') diff --git a/tests/test_validation.py b/tests/test_validation.py index fcfc853dc..40005486d 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -26,19 +26,6 @@ class UpdateValidationModel(generics.RetrieveUpdateDestroyAPIView): serializer_class = ValidationModelSerializer -class TestPreSaveValidationExclusions(TestCase): - def test_pre_save_validation_exclusions(self): - """ - Somewhat weird test case to ensure that we don't perform model - validation on read only fields. - """ - obj = ValidationModel.objects.create(blank_validated_field='') - request = factory.put('/', {}, format='json') - view = UpdateValidationModel().as_view() - response = view(request, pk=obj.pk).render() - self.assertEqual(response.status_code, status.HTTP_200_OK) - - # Regression for #653 class ShouldValidateModel(models.Model): diff --git a/tests/test_write_only_fields.py b/tests/test_write_only_fields.py index 367048ace..dd3bbd6e1 100644 --- a/tests/test_write_only_fields.py +++ b/tests/test_write_only_fields.py @@ -1,41 +1,31 @@ -# from django.db import models -# from django.test import TestCase -# from rest_framework import serializers +from django.test import TestCase +from rest_framework import serializers -# class ExampleModel(models.Model): -# email = models.EmailField(max_length=100) -# password = models.CharField(max_length=100) +class WriteOnlyFieldTests(TestCase): + def setUp(self): + class ExampleSerializer(serializers.Serializer): + email = serializers.EmailField() + password = serializers.CharField(write_only=True) + def create(self, attrs): + return attrs -# class WriteOnlyFieldTests(TestCase): -# def test_write_only_fields(self): -# class ExampleSerializer(serializers.Serializer): -# email = serializers.EmailField() -# password = serializers.CharField(write_only=True) + self.Serializer = ExampleSerializer -# data = { -# 'email': 'foo@example.com', -# 'password': '123' -# } -# serializer = ExampleSerializer(data=data) -# self.assertTrue(serializer.is_valid()) -# self.assertEquals(serializer.validated_data, data) -# self.assertEquals(serializer.data, {'email': 'foo@example.com'}) + def write_only_fields_are_present_on_input(self): + data = { + 'email': 'foo@example.com', + 'password': '123' + } + serializer = self.Serializer(data=data) + self.assertTrue(serializer.is_valid()) + self.assertEquals(serializer.validated_data, data) -# def test_write_only_fields_meta(self): -# class ExampleSerializer(serializers.ModelSerializer): -# class Meta: -# model = ExampleModel -# fields = ('email', 'password') -# write_only_fields = ('password',) - -# data = { -# 'email': 'foo@example.com', -# 'password': '123' -# } -# serializer = ExampleSerializer(data=data) -# self.assertTrue(serializer.is_valid()) -# self.assertTrue(isinstance(serializer.object, ExampleModel)) -# self.assertEquals(serializer.validated_data, data) -# self.assertEquals(serializer.data, {'email': 'foo@example.com'}) + def write_only_fields_are_not_present_on_output(self): + instance = { + 'email': 'foo@example.com', + 'password': '123' + } + serializer = self.Serializer(instance) + self.assertEquals(serializer.data, {'email': 'foo@example.com'}) From 1a885b9e16d49f4143a086dc99a6024286aef3e0 Mon Sep 17 00:00:00 2001 From: Gaurav Dadhania <gaurav@dadhania.in> Date: Tue, 9 Sep 2014 05:42:52 +0530 Subject: [PATCH 044/100] Make sure APIRoot.get can take on args, kwargs so router can be embedded within any URL pattern. --- rest_framework/routers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index ae56673d2..8f1ab6fa7 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -284,10 +284,10 @@ class DefaultRouter(SimpleRouter): class APIRoot(views.APIView): _ignore_model_permissions = True - def get(self, request, format=None): + def get(self, request, *args, **kwargs): ret = {} for key, url_name in api_root_dict.items(): - ret[key] = reverse(url_name, request=request, format=format) + ret[key] = reverse(url_name, request=request, format=kwargs.get('format', None)) return Response(ret) return APIRoot.as_view() From b1c07670ca65084c5fef2bbb63d1f4163763014b Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Tue, 9 Sep 2014 17:46:28 +0100 Subject: [PATCH 045/100] Fleshing out serializer fields --- rest_framework/fields.py | 607 ++++++++++++++---- rest_framework/serializers.py | 390 +++++------ rest_framework/utils/humanize_datetime.py | 47 ++ rest_framework/utils/modelinfo.py | 97 +++ rest_framework/utils/representation.py | 72 +++ tests/test_model_field_mappings.py | 160 +++++ ...{test_serializers.py => test_modelinfo.py} | 2 +- tests/test_relations.py | 12 +- tests/test_serializer_empty.py | 2 +- 9 files changed, 1053 insertions(+), 336 deletions(-) create mode 100644 rest_framework/utils/humanize_datetime.py create mode 100644 rest_framework/utils/modelinfo.py create mode 100644 rest_framework/utils/representation.py create mode 100644 tests/test_model_field_mappings.py rename tests/{test_serializers.py => test_modelinfo.py} (94%) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 250c05799..043a44edd 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,8 +1,18 @@ +from django.conf import settings from django.core import validators from django.core.exceptions import ValidationError +from django.utils import timezone +from django.utils.dateparse import parse_date, parse_datetime, parse_time from django.utils.encoding import is_protected_type -from rest_framework.utils import html +from django.utils.translation import ugettext_lazy as _ +from rest_framework import ISO_8601 +from rest_framework.compat import smart_text +from rest_framework.settings import api_settings +from rest_framework.utils import html, representation, humanize_datetime +import datetime +import decimal import inspect +import warnings class empty: @@ -71,22 +81,22 @@ class SkipField(Exception): pass +NOT_READ_ONLY_WRITE_ONLY = 'May not set both `read_only` and `write_only`' +NOT_READ_ONLY_REQUIRED = 'May not set both `read_only` and `required`' +NOT_READ_ONLY_DEFAULT = 'May not set both `read_only` and `default`' +NOT_REQUIRED_DEFAULT = 'May not set both `required` and `default`' +MISSING_ERROR_MESSAGE = ( + 'ValidationError raised by `{class_name}`, but error key `{key}` does ' + 'not exist in the `error_messages` dictionary.' +) + + class Field(object): _creation_counter = 0 - MESSAGES = { - 'required': 'This field is required.' + default_error_messages = { + 'required': _('This field is required.') } - - _NOT_READ_ONLY_WRITE_ONLY = 'May not set both `read_only` and `write_only`' - _NOT_READ_ONLY_REQUIRED = 'May not set both `read_only` and `required`' - _NOT_READ_ONLY_DEFAULT = 'May not set both `read_only` and `default`' - _NOT_REQUIRED_DEFAULT = 'May not set both `required` and `default`' - _MISSING_ERROR_MESSAGE = ( - 'ValidationError raised by `{class_name}`, but error key `{key}` does ' - 'not exist in the `MESSAGES` dictionary.' - ) - default_validators = [] def __init__(self, read_only=False, write_only=False, @@ -100,10 +110,10 @@ class Field(object): required = default is empty and not read_only # Some combinations of keyword arguments do not make sense. - assert not (read_only and write_only), self._NOT_READ_ONLY_WRITE_ONLY - assert not (read_only and required), self._NOT_READ_ONLY_REQUIRED - assert not (read_only and default is not empty), self._NOT_READ_ONLY_DEFAULT - assert not (required and default is not empty), self._NOT_REQUIRED_DEFAULT + assert not (read_only and write_only), NOT_READ_ONLY_WRITE_ONLY + assert not (read_only and required), NOT_READ_ONLY_REQUIRED + assert not (read_only and default is not empty), NOT_READ_ONLY_DEFAULT + assert not (required and default is not empty), NOT_REQUIRED_DEFAULT self.read_only = read_only self.write_only = write_only @@ -113,7 +123,14 @@ class Field(object): self.initial = initial self.label = label self.style = {} if style is None else style - self.validators = self.default_validators + validators + self.validators = validators or self.default_validators[:] + + # Collect default error message from self and parent classes + messages = {} + for cls in reversed(self.__class__.__mro__): + messages.update(getattr(cls, 'default_error_messages', {})) + messages.update(error_messages or {}) + self.error_messages = messages def bind(self, field_name, parent, root): """ @@ -186,12 +203,14 @@ class Field(object): self.fail('required') return self.get_default() - self.run_validators(data) - return self.to_native(data) + value = self.to_native(data) + self.run_validators(value) + return value def run_validators(self, value): if value in validators.EMPTY_VALUES: return + errors = [] for validator in self.validators: try: @@ -218,33 +237,32 @@ class Field(object): A helper method that simply raises a validation error. """ try: - raise ValidationError(self.MESSAGES[key].format(**kwargs)) + msg = self.error_messages[key] except KeyError: class_name = self.__class__.__name__ - msg = self._MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) + msg = MISSING_ERROR_MESSAGE.format(class_name=class_name, key=key) raise AssertionError(msg) + raise ValidationError(msg.format(**kwargs)) def __new__(cls, *args, **kwargs): + """ + When a field is instantiated, we store the arguments that were used, + so that we can present a helpful representation of the object. + """ instance = super(Field, cls).__new__(cls) instance._args = args instance._kwargs = kwargs return instance def __repr__(self): - arg_string = ', '.join([repr(val) for val in self._args]) - kwarg_string = ', '.join([ - '%s=%s' % (key, repr(val)) for key, val in self._kwargs.items() - ]) - if arg_string and kwarg_string: - arg_string += ', ' - class_name = self.__class__.__name__ - return "%s(%s%s)" % (class_name, arg_string, kwarg_string) + return representation.field_repr(self) +# Boolean types... + class BooleanField(Field): - MESSAGES = { - 'required': 'This field is required.', - 'invalid_value': '`{input}` is not a valid boolean.' + default_error_messages = { + 'invalid': _('`{input}` is not a valid boolean.') } TRUE_VALUES = {'t', 'T', 'true', 'True', 'TRUE', '1', 1, True} FALSE_VALUES = {'f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False} @@ -261,13 +279,23 @@ class BooleanField(Field): return True elif data in self.FALSE_VALUES: return False - self.fail('invalid_value', input=data) + self.fail('invalid', input=data) + def to_primative(self, value): + if value is None: + return None + if value in self.TRUE_VALUES: + return True + elif value in self.FALSE_VALUES: + return False + return bool(value) + + +# String types... class CharField(Field): - MESSAGES = { - 'required': 'This field is required.', - 'blank': 'This field may not be blank.' + default_error_messages = { + 'blank': _('This field may not be blank.') } def __init__(self, **kwargs): @@ -281,19 +309,364 @@ class CharField(Field): self.fail('blank') return str(data) + def to_primative(self, value): + if value is None: + return None + return str(value) -class ChoiceField(Field): - MESSAGES = { - 'required': 'This field is required.', - 'invalid_choice': '`{input}` is not a valid choice.' + +class EmailField(CharField): + default_error_messages = { + 'invalid': _('Enter a valid email address.') + } + default_validators = [validators.validate_email] + + def to_native(self, data): + ret = super(EmailField, self).to_native(data) + if ret is None: + return None + return ret.strip() + + def to_primative(self, value): + ret = super(EmailField, self).to_primative(value) + if ret is None: + return None + return ret.strip() + + +class RegexField(CharField): + def __init__(self, regex, **kwargs): + kwargs['validators'] = ( + [validators.RegexValidator(regex)] + + kwargs.get('validators', []) + ) + super(RegexField, self).__init__(**kwargs) + + +class SlugField(CharField): + default_error_messages = { + 'invalid': _("Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.") + } + default_validators = [validators.validate_slug] + + +class URLField(CharField): + default_error_messages = { + 'invalid': _("Enter a valid URL.") + } + default_validators = [validators.URLValidator()] + + +# Number types... + +class IntegerField(Field): + default_error_messages = { + 'invalid': _('A valid integer is required.') } - coerce_to_type = str def __init__(self, **kwargs): - choices = kwargs.pop('choices') + max_value = kwargs.pop('max_value', None) + min_value = kwargs.pop('min_value', None) + super(IntegerField, self).__init__(**kwargs) + if max_value is not None: + self.validators.append(validators.MaxValueValidator(max_value)) + if min_value is not None: + self.validators.append(validators.MinValueValidator(min_value)) + print self.__class__.__name__, self.validators - assert choices, '`choices` argument is required and may not be empty' + def to_native(self, data): + try: + data = int(str(data)) + except (ValueError, TypeError): + self.fail('invalid') + return data + def to_primative(self, value): + if value is None: + return None + return int(value) + + +class FloatField(Field): + default_error_messages = { + 'invalid': _("'%s' value must be a float."), + } + + def __init__(self, **kwargs): + max_value = kwargs.pop('max_value', None) + min_value = kwargs.pop('min_value', None) + super(FloatField, self).__init__(**kwargs) + if max_value is not None: + self.validators.append(validators.MaxValueValidator(max_value)) + if min_value is not None: + self.validators.append(validators.MinValueValidator(min_value)) + + def to_primative(self, value): + if value is None: + return None + try: + return float(value) + except (TypeError, ValueError): + self.fail('invalid', value=value) + + def to_native(self, value): + if value is None: + return None + return float(value) + + +class DecimalField(Field): + default_error_messages = { + 'invalid': _('Enter a number.'), + 'max_value': _('Ensure this value is less than or equal to {max_value}.'), + 'min_value': _('Ensure this value is greater than or equal to {min_value}.'), + 'max_digits': _('Ensure that there are no more than {max_digits} digits in total.'), + 'max_decimal_places': _('Ensure that there are no more than {max_decimal_places} decimal places.'), + 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.') + } + + def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, **kwargs): + self.max_value, self.min_value = max_value, min_value + self.max_digits, self.max_decimal_places = max_digits, decimal_places + super(DecimalField, self).__init__(**kwargs) + if max_value is not None: + self.validators.append(validators.MaxValueValidator(max_value)) + if min_value is not None: + self.validators.append(validators.MinValueValidator(min_value)) + + def from_native(self, value): + """ + Validates that the input is a decimal number. Returns a Decimal + instance. Returns None for empty values. Ensures that there are no more + than max_digits in the number, and no more than decimal_places digits + after the decimal point. + """ + if value in validators.EMPTY_VALUES: + return None + + value = smart_text(value).strip() + try: + value = decimal.Decimal(value) + except decimal.DecimalException: + self.fail('invalid') + + # Check for NaN. It is the only value that isn't equal to itself, + # so we can use this to identify NaN values. + if value != value: + self.fail('invalid') + + # Check for infinity and negative infinity. + if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')): + self.fail('invalid') + + sign, digittuple, exponent = value.as_tuple() + decimals = abs(exponent) + # digittuple doesn't include any leading zeros. + digits = len(digittuple) + if decimals > digits: + # We have leading zeros up to or past the decimal point. Count + # everything past the decimal point as a digit. We do not count + # 0 before the decimal point as a digit since that would mean + # we would not allow max_digits = decimal_places. + digits = decimals + whole_digits = digits - decimals + + if self.max_digits is not None and digits > self.max_digits: + self.fail('max_digits', max_digits=self.max_digits) + if self.decimal_places is not None and decimals > self.decimal_places: + self.fail('max_decimal_places', max_decimal_places=self.max_decimal_places) + if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places): + self.fail('max_whole_digits', max_while_digits=self.max_digits - self.decimal_places) + + return value + + +# Date & time fields... + +class DateField(Field): + default_error_messages = { + 'invalid': _("Date has wrong format. Use one of these formats instead: %s"), + } + input_formats = api_settings.DATE_INPUT_FORMATS + format = api_settings.DATE_FORMAT + + def __init__(self, input_formats=None, format=None, *args, **kwargs): + self.input_formats = input_formats if input_formats is not None else self.input_formats + self.format = format if format is not None else self.format + super(DateField, self).__init__(*args, **kwargs) + + def from_native(self, value): + if value in validators.EMPTY_VALUES: + return None + + if isinstance(value, datetime.datetime): + if timezone and settings.USE_TZ and timezone.is_aware(value): + # Convert aware datetimes to the default time zone + # before casting them to dates (#17742). + default_timezone = timezone.get_default_timezone() + value = timezone.make_naive(value, default_timezone) + return value.date() + if isinstance(value, datetime.date): + return value + + for format in self.input_formats: + if format.lower() == ISO_8601: + try: + parsed = parse_date(value) + except (ValueError, TypeError): + pass + else: + if parsed is not None: + return parsed + else: + try: + parsed = datetime.datetime.strptime(value, format) + except (ValueError, TypeError): + pass + else: + return parsed.date() + + humanized_format = humanize_datetime.date_formats(self.input_formats) + msg = self.error_messages['invalid'] % humanized_format + raise ValidationError(msg) + + def to_primative(self, value): + if value is None or self.format is None: + return value + + if isinstance(value, datetime.datetime): + value = value.date() + + if self.format.lower() == ISO_8601: + return value.isoformat() + return value.strftime(self.format) + + +class DateTimeField(Field): + default_error_messages = { + 'invalid': _("Datetime has wrong format. Use one of these formats instead: %s"), + } + input_formats = api_settings.DATETIME_INPUT_FORMATS + format = api_settings.DATETIME_FORMAT + + def __init__(self, input_formats=None, format=None, *args, **kwargs): + self.input_formats = input_formats if input_formats is not None else self.input_formats + self.format = format if format is not None else self.format + super(DateTimeField, self).__init__(*args, **kwargs) + + def from_native(self, value): + if value in validators.EMPTY_VALUES: + return None + + if isinstance(value, datetime.datetime): + return value + if isinstance(value, datetime.date): + value = datetime.datetime(value.year, value.month, value.day) + if settings.USE_TZ: + # For backwards compatibility, interpret naive datetimes in + # local time. This won't work during DST change, but we can't + # do much about it, so we let the exceptions percolate up the + # call stack. + warnings.warn("DateTimeField received a naive datetime (%s)" + " while time zone support is active." % value, + RuntimeWarning) + default_timezone = timezone.get_default_timezone() + value = timezone.make_aware(value, default_timezone) + return value + + for format in self.input_formats: + if format.lower() == ISO_8601: + try: + parsed = parse_datetime(value) + except (ValueError, TypeError): + pass + else: + if parsed is not None: + return parsed + else: + try: + parsed = datetime.datetime.strptime(value, format) + except (ValueError, TypeError): + pass + else: + return parsed + + humanized_format = humanize_datetime.datetime_formats(self.input_formats) + msg = self.error_messages['invalid'] % humanized_format + raise ValidationError(msg) + + def to_primative(self, value): + if value is None or self.format is None: + return value + + if self.format.lower() == ISO_8601: + ret = value.isoformat() + if ret.endswith('+00:00'): + ret = ret[:-6] + 'Z' + return ret + return value.strftime(self.format) + + +class TimeField(Field): + default_error_messages = { + 'invalid': _("Time has wrong format. Use one of these formats instead: %s"), + } + input_formats = api_settings.TIME_INPUT_FORMATS + format = api_settings.TIME_FORMAT + + def __init__(self, input_formats=None, format=None, *args, **kwargs): + self.input_formats = input_formats if input_formats is not None else self.input_formats + self.format = format if format is not None else self.format + super(TimeField, self).__init__(*args, **kwargs) + + def from_native(self, value): + if value in validators.EMPTY_VALUES: + return None + + if isinstance(value, datetime.time): + return value + + for format in self.input_formats: + if format.lower() == ISO_8601: + try: + parsed = parse_time(value) + except (ValueError, TypeError): + pass + else: + if parsed is not None: + return parsed + else: + try: + parsed = datetime.datetime.strptime(value, format) + except (ValueError, TypeError): + pass + else: + return parsed.time() + + humanized_format = humanize_datetime.time_formats(self.input_formats) + msg = self.error_messages['invalid'] % humanized_format + raise ValidationError(msg) + + def to_primative(self, value): + if value is None or self.format is None: + return value + + if isinstance(value, datetime.datetime): + value = value.time() + + if self.format.lower() == ISO_8601: + return value.isoformat() + return value.strftime(self.format) + + +# Choice types... + +class ChoiceField(Field): + default_error_messages = { + 'invalid_choice': _('`{input}` is not a valid choice.') + } + + def __init__(self, choices, **kwargs): # Allow either single or paired choices style: # choices = [1, 2, 3] # choices = [(1, 'First'), (2, 'Second'), (3, 'Third')] @@ -321,12 +694,14 @@ class ChoiceField(Field): except KeyError: self.fail('invalid_choice', input=data) + def to_primative(self, value): + return value + class MultipleChoiceField(ChoiceField): - MESSAGES = { - 'required': 'This field is required.', - 'invalid_choice': '`{input}` is not a valid choice.', - 'not_a_list': 'Expected a list of items but got type `{input_type}`' + default_error_messages = { + 'invalid_choice': _('`{input}` is not a valid choice.'), + 'not_a_list': _('Expected a list of items but got type `{input_type}`') } def to_native(self, data): @@ -337,72 +712,42 @@ class MultipleChoiceField(ChoiceField): for item in data ]) - -class IntegerField(Field): - MESSAGES = { - 'required': 'This field is required.', - 'invalid_integer': 'A valid integer is required.' - } - - def __init__(self, **kwargs): - max_value = kwargs.pop('max_value', None) - min_value = kwargs.pop('min_value', None) - super(IntegerField, self).__init__(**kwargs) - if max_value is not None: - self.validators.append(validators.MaxValueValidator(max_value)) - if min_value is not None: - self.validators.append(validators.MinValueValidator(min_value)) - - def to_native(self, data): - try: - data = int(str(data)) - except (ValueError, TypeError): - self.fail('invalid_integer') - return data - def to_primative(self, value): - if value is None: - return None - return int(value) + return value -class EmailField(CharField): - pass # TODO - - -class URLField(CharField): - pass # TODO - - -class RegexField(CharField): - def __init__(self, **kwargs): - self.regex = kwargs.pop('regex') - super(CharField, self).__init__(**kwargs) - - -class DateField(CharField): - def __init__(self, **kwargs): - self.input_formats = kwargs.pop('input_formats', None) - super(DateField, self).__init__(**kwargs) - - -class TimeField(CharField): - def __init__(self, **kwargs): - self.input_formats = kwargs.pop('input_formats', None) - super(TimeField, self).__init__(**kwargs) - - -class DateTimeField(CharField): - def __init__(self, **kwargs): - self.input_formats = kwargs.pop('input_formats', None) - super(DateTimeField, self).__init__(**kwargs) - +# File types... class FileField(Field): pass # TODO +class ImageField(Field): + pass # TODO + + +# Advanced field types... + class ReadOnlyField(Field): + """ + A read-only field that simply returns the field value. + + If the field is a method with no parameters, the method will be called + and it's return value used as the representation. + + For example, the following would call `get_expiry_date()` on the object: + + class ExampleSerializer(self): + expiry_date = ReadOnlyField(source='get_expiry_date') + """ + + def __init__(self, **kwargs): + kwargs['read_only'] = True + super(ReadOnlyField, self).__init__(**kwargs) + + def to_native(self, data): + raise NotImplemented('.to_native() not supported.') + def to_primative(self, value): if is_simple_callable(value): return value() @@ -410,11 +755,28 @@ class ReadOnlyField(Field): class MethodField(Field): + """ + A read-only field that get its representation from calling a method on the + parent serializer class. The method called will be of the form + "get_{field_name}", and should take a single argument, which is the + object being serialized. + + For example: + + class ExampleSerializer(self): + extra_info = MethodField() + + def get_extra_info(self, obj): + return ... # Calculate some data to return. + """ def __init__(self, **kwargs): kwargs['source'] = '*' kwargs['read_only'] = True super(MethodField, self).__init__(**kwargs) + def to_native(self, data): + raise NotImplemented('.to_native() not supported.') + def to_primative(self, value): attr = 'get_{field_name}'.format(field_name=self.field_name) method = getattr(self.parent, attr) @@ -424,35 +786,14 @@ class MethodField(Field): class ModelField(Field): """ A generic field that can be used against an arbitrary model field. + + This is used by `ModelSerializer` when dealing with custom model fields, + that do not have a serializer field to be mapped to. """ - def __init__(self, *args, **kwargs): - try: - self.model_field = kwargs.pop('model_field') - except KeyError: - raise ValueError("ModelField requires 'model_field' kwarg") - - self.min_length = kwargs.pop('min_length', - getattr(self.model_field, 'min_length', None)) - self.max_length = kwargs.pop('max_length', - getattr(self.model_field, 'max_length', None)) - self.min_value = kwargs.pop('min_value', - getattr(self.model_field, 'min_value', None)) - self.max_value = kwargs.pop('max_value', - getattr(self.model_field, 'max_value', None)) - - super(ModelField, self).__init__(*args, **kwargs) - - if self.min_length is not None: - self.validators.append(validators.MinLengthValidator(self.min_length)) - if self.max_length is not None: - self.validators.append(validators.MaxLengthValidator(self.max_length)) - if self.min_value is not None: - self.validators.append(validators.MinValueValidator(self.min_value)) - if self.max_value is not None: - self.validators.append(validators.MaxValueValidator(self.max_value)) - - def get_attribute(self, instance): - return get_attribute(instance, self.source_attrs[:-1]) + def __init__(self, model_field, **kwargs): + self.model_field = model_field + kwargs['source'] = '*' + super(ModelField, self).__init__(**kwargs) def to_native(self, data): rel = getattr(self.model_field, 'rel', None) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 93226d322..8ca283876 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -10,15 +10,15 @@ python primitives. 2. The process of marshalling between python primitives and request and response content is handled by parsers and renderers. """ +from django.core import validators from django.core.exceptions import ValidationError from django.db import models from django.utils import six from collections import namedtuple, OrderedDict from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings -from rest_framework.utils import html +from rest_framework.utils import html, modelinfo, representation import copy -import inspect # Note: We do the following so that users of the framework can use this style: # @@ -146,12 +146,10 @@ class SerializerMetaclass(type): class Serializer(BaseSerializer): def __new__(cls, *args, **kwargs): - many = kwargs.pop('many', False) - if many: - class DynamicListSerializer(ListSerializer): - child = cls() - return DynamicListSerializer(*args, **kwargs) - return super(Serializer, cls).__new__(cls) + if kwargs.pop('many', False): + kwargs['child'] = cls() + return ListSerializer(*args, **kwargs) + return super(Serializer, cls).__new__(cls, *args, **kwargs) def __init__(self, *args, **kwargs): self.context = kwargs.pop('context', {}) @@ -248,6 +246,9 @@ class Serializer(BaseSerializer): error = errors.get(field.field_name) yield FieldResult(field, value, error) + def __repr__(self): + return representation.serializer_repr(self, indent=1) + class ListSerializer(BaseSerializer): child = None @@ -299,26 +300,8 @@ class ListSerializer(BaseSerializer): self.instance = self.create(self.validated_data) return self.instance - -def _resolve_model(obj): - """ - Resolve supplied `obj` to a Django model class. - - `obj` must be a Django model class itself, or a string - representation of one. Useful in situtations like GH #1225 where - Django may not have resolved a string-based reference to a model in - another model's foreign key definition. - - String representations should have the format: - 'appname.ModelName' - """ - if isinstance(obj, six.string_types) and len(obj.split('.')) == 2: - app_name, model_name = obj.split('.') - return models.get_model(app_name, model_name) - elif inspect.isclass(obj) and issubclass(obj, models.Model): - return obj - else: - raise ValueError("{0} is not a Django model".format(obj)) + def __repr__(self): + return representation.list_repr(self, indent=1) class ModelSerializerOptions(object): @@ -334,24 +317,25 @@ class ModelSerializerOptions(object): class ModelSerializer(Serializer): field_mapping = { models.AutoField: IntegerField, - # models.FloatField: FloatField, - models.IntegerField: IntegerField, - models.PositiveIntegerField: IntegerField, - models.SmallIntegerField: IntegerField, - models.PositiveSmallIntegerField: IntegerField, - models.DateTimeField: DateTimeField, - models.DateField: DateField, - models.TimeField: TimeField, - # models.DecimalField: DecimalField, - models.EmailField: EmailField, - models.CharField: CharField, - models.URLField: URLField, - # models.SlugField: SlugField, - models.TextField: CharField, - models.CommaSeparatedIntegerField: CharField, + models.BigIntegerField: IntegerField, models.BooleanField: BooleanField, - models.NullBooleanField: BooleanField, + models.CharField: CharField, + models.CommaSeparatedIntegerField: CharField, + models.DateField: DateField, + models.DateTimeField: DateTimeField, + models.DecimalField: DecimalField, + models.EmailField: EmailField, models.FileField: FileField, + models.FloatField: FloatField, + models.IntegerField: IntegerField, + models.NullBooleanField: BooleanField, + models.PositiveIntegerField: IntegerField, + models.PositiveSmallIntegerField: IntegerField, + models.SlugField: SlugField, + models.SmallIntegerField: IntegerField, + models.TextField: CharField, + models.TimeField: TimeField, + models.URLField: URLField, # models.ImageField: ImageField, } @@ -392,85 +376,31 @@ class ModelSerializer(Serializer): """ Return all the fields that should be serialized for the model. """ - cls = self.opts.model - opts = cls._meta.concrete_model._meta + info = modelinfo.get_field_info(self.opts.model) ret = OrderedDict() - nested = bool(self.opts.depth) - # Deal with adding the primary key field - pk_field = opts.pk - while pk_field.rel and pk_field.rel.parent_link: - # If model is a child via multitable inheritance, use parent's pk - pk_field = pk_field.rel.to._meta.pk - - serializer_pk_field = self.get_pk_field(pk_field) + serializer_pk_field = self.get_pk_field(info.pk) if serializer_pk_field: - ret[pk_field.name] = serializer_pk_field + ret[info.pk.name] = serializer_pk_field - # Deal with forward relationships - forward_rels = [field for field in opts.fields if field.serialize] - forward_rels += [field for field in opts.many_to_many if field.serialize] + # Regular fields + for field_name, field in info.fields.items(): + ret[field_name] = self.get_field(field) - for model_field in forward_rels: - has_through_model = False - - if model_field.rel: - to_many = isinstance(model_field, - models.fields.related.ManyToManyField) - related_model = _resolve_model(model_field.rel.to) - - if to_many and not model_field.rel.through._meta.auto_created: - has_through_model = True - - if model_field.rel and nested: - field = self.get_nested_field(model_field, related_model, to_many) - elif model_field.rel: - field = self.get_related_field(model_field, related_model, to_many) + # Forward relations + for field_name, relation_info in info.forward_relations.items(): + if self.opts.depth: + ret[field_name] = self.get_nested_field(*relation_info) else: - field = self.get_field(model_field) + ret[field_name] = self.get_related_field(*relation_info) - if field: - if has_through_model: - field.read_only = True - - ret[model_field.name] = field - - # Deal with reverse relationships - if not self.opts.fields: - reverse_rels = [] - else: - # Reverse relationships are only included if they are explicitly - # present in the `fields` option on the serializer - reverse_rels = opts.get_all_related_objects() - reverse_rels += opts.get_all_related_many_to_many_objects() - - for relation in reverse_rels: - accessor_name = relation.get_accessor_name() - if not self.opts.fields or accessor_name not in self.opts.fields: - continue - related_model = relation.model - to_many = relation.field.rel.multiple - has_through_model = False - is_m2m = isinstance(relation.field, - models.fields.related.ManyToManyField) - - if ( - is_m2m and - hasattr(relation.field.rel, 'through') and - not relation.field.rel.through._meta.auto_created - ): - has_through_model = True - - if nested: - field = self.get_nested_field(None, related_model, to_many) - else: - field = self.get_related_field(None, related_model, to_many) - - if field: - if has_through_model: - field.read_only = True - - ret[accessor_name] = field + # Reverse relations + for accessor_name, relation_info in info.reverse_relations.items(): + if accessor_name in self.opts.fields: + if self.opts.depth: + ret[field_name] = self.get_nested_field(*relation_info) + else: + ret[field_name] = self.get_related_field(*relation_info) return ret @@ -480,7 +410,7 @@ class ModelSerializer(Serializer): """ return self.get_field(model_field) - def get_nested_field(self, model_field, related_model, to_many): + def get_nested_field(self, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a nested relational field. @@ -491,59 +421,148 @@ class ModelSerializer(Serializer): model = related_model depth = self.opts.depth - 1 - return NestedModelSerializer(many=to_many) + kwargs = {'read_only': True} + if to_many: + kwargs['many'] = True + return NestedModelSerializer(**kwargs) - def get_related_field(self, model_field, related_model, to_many): + def get_related_field(self, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a flat relational field. Note that model_field will be `None` for reverse relationships. """ - # TODO: filter queryset using: - # .using(db).complex_filter(self.rel.limit_choices_to) + kwargs = { + 'queryset': related_model._default_manager, + } - kwargs = {} - # 'queryset': related_model._default_manager, - # 'many': to_many - # } + if to_many: + kwargs['many'] = True + + if has_through_model: + kwargs['read_only'] = True + kwargs.pop('queryset', None) if model_field: - kwargs['required'] = not(model_field.null or model_field.blank) + if model_field.null or model_field.blank: + kwargs['required'] = False # if model_field.help_text is not None: # kwargs['help_text'] = model_field.help_text if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name if not model_field.editable: kwargs['read_only'] = True - if model_field.verbose_name is not None: - kwargs['label'] = model_field.verbose_name + kwargs.pop('queryset', None) - return IntegerField(**kwargs) - # TODO: return PrimaryKeyRelatedField(**kwargs) + return PrimaryKeyRelatedField(**kwargs) def get_field(self, model_field): """ Creates a default instance of a basic non-relational field. """ kwargs = {} + validator_kwarg = model_field.validators if model_field.null or model_field.blank: kwargs['required'] = False - if isinstance(model_field, models.AutoField) or not model_field.editable: - kwargs['read_only'] = True - - if model_field.has_default(): - kwargs['default'] = model_field.get_default() - - if issubclass(model_field.__class__, models.TextField): - kwargs['widget'] = widgets.Textarea - if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name - if model_field.validators is not None: - kwargs['validators'] = model_field.validators + if isinstance(model_field, models.AutoField) or not model_field.editable: + kwargs['read_only'] = True + # Read only implies that the field is not required. + # We have a cleaner repr on the instance if we don't set it. + kwargs.pop('required', None) + + if model_field.has_default(): + kwargs['default'] = model_field.get_default() + # Having a default implies that the field is not required. + # We have a cleaner repr on the instance if we don't set it. + kwargs.pop('required', None) + + # Ensure that max_length is passed explicitly as a keyword arg, + # rather than as a validator. + max_length = getattr(model_field, 'max_length', None) + if max_length is not None: + kwargs['max_length'] = max_length + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MaxLengthValidator) + ] + + # Ensure that min_length is passed explicitly as a keyword arg, + # rather than as a validator. + min_length = getattr(model_field, 'min_length', None) + if min_length is not None: + kwargs['min_length'] = min_length + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MinLengthValidator) + ] + + # Ensure that max_value is passed explicitly as a keyword arg, + # rather than as a validator. + max_value = next(( + validator.limit_value for validator in validator_kwarg + if isinstance(validator, validators.MaxValueValidator) + ), None) + if max_value is not None: + kwargs['max_value'] = max_value + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MaxValueValidator) + ] + + # Ensure that max_value is passed explicitly as a keyword arg, + # rather than as a validator. + min_value = next(( + validator.limit_value for validator in validator_kwarg + if isinstance(validator, validators.MinValueValidator) + ), None) + if min_value is not None: + kwargs['min_value'] = min_value + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MinValueValidator) + ] + + # URLField does not need to include the URLValidator argument, + # as it is explicitly added in. + if isinstance(model_field, models.URLField): + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.URLValidator) + ] + + # EmailField does not need to include the validate_email argument, + # as it is explicitly added in. + if isinstance(model_field, models.EmailField): + validator_kwarg = [ + validator for validator in validator_kwarg + if validator is not validators.validate_email + ] + + # SlugField do not need to include the 'validate_slug' argument, + if isinstance(model_field, models.SlugField): + validator_kwarg = [ + validator for validator in validator_kwarg + if validator is not validators.validate_slug + ] + + max_digits = getattr(model_field, 'max_digits', None) + if max_digits is not None: + kwargs['max_digits'] = max_digits + + decimal_places = getattr(model_field, 'decimal_places', None) + if decimal_places is not None: + kwargs['decimal_places'] = decimal_places + + if validator_kwarg: + kwargs['validators'] = validator_kwarg + + # if issubclass(model_field.__class__, models.TextField): + # kwargs['widget'] = widgets.Textarea # if model_field.help_text is not None: # kwargs['help_text'] = model_field.help_text @@ -555,31 +574,10 @@ class ModelSerializer(Serializer): kwargs['empty'] = None return ChoiceField(**kwargs) - # put this below the ChoiceField because min_value isn't a valid initializer - if issubclass(model_field.__class__, models.PositiveIntegerField) or \ - issubclass(model_field.__class__, models.PositiveSmallIntegerField): - kwargs['min_value'] = 0 - if model_field.null and \ issubclass(model_field.__class__, (models.CharField, models.TextField)): kwargs['allow_none'] = True - # attribute_dict = { - # models.CharField: ['max_length'], - # models.CommaSeparatedIntegerField: ['max_length'], - # models.DecimalField: ['max_digits', 'decimal_places'], - # models.EmailField: ['max_length'], - # models.FileField: ['max_length'], - # models.ImageField: ['max_length'], - # models.SlugField: ['max_length'], - # models.URLField: ['max_length'], - # } - - # if model_field.__class__ in attribute_dict: - # attributes = attribute_dict[model_field.__class__] - # for attribute in attributes: - # kwargs.update({attribute: getattr(model_field, attribute)}) - try: return self.field_mapping[model_field.__class__](**kwargs) except KeyError: @@ -594,28 +592,21 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions): super(HyperlinkedModelSerializerOptions, self).__init__(meta) self.view_name = getattr(meta, 'view_name', None) self.lookup_field = getattr(meta, 'lookup_field', None) - self.url_field_name = getattr(meta, 'url_field_name', api_settings.URL_FIELD_NAME) class HyperlinkedModelSerializer(ModelSerializer): _options_class = HyperlinkedModelSerializerOptions - _default_view_name = '%(model_name)s-detail' - _hyperlink_field_class = HyperlinkedRelatedField - _hyperlink_identify_field_class = HyperlinkedIdentityField def get_default_fields(self): fields = super(HyperlinkedModelSerializer, self).get_default_fields() if self.opts.view_name is None: - self.opts.view_name = self._get_default_view_name(self.opts.model) + self.opts.view_name = self.get_default_view_name(self.opts.model) - if self.opts.url_field_name not in fields: - url_field = self._hyperlink_identify_field_class( - view_name=self.opts.view_name, - lookup_field=self.opts.lookup_field - ) + url_field_name = api_settings.URL_FIELD_NAME + if url_field_name not in fields: ret = fields.__class__() - ret[self.opts.url_field_name] = url_field + ret[url_field_name] = self.get_url_field() ret.update(fields) fields = ret @@ -625,39 +616,48 @@ class HyperlinkedModelSerializer(ModelSerializer): if self.opts.fields and model_field.name in self.opts.fields: return self.get_field(model_field) - def get_related_field(self, model_field, related_model, to_many): + def get_url_field(self): + kwargs = { + 'view_name': self.get_default_view_name(self.opts.model) + } + if self.opts.lookup_field: + kwargs['lookup_field'] = self.opts.lookup_field + return HyperlinkedIdentityField(**kwargs) + + def get_related_field(self, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a flat relational field. """ - # TODO: filter queryset using: - # .using(db).complex_filter(self.rel.limit_choices_to) - # kwargs = { - # 'queryset': related_model._default_manager, - # 'view_name': self._get_default_view_name(related_model), - # 'many': to_many - # } - kwargs = {} + kwargs = { + 'queryset': related_model._default_manager, + 'view_name': self.get_default_view_name(related_model), + } + + if to_many: + kwargs['many'] = True + + if has_through_model: + kwargs['read_only'] = True + kwargs.pop('queryset', None) if model_field: - kwargs['required'] = not(model_field.null or model_field.blank) + if model_field.null or model_field.blank: + kwargs['required'] = False # if model_field.help_text is not None: # kwargs['help_text'] = model_field.help_text if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name + if not model_field.editable: + kwargs['read_only'] = True + kwargs.pop('queryset', None) - return IntegerField(**kwargs) - # if self.opts.lookup_field: - # kwargs['lookup_field'] = self.opts.lookup_field + return HyperlinkedRelatedField(**kwargs) - # return self._hyperlink_field_class(**kwargs) - - def _get_default_view_name(self, model): + def get_default_view_name(self, model): """ - Return the view name to use if 'view_name' is not specified in 'Meta' + Return the view name to use for related models. """ - model_meta = model._meta - format_kwargs = { - 'app_label': model_meta.app_label, - 'model_name': model_meta.object_name.lower() + return '%(model_name)s-detail' % { + 'app_label': model._meta.app_label, + 'model_name': model._meta.object_name.lower() } - return self._default_view_name % format_kwargs diff --git a/rest_framework/utils/humanize_datetime.py b/rest_framework/utils/humanize_datetime.py new file mode 100644 index 000000000..649f2abc6 --- /dev/null +++ b/rest_framework/utils/humanize_datetime.py @@ -0,0 +1,47 @@ +""" +Helper functions that convert strftime formats into more readable representations. +""" +from rest_framework import ISO_8601 + + +def datetime_formats(formats): + format = ', '.join(formats).replace( + ISO_8601, + 'YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]' + ) + return humanize_strptime(format) + + +def date_formats(formats): + format = ', '.join(formats).replace(ISO_8601, 'YYYY[-MM[-DD]]') + return humanize_strptime(format) + + +def time_formats(formats): + format = ', '.join(formats).replace(ISO_8601, 'hh:mm[:ss[.uuuuuu]]') + return humanize_strptime(format) + + +def humanize_strptime(format_string): + # Note that we're missing some of the locale specific mappings that + # don't really make sense. + mapping = { + "%Y": "YYYY", + "%y": "YY", + "%m": "MM", + "%b": "[Jan-Dec]", + "%B": "[January-December]", + "%d": "DD", + "%H": "hh", + "%I": "hh", # Requires '%p' to differentiate from '%H'. + "%M": "mm", + "%S": "ss", + "%f": "uuuuuu", + "%a": "[Mon-Sun]", + "%A": "[Monday-Sunday]", + "%p": "[AM|PM]", + "%z": "[+HHMM|-HHMM]" + } + for key, val in mapping.items(): + format_string = format_string.replace(key, val) + return format_string diff --git a/rest_framework/utils/modelinfo.py b/rest_framework/utils/modelinfo.py new file mode 100644 index 000000000..c0513886c --- /dev/null +++ b/rest_framework/utils/modelinfo.py @@ -0,0 +1,97 @@ +""" +Helper functions for returning the field information that is associated +with a model class. +""" +from collections import namedtuple, OrderedDict +from django.db import models +from django.utils import six +import inspect + +FieldInfo = namedtuple('FieldResult', ['pk', 'fields', 'forward_relations', 'reverse_relations']) +RelationInfo = namedtuple('RelationInfo', ['field', 'related', 'to_many', 'has_through_model']) + + +def _resolve_model(obj): + """ + Resolve supplied `obj` to a Django model class. + + `obj` must be a Django model class itself, or a string + representation of one. Useful in situtations like GH #1225 where + Django may not have resolved a string-based reference to a model in + another model's foreign key definition. + + String representations should have the format: + 'appname.ModelName' + """ + if isinstance(obj, six.string_types) and len(obj.split('.')) == 2: + app_name, model_name = obj.split('.') + return models.get_model(app_name, model_name) + elif inspect.isclass(obj) and issubclass(obj, models.Model): + return obj + raise ValueError("{0} is not a Django model".format(obj)) + + +def get_field_info(model): + """ + Given a model class, returns a `FieldInfo` instance containing metadata + about the various field types on the model. + """ + opts = model._meta.concrete_model._meta + + # Deal with the primary key. + pk = opts.pk + while pk.rel and pk.rel.parent_link: + # If model is a child via multitable inheritance, use parent's pk. + pk = pk.rel.to._meta.pk + + # Deal with regular fields. + fields = OrderedDict() + for field in [field for field in opts.fields if field.serialize and not field.rel]: + fields[field.name] = field + + # Deal with forward relationships. + forward_relations = OrderedDict() + for field in [field for field in opts.fields if field.serialize and field.rel]: + forward_relations[field.name] = RelationInfo( + field=field, + related=_resolve_model(field.rel.to), + to_many=False, + has_through_model=False + ) + + # Deal with forward many-to-many relationships. + for field in [field for field in opts.many_to_many if field.serialize]: + forward_relations[field.name] = RelationInfo( + field=field, + related=_resolve_model(field.rel.to), + to_many=True, + has_through_model=( + not field.rel.through._meta.auto_created + ) + ) + + # Deal with reverse relationships. + reverse_relations = OrderedDict() + for relation in opts.get_all_related_objects(): + accessor_name = relation.get_accessor_name() + reverse_relations[accessor_name] = RelationInfo( + field=None, + related=relation.model, + to_many=relation.field.rel.multiple, + has_through_model=False + ) + + # Deal with reverse many-to-many relationships. + for relation in opts.get_all_related_many_to_many_objects(): + accessor_name = relation.get_accessor_name() + reverse_relations[accessor_name] = RelationInfo( + field=None, + related=relation.model, + to_many=True, + has_through_model=( + hasattr(relation.field.rel, 'through') and + not relation.field.rel.through._meta.auto_created + ) + ) + + return FieldInfo(pk, fields, forward_relations, reverse_relations) diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py new file mode 100644 index 000000000..1de21597d --- /dev/null +++ b/rest_framework/utils/representation.py @@ -0,0 +1,72 @@ +""" +Helper functions for creating user-friendly representations +of serializer classes and serializer fields. +""" +import re + + +def smart_repr(value): + value = repr(value) + + # Representations like u'help text' + # should simply be presented as 'help text' + if value.startswith("u'") and value.endswith("'"): + return value[1:] + + # Representations like + # <django.core.validators.RegexValidator object at 0x1047af050> + # Should be presented as + # <django.core.validators.RegexValidator object> + value = re.sub(' at 0x[0-9a-f]{8,10}>', '>', value) + + return value + + +def field_repr(field, force_many=False): + kwargs = field._kwargs + if force_many: + kwargs = kwargs.copy() + kwargs['many'] = True + kwargs.pop('child', None) + + arg_string = ', '.join([smart_repr(val) for val in field._args]) + kwarg_string = ', '.join([ + '%s=%s' % (key, smart_repr(val)) + for key, val in sorted(kwargs.items()) + ]) + if arg_string and kwarg_string: + arg_string += ', ' + + if force_many: + class_name = force_many.__class__.__name__ + else: + class_name = field.__class__.__name__ + + return "%s(%s%s)" % (class_name, arg_string, kwarg_string) + + +def serializer_repr(serializer, indent, force_many=None): + ret = field_repr(serializer, force_many) + ':' + indent_str = ' ' * indent + + if force_many: + fields = force_many.fields + else: + fields = serializer.fields + + for field_name, field in fields.items(): + ret += '\n' + indent_str + field_name + ' = ' + if hasattr(field, 'fields'): + ret += serializer_repr(field, indent + 1) + elif hasattr(field, 'child'): + ret += list_repr(field, indent + 1) + else: + ret += field_repr(field) + return ret + + +def list_repr(serializer, indent): + child = serializer.child + if hasattr(child, 'fields'): + return serializer_repr(serializer, indent, force_many=child) + return field_repr(serializer) diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py new file mode 100644 index 000000000..dc254da45 --- /dev/null +++ b/tests/test_model_field_mappings.py @@ -0,0 +1,160 @@ +""" +The `ModelSerializer` and `HyperlinkedModelSerializer` classes are essentially +shortcuts for automatically creating serializers based on a given model class. + +These tests deal with ensuring that we correctly map the model fields onto +an appropriate set of serializer fields for each case. +""" +from django.db import models +from django.test import TestCase +from rest_framework import serializers + + +# Models for testing regular field mapping + +class RegularFieldsModel(models.Model): + auto_field = models.AutoField(primary_key=True) + big_integer_field = models.BigIntegerField() + boolean_field = models.BooleanField() + char_field = models.CharField(max_length=100) + comma_seperated_integer_field = models.CommaSeparatedIntegerField(max_length=100) + date_field = models.DateField() + datetime_field = models.DateTimeField() + decimal_field = models.DecimalField(max_digits=3, decimal_places=1) + email_field = models.EmailField(max_length=100) + float_field = models.FloatField() + integer_field = models.IntegerField() + null_boolean_field = models.NullBooleanField() + positive_integer_field = models.PositiveIntegerField() + positive_small_integer_field = models.PositiveSmallIntegerField() + slug_field = models.SlugField(max_length=100) + small_integer_field = models.SmallIntegerField() + text_field = models.TextField() + time_field = models.TimeField() + url_field = models.URLField(max_length=100) + + +REGULAR_FIELDS_REPR = """ +TestSerializer(): + auto_field = IntegerField(label='auto field', read_only=True) + big_integer_field = IntegerField(label='big integer field') + boolean_field = BooleanField(default=False, label='boolean field') + char_field = CharField(label='char field', max_length=100) + comma_seperated_integer_field = CharField(label='comma seperated integer field', max_length=100, validators=[<django.core.validators.RegexValidator object>]) + date_field = DateField(label='date field') + datetime_field = DateTimeField(label='datetime field') + decimal_field = DecimalField(decimal_places=1, label='decimal field', max_digits=3) + email_field = EmailField(label='email field', max_length=100) + float_field = FloatField(label='float field') + integer_field = IntegerField(label='integer field') + null_boolean_field = BooleanField(label='null boolean field', required=False) + positive_integer_field = IntegerField(label='positive integer field') + positive_small_integer_field = IntegerField(label='positive small integer field') + slug_field = SlugField(label='slug field', max_length=100) + small_integer_field = IntegerField(label='small integer field') + text_field = CharField(label='text field') + time_field = TimeField(label='time field') + url_field = URLField(label='url field', max_length=100) +""".strip() + + +# Model for testing relational field mapping + +class ForeignKeyTarget(models.Model): + char_field = models.CharField(max_length=100) + + +class ManyToManyTarget(models.Model): + char_field = models.CharField(max_length=100) + + +class OneToOneTarget(models.Model): + char_field = models.CharField(max_length=100) + + +class RelationalModel(models.Model): + foreign_key = models.ForeignKey(ForeignKeyTarget) + many_to_many = models.ManyToManyField(ManyToManyTarget) + one_to_one = models.OneToOneField(OneToOneTarget) + + +RELATIONAL_FLAT_REPR = """ +TestSerializer(): + id = IntegerField(label='ID', read_only=True) + foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>) + one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>) + many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=<django.db.models.manager.Manager object>) +""".strip() + + +RELATIONAL_NESTED_REPR = """ +TestSerializer(): + id = IntegerField(label='ID', read_only=True) + foreign_key = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) + one_to_one = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) + many_to_many = NestedModelSerializer(many=True, read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) +""".strip() + + +HYPERLINKED_FLAT_REPR = """ +TestSerializer(): + url = HyperlinkedIdentityField(view_name='relationalmodel-detail') + foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>, view_name='foreignkeytarget-detail') + one_to_one = HyperlinkedRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>, view_name='onetoonetarget-detail') + many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=<django.db.models.manager.Manager object>, view_name='manytomanytarget-detail') +""".strip() + + +HYPERLINKED_NESTED_REPR = """ +TestSerializer(): + url = HyperlinkedIdentityField(view_name='relationalmodel-detail') + foreign_key = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) + one_to_one = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) + many_to_many = NestedModelSerializer(many=True, read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(label='name', max_length=100) +""".strip() + + +class TestSerializerMappings(TestCase): + def test_regular_fields(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + self.assertEqual(repr(TestSerializer()), REGULAR_FIELDS_REPR) + + def test_flat_relational_fields(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RelationalModel + self.assertEqual(repr(TestSerializer()), RELATIONAL_FLAT_REPR) + + def test_nested_relational_fields(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RelationalModel + depth = 1 + self.assertEqual(repr(TestSerializer()), RELATIONAL_NESTED_REPR) + + def test_flat_hyperlinked_fields(self): + class TestSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = RelationalModel + self.assertEqual(repr(TestSerializer()), HYPERLINKED_FLAT_REPR) + + def test_nested_hyperlinked_fields(self): + class TestSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = RelationalModel + depth = 1 + self.assertEqual(repr(TestSerializer()), HYPERLINKED_NESTED_REPR) diff --git a/tests/test_serializers.py b/tests/test_modelinfo.py similarity index 94% rename from tests/test_serializers.py rename to tests/test_modelinfo.py index 31c417306..254a33c9c 100644 --- a/tests/test_serializers.py +++ b/tests/test_modelinfo.py @@ -1,6 +1,6 @@ from django.test import TestCase from django.utils import six -from rest_framework.serializers import _resolve_model +from rest_framework.utils.modelinfo import _resolve_model from tests.models import BasicModel diff --git a/tests/test_relations.py b/tests/test_relations.py index a30b12e68..b1bc66b62 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -22,18 +22,18 @@ # https://github.com/tomchristie/django-rest-framework/issues/446 # """ # field = serializers.PrimaryKeyRelatedField(queryset=NullModel.objects.all()) -# self.assertRaises(serializers.ValidationError, field.from_native, '') -# self.assertRaises(serializers.ValidationError, field.from_native, []) +# self.assertRaises(serializers.ValidationError, field.to_primative, '') +# self.assertRaises(serializers.ValidationError, field.to_primative, []) # def test_hyperlinked_related_field_with_empty_string(self): # field = serializers.HyperlinkedRelatedField(queryset=NullModel.objects.all(), view_name='') -# self.assertRaises(serializers.ValidationError, field.from_native, '') -# self.assertRaises(serializers.ValidationError, field.from_native, []) +# self.assertRaises(serializers.ValidationError, field.to_primative, '') +# self.assertRaises(serializers.ValidationError, field.to_primative, []) # def test_slug_related_field_with_empty_string(self): # field = serializers.SlugRelatedField(queryset=NullModel.objects.all(), slug_field='pk') -# self.assertRaises(serializers.ValidationError, field.from_native, '') -# self.assertRaises(serializers.ValidationError, field.from_native, []) +# self.assertRaises(serializers.ValidationError, field.to_primative, '') +# self.assertRaises(serializers.ValidationError, field.to_primative, []) # class TestManyRelatedMixin(TestCase): diff --git a/tests/test_serializer_empty.py b/tests/test_serializer_empty.py index d0006ad38..4e4a7b423 100644 --- a/tests/test_serializer_empty.py +++ b/tests/test_serializer_empty.py @@ -6,7 +6,7 @@ # def test_empty_serializer(self): # class FooBarSerializer(serializers.Serializer): # foo = serializers.IntegerField() -# bar = serializers.SerializerMethodField('get_bar') +# bar = serializers.MethodField() # def get_bar(self, obj): # return 'bar' From 234369aefdf08d7d0161d851866990754c00d31f Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 10 Sep 2014 08:53:33 +0100 Subject: [PATCH 046/100] Tweaks --- rest_framework/fields.py | 6 ++++-- rest_framework/serializers.py | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 043a44edd..e2bd57000 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -190,7 +190,7 @@ class Field(object): raise SkipField() return self.default - def validate(self, data=empty): + def validate_value(self, data=empty): """ Validate a simple representation and return the internal value. @@ -506,6 +506,7 @@ class DateField(Field): default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) return value.date() + if isinstance(value, datetime.date): return value @@ -560,6 +561,7 @@ class DateTimeField(Field): if isinstance(value, datetime.datetime): return value + if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: @@ -675,7 +677,7 @@ class ChoiceField(Field): for item in choices ] if all(pairs): - self.choices = {key: val for key, val in choices} + self.choices = {key: display_value for key, display_value in choices} else: self.choices = {item: item for item in choices} diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8ca283876..0727b8cdf 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -205,7 +205,7 @@ class Serializer(BaseSerializer): validate_method = getattr(self, 'validate_' + field.field_name, None) primitive_value = field.get_value(data) try: - validated_value = field.validate(primitive_value) + validated_value = field.validate_value(primitive_value) if validate_method is not None: validated_value = validate_method(validated_value) except ValidationError as exc: @@ -327,6 +327,7 @@ class ModelSerializer(Serializer): models.EmailField: EmailField, models.FileField: FileField, models.FloatField: FloatField, + models.ImageField: ImageField, models.IntegerField: IntegerField, models.NullBooleanField: BooleanField, models.PositiveIntegerField: IntegerField, @@ -336,7 +337,6 @@ class ModelSerializer(Serializer): models.TextField: CharField, models.TimeField: TimeField, models.URLField: URLField, - # models.ImageField: ImageField, } _options_class = ModelSerializerOptions From 01c8c0cad977fc0787dbfc78bd34f4fd37e613f4 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 10 Sep 2014 13:52:16 +0100 Subject: [PATCH 047/100] Added help_text argument to fields --- rest_framework/compat.py | 23 +++++----- rest_framework/fields.py | 5 +- rest_framework/serializers.py | 86 +++++++++++++++++------------------ 3 files changed, 56 insertions(+), 58 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index fa0f0bfb1..70b38df98 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -39,6 +39,17 @@ except ImportError: django_filters = None +if django.VERSION >= (1, 6): + def clean_manytomany_helptext(text): + return text +else: + # Up to version 1.5 many to many fields automatically suffix + # the `help_text` attribute with hardcoded text. + def clean_manytomany_helptext(text): + if text.endswith(' Hold down "Control", or "Command" on a Mac, to select more than one.'): + text = text[:-69] + return text + # Django-guardian is optional. Import only if guardian is in INSTALLED_APPS # Fixes (#1712). We keep the try/except for the test suite. guardian = None @@ -99,18 +110,8 @@ def get_concrete_model(model_cls): return model_cls -# View._allowed_methods only present from 1.5 onwards -if django.VERSION >= (1, 5): - from django.views.generic import View -else: - from django.views.generic import View as DjangoView - - class View(DjangoView): - def _allowed_methods(self): - return [m.upper() for m in self.http_method_names if hasattr(self, m)] - - # PATCH method is not implemented by Django +from django.views.generic import View if 'patch' not in View.http_method_names: View.http_method_names = View.http_method_names + ['patch'] diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e2bd57000..e71dce1df 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -101,7 +101,8 @@ class Field(object): def __init__(self, read_only=False, write_only=False, required=None, default=empty, initial=None, source=None, - label=None, style=None, error_messages=None, validators=[]): + label=None, help_text=None, style=None, + error_messages=None, validators=[]): self._creation_counter = Field._creation_counter Field._creation_counter += 1 @@ -122,6 +123,7 @@ class Field(object): self.source = source self.initial = initial self.label = label + self.help_text = help_text self.style = {} if style is None else style self.validators = validators or self.default_validators[:] @@ -372,7 +374,6 @@ class IntegerField(Field): self.validators.append(validators.MaxValueValidator(max_value)) if min_value is not None: self.validators.append(validators.MinValueValidator(min_value)) - print self.__class__.__name__, self.validators def to_native(self, data): try: diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 0727b8cdf..459f8a8ce 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -15,6 +15,7 @@ from django.core.exceptions import ValidationError from django.db import models from django.utils import six from collections import namedtuple, OrderedDict +from rest_framework.compat import clean_manytomany_helptext from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings from rest_framework.utils import html, modelinfo, representation @@ -117,8 +118,9 @@ class SerializerMetaclass(type): """ This metaclass sets a dictionary named `base_fields` on the class. - Any fields included as attributes on either the class or it's superclasses - will be include in the `base_fields` dictionary. + Any instances of `Field` included as attributes on either the class + or on any of its superclasses will be include in the + `base_fields` dictionary. """ @classmethod @@ -379,6 +381,10 @@ class ModelSerializer(Serializer): info = modelinfo.get_field_info(self.opts.model) ret = OrderedDict() + serializer_url_field = self.get_url_field() + if serializer_url_field: + ret[api_settings.URL_FIELD_NAME] = serializer_url_field + serializer_pk_field = self.get_pk_field(info.pk) if serializer_pk_field: ret[info.pk.name] = serializer_pk_field @@ -404,6 +410,9 @@ class ModelSerializer(Serializer): return ret + def get_url_field(self): + return None + def get_pk_field(self, model_field): """ Returns a default instance of the pk field. @@ -446,13 +455,14 @@ class ModelSerializer(Serializer): if model_field: if model_field.null or model_field.blank: kwargs['required'] = False - # if model_field.help_text is not None: - # kwargs['help_text'] = model_field.help_text - if model_field.verbose_name is not None: + if model_field.verbose_name: kwargs['label'] = model_field.verbose_name if not model_field.editable: kwargs['read_only'] = True kwargs.pop('queryset', None) + help_text = clean_manytomany_helptext(model_field.help_text) + if help_text: + kwargs['help_text'] = help_text return PrimaryKeyRelatedField(**kwargs) @@ -469,6 +479,9 @@ class ModelSerializer(Serializer): if model_field.verbose_name is not None: kwargs['label'] = model_field.verbose_name + if model_field.help_text: + kwargs['help_text'] = model_field.help_text + if isinstance(model_field, models.AutoField) or not model_field.editable: kwargs['read_only'] = True # Read only implies that the field is not required. @@ -481,6 +494,14 @@ class ModelSerializer(Serializer): # We have a cleaner repr on the instance if we don't set it. kwargs.pop('required', None) + if model_field.flatchoices: + # If this model field contains choices, then use a ChoiceField, + # rather than the standard serializer field for this type. + # Note that we return this prior to setting any validation type + # keyword arguments, as those are not valid initializers. + kwargs['choices'] = model_field.flatchoices + return ChoiceField(**kwargs) + # Ensure that max_length is passed explicitly as a keyword arg, # rather than as a validator. max_length = getattr(model_field, 'max_length', None) @@ -561,23 +582,6 @@ class ModelSerializer(Serializer): if validator_kwarg: kwargs['validators'] = validator_kwarg - # if issubclass(model_field.__class__, models.TextField): - # kwargs['widget'] = widgets.Textarea - - # if model_field.help_text is not None: - # kwargs['help_text'] = model_field.help_text - - # TODO: TypedChoiceField? - if model_field.flatchoices: # This ModelField contains choices - kwargs['choices'] = model_field.flatchoices - if model_field.null: - kwargs['empty'] = None - return ChoiceField(**kwargs) - - if model_field.null and \ - issubclass(model_field.__class__, (models.CharField, models.TextField)): - kwargs['allow_none'] = True - try: return self.field_mapping[model_field.__class__](**kwargs) except KeyError: @@ -597,33 +601,24 @@ class HyperlinkedModelSerializerOptions(ModelSerializerOptions): class HyperlinkedModelSerializer(ModelSerializer): _options_class = HyperlinkedModelSerializerOptions - def get_default_fields(self): - fields = super(HyperlinkedModelSerializer, self).get_default_fields() + def get_url_field(self): + if self.opts.view_name is not None: + view_name = self.opts.view_name + else: + view_name = self.get_default_view_name(self.opts.model) - if self.opts.view_name is None: - self.opts.view_name = self.get_default_view_name(self.opts.model) + kwargs = { + 'view_name': view_name + } + if self.opts.lookup_field: + kwargs['lookup_field'] = self.opts.lookup_field - url_field_name = api_settings.URL_FIELD_NAME - if url_field_name not in fields: - ret = fields.__class__() - ret[url_field_name] = self.get_url_field() - ret.update(fields) - fields = ret - - return fields + return HyperlinkedIdentityField(**kwargs) def get_pk_field(self, model_field): if self.opts.fields and model_field.name in self.opts.fields: return self.get_field(model_field) - def get_url_field(self): - kwargs = { - 'view_name': self.get_default_view_name(self.opts.model) - } - if self.opts.lookup_field: - kwargs['lookup_field'] = self.opts.lookup_field - return HyperlinkedIdentityField(**kwargs) - def get_related_field(self, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a flat relational field. @@ -643,13 +638,14 @@ class HyperlinkedModelSerializer(ModelSerializer): if model_field: if model_field.null or model_field.blank: kwargs['required'] = False - # if model_field.help_text is not None: - # kwargs['help_text'] = model_field.help_text - if model_field.verbose_name is not None: + if model_field.verbose_name: kwargs['label'] = model_field.verbose_name if not model_field.editable: kwargs['read_only'] = True kwargs.pop('queryset', None) + help_text = clean_manytomany_helptext(model_field.help_text) + if help_text: + kwargs['help_text'] = help_text return HyperlinkedRelatedField(**kwargs) From 80ba0473473501968154c5cc5dd5922e53d96a70 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 10 Sep 2014 16:57:22 +0100 Subject: [PATCH 048/100] Compat fixes --- rest_framework/compat.py | 12 +++++++++++- rest_framework/fields.py | 14 +++++++------- rest_framework/serializers.py | 25 +++++++++++++------------ rest_framework/utils/modelinfo.py | 9 +++++---- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 70b38df98..7c05bed9c 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -110,8 +110,18 @@ def get_concrete_model(model_cls): return model_cls +# View._allowed_methods only present from 1.5 onwards +if django.VERSION >= (1, 5): + from django.views.generic import View +else: + from django.views.generic import View as DjangoView + + class View(DjangoView): + def _allowed_methods(self): + return [m.upper() for m in self.http_method_names if hasattr(self, m)] + + # PATCH method is not implemented by Django -from django.views.generic import View if 'patch' not in View.http_method_names: View.http_method_names = View.http_method_names + ['patch'] diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e71dce1df..3ec28908d 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -266,8 +266,8 @@ class BooleanField(Field): default_error_messages = { 'invalid': _('`{input}` is not a valid boolean.') } - TRUE_VALUES = {'t', 'T', 'true', 'True', 'TRUE', '1', 1, True} - FALSE_VALUES = {'f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False} + TRUE_VALUES = set(('t', 'T', 'true', 'True', 'TRUE', '1', 1, True)) + FALSE_VALUES = set(('f', 'F', 'false', 'False', 'FALSE', '0', 0, 0.0, False)) def get_value(self, dictionary): if html.is_html_input(dictionary): @@ -678,16 +678,16 @@ class ChoiceField(Field): for item in choices ] if all(pairs): - self.choices = {key: display_value for key, display_value in choices} + self.choices = dict([(key, display_value) for key, display_value in choices]) else: - self.choices = {item: item for item in choices} + self.choices = dict([(item, item) for item in choices]) # Map the string representation of choices to the underlying value. # Allows us to deal with eg. integer choices while supporting either # integer or string input, but still get the correct datatype out. - self.choice_strings_to_values = { - str(key): key for key in self.choices.keys() - } + self.choice_strings_to_values = dict([ + (str(key), key) for key in self.choices.keys() + ]) super(ChoiceField, self).__init__(**kwargs) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 459f8a8ce..13e579398 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -14,7 +14,8 @@ from django.core import validators from django.core.exceptions import ValidationError from django.db import models from django.utils import six -from collections import namedtuple, OrderedDict +from django.utils.datastructures import SortedDict +from collections import namedtuple from rest_framework.compat import clean_manytomany_helptext from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings @@ -91,10 +92,10 @@ class BaseSerializer(Field): if self.instance is not None: self._data = self.to_primative(self.instance) elif self._initial_data is not None: - self._data = { - field_name: field.get_value(self._initial_data) + self._data = dict([ + (field_name, field.get_value(self._initial_data)) for field_name, field in self.fields.items() - } + ]) else: self._data = self.get_initial() return self._data @@ -137,7 +138,7 @@ class SerializerMetaclass(type): if hasattr(base, 'base_fields'): fields = list(base.base_fields.items()) + fields - return OrderedDict(fields) + return SortedDict(fields) def __new__(cls, name, bases, attrs): attrs['base_fields'] = cls._get_fields(bases, attrs) @@ -180,10 +181,10 @@ class Serializer(BaseSerializer): field.bind(field_name, self, root) def get_initial(self): - return { - field.field_name: field.get_initial() + return dict([ + (field.field_name, field.get_initial()) for field in self.fields.values() - } + ]) def get_value(self, dictionary): # We override the default field access in order to support @@ -222,14 +223,14 @@ class Serializer(BaseSerializer): try: return self.validate(ret) - except ValidationError, exc: + except ValidationError as exc: raise ValidationError({'non_field_errors': exc.messages}) def to_primative(self, instance): """ Object instance -> Dict of primitive datatypes. """ - ret = OrderedDict() + ret = SortedDict() fields = [field for field in self.fields.values() if not field.write_only] for field in fields: @@ -368,7 +369,7 @@ class ModelSerializer(Serializer): # If `fields` is set on the `Meta` class, # then use only those fields, and in that order. if self.opts.fields: - fields = OrderedDict([ + fields = SortedDict([ (key, fields[key]) for key in self.opts.fields ]) @@ -379,7 +380,7 @@ class ModelSerializer(Serializer): Return all the fields that should be serialized for the model. """ info = modelinfo.get_field_info(self.opts.model) - ret = OrderedDict() + ret = SortedDict() serializer_url_field = self.get_url_field() if serializer_url_field: diff --git a/rest_framework/utils/modelinfo.py b/rest_framework/utils/modelinfo.py index c0513886c..a7a0346c2 100644 --- a/rest_framework/utils/modelinfo.py +++ b/rest_framework/utils/modelinfo.py @@ -2,9 +2,10 @@ Helper functions for returning the field information that is associated with a model class. """ -from collections import namedtuple, OrderedDict +from collections import namedtuple from django.db import models from django.utils import six +from django.utils.datastructures import SortedDict import inspect FieldInfo = namedtuple('FieldResult', ['pk', 'fields', 'forward_relations', 'reverse_relations']) @@ -45,12 +46,12 @@ def get_field_info(model): pk = pk.rel.to._meta.pk # Deal with regular fields. - fields = OrderedDict() + fields = SortedDict() for field in [field for field in opts.fields if field.serialize and not field.rel]: fields[field.name] = field # Deal with forward relationships. - forward_relations = OrderedDict() + forward_relations = SortedDict() for field in [field for field in opts.fields if field.serialize and field.rel]: forward_relations[field.name] = RelationInfo( field=field, @@ -71,7 +72,7 @@ def get_field_info(model): ) # Deal with reverse relationships. - reverse_relations = OrderedDict() + reverse_relations = SortedDict() for relation in opts.get_all_related_objects(): accessor_name = relation.get_accessor_name() reverse_relations[accessor_name] = RelationInfo( From e76f56a3d687159327b8bef5bf9bc2ad28c99ea3 Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" <andrew@wq.io> Date: Wed, 10 Sep 2014 11:55:43 -0500 Subject: [PATCH 049/100] add django rest pandas --- docs/api-guide/renderers.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index 7a3429bfd..20eed70d6 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -444,6 +444,11 @@ Comma-separated values are a plain-text tabular data format, that can be easily [djangorestframework-camel-case] provides camel case JSON renderers and parsers for REST framework. This allows serializers to use Python-style underscored field names, but be exposed in the API as Javascript-style camel case field names. It is maintained by [Vitaly Babiy][vbabiy]. +## Pandas (CSV, Excel, PNG) + +[Django REST Pandas] provides a serializer and renderers that support additional data processing and output via the [Pandas] DataFrame API. Django REST Pandas includes renderers for Pandas-style CSV files, Excel workbooks (both `.xls` and `.xlsx`), and a number of [other formats]. It is maintained by [S. Andrew Sheppard][sheppard] as part of the [wq Project][wq]. + + [cite]: https://docs.djangoproject.com/en/dev/ref/template-response/#the-rendering-process [conneg]: content-negotiation.md [browser-accept-headers]: http://www.gethifi.com/blog/browser-rest-http-accept-headers @@ -466,4 +471,9 @@ Comma-separated values are a plain-text tabular data format, that can be easily [ultrajson]: https://github.com/esnme/ultrajson [hzy]: https://github.com/hzy [drf-ujson-renderer]: https://github.com/gizmag/drf-ujson-renderer -[djangorestframework-camel-case]: https://github.com/vbabiy/djangorestframework-camel-case \ No newline at end of file +[djangorestframework-camel-case]: https://github.com/vbabiy/djangorestframework-camel-case +[Django REST Pandas]: https://github.com/wq/django-rest-pandas +[Pandas]: http://pandas.pydata.org/ +[other formats]: https://github.com/wq/django-rest-pandas#supported-formats +[sheppard]: https://github.com/sheppard +[wq]: https://github.com/wq From 37d01f6088b1cf5673f66f4532dd51c73a0156f1 Mon Sep 17 00:00:00 2001 From: Joe Binney <joe@robinhood.com> Date: Wed, 10 Sep 2014 20:27:52 -0700 Subject: [PATCH 050/100] Fix grammar in login error message --- rest_framework/authtoken/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py index 99e99ae3d..472e59ee3 100644 --- a/rest_framework/authtoken/serializers.py +++ b/rest_framework/authtoken/serializers.py @@ -22,7 +22,7 @@ class AuthTokenSerializer(serializers.Serializer): attrs['user'] = user return attrs else: - msg = _('Unable to login with provided credentials.') + msg = _('Unable to log in with provided credentials.') raise serializers.ValidationError(msg) else: msg = _('Must include "username" and "password"') From ae8443853054635326598f6b06fb49429884d558 Mon Sep 17 00:00:00 2001 From: Marek Skrajnowski <marek.skrajnowski@10clouds.com> Date: Thu, 11 Sep 2014 12:42:36 +0200 Subject: [PATCH 051/100] Added DefaultRouter support (and test) for viewsets without the default action implemented, which is usually the list action. --- rest_framework/routers.py | 11 ++++++++++- tests/test_routers.py | 18 +++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 8f1ab6fa7..01fd4b91a 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -19,6 +19,7 @@ import itertools from collections import namedtuple from django.conf.urls import patterns, url from django.core.exceptions import ImproperlyConfigured +from django.core.urlresolvers import NoReverseMatch from rest_framework import views from rest_framework.response import Response from rest_framework.reverse import reverse @@ -287,7 +288,15 @@ class DefaultRouter(SimpleRouter): def get(self, request, *args, **kwargs): ret = {} for key, url_name in api_root_dict.items(): - ret[key] = reverse(url_name, request=request, format=kwargs.get('format', None)) + try: + ret[key] = reverse( + url_name, + request=request, + format=kwargs.get('format', None) + ) + except NoReverseMatch: + continue + return Response(ret) return APIRoot.as_view() diff --git a/tests/test_routers.py b/tests/test_routers.py index b076f134e..f6f5a977a 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -3,7 +3,7 @@ from django.conf.urls import patterns, url, include from django.db import models from django.test import TestCase from django.core.exceptions import ImproperlyConfigured -from rest_framework import serializers, viewsets, permissions +from rest_framework import serializers, viewsets, mixins, permissions from rest_framework.decorators import detail_route, list_route from rest_framework.response import Response from rest_framework.routers import SimpleRouter, DefaultRouter @@ -284,3 +284,19 @@ class TestDynamicListAndDetailRouter(TestCase): else: method_map = 'get' self.assertEqual(route.mapping[method_map], endpoint) + + +class TestRootWithAListlessViewset(TestCase): + def setUp(self): + class NoteViewSet(mixins.RetrieveModelMixin, + viewsets.GenericViewSet): + model = RouterTestModel + + self.router = DefaultRouter() + self.router.register(r'notes', NoteViewSet) + self.view = self.router.urls[0].callback + + def test_api_root(self): + request = factory.get('/') + response = self.view(request) + self.assertEqual(response.data, {}) From 54ccf7230d0fcdabe8c2457539e314893915a34b Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 13:43:46 +0100 Subject: [PATCH 052/100] Improve memory address removal for serializer representations --- rest_framework/utils/representation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 1de21597d..e2a374979 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -17,7 +17,7 @@ def smart_repr(value): # <django.core.validators.RegexValidator object at 0x1047af050> # Should be presented as # <django.core.validators.RegexValidator object> - value = re.sub(' at 0x[0-9a-f]{8,10}>', '>', value) + value = re.sub(' at 0x[0-9a-f]{8,32}>', '>', value) return value From 3318f75a7166cbac76a40d0461ca7b3e4640d3a2 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 13:50:53 +0100 Subject: [PATCH 053/100] Improve memory address removal for serializer representations --- rest_framework/utils/representation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index e2a374979..1a4d1a62c 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -17,7 +17,7 @@ def smart_repr(value): # <django.core.validators.RegexValidator object at 0x1047af050> # Should be presented as # <django.core.validators.RegexValidator object> - value = re.sub(' at 0x[0-9a-f]{8,32}>', '>', value) + value = re.sub(' at 0x[0-9a-f]{4,32}>', '>', value) return value From ab40780dc2f341a271c2f489659dcd48eb47c07d Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 20:22:32 +0100 Subject: [PATCH 054/100] Tidy up lookup_class --- rest_framework/serializers.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 8fe999aec..4322f2134 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -317,17 +317,17 @@ class ModelSerializerOptions(object): self.depth = getattr(meta, 'depth', 0) -def lookup_class(mapping, obj): +def lookup_class(mapping, instance): """ Takes a dictionary with classes as keys, and an object. Traverses the object's inheritance hierarchy in method resolution order, and returns the first matching value - from the dictionary or None. + from the dictionary or raises a KeyError if nothing matches. """ - return next( - (mapping[cls] for cls in inspect.getmro(obj.__class__) if cls in mapping), - None - ) + for cls in inspect.getmro(instance.__class__): + if cls in mapping: + return mapping[cls] + raise KeyError('Class %s not found in lookup.', cls.__name__) class ModelSerializer(Serializer): @@ -341,6 +341,7 @@ class ModelSerializer(Serializer): models.DateTimeField: DateTimeField, models.DecimalField: DecimalField, models.EmailField: EmailField, + models.Field: ModelField, models.FileField: FileField, models.FloatField: FloatField, models.ImageField: ImageField, @@ -484,6 +485,7 @@ class ModelSerializer(Serializer): """ Creates a default instance of a basic non-relational field. """ + serializer_cls = lookup_class(self.field_mapping, model_field) kwargs = {} validator_kwarg = model_field.validators @@ -602,11 +604,10 @@ class ModelSerializer(Serializer): if validator_kwarg: kwargs['validators'] = validator_kwarg - cls = lookup_class(self.field_mapping, model_field) - if cls is None: - cls = ModelField + if issubclass(serializer_cls, ModelField): kwargs['model_field'] = model_field - return cls(**kwargs) + + return serializer_cls(**kwargs) class HyperlinkedModelSerializerOptions(ModelSerializerOptions): From bf52d04f4c370d6917599d26c84b73124d5ef366 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 20:37:27 +0100 Subject: [PATCH 055/100] Nice manager representations on serializer classes --- rest_framework/utils/representation.py | 13 +++++++++++++ tests/test_model_field_mappings.py | 12 ++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 1a4d1a62c..71db18863 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -2,10 +2,23 @@ Helper functions for creating user-friendly representations of serializer classes and serializer fields. """ +from django.db import models import re +def manager_repr(value): + model = value.model + opts = model._meta + for _, name, manager in opts.concrete_managers + opts.abstract_managers: + if manager == value: + return '%s.%s.all()' % (model._meta.object_name, name) + return repr(value) + + def smart_repr(value): + if isinstance(value, models.Manager): + return manager_repr(value) + value = repr(value) # Representations like u'help text' diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index 57c97cb7d..6e9aaba6c 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -81,9 +81,9 @@ class RelationalModel(models.Model): RELATIONAL_FLAT_REPR = """ TestSerializer(): id = IntegerField(label='ID', read_only=True) - foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>) - one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>) - many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=<django.db.models.manager.Manager object>) + foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all()) + one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all()) + many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all()) """.strip() @@ -105,9 +105,9 @@ TestSerializer(): HYPERLINKED_FLAT_REPR = """ TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=<django.db.models.manager.Manager object>, view_name='foreignkeytargetmodel-detail') - one_to_one = HyperlinkedRelatedField(label='one to one', queryset=<django.db.models.manager.Manager object>, view_name='onetoonetargetmodel-detail') - many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=<django.db.models.manager.Manager object>, view_name='manytomanytargetmodel-detail') + foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') + one_to_one = HyperlinkedRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') + many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') """.strip() From 19b8f779de82fa4737b37fb4359145af0b07a56c Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 20:43:44 +0100 Subject: [PATCH 056/100] Throttles now use Retry-After header and no longer support the custom style --- docs/api-guide/throttling.md | 2 ++ rest_framework/views.py | 1 - tests/test_throttling.py | 6 +----- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md index 832304f15..16a7457b0 100644 --- a/docs/api-guide/throttling.md +++ b/docs/api-guide/throttling.md @@ -178,6 +178,8 @@ To create a custom throttle, override `BaseThrottle` and implement `.allow_reque Optionally you may also override the `.wait()` method. If implemented, `.wait()` should return a recommended number of seconds to wait before attempting the next request, or `None`. The `.wait()` method will only be called if `.allow_request()` has previously returned `False`. +If the `.wait()` method is implemented and the request is throttled, then a `Retry-After` header will be included in the response. + ## Example The following is an example of a rate throttle, that will randomly throttle 1 in every 10 requests. diff --git a/rest_framework/views.py b/rest_framework/views.py index 3b7b1c16c..cd394b2d9 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -62,7 +62,6 @@ def exception_handler(exc): if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): - headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait headers['Retry-After'] = '%d' % exc.wait return Response({'detail': exc.detail}, diff --git a/tests/test_throttling.py b/tests/test_throttling.py index 7b696f070..cc36a004c 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -109,7 +109,7 @@ class ThrottlingTests(TestCase): def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """ - Ensure the response returns an X-Throttle field with status and next attributes + Ensure the response returns an Retry-After field with status and next attributes set properly. """ request = self.factory.get('/') @@ -117,10 +117,8 @@ class ThrottlingTests(TestCase): self.set_throttle_timer(view, timer) response = view.as_view()(request) if expect is not None: - self.assertEqual(response['X-Throttle-Wait-Seconds'], expect) self.assertEqual(response['Retry-After'], expect) else: - self.assertFalse('X-Throttle-Wait-Seconds' in response) self.assertFalse('Retry-After' in response) def test_seconds_fields(self): @@ -173,13 +171,11 @@ class ThrottlingTests(TestCase): self.assertFalse(hasattr(MockView_NonTimeThrottling.throttle_classes[0], 'called')) response = MockView_NonTimeThrottling.as_view()(request) - self.assertFalse('X-Throttle-Wait-Seconds' in response) self.assertFalse('Retry-After' in response) self.assertTrue(MockView_NonTimeThrottling.throttle_classes[0].called) response = MockView_NonTimeThrottling.as_view()(request) - self.assertFalse('X-Throttle-Wait-Seconds' in response) self.assertFalse('Retry-After' in response) From 55650a743d579e0bc1643c8812428746b0271984 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 20:49:10 +0100 Subject: [PATCH 057/100] no longer tightly coupled to private queryset API --- rest_framework/generics.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index c2c59154a..408b12464 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -3,6 +3,7 @@ Generic views that provide commonly needed behaviour. """ from __future__ import unicode_literals +from django.db.models.query import QuerySet from django.core.exceptions import PermissionDenied from django.core.paginator import Paginator, InvalidPage from django.http import Http404 @@ -214,7 +215,9 @@ class GenericAPIView(views.APIView): % self.__class__.__name__ ) - return self.queryset._clone() + if isinstance(self.queryset, QuerySet): + return self.queryset.all() + return self.queryset def get_object(self): """ From a7518719917c7ad8e699119b442cfeb568ba1dde Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 20:50:26 +0100 Subject: [PATCH 058/100] no longer tightly coupled to private queryset API --- rest_framework/generics.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 408b12464..338d56a6a 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -215,9 +215,11 @@ class GenericAPIView(views.APIView): % self.__class__.__name__ ) + queryset = self.queryset if isinstance(self.queryset, QuerySet): - return self.queryset.all() - return self.queryset + # Ensure queryset is re-evaluated on each request. + queryset = queryset.all() + return queryset def get_object(self): """ From 040bfcc09c851bb3dadd60558c78a1f7937e9fbd Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 21:48:54 +0100 Subject: [PATCH 059/100] NotImplemented stubs for Field, and DecimalField improvements --- rest_framework/fields.py | 27 +++++++++++++++++++++------ rest_framework/pagination.py | 4 ++-- rest_framework/utils/encoders.py | 2 +- tests/test_filters.py | 10 +++++----- tests/test_pagination.py | 4 ++-- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 7496a629e..20b8ffbff 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -229,13 +229,13 @@ class Field(object): """ Transform the *incoming* primative data into a native value. """ - return data + raise NotImplementedError('to_native() must be implemented.') def to_primative(self, value): """ Transform the *outgoing* native value into primative data. """ - return value + raise NotImplementedError('to_primative() must be implemented.') def fail(self, key, **kwargs): """ @@ -429,9 +429,10 @@ class DecimalField(Field): 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.') } - def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, **kwargs): - self.max_value, self.min_value = max_value, min_value - self.max_digits, self.max_decimal_places = max_digits, decimal_places + def __init__(self, max_digits, decimal_places, coerce_to_string=True, max_value=None, min_value=None, **kwargs): + self.max_digits = max_digits + self.decimal_places = decimal_places + self.coerce_to_string = coerce_to_string super(DecimalField, self).__init__(**kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) @@ -478,12 +479,26 @@ class DecimalField(Field): if self.max_digits is not None and digits > self.max_digits: self.fail('max_digits', max_digits=self.max_digits) if self.decimal_places is not None and decimals > self.decimal_places: - self.fail('max_decimal_places', max_decimal_places=self.max_decimal_places) + self.fail('max_decimal_places', max_decimal_places=self.decimal_places) if self.max_digits is not None and self.decimal_places is not None and whole_digits > (self.max_digits - self.decimal_places): self.fail('max_whole_digits', max_while_digits=self.max_digits - self.decimal_places) return value + def to_primative(self, value): + if not self.coerce_to_string: + return value + + if isinstance(value, decimal.Decimal): + context = decimal.getcontext().copy() + context.prec = self.max_digits + quantized = value.quantize( + decimal.Decimal('.1') ** self.decimal_places, + context=context + ) + return '{0:f}'.format(quantized) + return '%.*f' % (self.max_decimal_places, value) + # Date & time fields... diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 9cf31629f..d82d2d3b3 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -37,7 +37,7 @@ class PreviousPageField(serializers.Field): return replace_query_param(url, self.page_field, page) -class DefaultObjectSerializer(serializers.Field): +class DefaultObjectSerializer(serializers.ReadOnlyField): """ If no object serializer is specified, then this serializer will be applied as the default. @@ -79,6 +79,6 @@ class PaginationSerializer(BasePaginationSerializer): """ A default implementation of a pagination serializer. """ - count = serializers.Field(source='paginator.count') + count = serializers.ReadOnlyField(source='paginator.count') next = NextPageField(source='*') previous = PreviousPageField(source='*') diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 6a2f61266..7992b6b1c 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -43,7 +43,7 @@ class JSONEncoder(json.JSONEncoder): elif isinstance(o, datetime.timedelta): return str(o.total_seconds()) elif isinstance(o, decimal.Decimal): - return str(o) + return float(o) elif isinstance(o, QuerySet): return list(o) elif hasattr(o, 'tolist'): diff --git a/tests/test_filters.py b/tests/test_filters.py index 300e47e45..01668114b 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -102,7 +102,7 @@ if django_filters: class CommonFilteringTestCase(TestCase): def _serialize_object(self, obj): - return {'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date} + return {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date} def setUp(self): """ @@ -145,7 +145,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): request = factory.get('/', {'decimal': '%s' % search_decimal}) response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['decimal'] == search_decimal] + expected_data = [f for f in self.data if Decimal(f['decimal']) == search_decimal] self.assertEqual(response.data, expected_data) # Tests that the date filter works. @@ -168,7 +168,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): request = factory.get('/', {'decimal': '%s' % search_decimal}) response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['decimal'] == search_decimal] + expected_data = [f for f in self.data if Decimal(f['decimal']) == search_decimal] self.assertEqual(response.data, expected_data) @unittest.skipUnless(django_filters, 'django-filter not installed') @@ -201,7 +201,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): request = factory.get('/', {'decimal': '%s' % search_decimal}) response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['decimal'] < search_decimal] + expected_data = [f for f in self.data if Decimal(f['decimal']) < search_decimal] self.assertEqual(response.data, expected_data) # Tests that the date filter set with 'gt' in the filter class works. @@ -230,7 +230,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) expected_data = [f for f in self.data if f['date'] > search_date and - f['decimal'] < search_decimal] + Decimal(f['decimal']) < search_decimal] self.assertEqual(response.data, expected_data) @unittest.skipUnless(django_filters, 'django-filter not installed') diff --git a/tests/test_pagination.py b/tests/test_pagination.py index 68983ba20..a7f8e691f 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -135,7 +135,7 @@ class IntegrationTestPaginationAndFiltering(TestCase): self.objects = FilterableItem.objects self.data = [ - {'id': obj.id, 'text': obj.text, 'decimal': obj.decimal, 'date': obj.date} + {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date} for obj in self.objects.all() ] @@ -381,7 +381,7 @@ class TestMaxPaginateByParam(TestCase): # Tests for context in pagination serializers -class CustomField(serializers.Field): +class CustomField(serializers.ReadOnlyField): def to_native(self, value): if 'view' not in self.context: raise RuntimeError("context isn't getting passed into custom field") From 1e53eb0aa2998385e26aa0a4d8542013bc7b575b Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 11 Sep 2014 21:57:32 +0100 Subject: [PATCH 060/100] DecimalFields should still be quantized even without coerce_to_string --- rest_framework/fields.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 20b8ffbff..a56ea96b0 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -486,9 +486,6 @@ class DecimalField(Field): return value def to_primative(self, value): - if not self.coerce_to_string: - return value - if isinstance(value, decimal.Decimal): context = decimal.getcontext().copy() context.prec = self.max_digits @@ -496,7 +493,12 @@ class DecimalField(Field): decimal.Decimal('.1') ** self.decimal_places, context=context ) + if not self.coerce_to_string: + return quantized return '{0:f}'.format(quantized) + + if not self.coerce_to_string: + return value return '%.*f' % (self.max_decimal_places, value) From adcb64ab4198f35c61d5be68956d201685ed3538 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 09:12:56 +0100 Subject: [PATCH 061/100] MethodField -> SerializerMethodField --- rest_framework/fields.py | 21 +++++++++------------ rest_framework/serializers.py | 3 ++- rest_framework/utils/modelinfo.py | 3 ++- tests/test_serializer_empty.py | 2 +- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a56ea96b0..5f198767c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -768,16 +768,13 @@ class ReadOnlyField(Field): kwargs['read_only'] = True super(ReadOnlyField, self).__init__(**kwargs) - def to_native(self, data): - raise NotImplemented('.to_native() not supported.') - def to_primative(self, value): if is_simple_callable(value): return value() return value -class MethodField(Field): +class SerializerMethodField(Field): """ A read-only field that get its representation from calling a method on the parent serializer class. The method called will be of the form @@ -787,22 +784,22 @@ class MethodField(Field): For example: class ExampleSerializer(self): - extra_info = MethodField() + extra_info = SerializerMethodField() def get_extra_info(self, obj): return ... # Calculate some data to return. """ - def __init__(self, **kwargs): + def __init__(self, method_attr=None, **kwargs): + self.method_attr = method_attr kwargs['source'] = '*' kwargs['read_only'] = True - super(MethodField, self).__init__(**kwargs) - - def to_native(self, data): - raise NotImplemented('.to_native() not supported.') + super(SerializerMethodField, self).__init__(**kwargs) def to_primative(self, value): - attr = 'get_{field_name}'.format(field_name=self.field_name) - method = getattr(self.parent, attr) + method_attr = self.method_attr + if method_attr is None: + method_attr = 'get_{field_name}'.format(field_name=self.field_name) + method = getattr(self.parent, method_attr) return method(value) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 4322f2134..388fe29f0 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -598,7 +598,8 @@ class ModelSerializer(Serializer): if isinstance(model_field, models.BooleanField): # models.BooleanField has `blank=True`, but *is* actually # required *unless* a default is provided. - # Also note that <1.6 `default=False`, >=1.6 `default=None`. + # Also note that Django<1.6 uses `default=False` for + # models.BooleanField, but Django>=1.6 uses `default=None`. kwargs.pop('required', None) if validator_kwarg: diff --git a/rest_framework/utils/modelinfo.py b/rest_framework/utils/modelinfo.py index a7a0346c2..960fa4d0f 100644 --- a/rest_framework/utils/modelinfo.py +++ b/rest_framework/utils/modelinfo.py @@ -1,6 +1,7 @@ """ Helper functions for returning the field information that is associated -with a model class. +with a model class. This includes returning all the forward and reverse +relationships and their associated metadata. """ from collections import namedtuple from django.db import models diff --git a/tests/test_serializer_empty.py b/tests/test_serializer_empty.py index 4e4a7b423..805ac7d46 100644 --- a/tests/test_serializer_empty.py +++ b/tests/test_serializer_empty.py @@ -6,7 +6,7 @@ # def test_empty_serializer(self): # class FooBarSerializer(serializers.Serializer): # foo = serializers.IntegerField() -# bar = serializers.MethodField() +# bar = serializers.SerializerMethodField() # def get_bar(self, obj): # return 'bar' From 0d354e8f92c7daaf8dac3b80f0fd64f983f21e0b Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 09:49:35 +0100 Subject: [PATCH 062/100] to_internal_value() and to_representation() --- rest_framework/fields.py | 86 ++++++++++++++++++----------------- rest_framework/pagination.py | 4 +- rest_framework/relations.py | 2 +- rest_framework/serializers.py | 28 ++++++------ tests/test_validation.py | 2 +- 5 files changed, 62 insertions(+), 60 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 5f198767c..a96f9ba89 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -195,7 +195,7 @@ class Field(object): raise SkipField() return self.default - def validate_value(self, data=empty): + def run_validation(self, data=empty): """ Validate a simple representation and return the internal value. @@ -208,7 +208,7 @@ class Field(object): self.fail('required') return self.get_default() - value = self.to_native(data) + value = self.to_internal_value(data) self.run_validators(value) return value @@ -225,17 +225,17 @@ class Field(object): if errors: raise ValidationError(errors) - def to_native(self, data): + def to_internal_value(self, data): """ Transform the *incoming* primative data into a native value. """ - raise NotImplementedError('to_native() must be implemented.') + raise NotImplementedError('to_internal_value() must be implemented.') - def to_primative(self, value): + def to_representation(self, value): """ Transform the *outgoing* native value into primative data. """ - raise NotImplementedError('to_primative() must be implemented.') + raise NotImplementedError('to_representation() must be implemented.') def fail(self, key, **kwargs): """ @@ -279,14 +279,14 @@ class BooleanField(Field): return dictionary.get(self.field_name, False) return dictionary.get(self.field_name, empty) - def to_native(self, data): + def to_internal_value(self, data): if data in self.TRUE_VALUES: return True elif data in self.FALSE_VALUES: return False self.fail('invalid', input=data) - def to_primative(self, value): + def to_representation(self, value): if value is None: return None if value in self.TRUE_VALUES: @@ -309,12 +309,14 @@ class CharField(Field): self.min_length = kwargs.pop('min_length', None) super(CharField, self).__init__(**kwargs) - def to_native(self, data): + def to_internal_value(self, data): if data == '' and not self.allow_blank: self.fail('blank') + if data is None: + return None return str(data) - def to_primative(self, value): + def to_representation(self, value): if value is None: return None return str(value) @@ -326,17 +328,17 @@ class EmailField(CharField): } default_validators = [validators.validate_email] - def to_native(self, data): - ret = super(EmailField, self).to_native(data) - if ret is None: + def to_internal_value(self, data): + if data == '' and not self.allow_blank: + self.fail('blank') + if data is None: return None - return ret.strip() + return str(data).strip() - def to_primative(self, value): - ret = super(EmailField, self).to_primative(value) - if ret is None: + def to_representation(self, value): + if value is None: return None - return ret.strip() + return str(value).strip() class RegexField(CharField): @@ -378,14 +380,14 @@ class IntegerField(Field): if min_value is not None: self.validators.append(validators.MinValueValidator(min_value)) - def to_native(self, data): + def to_internal_value(self, data): try: data = int(str(data)) except (ValueError, TypeError): self.fail('invalid') return data - def to_primative(self, value): + def to_representation(self, value): if value is None: return None return int(value) @@ -405,7 +407,12 @@ class FloatField(Field): if min_value is not None: self.validators.append(validators.MinValueValidator(min_value)) - def to_primative(self, value): + def to_internal_value(self, value): + if value is None: + return None + return float(value) + + def to_representation(self, value): if value is None: return None try: @@ -413,11 +420,6 @@ class FloatField(Field): except (TypeError, ValueError): self.fail('invalid', value=value) - def to_native(self, value): - if value is None: - return None - return float(value) - class DecimalField(Field): default_error_messages = { @@ -439,7 +441,7 @@ class DecimalField(Field): if min_value is not None: self.validators.append(validators.MinValueValidator(min_value)) - def from_native(self, value): + def to_internal_value(self, value): """ Validates that the input is a decimal number. Returns a Decimal instance. Returns None for empty values. Ensures that there are no more @@ -485,7 +487,7 @@ class DecimalField(Field): return value - def to_primative(self, value): + def to_representation(self, value): if isinstance(value, decimal.Decimal): context = decimal.getcontext().copy() context.prec = self.max_digits @@ -516,7 +518,7 @@ class DateField(Field): self.format = format if format is not None else self.format super(DateField, self).__init__(*args, **kwargs) - def from_native(self, value): + def to_internal_value(self, value): if value in validators.EMPTY_VALUES: return None @@ -552,7 +554,7 @@ class DateField(Field): msg = self.error_messages['invalid'] % humanized_format raise ValidationError(msg) - def to_primative(self, value): + def to_representation(self, value): if value is None or self.format is None: return value @@ -576,7 +578,7 @@ class DateTimeField(Field): self.format = format if format is not None else self.format super(DateTimeField, self).__init__(*args, **kwargs) - def from_native(self, value): + def to_internal_value(self, value): if value in validators.EMPTY_VALUES: return None @@ -618,7 +620,7 @@ class DateTimeField(Field): msg = self.error_messages['invalid'] % humanized_format raise ValidationError(msg) - def to_primative(self, value): + def to_representation(self, value): if value is None or self.format is None: return value @@ -670,7 +672,7 @@ class TimeField(Field): msg = self.error_messages['invalid'] % humanized_format raise ValidationError(msg) - def to_primative(self, value): + def to_representation(self, value): if value is None or self.format is None: return value @@ -711,13 +713,13 @@ class ChoiceField(Field): super(ChoiceField, self).__init__(**kwargs) - def to_native(self, data): + def to_internal_value(self, data): try: return self.choice_strings_to_values[str(data)] except KeyError: self.fail('invalid_choice', input=data) - def to_primative(self, value): + def to_representation(self, value): return value @@ -727,15 +729,15 @@ class MultipleChoiceField(ChoiceField): 'not_a_list': _('Expected a list of items but got type `{input_type}`') } - def to_native(self, data): + def to_internal_value(self, data): if not hasattr(data, '__iter__'): self.fail('not_a_list', input_type=type(data).__name__) return set([ - super(MultipleChoiceField, self).to_native(item) + super(MultipleChoiceField, self).to_internal_value(item) for item in data ]) - def to_primative(self, value): + def to_representation(self, value): return value @@ -768,7 +770,7 @@ class ReadOnlyField(Field): kwargs['read_only'] = True super(ReadOnlyField, self).__init__(**kwargs) - def to_primative(self, value): + def to_representation(self, value): if is_simple_callable(value): return value() return value @@ -795,7 +797,7 @@ class SerializerMethodField(Field): kwargs['read_only'] = True super(SerializerMethodField, self).__init__(**kwargs) - def to_primative(self, value): + def to_representation(self, value): method_attr = self.method_attr if method_attr is None: method_attr = 'get_{field_name}'.format(field_name=self.field_name) @@ -815,13 +817,13 @@ class ModelField(Field): kwargs['source'] = '*' super(ModelField, self).__init__(**kwargs) - def to_native(self, data): + def to_internal_value(self, data): rel = getattr(self.model_field, 'rel', None) if rel is not None: return rel.to._meta.get_field(rel.field_name).to_python(data) return self.model_field.to_python(data) - def to_primative(self, obj): + def to_representation(self, obj): value = self.model_field._get_val_from_obj(obj) if is_protected_type(value): return value diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index d82d2d3b3..c5a9270a4 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -13,7 +13,7 @@ class NextPageField(serializers.Field): """ page_field = 'page' - def to_primative(self, value): + def to_representation(self, value): if not value.has_next(): return None page = value.next_page_number() @@ -28,7 +28,7 @@ class PreviousPageField(serializers.Field): """ page_field = 'page' - def to_primative(self, value): + def to_representation(self, value): if not value.has_previous(): return None page = value.previous_page_number() diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 661a1249a..30a252db9 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -110,7 +110,7 @@ class HyperlinkedIdentityField(RelatedField): def get_attribute(self, instance): return instance - def to_primative(self, value): + def to_representation(self, value): request = self.context.get('request', None) format = self.context.get('format', None) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 388fe29f0..502b1e190 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -47,11 +47,11 @@ class BaseSerializer(Field): self.instance = instance self._initial_data = data - def to_native(self, data): - raise NotImplementedError('`to_native()` must be implemented.') + def to_internal_value(self, data): + raise NotImplementedError('`to_internal_value()` must be implemented.') - def to_primative(self, instance): - raise NotImplementedError('`to_primative()` must be implemented.') + def to_representation(self, instance): + raise NotImplementedError('`to_representation()` must be implemented.') def update(self, instance, attrs): raise NotImplementedError('`update()` must be implemented.') @@ -74,7 +74,7 @@ class BaseSerializer(Field): def is_valid(self, raise_exception=False): if not hasattr(self, '_validated_data'): try: - self._validated_data = self.to_native(self._initial_data) + self._validated_data = self.to_internal_value(self._initial_data) except ValidationError as exc: self._validated_data = {} self._errors = exc.message_dict @@ -90,7 +90,7 @@ class BaseSerializer(Field): def data(self): if not hasattr(self, '_data'): if self.instance is not None: - self._data = self.to_primative(self.instance) + self._data = self.to_representation(self.instance) elif self._initial_data is not None: self._data = dict([ (field_name, field.get_value(self._initial_data)) @@ -193,7 +193,7 @@ class Serializer(BaseSerializer): return html.parse_html_dict(dictionary, prefix=self.field_name) return dictionary.get(self.field_name, empty) - def to_native(self, data): + def to_internal_value(self, data): """ Dict of native values <- Dict of primitive datatypes. """ @@ -208,7 +208,7 @@ class Serializer(BaseSerializer): validate_method = getattr(self, 'validate_' + field.field_name, None) primitive_value = field.get_value(data) try: - validated_value = field.validate_value(primitive_value) + validated_value = field.run_validation(primitive_value) if validate_method is not None: validated_value = validate_method(validated_value) except ValidationError as exc: @@ -226,7 +226,7 @@ class Serializer(BaseSerializer): except ValidationError as exc: raise ValidationError({'non_field_errors': exc.messages}) - def to_primative(self, instance): + def to_representation(self, instance): """ Object instance -> Dict of primitive datatypes. """ @@ -235,7 +235,7 @@ class Serializer(BaseSerializer): for field in fields: native_value = field.get_attribute(instance) - ret[field.field_name] = field.to_primative(native_value) + ret[field.field_name] = field.to_representation(native_value) return ret @@ -279,20 +279,20 @@ class ListSerializer(BaseSerializer): return html.parse_html_list(dictionary, prefix=self.field_name) return dictionary.get(self.field_name, empty) - def to_native(self, data): + def to_internal_value(self, data): """ List of dicts of native values <- List of dicts of primitive datatypes. """ if html.is_html_input(data): data = html.parse_html_list(data) - return [self.child.validate(item) for item in data] + return [self.child.run_validation(item) for item in data] - def to_primative(self, data): + def to_representation(self, data): """ List of object instances -> List of dicts of primitive datatypes. """ - return [self.child.to_primative(item) for item in data] + return [self.child.to_representation(item) for item in data] def create(self, attrs_list): return [self.child.create(attrs) for attrs in attrs_list] diff --git a/tests/test_validation.py b/tests/test_validation.py index c4506e7e9..7543d849b 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -158,7 +158,7 @@ class TestChoiceFieldChoicesValidate(TestCase): f = serializers.ChoiceField(choices=self.CHOICES) value = self.CHOICES[0][0] try: - f.to_native(value) + f.to_internal_value(value) except ValidationError: self.fail("Value %s does not validate" % str(value)) From 6db3356c4d1aa4f9a042b0ec67d47238abc16dd7 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 10:21:35 +0100 Subject: [PATCH 063/100] NON_FIELD_ERRORS_KEY setting --- rest_framework/serializers.py | 8 ++++++-- rest_framework/settings.py | 1 + rest_framework/views.py | 8 +++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 502b1e190..0c2aedfa0 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -198,7 +198,9 @@ class Serializer(BaseSerializer): Dict of native values <- Dict of primitive datatypes. """ if not isinstance(data, dict): - raise ValidationError({'non_field_errors': ['Invalid data']}) + raise ValidationError({ + api_settings.NON_FIELD_ERRORS_KEY: ['Invalid data'] + }) ret = {} errors = {} @@ -224,7 +226,9 @@ class Serializer(BaseSerializer): try: return self.validate(ret) except ValidationError as exc: - raise ValidationError({'non_field_errors': exc.messages}) + raise ValidationError({ + api_settings.NON_FIELD_ERRORS_KEY: exc.messages + }) def to_representation(self, instance): """ diff --git a/rest_framework/settings.py b/rest_framework/settings.py index bbe7a56ad..f48643b5d 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -77,6 +77,7 @@ DEFAULTS = { # Exception handling 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler', + 'NON_FIELD_ERRORS_KEY': 'non_field_errors', # Testing 'TEST_REQUEST_RENDERER_CLASSES': ( diff --git a/rest_framework/views.py b/rest_framework/views.py index cd394b2d9..9f08a4ad5 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -3,7 +3,7 @@ Provides an APIView class that is the base of all views in REST framework. """ from __future__ import unicode_literals -from django.core.exceptions import PermissionDenied, ValidationError +from django.core.exceptions import PermissionDenied, ValidationError, NON_FIELD_ERRORS from django.http import Http404 from django.utils.datastructures import SortedDict from django.views.decorators.csrf import csrf_exempt @@ -69,6 +69,12 @@ def exception_handler(exc): headers=headers) elif isinstance(exc, ValidationError): + # ValidationErrors may include the non-field key named '__all__'. + # When returning a response we map this to a key name that can be + # modified in settings. + if NON_FIELD_ERRORS in exc.message_dict: + errors = exc.message_dict.pop(NON_FIELD_ERRORS) + exc.message_dict[api_settings.NON_FIELD_ERRORS_KEY] = errors return Response(exc.message_dict, status=status.HTTP_400_BAD_REQUEST) From 250755def707e1397876614fa0c08130d9fcc449 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 10:59:51 +0100 Subject: [PATCH 064/100] Clean up relational fields queryset usage --- rest_framework/fields.py | 15 +++----- rest_framework/generics.py | 2 +- rest_framework/relations.py | 73 ++++++++++++++++++++----------------- tests/test_generics.py | 4 +- 4 files changed, 49 insertions(+), 45 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index a96f9ba89..4f06d1868 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -508,7 +508,7 @@ class DecimalField(Field): class DateField(Field): default_error_messages = { - 'invalid': _("Date has wrong format. Use one of these formats instead: %s"), + 'invalid': _('Date has wrong format. Use one of these formats instead: {format}'), } input_formats = api_settings.DATE_INPUT_FORMATS format = api_settings.DATE_FORMAT @@ -551,8 +551,7 @@ class DateField(Field): return parsed.date() humanized_format = humanize_datetime.date_formats(self.input_formats) - msg = self.error_messages['invalid'] % humanized_format - raise ValidationError(msg) + self.fail('invalid', format=humanized_format) def to_representation(self, value): if value is None or self.format is None: @@ -568,7 +567,7 @@ class DateField(Field): class DateTimeField(Field): default_error_messages = { - 'invalid': _("Datetime has wrong format. Use one of these formats instead: %s"), + 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}'), } input_formats = api_settings.DATETIME_INPUT_FORMATS format = api_settings.DATETIME_FORMAT @@ -617,8 +616,7 @@ class DateTimeField(Field): return parsed humanized_format = humanize_datetime.datetime_formats(self.input_formats) - msg = self.error_messages['invalid'] % humanized_format - raise ValidationError(msg) + self.fail('invalid', format=humanized_format) def to_representation(self, value): if value is None or self.format is None: @@ -634,7 +632,7 @@ class DateTimeField(Field): class TimeField(Field): default_error_messages = { - 'invalid': _("Time has wrong format. Use one of these formats instead: %s"), + 'invalid': _('Time has wrong format. Use one of these formats instead: {format}'), } input_formats = api_settings.TIME_INPUT_FORMATS format = api_settings.TIME_FORMAT @@ -669,8 +667,7 @@ class TimeField(Field): return parsed.time() humanized_format = humanize_datetime.time_formats(self.input_formats) - msg = self.error_messages['invalid'] % humanized_format - raise ValidationError(msg) + self.fail('invalid', format=humanized_format) def to_representation(self, value): if value is None or self.format is None: diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 338d56a6a..eb6b64efa 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -216,7 +216,7 @@ class GenericAPIView(views.APIView): ) queryset = self.queryset - if isinstance(self.queryset, QuerySet): + if isinstance(queryset, QuerySet): # Ensure queryset is re-evaluated on each request. queryset = queryset.all() return queryset diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 30a252db9..e23a41526 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -2,28 +2,35 @@ from rest_framework.fields import Field from rest_framework.reverse import reverse from django.core.exceptions import ObjectDoesNotExist from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch +from django.db.models.query import QuerySet from rest_framework.compat import urlparse -def get_default_queryset(serializer_class, field_name): - manager = getattr(serializer_class.opts.model, field_name) - if hasattr(manager, 'related'): - # Forward relationships - return manager.related.model._default_manager.all() - # Reverse relationships - return manager.field.rel.to._default_manager.all() - - class RelatedField(Field): def __init__(self, **kwargs): self.queryset = kwargs.pop('queryset', None) self.many = kwargs.pop('many', False) + assert self.queryset is not None or kwargs.get('read_only', False), ( + 'Relational field must provide a `queryset` argument, ' + 'or set read_only=`True`.' + ) super(RelatedField, self).__init__(**kwargs) - def bind(self, field_name, parent, root): - super(RelatedField, self).bind(field_name, parent, root) - if self.queryset is None and not self.read_only: - self.queryset = get_default_queryset(parent, self.source) + def get_queryset(self): + queryset = self.queryset + if isinstance(queryset, QuerySet): + # Ensure queryset is re-evaluated whenever used. + queryset = queryset.all() + return queryset + + +class StringRelatedField(Field): + def __init__(self, **kwargs): + kwargs['read_only'] = True + super(StringRelatedField, self).__init__(**kwargs) + + def to_representation(self, value): + return str(value) class PrimaryKeyRelatedField(RelatedField): @@ -33,9 +40,9 @@ class PrimaryKeyRelatedField(RelatedField): 'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.', } - def from_native(self, data): + def to_internal_value(self, data): try: - return self.queryset.get(pk=data) + return self.get_queryset().get(pk=data) except ObjectDoesNotExist: self.fail('does_not_exist', pk_value=data) except (TypeError, ValueError): @@ -68,9 +75,9 @@ class HyperlinkedRelatedField(RelatedField): """ lookup_value = view_kwargs[self.lookup_url_kwarg] lookup_kwargs = {self.lookup_field: lookup_value} - return self.queryset.get(**lookup_kwargs) + return self.get_queryset().get(**lookup_kwargs) - def from_native(self, value): + def to_internal_value(self, value): try: http_prefix = value.startswith(('http:', 'https:')) except AttributeError: @@ -102,13 +109,26 @@ class HyperlinkedIdentityField(RelatedField): def __init__(self, **kwargs): kwargs['read_only'] = True + kwargs['source'] = '*' self.view_name = kwargs.pop('view_name') self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) super(HyperlinkedIdentityField, self).__init__(**kwargs) - def get_attribute(self, instance): - return instance + def get_url(self, obj, view_name, request, format): + """ + Given an object, return the URL that hyperlinks to the object. + + May raise a `NoReverseMatch` if the `view_name` and `lookup_field` + attributes are not configured to correctly match the URL conf. + """ + # Unsaved objects will not yet have a valid URL. + if obj.pk is None: + return None + + lookup_value = getattr(obj, self.lookup_field) + kwargs = {self.lookup_url_kwarg: lookup_value} + return reverse(view_name, kwargs=kwargs, request=request, format=format) def to_representation(self, value): request = self.context.get('request', None) @@ -144,21 +164,6 @@ class HyperlinkedIdentityField(RelatedField): ) raise Exception(msg % self.view_name) - def get_url(self, obj, view_name, request, format): - """ - Given an object, return the URL that hyperlinks to the object. - - May raise a `NoReverseMatch` if the `view_name` and `lookup_field` - attributes are not configured to correctly match the URL conf. - """ - # Unsaved objects will not yet have a valid URL. - if obj.pk is None: - return None - - lookup_value = getattr(obj, self.lookup_field) - kwargs = {self.lookup_url_kwarg: lookup_value} - return reverse(view_name, kwargs=kwargs, request=request, format=format) - class SlugRelatedField(RelatedField): def __init__(self, **kwargs): diff --git a/tests/test_generics.py b/tests/test_generics.py index 17bfca2ff..51004edfe 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -547,7 +547,9 @@ class ClassA(models.Model): class ClassASerializer(serializers.ModelSerializer): - childs = serializers.PrimaryKeyRelatedField(many=True, source='childs') + childs = serializers.PrimaryKeyRelatedField( + many=True, queryset=ClassB.objects.all() + ) class Meta: model = ClassA From 5e39e159ee6aee90755709cfecec7d22c7ea3049 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 11:38:22 +0100 Subject: [PATCH 065/100] UNICODE_JSON and COMPACT_JSON settings --- rest_framework/fields.py | 20 ++++++++++---------- rest_framework/parsers.py | 2 +- rest_framework/renderers.py | 33 ++++++++++++--------------------- rest_framework/settings.py | 3 +++ tests/test_renderers.py | 35 +++++++++++++++++++---------------- tests/test_validation.py | 2 +- 6 files changed, 46 insertions(+), 49 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 4f06d1868..9d96cf5cc 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -137,6 +137,16 @@ class Field(object): messages.update(error_messages or {}) self.error_messages = messages + def __new__(cls, *args, **kwargs): + """ + When a field is instantiated, we store the arguments that were used, + so that we can present a helpful representation of the object. + """ + instance = super(Field, cls).__new__(cls) + instance._args = args + instance._kwargs = kwargs + return instance + def bind(self, field_name, parent, root): """ Setup the context for the field instance. @@ -249,16 +259,6 @@ class Field(object): raise AssertionError(msg) raise ValidationError(msg.format(**kwargs)) - def __new__(cls, *args, **kwargs): - """ - When a field is instantiated, we store the arguments that were used, - so that we can present a helpful representation of the object. - """ - instance = super(Field, cls).__new__(cls) - instance._args = args - instance._kwargs = kwargs - return instance - def __repr__(self): return representation.field_repr(self) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index c287908dc..fa02ecf1e 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -48,7 +48,7 @@ class JSONParser(BaseParser): """ media_type = 'application/json' - renderer_class = renderers.UnicodeJSONRenderer + renderer_class = renderers.JSONRenderer def parse(self, stream, media_type=None, parser_context=None): """ diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index dfc5a39f6..3bf03e62c 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -26,6 +26,10 @@ from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework import exceptions, status, VERSION +def zero_as_none(value): + return None if value == 0 else value + + class BaseRenderer(object): """ All renderers should extend this class, setting the `media_type` @@ -44,13 +48,13 @@ class BaseRenderer(object): class JSONRenderer(BaseRenderer): """ Renderer which serializes to JSON. - Applies JSON's backslash-u character escaping for non-ascii characters. """ media_type = 'application/json' format = 'json' encoder_class = encoders.JSONEncoder - ensure_ascii = True + ensure_ascii = not api_settings.UNICODE_JSON + compact = api_settings.COMPACT_JSON # We don't set a charset because JSON is a binary encoding, # that can be encoded as utf-8, utf-16 or utf-32. @@ -62,9 +66,10 @@ class JSONRenderer(BaseRenderer): if accepted_media_type: # If the media type looks like 'application/json; indent=4', # then pretty print the result. + # Note that we coerce `indent=0` into `indent=None`. base_media_type, params = parse_header(accepted_media_type.encode('ascii')) try: - return max(min(int(params['indent']), 8), 0) + return zero_as_none(max(min(int(params['indent']), 8), 0)) except (KeyError, ValueError, TypeError): pass @@ -81,10 +86,12 @@ class JSONRenderer(BaseRenderer): renderer_context = renderer_context or {} indent = self.get_indent(accepted_media_type, renderer_context) + separators = (',', ':') if (indent is None and self.compact) else (', ', ': ') ret = json.dumps( data, cls=self.encoder_class, - indent=indent, ensure_ascii=self.ensure_ascii + indent=indent, ensure_ascii=self.ensure_ascii, + separators=separators ) # On python 2.x json.dumps() returns bytestrings if ensure_ascii=True, @@ -96,14 +103,6 @@ class JSONRenderer(BaseRenderer): return ret -class UnicodeJSONRenderer(JSONRenderer): - ensure_ascii = False - """ - Renderer which serializes to JSON. - Does *not* apply JSON's character escaping for non-ascii characters. - """ - - class JSONPRenderer(JSONRenderer): """ Renderer which serializes to json, @@ -196,7 +195,7 @@ class YAMLRenderer(BaseRenderer): format = 'yaml' encoder = encoders.SafeDumper charset = 'utf-8' - ensure_ascii = True + ensure_ascii = False def render(self, data, accepted_media_type=None, renderer_context=None): """ @@ -210,14 +209,6 @@ class YAMLRenderer(BaseRenderer): return yaml.dump(data, stream=None, encoding=self.charset, Dumper=self.encoder, allow_unicode=not self.ensure_ascii) -class UnicodeYAMLRenderer(YAMLRenderer): - """ - Renderer which serializes to YAML. - Does *not* apply character escaping for non-ascii characters. - """ - ensure_ascii = False - - class TemplateHTMLRenderer(BaseRenderer): """ An HTML renderer for use with templates. diff --git a/rest_framework/settings.py b/rest_framework/settings.py index f48643b5d..e55610bbc 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -112,6 +112,9 @@ DEFAULTS = { ), 'TIME_FORMAT': None, + # Encoding + 'UNICODE_JSON': True, + 'COMPACT_JSON': True } diff --git a/tests/test_renderers.py b/tests/test_renderers.py index 91244e261..a8fd5f460 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -13,7 +13,7 @@ from rest_framework.compat import yaml, etree, StringIO from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \ - XMLRenderer, JSONPRenderer, BrowsableAPIRenderer, UnicodeJSONRenderer, UnicodeYAMLRenderer + XMLRenderer, JSONPRenderer, BrowsableAPIRenderer from rest_framework.parsers import YAMLParser, XMLParser from rest_framework.settings import api_settings from rest_framework.test import APIRequestFactory @@ -32,7 +32,7 @@ RENDERER_B_SERIALIZER = lambda x: ('Renderer B: %s' % x).encode('ascii') expected_results = [ - ((elem for elem in [1, 2, 3]), JSONRenderer, b'[1, 2, 3]') # Generator + ((elem for elem in [1, 2, 3]), JSONRenderer, b'[1,2,3]') # Generator ] @@ -270,7 +270,7 @@ class RendererEndToEndTests(TestCase): self.assertNotContains(resp, '>text/html; charset=utf-8<') -_flat_repr = '{"foo": ["bar", "baz"]}' +_flat_repr = '{"foo":["bar","baz"]}' _indented_repr = '{\n "foo": [\n "bar",\n "baz"\n ]\n}' @@ -373,12 +373,6 @@ class JSONRendererTests(TestCase): content = renderer.render(obj, 'application/json; indent=2') self.assertEqual(strip_trailing_whitespace(content.decode('utf-8')), _indented_repr) - def test_check_ascii(self): - obj = {'countries': ['United Kingdom', 'France', 'España']} - renderer = JSONRenderer() - content = renderer.render(obj, 'application/json') - self.assertEqual(content, '{"countries": ["United Kingdom", "France", "Espa\\u00f1a"]}'.encode('utf-8')) - class UnicodeJSONRendererTests(TestCase): """ @@ -386,9 +380,22 @@ class UnicodeJSONRendererTests(TestCase): """ def test_proper_encoding(self): obj = {'countries': ['United Kingdom', 'France', 'España']} - renderer = UnicodeJSONRenderer() + renderer = JSONRenderer() content = renderer.render(obj, 'application/json') - self.assertEqual(content, '{"countries": ["United Kingdom", "France", "España"]}'.encode('utf-8')) + self.assertEqual(content, '{"countries":["United Kingdom","France","España"]}'.encode('utf-8')) + + +class AsciiJSONRendererTests(TestCase): + """ + Tests specific for the Unicode JSON Renderer + """ + def test_proper_encoding(self): + class AsciiJSONRenderer(JSONRenderer): + ensure_ascii = True + obj = {'countries': ['United Kingdom', 'France', 'España']} + renderer = AsciiJSONRenderer() + content = renderer.render(obj, 'application/json') + self.assertEqual(content, '{"countries":["United Kingdom","France","Espa\\u00f1a"]}'.encode('utf-8')) class JSONPRendererTests(TestCase): @@ -487,13 +494,9 @@ if yaml: def assertYAMLContains(self, content, string): self.assertTrue(string in content, '%r not in %r' % (string, content)) - class UnicodeYAMLRendererTests(TestCase): - """ - Tests specific for the Unicode YAML Renderer - """ def test_proper_encoding(self): obj = {'countries': ['United Kingdom', 'France', 'España']} - renderer = UnicodeYAMLRenderer() + renderer = YAMLRenderer() content = renderer.render(obj, 'application/yaml') self.assertEqual(content.strip(), 'countries: [United Kingdom, France, España]'.encode('utf-8')) diff --git a/tests/test_validation.py b/tests/test_validation.py index 7543d849b..ce39714d1 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -131,7 +131,7 @@ class TestMaxValueValidatorValidation(TestCase): request = factory.patch('/{0}'.format(obj.pk), {'number_value': 101}, format='json') view = UpdateMaxValueValidationModel().as_view() response = view(request, pk=obj.pk).render() - self.assertEqual(response.content, b'{"number_value": ["Ensure this value is less than or equal to 100."]}') + self.assertEqual(response.content, b'{"number_value":["Ensure this value is less than or equal to 100."]}') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) From 22af49bf8ffc73afc9b638f1b9cd2e909c6c89a8 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 11:50:20 +0100 Subject: [PATCH 066/100] Tidy up JSONEncoder --- rest_framework/utils/encoders.py | 67 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 33 deletions(-) diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 7992b6b1c..174b08b8b 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -7,7 +7,6 @@ from django.db.models.query import QuerySet from django.utils.datastructures import SortedDict from django.utils.functional import Promise from rest_framework.compat import force_text -# from rest_framework.serializers import DictWithMetadata, SortedDictWithMetadata import datetime import decimal import types @@ -17,45 +16,47 @@ import json class JSONEncoder(json.JSONEncoder): """ JSONEncoder subclass that knows how to encode date/time/timedelta, - decimal types, and generators. + decimal types, generators and other basic python objects. """ - def default(self, o): + def default(self, obj): # For Date Time string spec, see ECMA 262 # http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 - if isinstance(o, Promise): - return force_text(o) - elif isinstance(o, datetime.datetime): - r = o.isoformat() - if o.microsecond: - r = r[:23] + r[26:] - if r.endswith('+00:00'): - r = r[:-6] + 'Z' - return r - elif isinstance(o, datetime.date): - return o.isoformat() - elif isinstance(o, datetime.time): - if timezone and timezone.is_aware(o): + if isinstance(obj, Promise): + return force_text(obj) + elif isinstance(obj, datetime.datetime): + representation = obj.isoformat() + if obj.microsecond: + representation = representation[:23] + representation[26:] + if representation.endswith('+00:00'): + representation = representation[:-6] + 'Z' + return representation + elif isinstance(obj, datetime.date): + return obj.isoformat() + elif isinstance(obj, datetime.time): + if timezone and timezone.is_aware(obj): raise ValueError("JSON can't represent timezone-aware times.") - r = o.isoformat() - if o.microsecond: - r = r[:12] - return r - elif isinstance(o, datetime.timedelta): - return str(o.total_seconds()) - elif isinstance(o, decimal.Decimal): - return float(o) - elif isinstance(o, QuerySet): - return list(o) - elif hasattr(o, 'tolist'): - return o.tolist() - elif hasattr(o, '__getitem__'): + representation = obj.isoformat() + if obj.microsecond: + representation = representation[:12] + return representation + elif isinstance(obj, datetime.timedelta): + return str(obj.total_seconds()) + elif isinstance(obj, decimal.Decimal): + # Serializers will coerce decimals to strings by default. + return float(obj) + elif isinstance(obj, QuerySet): + return list(obj) + elif hasattr(obj, 'tolist'): + # Numpy arrays and array scalars. + return obj.tolist() + elif hasattr(obj, '__getitem__'): try: - return dict(o) + return dict(obj) except: pass - elif hasattr(o, '__iter__'): - return [i for i in o] - return super(JSONEncoder, self).default(o) + elif hasattr(obj, '__iter__'): + return [item for item in obj] + return super(JSONEncoder, self).default(obj) try: From 79715f01f8c34fdd55c2291b6b21d09fa3a8153e Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 12:10:22 +0100 Subject: [PATCH 067/100] Coerce dates etc to ISO_8601 in seralizer, by default. --- rest_framework/fields.py | 24 +++++++++++++----------- rest_framework/settings.py | 21 ++++++++------------- tests/test_filters.py | 17 +++++++++-------- tests/test_pagination.py | 2 +- 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 9d96cf5cc..e1855ff74 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -431,10 +431,12 @@ class DecimalField(Field): 'max_whole_digits': _('Ensure that there are no more than {max_whole_digits} digits before the decimal point.') } - def __init__(self, max_digits, decimal_places, coerce_to_string=True, max_value=None, min_value=None, **kwargs): + coerce_to_string = api_settings.COERCE_DECIMAL_TO_STRING + + def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=None, min_value=None, **kwargs): self.max_digits = max_digits self.decimal_places = decimal_places - self.coerce_to_string = coerce_to_string + self.coerce_to_string = coerce_to_string if (coerce_to_string is not None) else self.coerce_to_string super(DecimalField, self).__init__(**kwargs) if max_value is not None: self.validators.append(validators.MaxValueValidator(max_value)) @@ -510,12 +512,12 @@ class DateField(Field): default_error_messages = { 'invalid': _('Date has wrong format. Use one of these formats instead: {format}'), } - input_formats = api_settings.DATE_INPUT_FORMATS format = api_settings.DATE_FORMAT + input_formats = api_settings.DATE_INPUT_FORMATS - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats + def __init__(self, format=None, input_formats=None, *args, **kwargs): self.format = format if format is not None else self.format + self.input_formats = input_formats if input_formats is not None else self.input_formats super(DateField, self).__init__(*args, **kwargs) def to_internal_value(self, value): @@ -569,12 +571,12 @@ class DateTimeField(Field): default_error_messages = { 'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}'), } - input_formats = api_settings.DATETIME_INPUT_FORMATS format = api_settings.DATETIME_FORMAT + input_formats = api_settings.DATETIME_INPUT_FORMATS - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats + def __init__(self, format=None, input_formats=None, *args, **kwargs): self.format = format if format is not None else self.format + self.input_formats = input_formats if input_formats is not None else self.input_formats super(DateTimeField, self).__init__(*args, **kwargs) def to_internal_value(self, value): @@ -634,12 +636,12 @@ class TimeField(Field): default_error_messages = { 'invalid': _('Time has wrong format. Use one of these formats instead: {format}'), } - input_formats = api_settings.TIME_INPUT_FORMATS format = api_settings.TIME_FORMAT + input_formats = api_settings.TIME_INPUT_FORMATS - def __init__(self, input_formats=None, format=None, *args, **kwargs): - self.input_formats = input_formats if input_formats is not None else self.input_formats + def __init__(self, format=None, input_formats=None, *args, **kwargs): self.format = format if format is not None else self.format + self.input_formats = input_formats if input_formats is not None else self.input_formats super(TimeField, self).__init__(*args, **kwargs) def from_native(self, value): diff --git a/rest_framework/settings.py b/rest_framework/settings.py index e55610bbc..421e146c8 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -97,24 +97,19 @@ DEFAULTS = { 'URL_FIELD_NAME': 'url', # Input and output formats - 'DATE_INPUT_FORMATS': ( - ISO_8601, - ), - 'DATE_FORMAT': None, + 'DATE_FORMAT': ISO_8601, + 'DATE_INPUT_FORMATS': (ISO_8601,), - 'DATETIME_INPUT_FORMATS': ( - ISO_8601, - ), - 'DATETIME_FORMAT': None, + 'DATETIME_FORMAT': ISO_8601, + 'DATETIME_INPUT_FORMATS': (ISO_8601,), - 'TIME_INPUT_FORMATS': ( - ISO_8601, - ), - 'TIME_FORMAT': None, + 'TIME_FORMAT': ISO_8601, + 'TIME_INPUT_FORMATS': (ISO_8601,), # Encoding 'UNICODE_JSON': True, - 'COMPACT_JSON': True + 'COMPACT_JSON': True, + 'COERCE_DECIMAL_TO_STRING': True } diff --git a/tests/test_filters.py b/tests/test_filters.py index 01668114b..dc84dcbd0 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -2,10 +2,11 @@ from __future__ import unicode_literals import datetime from decimal import Decimal from django.db import models +from django.conf.urls import patterns, url from django.core.urlresolvers import reverse from django.test import TestCase from django.utils import unittest -from django.conf.urls import patterns, url +from django.utils.dateparse import parse_date from rest_framework import generics, serializers, status, filters from rest_framework.compat import django_filters from rest_framework.test import APIRequestFactory @@ -102,7 +103,7 @@ if django_filters: class CommonFilteringTestCase(TestCase): def _serialize_object(self, obj): - return {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date} + return {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date.isoformat()} def setUp(self): """ @@ -153,7 +154,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): request = factory.get('/', {'date': '%s' % search_date}) # search_date str: '2012-09-22' response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['date'] == search_date] + expected_data = [f for f in self.data if parse_date(f['date']) == search_date] self.assertEqual(response.data, expected_data) @unittest.skipUnless(django_filters, 'django-filter not installed') @@ -209,7 +210,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): request = factory.get('/', {'date': '%s' % search_date}) # search_date str: '2012-10-02' response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['date'] > search_date] + expected_data = [f for f in self.data if parse_date(f['date']) > search_date] self.assertEqual(response.data, expected_data) # Tests that the text filter set with 'icontains' in the filter class works. @@ -229,7 +230,7 @@ class IntegrationTestFiltering(CommonFilteringTestCase): }) response = view(request).render() self.assertEqual(response.status_code, status.HTTP_200_OK) - expected_data = [f for f in self.data if f['date'] > search_date and + expected_data = [f for f in self.data if parse_date(f['date']) > search_date and Decimal(f['decimal']) < search_decimal] self.assertEqual(response.data, expected_data) @@ -481,9 +482,9 @@ class DjangoFilterOrderingTests(TestCase): self.assertEqual( response.data, [ - {'id': 3, 'date': datetime.date(2014, 10, 8), 'text': 'cde'}, - {'id': 2, 'date': datetime.date(2013, 10, 8), 'text': 'bcd'}, - {'id': 1, 'date': datetime.date(2012, 10, 8), 'text': 'abc'} + {'id': 3, 'date': '2014-10-08', 'text': 'cde'}, + {'id': 2, 'date': '2013-10-08', 'text': 'bcd'}, + {'id': 1, 'date': '2012-10-08', 'text': 'abc'} ] ) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index a7f8e691f..1fd9cf9c4 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -135,7 +135,7 @@ class IntegrationTestPaginationAndFiltering(TestCase): self.objects = FilterableItem.objects self.data = [ - {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date} + {'id': obj.id, 'text': obj.text, 'decimal': str(obj.decimal), 'date': obj.date.isoformat()} for obj in self.objects.all() ] From f95e7fae38968f58e742b93842bda9110a61b9f7 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 12:40:53 +0100 Subject: [PATCH 068/100] Updating docs --- docs/api-guide/renderers.md | 29 ++++------------------ docs/api-guide/settings.md | 48 ++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/docs/api-guide/renderers.md b/docs/api-guide/renderers.md index 7a3429bfd..b1adf31f8 100644 --- a/docs/api-guide/renderers.md +++ b/docs/api-guide/renderers.md @@ -74,37 +74,18 @@ If your API includes views that can serve both regular webpages and API response Renders the request data into `JSON`, using utf-8 encoding. -Note that non-ascii characters will be rendered using JSON's `\uXXXX` character escape. For example: +Note that the default style is to include unicode characters, and render the response using a compact style with no uneccessary whitespace: - {"unicode black star": "\u2605"} + {"unicode black star":"★","value":999} The client may additionally include an `'indent'` media type parameter, in which case the returned `JSON` will be indented. For example `Accept: application/json; indent=4`. { - "unicode black star": "\u2605" + "unicode black star": "★", + "value": 999 } -**.media_type**: `application/json` - -**.format**: `'.json'` - -**.charset**: `None` - -## UnicodeJSONRenderer - -Renders the request data into `JSON`, using utf-8 encoding. - -Note that non-ascii characters will not be character escaped. For example: - - {"unicode black star": "★"} - -The client may additionally include an `'indent'` media type parameter, in which case the returned `JSON` will be indented. For example `Accept: application/json; indent=4`. - - { - "unicode black star": "★" - } - -Both the `JSONRenderer` and `UnicodeJSONRenderer` styles conform to [RFC 4627][rfc4627], and are syntactically valid JSON. +The default JSON encoding style can be altered using the `UNICODE_JSON` and `COMPACT_JSON` settings keys. **.media_type**: `application/json` diff --git a/docs/api-guide/settings.md b/docs/api-guide/settings.md index 27a091633..6a855c929 100644 --- a/docs/api-guide/settings.md +++ b/docs/api-guide/settings.md @@ -265,7 +265,7 @@ A format string that should be used by default for rendering the output of `Date May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'` #### DATETIME_INPUT_FORMATS @@ -281,7 +281,7 @@ A format string that should be used by default for rendering the output of `Date May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'` #### DATE_INPUT_FORMATS @@ -297,7 +297,7 @@ A format string that should be used by default for rendering the output of `Time May be any of `None`, `'iso-8601'` or a Python [strftime format][strftime] string. -Default: `None` +Default: `'iso-8601'` #### TIME_INPUT_FORMATS @@ -309,6 +309,46 @@ Default: `['iso-8601']` --- +## Encodings + +#### UNICODE_JSON + +When set to `True`, JSON responses will allow unicode characters in responses. For example: + + {"unicode black star":"★"} + +When set to `False`, JSON responses will escape non-ascii characters, like so: + + {"unicode black star":"\u2605"} + +Both styles conform to [RFC 4627][rfc4627], and are syntactically valid JSON. The unicode style is prefered as being more user-friendly when inspecting API responses. + +Default: `True` + +#### COMPACT_JSON + +When set to `True`, JSON responses will return compact representations, with no spacing after `':'` and `','` characters. For example: + + {"is_admin":false,"email":"jane@example"} + +When set to `False`, JSON responses will return slightly more verbose representations, like so: + + {"is_admin": false, "email": "jane@example"} + +The default style is to return minified responses, in line with [Heroku's API design guidelines][heroku-minified-json]. + +Default: `True` + +#### COERCE_DECIMAL_TO_STRING + +When returning decimal objects in API representations that do not support a native decimal type, it is normally best to return the value as a string. This avoids the loss of precision that occurs with binary floating point implementations. + +When set to `True`, the serializer `DecimalField` class will return strings instead of `Decimal` objects. When set to `False`, serializers will return `Decimal` objects, which the default JSON encoder will return as floats. + +Default: `True` + +--- + ## View names and descriptions **The following settings are used to generate the view names and descriptions, as used in responses to `OPTIONS` requests, and as used in the browsable API.** @@ -378,4 +418,6 @@ An integer of 0 or more, that may be used to specify the number of application p Default: `None` [cite]: http://www.python.org/dev/peps/pep-0020/ +[rfc4627]: http://www.ietf.org/rfc/rfc4627.txt +[heroku-minified-json]: https://github.com/interagent/http-api-design#keep-json-minified-in-all-responses [strftime]: http://docs.python.org/2/library/time.html#time.strftime From b73a205cc021983d9a508b447f30e144a1ce4129 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 17:03:42 +0100 Subject: [PATCH 069/100] Tests for relational fields (not including many=True) --- rest_framework/relations.py | 153 +++++++++++++++++++++++------------- tests/test_relations.py | 139 ++++++++++++++++++++++++++++++++ tests/utils.py | 53 +++++++++++++ 3 files changed, 291 insertions(+), 54 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index e23a41526..75ec89a86 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -1,19 +1,24 @@ +from rest_framework.compat import smart_text, urlparse from rest_framework.fields import Field from rest_framework.reverse import reverse -from django.core.exceptions import ObjectDoesNotExist +from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch from django.db.models.query import QuerySet -from rest_framework.compat import urlparse +from django.utils.translation import ugettext_lazy as _ class RelatedField(Field): def __init__(self, **kwargs): self.queryset = kwargs.pop('queryset', None) self.many = kwargs.pop('many', False) - assert self.queryset is not None or kwargs.get('read_only', False), ( + assert self.queryset is not None or kwargs.get('read_only', None), ( 'Relational field must provide a `queryset` argument, ' 'or set read_only=`True`.' ) + assert not (self.queryset is not None and kwargs.get('read_only', None)), ( + 'Relational fields should not provide a `queryset` argument, ' + 'when setting read_only=`True`.' + ) super(RelatedField, self).__init__(**kwargs) def get_queryset(self): @@ -25,6 +30,11 @@ class RelatedField(Field): class StringRelatedField(Field): + """ + A read only field that represents its targets using their + plain string representation. + """ + def __init__(self, **kwargs): kwargs['read_only'] = True super(StringRelatedField, self).__init__(**kwargs) @@ -34,10 +44,10 @@ class StringRelatedField(Field): class PrimaryKeyRelatedField(RelatedField): - MESSAGES = { + default_error_messages = { 'required': 'This field is required.', 'does_not_exist': "Invalid pk '{pk_value}' - object does not exist.", - 'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.', + 'incorrect_type': 'Incorrect type. Expected pk value, received {data_type}.', } def to_internal_value(self, data): @@ -48,22 +58,33 @@ class PrimaryKeyRelatedField(RelatedField): except (TypeError, ValueError): self.fail('incorrect_type', data_type=type(data).__name__) + def to_representation(self, value): + return value.pk + class HyperlinkedRelatedField(RelatedField): lookup_field = 'pk' - MESSAGES = { + default_error_messages = { 'required': 'This field is required.', 'no_match': 'Invalid hyperlink - No URL match', 'incorrect_match': 'Invalid hyperlink - Incorrect URL match.', - 'does_not_exist': "Invalid hyperlink - Object does not exist.", - 'incorrect_type': 'Incorrect type. Expected URL string, received {data_type}.', + 'does_not_exist': 'Invalid hyperlink - Object does not exist.', + 'incorrect_type': 'Incorrect type. Expected URL string, received {data_type}.', } - def __init__(self, **kwargs): - self.view_name = kwargs.pop('view_name') + def __init__(self, view_name, **kwargs): + self.view_name = view_name self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) + self.format = kwargs.pop('format', None) + + # We include these simply for dependancy injection in tests. + # We can't add them as class attributes or they would expect an + # implict `self` argument to be passed. + self.reverse = reverse + self.resolve = resolve + super(HyperlinkedRelatedField, self).__init__(**kwargs) def get_object(self, view_name, view_args, view_kwargs): @@ -77,44 +98,6 @@ class HyperlinkedRelatedField(RelatedField): lookup_kwargs = {self.lookup_field: lookup_value} return self.get_queryset().get(**lookup_kwargs) - def to_internal_value(self, value): - try: - http_prefix = value.startswith(('http:', 'https:')) - except AttributeError: - self.fail('incorrect_type', data_type=type(value).__name__) - - if http_prefix: - # If needed convert absolute URLs to relative path - value = urlparse.urlparse(value).path - prefix = get_script_prefix() - if value.startswith(prefix): - value = '/' + value[len(prefix):] - - try: - match = resolve(value) - except Exception: - self.fail('no_match') - - if match.view_name != self.view_name: - self.fail('incorrect_match') - - try: - return self.get_object(match.view_name, match.args, match.kwargs) - except (ObjectDoesNotExist, TypeError, ValueError): - self.fail('does_not_exist') - - -class HyperlinkedIdentityField(RelatedField): - lookup_field = 'pk' - - def __init__(self, **kwargs): - kwargs['read_only'] = True - kwargs['source'] = '*' - self.view_name = kwargs.pop('view_name') - self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) - self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) - super(HyperlinkedIdentityField, self).__init__(**kwargs) - def get_url(self, obj, view_name, request, format): """ Given an object, return the URL that hyperlinks to the object. @@ -128,16 +111,42 @@ class HyperlinkedIdentityField(RelatedField): lookup_value = getattr(obj, self.lookup_field) kwargs = {self.lookup_url_kwarg: lookup_value} - return reverse(view_name, kwargs=kwargs, request=request, format=format) + return self.reverse(view_name, kwargs=kwargs, request=request, format=format) + + def to_internal_value(self, data): + try: + http_prefix = data.startswith(('http:', 'https:')) + except AttributeError: + self.fail('incorrect_type', data_type=type(data).__name__) + + if http_prefix: + # If needed convert absolute URLs to relative path + data = urlparse.urlparse(data).path + prefix = get_script_prefix() + if data.startswith(prefix): + data = '/' + data[len(prefix):] + + try: + match = self.resolve(data) + except Exception: + self.fail('no_match') + + if match.view_name != self.view_name: + self.fail('incorrect_match') + + try: + return self.get_object(match.view_name, match.args, match.kwargs) + except (ObjectDoesNotExist, TypeError, ValueError): + self.fail('does_not_exist') def to_representation(self, value): request = self.context.get('request', None) format = self.context.get('format', None) assert request is not None, ( - "`HyperlinkedIdentityField` requires the request in the serializer" + "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " - "the serializer." + "the serializer." % self.__class__.__name__ ) # By default use whatever format is given for the current context @@ -162,9 +171,45 @@ class HyperlinkedIdentityField(RelatedField): 'model in your API, or incorrectly configured the ' '`lookup_field` attribute on this field.' ) - raise Exception(msg % self.view_name) + raise ImproperlyConfigured(msg % self.view_name) + + +class HyperlinkedIdentityField(HyperlinkedRelatedField): + """ + A read-only field that represents the identity URL for an object, itself. + + This is in contrast to `HyperlinkedRelatedField` which represents the + URL of relationships to other objects. + """ + + def __init__(self, view_name, **kwargs): + kwargs['read_only'] = True + kwargs['source'] = '*' + super(HyperlinkedIdentityField, self).__init__(view_name, **kwargs) class SlugRelatedField(RelatedField): - def __init__(self, **kwargs): - self.slug_field = kwargs.pop('slug_field', None) + """ + A read-write field the represents the target of the relationship + by a unique 'slug' attribute. + """ + + default_error_messages = { + 'does_not_exist': _("Object with {slug_name}={value} does not exist."), + 'invalid': _('Invalid value.'), + } + + def __init__(self, slug_field, **kwargs): + self.slug_field = slug_field + super(SlugRelatedField, self).__init__(**kwargs) + + def to_internal_value(self, data): + try: + return self.get_queryset().get(**{self.slug_field: data}) + except ObjectDoesNotExist: + self.fail('does_not_exist', slug_name=self.slug_field, value=smart_text(data)) + except (TypeError, ValueError): + self.fail('invalid') + + def to_representation(self, obj): + return getattr(obj, self.slug_field) diff --git a/tests/test_relations.py b/tests/test_relations.py index b1bc66b62..a36721175 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -1,3 +1,142 @@ +from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset +from django.core.exceptions import ImproperlyConfigured, ValidationError +from rest_framework import serializers +from rest_framework.test import APISimpleTestCase +import pytest + + +class TestStringRelatedField(APISimpleTestCase): + def setUp(self): + self.instance = MockObject(pk=1, name='foo') + self.field = serializers.StringRelatedField() + + def test_string_related_representation(self): + representation = self.field.to_representation(self.instance) + assert representation == '<MockObject name=foo, pk=1>' + + +class TestPrimaryKeyRelatedField(APISimpleTestCase): + def setUp(self): + self.queryset = MockQueryset([ + MockObject(pk=1, name='foo'), + MockObject(pk=2, name='bar'), + MockObject(pk=3, name='baz') + ]) + self.instance = self.queryset.items[2] + self.field = serializers.PrimaryKeyRelatedField(queryset=self.queryset) + + def test_pk_related_lookup_exists(self): + instance = self.field.to_internal_value(self.instance.pk) + assert instance is self.instance + + def test_pk_related_lookup_does_not_exist(self): + with pytest.raises(ValidationError) as excinfo: + self.field.to_internal_value(4) + msg = excinfo.value.message + assert msg == "Invalid pk '4' - object does not exist." + + def test_pk_related_lookup_invalid_type(self): + with pytest.raises(ValidationError) as excinfo: + self.field.to_internal_value(BadType()) + msg = excinfo.value.message + assert msg == 'Incorrect type. Expected pk value, received BadType.' + + def test_pk_representation(self): + representation = self.field.to_representation(self.instance) + assert representation == self.instance.pk + + +class TestHyperlinkedIdentityField(APISimpleTestCase): + def setUp(self): + self.instance = MockObject(pk=1, name='foo') + self.field = serializers.HyperlinkedIdentityField(view_name='example') + self.field.reverse = mock_reverse + self.field.context = {'request': True} + + def test_representation(self): + representation = self.field.to_representation(self.instance) + assert representation == 'http://example.org/example/1/' + + def test_representation_unsaved_object(self): + representation = self.field.to_representation(MockObject(pk=None)) + assert representation is None + + def test_representation_with_format(self): + self.field.context['format'] = 'xml' + representation = self.field.to_representation(self.instance) + assert representation == 'http://example.org/example/1.xml/' + + def test_improperly_configured(self): + """ + If a matching view cannot be reversed with the given instance, + the the user has misconfigured something, as the URL conf and the + hyperlinked field do not match. + """ + self.field.reverse = fail_reverse + with pytest.raises(ImproperlyConfigured): + self.field.to_representation(self.instance) + + +class TestHyperlinkedIdentityFieldWithFormat(APISimpleTestCase): + """ + Tests for a hyperlinked identity field that has a `format` set, + which enforces that alternate formats are never linked too. + + Eg. If your API includes some endpoints that accept both `.xml` and `.json`, + but other endpoints that only accept `.json`, we allow for hyperlinked + relationships that enforce only a single suffix type. + """ + + def setUp(self): + self.instance = MockObject(pk=1, name='foo') + self.field = serializers.HyperlinkedIdentityField(view_name='example', format='json') + self.field.reverse = mock_reverse + self.field.context = {'request': True} + + def test_representation(self): + representation = self.field.to_representation(self.instance) + assert representation == 'http://example.org/example/1/' + + def test_representation_with_format(self): + self.field.context['format'] = 'xml' + representation = self.field.to_representation(self.instance) + assert representation == 'http://example.org/example/1.json/' + + +class TestSlugRelatedField(APISimpleTestCase): + def setUp(self): + self.queryset = MockQueryset([ + MockObject(pk=1, name='foo'), + MockObject(pk=2, name='bar'), + MockObject(pk=3, name='baz') + ]) + self.instance = self.queryset.items[2] + self.field = serializers.SlugRelatedField( + slug_field='name', queryset=self.queryset + ) + + def test_slug_related_lookup_exists(self): + instance = self.field.to_internal_value(self.instance.name) + assert instance is self.instance + + def test_slug_related_lookup_does_not_exist(self): + with pytest.raises(ValidationError) as excinfo: + self.field.to_internal_value('doesnotexist') + msg = excinfo.value.message + assert msg == 'Object with name=doesnotexist does not exist.' + + def test_slug_related_lookup_invalid_type(self): + with pytest.raises(ValidationError) as excinfo: + self.field.to_internal_value(BadType()) + msg = excinfo.value.message + assert msg == 'Invalid value.' + + def test_representation(self): + representation = self.field.to_representation(self.instance) + assert representation == self.instance.name + +# Older tests, for review... + # """ # General tests for relational fields. # """ diff --git a/tests/utils.py b/tests/utils.py index 28be81bd1..5e902ba94 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,4 +1,6 @@ from contextlib import contextmanager +from django.core.exceptions import ObjectDoesNotExist +from django.core.urlresolvers import NoReverseMatch from django.utils import six from rest_framework.settings import api_settings @@ -23,3 +25,54 @@ def temporary_setting(setting, value, module=None): if module is not None: six.moves.reload_module(module) + + +class MockObject(object): + def __init__(self, **kwargs): + self._kwargs = kwargs + for key, val in kwargs.items(): + setattr(self, key, val) + + def __str__(self): + kwargs_str = ', '.join([ + '%s=%s' % (key, value) + for key, value in sorted(self._kwargs.items()) + ]) + return '<MockObject %s>' % kwargs_str + + +class MockQueryset(object): + def __init__(self, iterable): + self.items = iterable + + def get(self, **lookup): + for item in self.items: + if all([ + getattr(item, key, None) == value + for key, value in lookup.items() + ]): + return item + raise ObjectDoesNotExist() + + +class BadType(object): + """ + When used as a lookup with a `MockQueryset`, these objects + will raise a `TypeError`, as occurs in Django when making + queryset lookups with an incorrect type for the lookup value. + """ + def __eq__(self): + raise TypeError() + + +def mock_reverse(view_name, args=None, kwargs=None, request=None, format=None): + args = args or [] + kwargs = kwargs or {} + value = (args + list(kwargs.values()) + ['-'])[0] + prefix = 'http://example.org' if request else '' + suffix = ('.' + format) if (format is not None) else '' + return '%s/%s/%s%s/' % (prefix, view_name, value, suffix) + + +def fail_reverse(view_name, args=None, kwargs=None, request=None, format=None): + raise NoReverseMatch() From 0ac52e0808288892717c017e57c57aa8ad81e6d3 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 17:06:37 +0100 Subject: [PATCH 070/100] Use Resolver404 instead of base Exception --- rest_framework/relations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 75ec89a86..46fe55ef9 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -2,7 +2,7 @@ from rest_framework.compat import smart_text, urlparse from rest_framework.fields import Field from rest_framework.reverse import reverse from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured -from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch +from django.core.urlresolvers import resolve, get_script_prefix, NoReverseMatch, Resolver404 from django.db.models.query import QuerySet from django.utils.translation import ugettext_lazy as _ @@ -128,7 +128,7 @@ class HyperlinkedRelatedField(RelatedField): try: match = self.resolve(data) - except Exception: + except Resolver404: self.fail('no_match') if match.view_name != self.view_name: From 1f75ffc7912725e771a67bf3e6c3c6362dc3199a Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 19:50:30 +0100 Subject: [PATCH 071/100] Access validation messages in a way thats compatible with 1.4, 1.5 --- tests/test_relations.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_relations.py b/tests/test_relations.py index a36721175..c29618ce2 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -32,13 +32,13 @@ class TestPrimaryKeyRelatedField(APISimpleTestCase): def test_pk_related_lookup_does_not_exist(self): with pytest.raises(ValidationError) as excinfo: self.field.to_internal_value(4) - msg = excinfo.value.message + msg = excinfo.value.messages[0] assert msg == "Invalid pk '4' - object does not exist." def test_pk_related_lookup_invalid_type(self): with pytest.raises(ValidationError) as excinfo: self.field.to_internal_value(BadType()) - msg = excinfo.value.message + msg = excinfo.value.messages[0] assert msg == 'Incorrect type. Expected pk value, received BadType.' def test_pk_representation(self): @@ -122,13 +122,13 @@ class TestSlugRelatedField(APISimpleTestCase): def test_slug_related_lookup_does_not_exist(self): with pytest.raises(ValidationError) as excinfo: self.field.to_internal_value('doesnotexist') - msg = excinfo.value.message + msg = excinfo.value.messages[0] assert msg == 'Object with name=doesnotexist does not exist.' def test_slug_related_lookup_invalid_type(self): with pytest.raises(ValidationError) as excinfo: self.field.to_internal_value(BadType()) - msg = excinfo.value.message + msg = excinfo.value.messages[0] assert msg == 'Invalid value.' def test_representation(self): From e6c88a423361b084ba171af7a74a183bd557e73e Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 19:54:27 +0100 Subject: [PATCH 072/100] Drop usage of validatiors.EMPTY_VALUES --- rest_framework/fields.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e1855ff74..33ab0682c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -223,7 +223,7 @@ class Field(object): return value def run_validators(self, value): - if value in validators.EMPTY_VALUES: + if value in (None, '', [], (), {}): return errors = [] @@ -450,7 +450,7 @@ class DecimalField(Field): than max_digits in the number, and no more than decimal_places digits after the decimal point. """ - if value in validators.EMPTY_VALUES: + if value in (None, ''): return None value = smart_text(value).strip() @@ -521,7 +521,7 @@ class DateField(Field): super(DateField, self).__init__(*args, **kwargs) def to_internal_value(self, value): - if value in validators.EMPTY_VALUES: + if value in (None, ''): return None if isinstance(value, datetime.datetime): @@ -580,7 +580,7 @@ class DateTimeField(Field): super(DateTimeField, self).__init__(*args, **kwargs) def to_internal_value(self, value): - if value in validators.EMPTY_VALUES: + if value in (None, ''): return None if isinstance(value, datetime.datetime): @@ -645,7 +645,7 @@ class TimeField(Field): super(TimeField, self).__init__(*args, **kwargs) def from_native(self, value): - if value in validators.EMPTY_VALUES: + if value in (None, ''): return None if isinstance(value, datetime.time): From 4fceceae7d1abf4d841677daee2636273ff09de8 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 20:09:08 +0100 Subject: [PATCH 073/100] DecimalField documentation --- docs/api-guide/fields.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index bfbff2adb..f07783186 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -274,7 +274,27 @@ Corresponds to `django.db.models.fields.FloatField`. ## DecimalField -A decimal representation. +A decimal representation, represented in Python by a Decimal instance. + +Has two required arguments: + +- `max_digits` The maximum number of digits allowed in the number. Note that this number must be greater than or equal to decimal_places. + +- `decimal_places` The number of decimal places to store with the number. + +For example, to validate numbers up to 999 with a resolution of 2 decimal places, you would use: + + serializers.DecimalField(max_digits=5, decimal_places=2) + +And to validate numbers up to anything lesss than one billion with a resolution of 10 decimal places: + + serializers.DecimalField(max_digits=19, decimal_places=10) + +This field also takes an optional argument, `coerce_to_string`. If set to `True` the representation will be output as a string. If set to `False` the representation will be left as a `Decimal` instance and the final representation will be determined by the renderer. + +If unset, this will default to the same value as the `COERCE_DECIMAL_TO_STRING` setting, which is `True` unless set otherwise. + +**Signature:** `DecimalField(max_digits, decimal_places, coerce_to_string=None)` Corresponds to `django.db.models.fields.DecimalField`. From afb28a44ad1737cd6fcd6da50ba9552f38293368 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 12 Sep 2014 21:32:20 +0100 Subject: [PATCH 074/100] Dealing with reverse relationships --- rest_framework/serializers.py | 8 ++++---- tests/test_model_field_mappings.py | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 0c2aedfa0..ecb2829b6 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -157,7 +157,7 @@ class Serializer(BaseSerializer): def __init__(self, *args, **kwargs): self.context = kwargs.pop('context', {}) kwargs.pop('partial', None) - kwargs.pop('many', False) + kwargs.pop('many', None) super(Serializer, self).__init__(*args, **kwargs) @@ -423,9 +423,9 @@ class ModelSerializer(Serializer): for accessor_name, relation_info in info.reverse_relations.items(): if accessor_name in self.opts.fields: if self.opts.depth: - ret[field_name] = self.get_nested_field(*relation_info) + ret[accessor_name] = self.get_nested_field(*relation_info) else: - ret[field_name] = self.get_related_field(*relation_info) + ret[accessor_name] = self.get_related_field(*relation_info) return ret @@ -444,7 +444,7 @@ class ModelSerializer(Serializer): Note that model_field will be `None` for reverse relationships. """ - class NestedModelSerializer(ModelSerializer): + class NestedModelSerializer(ModelSerializer): # Not right! class Meta: model = related_model depth = self.opts.depth - 1 diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index 6e9aaba6c..d5750f9e3 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -73,9 +73,9 @@ class OneToOneTargetModel(models.Model): class RelationalModel(models.Model): - foreign_key = models.ForeignKey(ForeignKeyTargetModel) - many_to_many = models.ManyToManyField(ManyToManyTargetModel) - one_to_one = models.OneToOneField(OneToOneTargetModel) + foreign_key = models.ForeignKey(ForeignKeyTargetModel, related_name='reverse_foreign_key') + many_to_many = models.ManyToManyField(ManyToManyTargetModel, related_name='reverse_many_to_many') + one_to_one = models.OneToOneField(OneToOneTargetModel, related_name='reverse_one_to_one') RELATIONAL_FLAT_REPR = """ @@ -160,3 +160,24 @@ class TestSerializerMappings(TestCase): model = RelationalModel depth = 1 self.assertEqual(repr(TestSerializer()), HYPERLINKED_NESTED_REPR) + + # def test_flat_reverse_foreign_key(self): + # class TestSerializer(serializers.ModelSerializer): + # class Meta: + # model = ForeignKeyTargetModel + # fields = ('id', 'name', 'reverse_foreign_key') + # print repr(TestSerializer()) + + # def test_flat_reverse_one_to_one(self): + # class TestSerializer(serializers.ModelSerializer): + # class Meta: + # model = OneToOneTargetModel + # fields = ('id', 'name', 'reverse_one_to_one') + # print repr(TestSerializer()) + + # def test_flat_reverse_many_to_many(self): + # class TestSerializer(serializers.ModelSerializer): + # class Meta: + # model = ManyToManyTargetModel + # fields = ('id', 'name', 'reverse_many_to_many') + # print repr(TestSerializer()) \ No newline at end of file From 96c21b81f5031a81b42b89371af48677081a6323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Sat, 13 Sep 2014 13:53:40 -0400 Subject: [PATCH 075/100] Add Third Party Resources Topic section --- docs/index.md | 4 +- docs/template.html | 1 + docs/topics/third-party-resources.md | 87 ++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 docs/topics/third-party-resources.md diff --git a/docs/index.md b/docs/index.md index 1888bfe4b..6dcb962f5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -195,6 +195,7 @@ General guides to using REST framework. * [Browser enhancements][browser-enhancements] * [The Browsable API][browsableapi] * [REST, Hypermedia & HATEOAS][rest-hypermedia-hateoas] +* [Third Party Resources][third-party-resources] * [Contributing to REST framework][contributing] * [2.0 Announcement][rest-framework-2-announcement] * [2.2 Announcement][2.2-announcement] @@ -312,11 +313,12 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. [browsableapi]: topics/browsable-api.md [rest-hypermedia-hateoas]: topics/rest-hypermedia-hateoas.md [contributing]: topics/contributing.md +[third-party-resources]: topics/third-party-resources.md [rest-framework-2-announcement]: topics/rest-framework-2-announcement.md [2.2-announcement]: topics/2.2-announcement.md [2.3-announcement]: topics/2.3-announcement.md [2.4-announcement]: topics/2.4-announcement.md -[kickstarter-announcement]: topics/kickstarter-announcement.md +[kickstarter-announcement]: topics/kickstarter-announcement.md [release-notes]: topics/release-notes.md [credits]: topics/credits.md diff --git a/docs/template.html b/docs/template.html index 19542bfe6..bb3ae221a 100644 --- a/docs/template.html +++ b/docs/template.html @@ -117,6 +117,7 @@ a.fusion-poweredby { <li><a href="{{ base_url }}/topics/browser-enhancements{{ suffix }}">Browser enhancements</a></li> <li><a href="{{ base_url }}/topics/browsable-api{{ suffix }}">The Browsable API</a></li> <li><a href="{{ base_url }}/topics/rest-hypermedia-hateoas{{ suffix }}">REST, Hypermedia & HATEOAS</a></li> + <li><a href="{{ base_url }}/topics/third-party-resources{{ suffix }}">Third Party Resources</a></li> <li><a href="{{ base_url }}/topics/contributing{{ suffix }}">Contributing to REST framework</a></li> <li><a href="{{ base_url }}/topics/rest-framework-2-announcement{{ suffix }}">2.0 Announcement</a></li> <li><a href="{{ base_url }}/topics/2.2-announcement{{ suffix }}">2.2 Announcement</a></li> diff --git a/docs/topics/third-party-resources.md b/docs/topics/third-party-resources.md new file mode 100644 index 000000000..da92eb33a --- /dev/null +++ b/docs/topics/third-party-resources.md @@ -0,0 +1,87 @@ +# Third Party Resources + +Django REST Framework has a growing community of developers, packages, and resources. + +Check out a grid detailing all the packages and ecosystem around Django REST Framework at [Django Packages](https://www.djangopackages.com/grids/g/django-rest-framework/). + +## Libraries and Extensions + +### Authentication + +* [djangorestframework-digestauth](https://github.com/juanriaza/django-rest-framework-digestauth) - Provides Digest Access Authentication support. +* [django-oauth-toolkit](https://github.com/evonove/django-oauth-toolkit) - Provides OAuth 2.0 support. +* [doac](https://github.com/Rediker-Software/doac) - Provides OAuth 2.0 support. +* [djangorestframework-jwt](https://github.com/GetBlimp/django-rest-framework-jwt) - Provides JSON Web Token Authentication support. +* [hawkrest](https://github.com/kumar303/hawkrest) - Provides Hawk HTTP Authorization. +* [djangorestframework-httpsignature](https://github.com/etoccalino/django-rest-framework-httpsignature) - Provides an easy to use HTTP Signature Authentication mechanism. + +### Permissions + +* [drf-any-permissions](https://github.com/kevin-brown/drf-any-permissions) - Provides alternative permission handling. +* [djangorestframework-composed-permissions](https://github.com/niwibe/djangorestframework-composed-permissions) - Provides a simple way to define complex permissions. +* [rest_condition](https://github.com/caxap/rest_condition) - Another extension for building complex permissions in a simple and convenient way. + +### Serializers + +* [django-rest-framework-mongoengine](https://github.com/umutbozkurt/django-rest-framework-mongoengine) - Serializer class that supports using MongoDB as the storage layer for Django REST framework. +* [djangorestframework-gis](https://github.com/djangonauts/django-rest-framework-gis) - Geographic add-ons +* [djangorestframework-hstore](https://github.com/djangonauts/django-rest-framework-hstore) - Serializer class to support django-hstore DictionaryField model field and its schema-mode feature. + +### Serializer fields + +* [drf-compound-fields](https://github.com/estebistec/drf-compound-fields) - Provides "compound" serializer fields, such as lists of simple values. +* [django-extra-fields](https://github.com/Hipo/drf-extra-fields) - Provides extra serializer fields. + +### Views + +* [djangorestframework-bulk](https://github.com/miki725/django-rest-framework-bulk) - Implements generic view mixins as well as some common concrete generic views to allow to apply bulk operations via API requests. + +### Routers + +* [drf-nested-routers](https://github.com/alanjds/drf-nested-routers) - Provides routers and relationship fields for working with nested resources. + +### Parsers + +* [djangorestframework-msgpack](https://github.com/juanriaza/django-rest-framework-msgpack) - Provides MessagePack renderer and parser support. +* [djangorestframework-camel-case](https://github.com/vbabiy/djangorestframework-camel-case) - Provides camel case JSON renderers and parsers. + +### Renderers + +* [djangorestframework-csv](https://github.com/mjumbewu/django-rest-framework-csv) - Provides CSV renderer support. +* [drf_ujson](https://github.com/gizmag/drf-ujson-renderer) - Implements JSON rendering using the UJSON package. + +### Filtering + +* [djangorestframework-chain](https://github.com/philipn/django-rest-framework-chain) - Allows arbitrary chaining of both relations and lookup filters. + +### Misc + +* [djangorestrelationalhyperlink](https://github.com/fredkingham/django_rest_model_hyperlink_serializers_project) - A hyperlinked serialiser that can can be used to alter relationships via hyperlinks, but otherwise like a hyperlink model serializer. +* [django-rest-swagger](https://github.com/marcgibbons/django-rest-swagger) - An API documentation generator for Swagger UI. +* [django-rest-framework-proxy ](https://github.com/eofs/django-rest-framework-proxy) - Proxy to redirect incoming request to another API server. +* [gaiarestframework](https://github.com/AppsFuel/gaiarestframework) - Utils for django-rest-framewok +* [drf-extensions](https://github.com/chibisov/drf-extensions) - A collection of custom extensions +* [ember-data-django-rest-adapter](https://github.com/toranb/ember-data-django-rest-adapter) - An ember-data adapter + +## Tutorials + +* [Beginner's Guide to the Django Rest Framework](http://code.tutsplus.com/tutorials/beginners-guide-to-the-django-rest-framework--cms-19786) +* [Getting Started with Django Rest Framework and AngularJS](http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html) +* [End to end web app with Django-Rest-Framework & AngularJS](http://blog.mourafiq.com/post/55034504632/end-to-end-web-app-with-django-rest-framework) +* [Start Your API - django-rest-framework part 1](https://godjango.com/41-start-your-api-django-rest-framework-part-1/) +* [Permissions & Authentication - django-rest-framework part 2](https://godjango.com/43-permissions-authentication-django-rest-framework-part-2/) +* [ViewSets and Routers - django-rest-framework part 3](https://godjango.com/45-viewsets-and-routers-django-rest-framework-part-3/) +* [Django Rest Framework User Endpoint](http://richardtier.com/2014/02/25/django-rest-framework-user-endpoint/) +* [Check credentials using Django Rest Framework](http://richardtier.com/2014/03/06/110/) + +## Videos + +* [Ember and Django Part 1 (Video)](http://www.neckbeardrepublic.com/screencasts/ember-and-django-part-1) +* [Django Rest Framework Part 1 (Video)](http://www.neckbeardrepublic.com/screencasts/django-rest-framework-part-1) +* [Pyowa July 2013 - Django Rest Framework (Video)](http://www.youtube.com/watch?v=E1ZrehVxpBo) +* [django-rest-framework and angularjs (Video)](http://www.youtube.com/watch?v=Q8FRBGTJ020) + +## Articles + +* [Web API performance: profiling Django REST framework](http://dabapps.com/blog/api-performance-profiling-django-rest-framework/) +* [API Development with Django and Django REST Framework](https://bnotions.com/api-development-with-django-and-django-rest-framework/) From 1390b6801e45c38db474e77daefd867e7ea7f825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Sat, 13 Sep 2014 16:20:16 -0400 Subject: [PATCH 076/100] Include third party resources link in home page --- mkdocs.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mkdocs.py b/mkdocs.py index b4c414080..25cb55e21 100755 --- a/mkdocs.py +++ b/mkdocs.py @@ -76,6 +76,7 @@ path_list = [ 'topics/browser-enhancements.md', 'topics/browsable-api.md', 'topics/rest-hypermedia-hateoas.md', + 'topics/third-party-resources.md', 'topics/contributing.md', 'topics/rest-framework-2-announcement.md', 'topics/2.2-announcement.md', From 4871dbdc73632be4fb00befbc816bc670bb609cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Sat, 13 Sep 2014 16:20:37 -0400 Subject: [PATCH 077/100] Add invitation to add new content --- docs/topics/third-party-resources.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/topics/third-party-resources.md b/docs/topics/third-party-resources.md index da92eb33a..2df13ef8a 100644 --- a/docs/topics/third-party-resources.md +++ b/docs/topics/third-party-resources.md @@ -4,6 +4,8 @@ Django REST Framework has a growing community of developers, packages, and resou Check out a grid detailing all the packages and ecosystem around Django REST Framework at [Django Packages](https://www.djangopackages.com/grids/g/django-rest-framework/). +To submit new content, [open an issue](https://github.com/tomchristie/django-rest-framework/issues/new) or [create a pull request](https://github.com/tomchristie/django-rest-framework/). Pull requests will be given higher priority since they are easier to include. + ## Libraries and Extensions ### Authentication From 915dfb9b3d6c235437a6459493cf26d248022e75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Sun, 14 Sep 2014 10:19:54 -0400 Subject: [PATCH 078/100] Update third-party-resources.md --- docs/topics/third-party-resources.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/topics/third-party-resources.md b/docs/topics/third-party-resources.md index 2df13ef8a..e337cbd54 100644 --- a/docs/topics/third-party-resources.md +++ b/docs/topics/third-party-resources.md @@ -4,7 +4,7 @@ Django REST Framework has a growing community of developers, packages, and resou Check out a grid detailing all the packages and ecosystem around Django REST Framework at [Django Packages](https://www.djangopackages.com/grids/g/django-rest-framework/). -To submit new content, [open an issue](https://github.com/tomchristie/django-rest-framework/issues/new) or [create a pull request](https://github.com/tomchristie/django-rest-framework/). Pull requests will be given higher priority since they are easier to include. +To submit new content, [open an issue](https://github.com/tomchristie/django-rest-framework/issues/new) or [create a pull request](https://github.com/tomchristie/django-rest-framework/). ## Libraries and Extensions From 40dc588a372375608701f7e521dea6d860a49eb2 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 09:50:51 +0100 Subject: [PATCH 079/100] Drop label from serializer fields when not needed --- rest_framework/fields.py | 6 +- rest_framework/serializers.py | 56 +++++++++------- .../utils/{modelinfo.py => model_meta.py} | 0 tests/test_model_field_mappings.py | 64 +++++++++---------- 4 files changed, 70 insertions(+), 56 deletions(-) rename rest_framework/utils/{modelinfo.py => model_meta.py} (100%) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 33ab0682c..1818e705e 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -80,6 +80,10 @@ def set_value(dictionary, keys, value): dictionary[keys[-1]] = value +def field_name_to_label(field_name): + return field_name.replace('_', ' ').capitalize() + + class SkipField(Exception): pass @@ -158,7 +162,7 @@ class Field(object): # `self.label` should deafult to being based on the field name. if self.label is None: - self.label = self.field_name.replace('_', ' ').capitalize() + self.label = field_name_to_label(self.field_name) # self.source should default to being the same as the field name. if self.source is None: diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ecb2829b6..ba8d475f8 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -15,11 +15,12 @@ from django.core.exceptions import ValidationError from django.db import models from django.utils import six from django.utils.datastructures import SortedDict +from django.utils.text import capfirst from collections import namedtuple from rest_framework.compat import clean_manytomany_helptext from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings -from rest_framework.utils import html, modelinfo, representation +from rest_framework.utils import html, model_meta, representation import copy # Note: We do the following so that users of the framework can use this style: @@ -334,6 +335,14 @@ def lookup_class(mapping, instance): raise KeyError('Class %s not found in lookup.', cls.__name__) +def needs_label(model_field, field_name): + """ + Returns `True` if the label based on the model's verbose name + is not equal to the default label it would have based on it's field name. + """ + return capfirst(model_field.verbose_name) != field_name_to_label(field_name) + + class ModelSerializer(Serializer): field_mapping = { models.AutoField: IntegerField, @@ -397,54 +406,55 @@ class ModelSerializer(Serializer): """ Return all the fields that should be serialized for the model. """ - info = modelinfo.get_field_info(self.opts.model) + info = model_meta.get_field_info(self.opts.model) ret = SortedDict() serializer_url_field = self.get_url_field() if serializer_url_field: ret[api_settings.URL_FIELD_NAME] = serializer_url_field - serializer_pk_field = self.get_pk_field(info.pk) + field_name = info.pk.name + serializer_pk_field = self.get_pk_field(field_name, info.pk) if serializer_pk_field: - ret[info.pk.name] = serializer_pk_field + ret[field_name] = serializer_pk_field # Regular fields for field_name, field in info.fields.items(): - ret[field_name] = self.get_field(field) + ret[field_name] = self.get_field(field_name, field) # Forward relations for field_name, relation_info in info.forward_relations.items(): if self.opts.depth: - ret[field_name] = self.get_nested_field(*relation_info) + ret[field_name] = self.get_nested_field(field_name, *relation_info) else: - ret[field_name] = self.get_related_field(*relation_info) + ret[field_name] = self.get_related_field(field_name, *relation_info) # Reverse relations for accessor_name, relation_info in info.reverse_relations.items(): if accessor_name in self.opts.fields: if self.opts.depth: - ret[accessor_name] = self.get_nested_field(*relation_info) + ret[accessor_name] = self.get_nested_field(accessor_name, *relation_info) else: - ret[accessor_name] = self.get_related_field(*relation_info) + ret[accessor_name] = self.get_related_field(accessor_name, *relation_info) return ret def get_url_field(self): return None - def get_pk_field(self, model_field): + def get_pk_field(self, field_name, model_field): """ Returns a default instance of the pk field. """ - return self.get_field(model_field) + return self.get_field(field_name, model_field) - def get_nested_field(self, model_field, related_model, to_many, has_through_model): + def get_nested_field(self, field_name, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a nested relational field. Note that model_field will be `None` for reverse relationships. """ - class NestedModelSerializer(ModelSerializer): # Not right! + class NestedModelSerializer(ModelSerializer): class Meta: model = related_model depth = self.opts.depth - 1 @@ -454,7 +464,7 @@ class ModelSerializer(Serializer): kwargs['many'] = True return NestedModelSerializer(**kwargs) - def get_related_field(self, model_field, related_model, to_many, has_through_model): + def get_related_field(self, field_name, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a flat relational field. @@ -474,8 +484,8 @@ class ModelSerializer(Serializer): if model_field: if model_field.null or model_field.blank: kwargs['required'] = False - if model_field.verbose_name: - kwargs['label'] = model_field.verbose_name + if model_field.verbose_name and needs_label(model_field, field_name): + kwargs['label'] = capfirst(model_field.verbose_name) if not model_field.editable: kwargs['read_only'] = True kwargs.pop('queryset', None) @@ -485,7 +495,7 @@ class ModelSerializer(Serializer): return PrimaryKeyRelatedField(**kwargs) - def get_field(self, model_field): + def get_field(self, field_name, model_field): """ Creates a default instance of a basic non-relational field. """ @@ -496,8 +506,8 @@ class ModelSerializer(Serializer): if model_field.null or model_field.blank: kwargs['required'] = False - if model_field.verbose_name is not None: - kwargs['label'] = model_field.verbose_name + if model_field.verbose_name and needs_label(model_field, field_name): + kwargs['label'] = capfirst(model_field.verbose_name) if model_field.help_text: kwargs['help_text'] = model_field.help_text @@ -642,11 +652,11 @@ class HyperlinkedModelSerializer(ModelSerializer): return HyperlinkedIdentityField(**kwargs) - def get_pk_field(self, model_field): + def get_pk_field(self, field_name, model_field): if self.opts.fields and model_field.name in self.opts.fields: return self.get_field(model_field) - def get_related_field(self, model_field, related_model, to_many, has_through_model): + def get_related_field(self, field_name, model_field, related_model, to_many, has_through_model): """ Creates a default instance of a flat relational field. """ @@ -665,8 +675,8 @@ class HyperlinkedModelSerializer(ModelSerializer): if model_field: if model_field.null or model_field.blank: kwargs['required'] = False - if model_field.verbose_name: - kwargs['label'] = model_field.verbose_name + if model_field.verbose_name and needs_label(model_field, field_name): + kwargs['label'] = capfirst(model_field.verbose_name) if not model_field.editable: kwargs['read_only'] = True kwargs.pop('queryset', None) diff --git a/rest_framework/utils/modelinfo.py b/rest_framework/utils/model_meta.py similarity index 100% rename from rest_framework/utils/modelinfo.py rename to rest_framework/utils/model_meta.py diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index d5750f9e3..b04ad5f2f 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -36,25 +36,25 @@ class RegularFieldsModel(models.Model): REGULAR_FIELDS_REPR = """ TestSerializer(): - auto_field = IntegerField(label='auto field', read_only=True) - big_integer_field = IntegerField(label='big integer field') - boolean_field = BooleanField(default=False, label='boolean field') - char_field = CharField(label='char field', max_length=100) - comma_seperated_integer_field = CharField(label='comma seperated integer field', max_length=100, validators=[<django.core.validators.RegexValidator object>]) - date_field = DateField(label='date field') - datetime_field = DateTimeField(label='datetime field') - decimal_field = DecimalField(decimal_places=1, label='decimal field', max_digits=3) - email_field = EmailField(label='email field', max_length=100) - float_field = FloatField(label='float field') - integer_field = IntegerField(label='integer field') - null_boolean_field = BooleanField(label='null boolean field', required=False) - positive_integer_field = IntegerField(label='positive integer field') - positive_small_integer_field = IntegerField(label='positive small integer field') - slug_field = SlugField(label='slug field', max_length=100) - small_integer_field = IntegerField(label='small integer field') - text_field = CharField(label='text field') - time_field = TimeField(label='time field') - url_field = URLField(label='url field', max_length=100) + auto_field = IntegerField(read_only=True) + big_integer_field = IntegerField() + boolean_field = BooleanField(default=False) + char_field = CharField(max_length=100) + comma_seperated_integer_field = CharField(max_length=100, validators=[<django.core.validators.RegexValidator object>]) + date_field = DateField() + datetime_field = DateTimeField() + decimal_field = DecimalField(decimal_places=1, max_digits=3) + email_field = EmailField(max_length=100) + float_field = FloatField() + integer_field = IntegerField() + null_boolean_field = BooleanField(required=False) + positive_integer_field = IntegerField() + positive_small_integer_field = IntegerField() + slug_field = SlugField(max_length=100) + small_integer_field = IntegerField() + text_field = CharField() + time_field = TimeField() + url_field = URLField(max_length=100) """.strip() @@ -81,9 +81,9 @@ class RelationalModel(models.Model): RELATIONAL_FLAT_REPR = """ TestSerializer(): id = IntegerField(label='ID', read_only=True) - foreign_key = PrimaryKeyRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all()) - one_to_one = PrimaryKeyRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all()) - many_to_many = PrimaryKeyRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all()) + foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all()) + one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all()) + many_to_many = PrimaryKeyRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all()) """.strip() @@ -92,22 +92,22 @@ TestSerializer(): id = IntegerField(label='ID', read_only=True) foreign_key = NestedModelSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) one_to_one = NestedModelSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) many_to_many = NestedModelSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) """.strip() HYPERLINKED_FLAT_REPR = """ TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = HyperlinkedRelatedField(label='foreign key', queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') - one_to_one = HyperlinkedRelatedField(label='one to one', queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') - many_to_many = HyperlinkedRelatedField(label='many to many', many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') + foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') + one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') + many_to_many = HyperlinkedRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') """.strip() @@ -116,13 +116,13 @@ TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') foreign_key = NestedModelSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) one_to_one = NestedModelSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) many_to_many = NestedModelSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) - name = CharField(label='name', max_length=100) + name = CharField(max_length=100) """.strip() @@ -180,4 +180,4 @@ class TestSerializerMappings(TestCase): # class Meta: # model = ManyToManyTargetModel # fields = ('id', 'name', 'reverse_many_to_many') - # print repr(TestSerializer()) \ No newline at end of file + # print repr(TestSerializer()) From 3cf7ed10d23300104a4613bd6cf3333fd05556ab Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 13:10:58 +0100 Subject: [PATCH 080/100] Fix erronous import --- tests/test_modelinfo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_modelinfo.py b/tests/test_modelinfo.py index 254a33c9c..04b67f041 100644 --- a/tests/test_modelinfo.py +++ b/tests/test_modelinfo.py @@ -1,6 +1,6 @@ from django.test import TestCase from django.utils import six -from rest_framework.utils.modelinfo import _resolve_model +from rest_framework.utils.model_meta import _resolve_model from tests.models import BasicModel From c1e2a9cba6b4b69281ad47b64132bf5ac5691875 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 13:38:28 +0100 Subject: [PATCH 081/100] Clean up field mapping tests --- tests/test_model_field_mappings.py | 166 ++++++++++++++--------------- 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index b04ad5f2f..8d59e9853 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -10,6 +10,10 @@ from django.test import TestCase from rest_framework import serializers +def dedent(blocktext): + return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]]) + + # Models for testing regular field mapping class RegularFieldsModel(models.Model): @@ -34,28 +38,36 @@ class RegularFieldsModel(models.Model): url_field = models.URLField(max_length=100) -REGULAR_FIELDS_REPR = """ -TestSerializer(): - auto_field = IntegerField(read_only=True) - big_integer_field = IntegerField() - boolean_field = BooleanField(default=False) - char_field = CharField(max_length=100) - comma_seperated_integer_field = CharField(max_length=100, validators=[<django.core.validators.RegexValidator object>]) - date_field = DateField() - datetime_field = DateTimeField() - decimal_field = DecimalField(decimal_places=1, max_digits=3) - email_field = EmailField(max_length=100) - float_field = FloatField() - integer_field = IntegerField() - null_boolean_field = BooleanField(required=False) - positive_integer_field = IntegerField() - positive_small_integer_field = IntegerField() - slug_field = SlugField(max_length=100) - small_integer_field = IntegerField() - text_field = CharField() - time_field = TimeField() - url_field = URLField(max_length=100) -""".strip() +class TestRegularFieldMappings(TestCase): + def test_regular_fields(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + + expected = dedent(""" + TestSerializer(): + auto_field = IntegerField(read_only=True) + big_integer_field = IntegerField() + boolean_field = BooleanField(default=False) + char_field = CharField(max_length=100) + comma_seperated_integer_field = CharField(max_length=100, validators=[<django.core.validators.RegexValidator object>]) + date_field = DateField() + datetime_field = DateTimeField() + decimal_field = DecimalField(decimal_places=1, max_digits=3) + email_field = EmailField(max_length=100) + float_field = FloatField() + integer_field = IntegerField() + null_boolean_field = BooleanField(required=False) + positive_integer_field = IntegerField() + positive_small_integer_field = IntegerField() + slug_field = SlugField(max_length=100) + small_integer_field = IntegerField() + text_field = CharField() + time_field = TimeField() + url_field = URLField(max_length=100) + """) + + self.assertEqual(repr(TestSerializer()), expected) # Model for testing relational field mapping @@ -78,88 +90,76 @@ class RelationalModel(models.Model): one_to_one = models.OneToOneField(OneToOneTargetModel, related_name='reverse_one_to_one') -RELATIONAL_FLAT_REPR = """ -TestSerializer(): - id = IntegerField(label='ID', read_only=True) - foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all()) - one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all()) - many_to_many = PrimaryKeyRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all()) -""".strip() - - -RELATIONAL_NESTED_REPR = """ -TestSerializer(): - id = IntegerField(label='ID', read_only=True) - foreign_key = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) - one_to_one = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) - many_to_many = NestedModelSerializer(many=True, read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) -""".strip() - - -HYPERLINKED_FLAT_REPR = """ -TestSerializer(): - url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') - one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') - many_to_many = HyperlinkedRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') -""".strip() - - -HYPERLINKED_NESTED_REPR = """ -TestSerializer(): - url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) - one_to_one = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) - many_to_many = NestedModelSerializer(many=True, read_only=True): - id = IntegerField(label='ID', read_only=True) - name = CharField(max_length=100) -""".strip() - - -class TestSerializerMappings(TestCase): - maxDiff = 10000 - - def test_regular_fields(self): - class TestSerializer(serializers.ModelSerializer): - class Meta: - model = RegularFieldsModel - self.assertEqual(repr(TestSerializer()), REGULAR_FIELDS_REPR) - +class TestRelationalFieldMappings(TestCase): def test_flat_relational_fields(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = RelationalModel - self.assertEqual(repr(TestSerializer()), RELATIONAL_FLAT_REPR) + + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all()) + one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all()) + many_to_many = PrimaryKeyRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all()) + """) + self.assertEqual(repr(TestSerializer()), expected) def test_nested_relational_fields(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = RelationalModel depth = 1 - self.assertEqual(repr(TestSerializer()), RELATIONAL_NESTED_REPR) + + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + foreign_key = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + one_to_one = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + many_to_many = NestedModelSerializer(many=True, read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + """) + self.assertEqual(repr(TestSerializer()), expected) def test_flat_hyperlinked_fields(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = RelationalModel - self.assertEqual(repr(TestSerializer()), HYPERLINKED_FLAT_REPR) + + expected = dedent(""" + TestSerializer(): + url = HyperlinkedIdentityField(view_name='relationalmodel-detail') + foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') + one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') + many_to_many = HyperlinkedRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') + """) + self.assertEqual(repr(TestSerializer()), expected) def test_nested_hyperlinked_fields(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = RelationalModel depth = 1 - self.assertEqual(repr(TestSerializer()), HYPERLINKED_NESTED_REPR) + + expected = dedent(""" + TestSerializer(): + url = HyperlinkedIdentityField(view_name='relationalmodel-detail') + foreign_key = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + one_to_one = NestedModelSerializer(read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + many_to_many = NestedModelSerializer(many=True, read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + """) + self.assertEqual(repr(TestSerializer()), expected) # def test_flat_reverse_foreign_key(self): # class TestSerializer(serializers.ModelSerializer): From 0c15b97b331f7937cbc5d85f660c07b3c92917a0 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 13:48:03 +0100 Subject: [PATCH 082/100] Tests for reverse relationships --- tests/test_model_field_mappings.py | 59 ++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index 8d59e9853..a32345b31 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -158,26 +158,47 @@ class TestRelationalFieldMappings(TestCase): many_to_many = NestedModelSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) - """) + """) self.assertEqual(repr(TestSerializer()), expected) - # def test_flat_reverse_foreign_key(self): - # class TestSerializer(serializers.ModelSerializer): - # class Meta: - # model = ForeignKeyTargetModel - # fields = ('id', 'name', 'reverse_foreign_key') - # print repr(TestSerializer()) + def test_flat_reverse_foreign_key(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = ForeignKeyTargetModel + fields = ('id', 'name', 'reverse_foreign_key') - # def test_flat_reverse_one_to_one(self): - # class TestSerializer(serializers.ModelSerializer): - # class Meta: - # model = OneToOneTargetModel - # fields = ('id', 'name', 'reverse_one_to_one') - # print repr(TestSerializer()) + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + reverse_foreign_key = PrimaryKeyRelatedField(many=True, queryset=RelationalModel.objects.all()) + """) + self.assertEqual(repr(TestSerializer()), expected) - # def test_flat_reverse_many_to_many(self): - # class TestSerializer(serializers.ModelSerializer): - # class Meta: - # model = ManyToManyTargetModel - # fields = ('id', 'name', 'reverse_many_to_many') - # print repr(TestSerializer()) + def test_flat_reverse_one_to_one(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = OneToOneTargetModel + fields = ('id', 'name', 'reverse_one_to_one') + + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + reverse_one_to_one = PrimaryKeyRelatedField(queryset=RelationalModel.objects.all()) + """) + self.assertEqual(repr(TestSerializer()), expected) + + def test_flat_reverse_many_to_many(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = ManyToManyTargetModel + fields = ('id', 'name', 'reverse_many_to_many') + + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + reverse_many_to_many = PrimaryKeyRelatedField(many=True, queryset=RelationalModel.objects.all()) + """) + self.assertEqual(repr(TestSerializer()), expected) From d196608d5af912057baba79ab13d05d876368ad2 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 13:55:09 +0100 Subject: [PATCH 083/100] Fix nested model serializer base class --- rest_framework/serializers.py | 7 ++++++- tests/test_model_field_mappings.py | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index ba8d475f8..40d768975 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -368,6 +368,7 @@ class ModelSerializer(Serializer): models.TimeField: TimeField, models.URLField: URLField, } + nested_class = None # We fill this in at the end of this module. _options_class = ModelSerializerOptions @@ -454,7 +455,7 @@ class ModelSerializer(Serializer): Note that model_field will be `None` for reverse relationships. """ - class NestedModelSerializer(ModelSerializer): + class NestedModelSerializer(self.nested_class): class Meta: model = related_model depth = self.opts.depth - 1 @@ -694,3 +695,7 @@ class HyperlinkedModelSerializer(ModelSerializer): 'app_label': model._meta.app_label, 'model_name': model._meta.object_name.lower() } + + +ModelSerializer.nested_class = ModelSerializer +HyperlinkedModelSerializer.nested_class = HyperlinkedModelSerializer diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index a32345b31..d946172e4 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -150,13 +150,13 @@ class TestRelationalFieldMappings(TestCase): TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') foreign_key = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail') name = CharField(max_length=100) one_to_one = NestedModelSerializer(read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail') name = CharField(max_length=100) many_to_many = NestedModelSerializer(many=True, read_only=True): - id = IntegerField(label='ID', read_only=True) + url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') name = CharField(max_length=100) """) self.assertEqual(repr(TestSerializer()), expected) From 4ddc661b01410d0073d463ae729f152cabf2ef4d Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Mon, 15 Sep 2014 14:05:58 +0100 Subject: [PATCH 084/100] Tests for through relationships --- tests/test_model_field_mappings.py | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index d946172e4..957bf6613 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -84,10 +84,21 @@ class OneToOneTargetModel(models.Model): name = models.CharField(max_length=100) +class ThroughTargetModel(models.Model): + name = models.CharField(max_length=100) + + +class Supplementary(models.Model): + extra = models.IntegerField() + forwards = models.ForeignKey('ThroughTargetModel') + backwards = models.ForeignKey('RelationalModel') + + class RelationalModel(models.Model): foreign_key = models.ForeignKey(ForeignKeyTargetModel, related_name='reverse_foreign_key') many_to_many = models.ManyToManyField(ManyToManyTargetModel, related_name='reverse_many_to_many') one_to_one = models.OneToOneField(OneToOneTargetModel, related_name='reverse_one_to_one') + through = models.ManyToManyField(ThroughTargetModel, through=Supplementary, related_name='reverse_through') class TestRelationalFieldMappings(TestCase): @@ -102,6 +113,7 @@ class TestRelationalFieldMappings(TestCase): foreign_key = PrimaryKeyRelatedField(queryset=ForeignKeyTargetModel.objects.all()) one_to_one = PrimaryKeyRelatedField(queryset=OneToOneTargetModel.objects.all()) many_to_many = PrimaryKeyRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all()) + through = PrimaryKeyRelatedField(many=True, read_only=True) """) self.assertEqual(repr(TestSerializer()), expected) @@ -123,6 +135,9 @@ class TestRelationalFieldMappings(TestCase): many_to_many = NestedModelSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) + through = NestedModelSerializer(many=True, read_only=True): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) """) self.assertEqual(repr(TestSerializer()), expected) @@ -137,6 +152,7 @@ class TestRelationalFieldMappings(TestCase): foreign_key = HyperlinkedRelatedField(queryset=ForeignKeyTargetModel.objects.all(), view_name='foreignkeytargetmodel-detail') one_to_one = HyperlinkedRelatedField(queryset=OneToOneTargetModel.objects.all(), view_name='onetoonetargetmodel-detail') many_to_many = HyperlinkedRelatedField(many=True, queryset=ManyToManyTargetModel.objects.all(), view_name='manytomanytargetmodel-detail') + through = HyperlinkedRelatedField(many=True, read_only=True, view_name='throughtargetmodel-detail') """) self.assertEqual(repr(TestSerializer()), expected) @@ -158,6 +174,9 @@ class TestRelationalFieldMappings(TestCase): many_to_many = NestedModelSerializer(many=True, read_only=True): url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') name = CharField(max_length=100) + through = NestedModelSerializer(many=True, read_only=True): + url = HyperlinkedIdentityField(view_name='throughtargetmodel-detail') + name = CharField(max_length=100) """) self.assertEqual(repr(TestSerializer()), expected) @@ -202,3 +221,17 @@ class TestRelationalFieldMappings(TestCase): reverse_many_to_many = PrimaryKeyRelatedField(many=True, queryset=RelationalModel.objects.all()) """) self.assertEqual(repr(TestSerializer()), expected) + + def test_flat_reverse_through(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = ThroughTargetModel + fields = ('id', 'name', 'reverse_through') + + expected = dedent(""" + TestSerializer(): + id = IntegerField(label='ID', read_only=True) + name = CharField(max_length=100) + reverse_through = PrimaryKeyRelatedField(many=True, read_only=True) + """) + self.assertEqual(repr(TestSerializer()), expected) From 3725a1e77dafef4c6bdb7847bace92ff61f7f4e5 Mon Sep 17 00:00:00 2001 From: "S. Andrew Sheppard" <andrew@wq.io> Date: Mon, 15 Sep 2014 14:46:09 -0500 Subject: [PATCH 085/100] add wq.db router and django-rest-pandas renderers --- docs/topics/third-party-resources.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/topics/third-party-resources.md b/docs/topics/third-party-resources.md index e337cbd54..1ca917427 100644 --- a/docs/topics/third-party-resources.md +++ b/docs/topics/third-party-resources.md @@ -41,6 +41,7 @@ To submit new content, [open an issue](https://github.com/tomchristie/django-res ### Routers * [drf-nested-routers](https://github.com/alanjds/drf-nested-routers) - Provides routers and relationship fields for working with nested resources. +* [wq.db.rest](http://wq.io/docs/about-rest) - Provides an admin-style model registration API with reasonable default URLs and viewsets. ### Parsers @@ -51,6 +52,7 @@ To submit new content, [open an issue](https://github.com/tomchristie/django-res * [djangorestframework-csv](https://github.com/mjumbewu/django-rest-framework-csv) - Provides CSV renderer support. * [drf_ujson](https://github.com/gizmag/drf-ujson-renderer) - Implements JSON rendering using the UJSON package. +* [Django REST Pandas](https://github.com/wq/django-rest-pandas) - Pandas DataFrame-powered renderers including Excel, CSV, and SVG formats. ### Filtering From e5af0bbb353e772473b1d9fcfc896bfd7365db2a Mon Sep 17 00:00:00 2001 From: Matthew J Morrison <mattjmorrison@mattjmorrison.com> Date: Wed, 17 Sep 2014 07:49:54 -0500 Subject: [PATCH 086/100] Clarify "raised inside REST framework" I ran into an issue today where I was not seeing the rest_framework.views.exception_handler do what I thought it should be doing. It turned out that I had imported View from rest_framework.views rather than importing APIView from rest_framework.views. The phrase "raised inside REST framework" was confusing as I was debugging this issue. I was unsure if that meant that I could raise those exceptions in my code or if it had to originate from within framework code. I'm not sure if the proposed wording is ideal, I just wanted to point out what I found to be confusing. --- docs/api-guide/exceptions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/exceptions.md b/docs/api-guide/exceptions.md index 66e181737..33c4dc910 100644 --- a/docs/api-guide/exceptions.md +++ b/docs/api-guide/exceptions.md @@ -84,7 +84,7 @@ Note that the exception handler will only be called for responses generated by r **Signature:** `APIException()` -The **base class** for all exceptions raised inside REST framework. +The **base class** for all exceptions raised inside an APIView class or @api_view. To provide a custom exception, subclass `APIException` and set the `.status_code` and `.default_detail` properties on the class. From a37db382c69293953c709365cd2c64c4a99f419e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Wed, 17 Sep 2014 09:01:49 -0400 Subject: [PATCH 087/100] Update authtoken latest Django 1.7 migration --- rest_framework/authtoken/migrations/0001_initial.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 2e5d6b47e..6ea761822 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# encoding: utf8 + from __future__ import unicode_literals from django.db import models, migrations @@ -16,11 +16,10 @@ class Migration(migrations.Migration): name='Token', fields=[ ('key', models.CharField(max_length=40, serialize=False, primary_key=True)), - ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL, to_field='id')), ('created', models.DateTimeField(auto_now_add=True)), + ('user', models.OneToOneField(related_name=b'auth_token', to=settings.AUTH_USER_MODEL)), ], options={ - 'abstract': False, }, bases=(models.Model,), ), From c0155fd9dc654dc5932effd46a00f66495ce700b Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 17 Sep 2014 14:11:53 +0100 Subject: [PATCH 088/100] Update comments --- rest_framework/serializers.py | 2 ++ tests/test_model_field_mappings.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 40d768975..1fea13802 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -410,10 +410,12 @@ class ModelSerializer(Serializer): info = model_meta.get_field_info(self.opts.model) ret = SortedDict() + # URL field serializer_url_field = self.get_url_field() if serializer_url_field: ret[api_settings.URL_FIELD_NAME] = serializer_url_field + # Primary key field field_name = info.pk.name serializer_pk_field = self.get_pk_field(field_name, info.pk) if serializer_pk_field: diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index 957bf6613..bae63e5a7 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -14,7 +14,7 @@ def dedent(blocktext): return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]]) -# Models for testing regular field mapping +# Testing regular field mappings class RegularFieldsModel(models.Model): auto_field = models.AutoField(primary_key=True) @@ -70,7 +70,7 @@ class TestRegularFieldMappings(TestCase): self.assertEqual(repr(TestSerializer()), expected) -# Model for testing relational field mapping +# Testing relational field mappings class ForeignKeyTargetModel(models.Model): name = models.CharField(max_length=100) From de5fbf7d63245e9d14844e66fdf16f88dbfae2e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= <jpadilla@webapplicate.com> Date: Wed, 17 Sep 2014 10:23:53 -0400 Subject: [PATCH 089/100] Update initial migration to work on Python 3 --- rest_framework/authtoken/migrations/0001_initial.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 6ea761822..769f62029 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -1,4 +1,4 @@ - +# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models, migrations @@ -15,9 +15,9 @@ class Migration(migrations.Migration): migrations.CreateModel( name='Token', fields=[ - ('key', models.CharField(max_length=40, serialize=False, primary_key=True)), + ('key', models.CharField(primary_key=True, serialize=False, max_length=40)), ('created', models.DateTimeField(auto_now_add=True)), - ('user', models.OneToOneField(related_name=b'auth_token', to=settings.AUTH_USER_MODEL)), + ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL, related_name='auth_token')), ], options={ }, From 8c8d355e761a67fffd334dc15bc7bb74238300bc Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Wed, 17 Sep 2014 15:51:17 +0100 Subject: [PATCH 090/100] Update routers.py --- rest_framework/routers.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 01fd4b91a..f2d062118 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -295,6 +295,7 @@ class DefaultRouter(SimpleRouter): format=kwargs.get('format', None) ) except NoReverseMatch: + # Don't bail out if eg. no list routes exist, only detail routes. continue return Response(ret) From 764366b2e11ad9ad85dd34500e95721011cae7d4 Mon Sep 17 00:00:00 2001 From: Matthew J Morrison <mattjmorrison@mattjmorrison.com> Date: Wed, 17 Sep 2014 11:29:15 -0500 Subject: [PATCH 091/100] Fixed code formatting --- docs/api-guide/exceptions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/exceptions.md b/docs/api-guide/exceptions.md index 33c4dc910..e61dcfa90 100644 --- a/docs/api-guide/exceptions.md +++ b/docs/api-guide/exceptions.md @@ -84,7 +84,7 @@ Note that the exception handler will only be called for responses generated by r **Signature:** `APIException()` -The **base class** for all exceptions raised inside an APIView class or @api_view. +The **base class** for all exceptions raised inside an `APIView` class or `@api_view`. To provide a custom exception, subclass `APIException` and set the `.status_code` and `.default_detail` properties on the class. From 5b7e4af0d657a575cb15eea85a63a7100c636085 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 11:20:56 +0100 Subject: [PATCH 092/100] get_base_field() refactor --- rest_framework/fields.py | 6 +- rest_framework/relations.py | 9 +- rest_framework/serializers.py | 454 ++++++-------------------- rest_framework/utils/field_mapping.py | 215 ++++++++++++ rest_framework/utils/model_meta.py | 46 ++- tests/models.py | 7 - tests/test_model_field_mappings.py | 16 +- tests/test_response.py | 9 +- tests/test_routers.py | 7 +- 9 files changed, 379 insertions(+), 390 deletions(-) create mode 100644 rest_framework/utils/field_mapping.py diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 1818e705e..0c78b3fbb 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -80,10 +80,6 @@ def set_value(dictionary, keys, value): dictionary[keys[-1]] = value -def field_name_to_label(field_name): - return field_name.replace('_', ' ').capitalize() - - class SkipField(Exception): pass @@ -162,7 +158,7 @@ class Field(object): # `self.label` should deafult to being based on the field name. if self.label is None: - self.label = field_name_to_label(self.field_name) + self.label = field_name.replace('_', ' ').capitalize() # self.source should default to being the same as the field name. if self.source is None: diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 46fe55ef9..9f44ab633 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -73,7 +73,8 @@ class HyperlinkedRelatedField(RelatedField): 'incorrect_type': 'Incorrect type. Expected URL string, received {data_type}.', } - def __init__(self, view_name, **kwargs): + def __init__(self, view_name=None, **kwargs): + assert view_name is not None, 'The `view_name` argument is required.' self.view_name = view_name self.lookup_field = kwargs.pop('lookup_field', self.lookup_field) self.lookup_url_kwarg = kwargs.pop('lookup_url_kwarg', self.lookup_field) @@ -182,7 +183,8 @@ class HyperlinkedIdentityField(HyperlinkedRelatedField): URL of relationships to other objects. """ - def __init__(self, view_name, **kwargs): + def __init__(self, view_name=None, **kwargs): + assert view_name is not None, 'The `view_name` argument is required.' kwargs['read_only'] = True kwargs['source'] = '*' super(HyperlinkedIdentityField, self).__init__(view_name, **kwargs) @@ -199,7 +201,8 @@ class SlugRelatedField(RelatedField): 'invalid': _('Invalid value.'), } - def __init__(self, slug_field, **kwargs): + def __init__(self, slug_field=None, **kwargs): + assert slug_field is not None, 'The `slug_field` argument is required.' self.slug_field = slug_field super(SlugRelatedField, self).__init__(**kwargs) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 1fea13802..99dcc349f 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -10,17 +10,19 @@ python primitives. 2. The process of marshalling between python primitives and request and response content is handled by parsers and renderers. """ -from django.core import validators from django.core.exceptions import ValidationError from django.db import models from django.utils import six from django.utils.datastructures import SortedDict -from django.utils.text import capfirst from collections import namedtuple -from rest_framework.compat import clean_manytomany_helptext from rest_framework.fields import empty, set_value, Field, SkipField from rest_framework.settings import api_settings from rest_framework.utils import html, model_meta, representation +from rest_framework.utils.field_mapping import ( + get_url_kwargs, get_field_kwargs, + get_relation_kwargs, get_nested_relation_kwargs, + lookup_class +) import copy # Note: We do the following so that users of the framework can use this style: @@ -126,7 +128,7 @@ class SerializerMetaclass(type): """ @classmethod - def _get_fields(cls, bases, attrs): + def _get_declared_fields(cls, bases, attrs): fields = [(field_name, attrs.pop(field_name)) for field_name, obj in list(attrs.items()) if isinstance(obj, Field)] @@ -136,25 +138,18 @@ class SerializerMetaclass(type): # fields. Note that we loop over the bases in *reverse*. This is necessary # in order to maintain the correct order of fields. for base in bases[::-1]: - if hasattr(base, 'base_fields'): - fields = list(base.base_fields.items()) + fields + if hasattr(base, '_declared_fields'): + fields = list(base._declared_fields.items()) + fields return SortedDict(fields) def __new__(cls, name, bases, attrs): - attrs['base_fields'] = cls._get_fields(bases, attrs) + attrs['_declared_fields'] = cls._get_declared_fields(bases, attrs) return super(SerializerMetaclass, cls).__new__(cls, name, bases, attrs) @six.add_metaclass(SerializerMetaclass) class Serializer(BaseSerializer): - - def __new__(cls, *args, **kwargs): - if kwargs.pop('many', False): - kwargs['child'] = cls() - return ListSerializer(*args, **kwargs) - return super(Serializer, cls).__new__(cls, *args, **kwargs) - def __init__(self, *args, **kwargs): self.context = kwargs.pop('context', {}) kwargs.pop('partial', None) @@ -165,14 +160,22 @@ class Serializer(BaseSerializer): # Every new serializer is created with a clone of the field instances. # This allows users to dynamically modify the fields on a serializer # instance without affecting every other serializer class. - self.fields = self.get_fields() + self.fields = self._get_base_fields() # Setup all the child fields, to provide them with the current context. for field_name, field in self.fields.items(): field.bind(field_name, self, self) - def get_fields(self): - return copy.deepcopy(self.base_fields) + def __new__(cls, *args, **kwargs): + # We override this method in order to automagically create + # `ListSerializer` classes instead when `many=True` is set. + if kwargs.pop('many', False): + kwargs['child'] = cls() + return ListSerializer(*args, **kwargs) + return super(Serializer, cls).__new__(cls, *args, **kwargs) + + def _get_base_fields(self): + return copy.deepcopy(self._declared_fields) def bind(self, field_name, parent, root): # If the serializer is used as a field then when it becomes bound @@ -312,39 +315,8 @@ class ListSerializer(BaseSerializer): return representation.list_repr(self, indent=1) -class ModelSerializerOptions(object): - """ - Meta class options for ModelSerializer - """ - def __init__(self, meta): - self.model = getattr(meta, 'model') - self.fields = getattr(meta, 'fields', ()) - self.depth = getattr(meta, 'depth', 0) - - -def lookup_class(mapping, instance): - """ - Takes a dictionary with classes as keys, and an object. - Traverses the object's inheritance hierarchy in method - resolution order, and returns the first matching value - from the dictionary or raises a KeyError if nothing matches. - """ - for cls in inspect.getmro(instance.__class__): - if cls in mapping: - return mapping[cls] - raise KeyError('Class %s not found in lookup.', cls.__name__) - - -def needs_label(model_field, field_name): - """ - Returns `True` if the label based on the model's verbose name - is not equal to the default label it would have based on it's field name. - """ - return capfirst(model_field.verbose_name) != field_name_to_label(field_name) - - class ModelSerializer(Serializer): - field_mapping = { + _field_mapping = { models.AutoField: IntegerField, models.BigIntegerField: IntegerField, models.BooleanField: BooleanField, @@ -368,16 +340,10 @@ class ModelSerializer(Serializer): models.TimeField: TimeField, models.URLField: URLField, } - nested_class = None # We fill this in at the end of this module. - - _options_class = ModelSerializerOptions - - def __init__(self, *args, **kwargs): - self.opts = self._options_class(self.Meta) - super(ModelSerializer, self).__init__(*args, **kwargs) + _related_class = PrimaryKeyRelatedField def create(self, attrs): - ModelClass = self.opts.model + ModelClass = self.Meta.model return ModelClass.objects.create(**attrs) def update(self, obj, attrs): @@ -385,319 +351,97 @@ class ModelSerializer(Serializer): setattr(obj, attr, value) obj.save() - def get_fields(self): - # Get the explicitly declared fields. - fields = copy.deepcopy(self.base_fields) + def _get_base_fields(self): + declared_fields = copy.deepcopy(self._declared_fields) - # Add in the default fields. - for key, val in self.get_default_fields().items(): - if key not in fields: - fields[key] = val - - # If `fields` is set on the `Meta` class, - # then use only those fields, and in that order. - if self.opts.fields: - fields = SortedDict([ - (key, fields[key]) for key in self.opts.fields - ]) - - return fields - - def get_default_fields(self): - """ - Return all the fields that should be serialized for the model. - """ - info = model_meta.get_field_info(self.opts.model) ret = SortedDict() + model = getattr(self.Meta, 'model') + fields = getattr(self.Meta, 'fields', None) + depth = getattr(self.Meta, 'depth', 0) - # URL field - serializer_url_field = self.get_url_field() - if serializer_url_field: - ret[api_settings.URL_FIELD_NAME] = serializer_url_field + # Retrieve metadata about fields & relationships on the model class. + info = model_meta.get_field_info(model) - # Primary key field - field_name = info.pk.name - serializer_pk_field = self.get_pk_field(field_name, info.pk) - if serializer_pk_field: - ret[field_name] = serializer_pk_field + # Use the default set of fields if none is supplied explicitly. + if fields is None: + fields = self._get_default_field_names(declared_fields, info) - # Regular fields - for field_name, field in info.fields.items(): - ret[field_name] = self.get_field(field_name, field) + for field_name in fields: + if field_name in declared_fields: + # Field is explicitly declared on the class, use that. + ret[field_name] = declared_fields[field_name] + continue - # Forward relations - for field_name, relation_info in info.forward_relations.items(): - if self.opts.depth: - ret[field_name] = self.get_nested_field(field_name, *relation_info) - else: - ret[field_name] = self.get_related_field(field_name, *relation_info) + elif field_name == api_settings.URL_FIELD_NAME: + # Create the URL field. + field_cls = HyperlinkedIdentityField + kwargs = get_url_kwargs(model) - # Reverse relations - for accessor_name, relation_info in info.reverse_relations.items(): - if accessor_name in self.opts.fields: - if self.opts.depth: - ret[accessor_name] = self.get_nested_field(accessor_name, *relation_info) + elif field_name in info.fields_and_pk: + # Create regular model fields. + model_field = info.fields_and_pk[field_name] + field_cls = lookup_class(self._field_mapping, model_field) + kwargs = get_field_kwargs(field_name, model_field) + if 'choices' in kwargs: + # Fields with choices get coerced into `ChoiceField` + # instead of using their regular typed field. + field_cls = ChoiceField + if not issubclass(field_cls, ModelField): + # `model_field` is only valid for the fallback case of + # `ModelField`, which is used when no other typed field + # matched to the model field. + kwargs.pop('model_field', None) + + elif field_name in info.relations: + # Create forward and reverse relationships. + relation_info = info.relations[field_name] + if depth: + field_cls = self._get_nested_class(depth, relation_info) + kwargs = get_nested_relation_kwargs(relation_info) else: - ret[accessor_name] = self.get_related_field(accessor_name, *relation_info) + field_cls = self._related_class + kwargs = get_relation_kwargs(field_name, relation_info) + # `view_name` is only valid for hyperlinked relationships. + if not issubclass(field_cls, HyperlinkedRelatedField): + kwargs.pop('view_name', None) + + else: + assert False, 'Field name `%s` is not valid.' % field_name + + ret[field_name] = field_cls(**kwargs) return ret - def get_url_field(self): - return None + def _get_default_field_names(self, declared_fields, model_info): + return ( + [model_info.pk.name] + + list(declared_fields.keys()) + + list(model_info.fields.keys()) + + list(model_info.forward_relations.keys()) + ) - def get_pk_field(self, field_name, model_field): - """ - Returns a default instance of the pk field. - """ - return self.get_field(field_name, model_field) - - def get_nested_field(self, field_name, model_field, related_model, to_many, has_through_model): - """ - Creates a default instance of a nested relational field. - - Note that model_field will be `None` for reverse relationships. - """ - class NestedModelSerializer(self.nested_class): + def _get_nested_class(self, nested_depth, relation_info): + class NestedSerializer(ModelSerializer): class Meta: - model = related_model - depth = self.opts.depth - 1 - - kwargs = {'read_only': True} - if to_many: - kwargs['many'] = True - return NestedModelSerializer(**kwargs) - - def get_related_field(self, field_name, model_field, related_model, to_many, has_through_model): - """ - Creates a default instance of a flat relational field. - - Note that model_field will be `None` for reverse relationships. - """ - kwargs = { - 'queryset': related_model._default_manager, - } - - if to_many: - kwargs['many'] = True - - if has_through_model: - kwargs['read_only'] = True - kwargs.pop('queryset', None) - - if model_field: - if model_field.null or model_field.blank: - kwargs['required'] = False - if model_field.verbose_name and needs_label(model_field, field_name): - kwargs['label'] = capfirst(model_field.verbose_name) - if not model_field.editable: - kwargs['read_only'] = True - kwargs.pop('queryset', None) - help_text = clean_manytomany_helptext(model_field.help_text) - if help_text: - kwargs['help_text'] = help_text - - return PrimaryKeyRelatedField(**kwargs) - - def get_field(self, field_name, model_field): - """ - Creates a default instance of a basic non-relational field. - """ - serializer_cls = lookup_class(self.field_mapping, model_field) - kwargs = {} - validator_kwarg = model_field.validators - - if model_field.null or model_field.blank: - kwargs['required'] = False - - if model_field.verbose_name and needs_label(model_field, field_name): - kwargs['label'] = capfirst(model_field.verbose_name) - - if model_field.help_text: - kwargs['help_text'] = model_field.help_text - - if isinstance(model_field, models.AutoField) or not model_field.editable: - kwargs['read_only'] = True - # Read only implies that the field is not required. - # We have a cleaner repr on the instance if we don't set it. - kwargs.pop('required', None) - - if model_field.has_default(): - kwargs['default'] = model_field.get_default() - # Having a default implies that the field is not required. - # We have a cleaner repr on the instance if we don't set it. - kwargs.pop('required', None) - - if model_field.flatchoices: - # If this model field contains choices, then use a ChoiceField, - # rather than the standard serializer field for this type. - # Note that we return this prior to setting any validation type - # keyword arguments, as those are not valid initializers. - kwargs['choices'] = model_field.flatchoices - return ChoiceField(**kwargs) - - # Ensure that max_length is passed explicitly as a keyword arg, - # rather than as a validator. - max_length = getattr(model_field, 'max_length', None) - if max_length is not None: - kwargs['max_length'] = max_length - validator_kwarg = [ - validator for validator in validator_kwarg - if not isinstance(validator, validators.MaxLengthValidator) - ] - - # Ensure that min_length is passed explicitly as a keyword arg, - # rather than as a validator. - min_length = getattr(model_field, 'min_length', None) - if min_length is not None: - kwargs['min_length'] = min_length - validator_kwarg = [ - validator for validator in validator_kwarg - if not isinstance(validator, validators.MinLengthValidator) - ] - - # Ensure that max_value is passed explicitly as a keyword arg, - # rather than as a validator. - max_value = next(( - validator.limit_value for validator in validator_kwarg - if isinstance(validator, validators.MaxValueValidator) - ), None) - if max_value is not None: - kwargs['max_value'] = max_value - validator_kwarg = [ - validator for validator in validator_kwarg - if not isinstance(validator, validators.MaxValueValidator) - ] - - # Ensure that max_value is passed explicitly as a keyword arg, - # rather than as a validator. - min_value = next(( - validator.limit_value for validator in validator_kwarg - if isinstance(validator, validators.MinValueValidator) - ), None) - if min_value is not None: - kwargs['min_value'] = min_value - validator_kwarg = [ - validator for validator in validator_kwarg - if not isinstance(validator, validators.MinValueValidator) - ] - - # URLField does not need to include the URLValidator argument, - # as it is explicitly added in. - if isinstance(model_field, models.URLField): - validator_kwarg = [ - validator for validator in validator_kwarg - if not isinstance(validator, validators.URLValidator) - ] - - # EmailField does not need to include the validate_email argument, - # as it is explicitly added in. - if isinstance(model_field, models.EmailField): - validator_kwarg = [ - validator for validator in validator_kwarg - if validator is not validators.validate_email - ] - - # SlugField do not need to include the 'validate_slug' argument, - if isinstance(model_field, models.SlugField): - validator_kwarg = [ - validator for validator in validator_kwarg - if validator is not validators.validate_slug - ] - - max_digits = getattr(model_field, 'max_digits', None) - if max_digits is not None: - kwargs['max_digits'] = max_digits - - decimal_places = getattr(model_field, 'decimal_places', None) - if decimal_places is not None: - kwargs['decimal_places'] = decimal_places - - if isinstance(model_field, models.BooleanField): - # models.BooleanField has `blank=True`, but *is* actually - # required *unless* a default is provided. - # Also note that Django<1.6 uses `default=False` for - # models.BooleanField, but Django>=1.6 uses `default=None`. - kwargs.pop('required', None) - - if validator_kwarg: - kwargs['validators'] = validator_kwarg - - if issubclass(serializer_cls, ModelField): - kwargs['model_field'] = model_field - - return serializer_cls(**kwargs) - - -class HyperlinkedModelSerializerOptions(ModelSerializerOptions): - """ - Options for HyperlinkedModelSerializer - """ - def __init__(self, meta): - super(HyperlinkedModelSerializerOptions, self).__init__(meta) - self.view_name = getattr(meta, 'view_name', None) - self.lookup_field = getattr(meta, 'lookup_field', None) + model = relation_info.related + depth = nested_depth + return NestedSerializer class HyperlinkedModelSerializer(ModelSerializer): - _options_class = HyperlinkedModelSerializerOptions + _related_class = HyperlinkedRelatedField - def get_url_field(self): - if self.opts.view_name is not None: - view_name = self.opts.view_name - else: - view_name = self.get_default_view_name(self.opts.model) + def _get_default_field_names(self, declared_fields, model_info): + return ( + [api_settings.URL_FIELD_NAME] + + list(declared_fields.keys()) + + list(model_info.fields.keys()) + + list(model_info.forward_relations.keys()) + ) - kwargs = { - 'view_name': view_name - } - if self.opts.lookup_field: - kwargs['lookup_field'] = self.opts.lookup_field - - return HyperlinkedIdentityField(**kwargs) - - def get_pk_field(self, field_name, model_field): - if self.opts.fields and model_field.name in self.opts.fields: - return self.get_field(model_field) - - def get_related_field(self, field_name, model_field, related_model, to_many, has_through_model): - """ - Creates a default instance of a flat relational field. - """ - kwargs = { - 'queryset': related_model._default_manager, - 'view_name': self.get_default_view_name(related_model), - } - - if to_many: - kwargs['many'] = True - - if has_through_model: - kwargs['read_only'] = True - kwargs.pop('queryset', None) - - if model_field: - if model_field.null or model_field.blank: - kwargs['required'] = False - if model_field.verbose_name and needs_label(model_field, field_name): - kwargs['label'] = capfirst(model_field.verbose_name) - if not model_field.editable: - kwargs['read_only'] = True - kwargs.pop('queryset', None) - help_text = clean_manytomany_helptext(model_field.help_text) - if help_text: - kwargs['help_text'] = help_text - - return HyperlinkedRelatedField(**kwargs) - - def get_default_view_name(self, model): - """ - Return the view name to use for related models. - """ - return '%(model_name)s-detail' % { - 'app_label': model._meta.app_label, - 'model_name': model._meta.object_name.lower() - } - - -ModelSerializer.nested_class = ModelSerializer -HyperlinkedModelSerializer.nested_class = HyperlinkedModelSerializer + def _get_nested_class(self, nested_depth, relation_info): + class NestedSerializer(HyperlinkedModelSerializer): + class Meta: + model = relation_info.related + depth = nested_depth + return NestedSerializer diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py new file mode 100644 index 000000000..be72e444e --- /dev/null +++ b/rest_framework/utils/field_mapping.py @@ -0,0 +1,215 @@ +""" +Helper functions for mapping model fields to a dictionary of default +keyword arguments that should be used for their equivelent serializer fields. +""" +from django.core import validators +from django.db import models +from django.utils.text import capfirst +from rest_framework.compat import clean_manytomany_helptext +import inspect + + +def lookup_class(mapping, instance): + """ + Takes a dictionary with classes as keys, and an object. + Traverses the object's inheritance hierarchy in method + resolution order, and returns the first matching value + from the dictionary or raises a KeyError if nothing matches. + """ + for cls in inspect.getmro(instance.__class__): + if cls in mapping: + return mapping[cls] + raise KeyError('Class %s not found in lookup.', cls.__name__) + + +def needs_label(model_field, field_name): + """ + Returns `True` if the label based on the model's verbose name + is not equal to the default label it would have based on it's field name. + """ + default_label = field_name.replace('_', ' ').capitalize() + return capfirst(model_field.verbose_name) != default_label + + +def get_detail_view_name(model): + """ + Given a model class, return the view name to use for URL relationships + that refer to instances of the model. + """ + return '%(model_name)s-detail' % { + 'app_label': model._meta.app_label, + 'model_name': model._meta.object_name.lower() + } + + +def get_field_kwargs(field_name, model_field): + """ + Creates a default instance of a basic non-relational field. + """ + kwargs = {} + validator_kwarg = model_field.validators + + if model_field.null or model_field.blank: + kwargs['required'] = False + + if model_field.verbose_name and needs_label(model_field, field_name): + kwargs['label'] = capfirst(model_field.verbose_name) + + if model_field.help_text: + kwargs['help_text'] = model_field.help_text + + if isinstance(model_field, models.AutoField) or not model_field.editable: + kwargs['read_only'] = True + # Read only implies that the field is not required. + # We have a cleaner repr on the instance if we don't set it. + kwargs.pop('required', None) + + if model_field.has_default(): + kwargs['default'] = model_field.get_default() + # Having a default implies that the field is not required. + # We have a cleaner repr on the instance if we don't set it. + kwargs.pop('required', None) + + if model_field.flatchoices: + # If this model field contains choices, then return now, + # any further keyword arguments are not valid. + kwargs['choices'] = model_field.flatchoices + return kwargs + + # Ensure that max_length is passed explicitly as a keyword arg, + # rather than as a validator. + max_length = getattr(model_field, 'max_length', None) + if max_length is not None: + kwargs['max_length'] = max_length + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MaxLengthValidator) + ] + + # Ensure that min_length is passed explicitly as a keyword arg, + # rather than as a validator. + min_length = getattr(model_field, 'min_length', None) + if min_length is not None: + kwargs['min_length'] = min_length + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MinLengthValidator) + ] + + # Ensure that max_value is passed explicitly as a keyword arg, + # rather than as a validator. + max_value = next(( + validator.limit_value for validator in validator_kwarg + if isinstance(validator, validators.MaxValueValidator) + ), None) + if max_value is not None: + kwargs['max_value'] = max_value + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MaxValueValidator) + ] + + # Ensure that max_value is passed explicitly as a keyword arg, + # rather than as a validator. + min_value = next(( + validator.limit_value for validator in validator_kwarg + if isinstance(validator, validators.MinValueValidator) + ), None) + if min_value is not None: + kwargs['min_value'] = min_value + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.MinValueValidator) + ] + + # URLField does not need to include the URLValidator argument, + # as it is explicitly added in. + if isinstance(model_field, models.URLField): + validator_kwarg = [ + validator for validator in validator_kwarg + if not isinstance(validator, validators.URLValidator) + ] + + # EmailField does not need to include the validate_email argument, + # as it is explicitly added in. + if isinstance(model_field, models.EmailField): + validator_kwarg = [ + validator for validator in validator_kwarg + if validator is not validators.validate_email + ] + + # SlugField do not need to include the 'validate_slug' argument, + if isinstance(model_field, models.SlugField): + validator_kwarg = [ + validator for validator in validator_kwarg + if validator is not validators.validate_slug + ] + + max_digits = getattr(model_field, 'max_digits', None) + if max_digits is not None: + kwargs['max_digits'] = max_digits + + decimal_places = getattr(model_field, 'decimal_places', None) + if decimal_places is not None: + kwargs['decimal_places'] = decimal_places + + if isinstance(model_field, models.BooleanField): + # models.BooleanField has `blank=True`, but *is* actually + # required *unless* a default is provided. + # Also note that Django<1.6 uses `default=False` for + # models.BooleanField, but Django>=1.6 uses `default=None`. + kwargs.pop('required', None) + + if validator_kwarg: + kwargs['validators'] = validator_kwarg + + # The following will only be used by ModelField classes. + # Gets removed for everything else. + kwargs['model_field'] = model_field + + return kwargs + + +def get_relation_kwargs(field_name, relation_info): + """ + Creates a default instance of a flat relational field. + """ + model_field, related_model, to_many, has_through_model = relation_info + kwargs = { + 'queryset': related_model._default_manager, + 'view_name': get_detail_view_name(related_model) + } + + if to_many: + kwargs['many'] = True + + if has_through_model: + kwargs['read_only'] = True + kwargs.pop('queryset', None) + + if model_field: + if model_field.null or model_field.blank: + kwargs['required'] = False + if model_field.verbose_name and needs_label(model_field, field_name): + kwargs['label'] = capfirst(model_field.verbose_name) + if not model_field.editable: + kwargs['read_only'] = True + kwargs.pop('queryset', None) + help_text = clean_manytomany_helptext(model_field.help_text) + if help_text: + kwargs['help_text'] = help_text + + return kwargs + + +def get_nested_relation_kwargs(relation_info): + kwargs = {'read_only': True} + if relation_info.to_many: + kwargs['many'] = True + return kwargs + + +def get_url_kwargs(model_field): + return { + 'view_name': get_detail_view_name(model_field) + } diff --git a/rest_framework/utils/model_meta.py b/rest_framework/utils/model_meta.py index 960fa4d0f..b6c41174e 100644 --- a/rest_framework/utils/model_meta.py +++ b/rest_framework/utils/model_meta.py @@ -1,7 +1,9 @@ """ -Helper functions for returning the field information that is associated +Helper function for returning the field information that is associated with a model class. This includes returning all the forward and reverse relationships and their associated metadata. + +Usage: `get_field_info(model)` returns a `FieldInfo` instance. """ from collections import namedtuple from django.db import models @@ -9,8 +11,22 @@ from django.utils import six from django.utils.datastructures import SortedDict import inspect -FieldInfo = namedtuple('FieldResult', ['pk', 'fields', 'forward_relations', 'reverse_relations']) -RelationInfo = namedtuple('RelationInfo', ['field', 'related', 'to_many', 'has_through_model']) + +FieldInfo = namedtuple('FieldResult', [ + 'pk', # Model field instance + 'fields', # Dict of field name -> model field instance + 'forward_relations', # Dict of field name -> RelationInfo + 'reverse_relations', # Dict of field name -> RelationInfo + 'fields_and_pk', # Shortcut for 'pk' + 'fields' + 'relations' # Shortcut for 'forward_relations' + 'reverse_relations' +]) + +RelationInfo = namedtuple('RelationInfo', [ + 'model_field', + 'related', + 'to_many', + 'has_through_model' +]) def _resolve_model(obj): @@ -55,7 +71,7 @@ def get_field_info(model): forward_relations = SortedDict() for field in [field for field in opts.fields if field.serialize and field.rel]: forward_relations[field.name] = RelationInfo( - field=field, + model_field=field, related=_resolve_model(field.rel.to), to_many=False, has_through_model=False @@ -64,7 +80,7 @@ def get_field_info(model): # Deal with forward many-to-many relationships. for field in [field for field in opts.many_to_many if field.serialize]: forward_relations[field.name] = RelationInfo( - field=field, + model_field=field, related=_resolve_model(field.rel.to), to_many=True, has_through_model=( @@ -77,7 +93,7 @@ def get_field_info(model): for relation in opts.get_all_related_objects(): accessor_name = relation.get_accessor_name() reverse_relations[accessor_name] = RelationInfo( - field=None, + model_field=None, related=relation.model, to_many=relation.field.rel.multiple, has_through_model=False @@ -87,7 +103,7 @@ def get_field_info(model): for relation in opts.get_all_related_many_to_many_objects(): accessor_name = relation.get_accessor_name() reverse_relations[accessor_name] = RelationInfo( - field=None, + model_field=None, related=relation.model, to_many=True, has_through_model=( @@ -96,4 +112,18 @@ def get_field_info(model): ) ) - return FieldInfo(pk, fields, forward_relations, reverse_relations) + # Shortcut that merges both regular fields and the pk, + # for simplifying regular field lookup. + fields_and_pk = SortedDict() + fields_and_pk['pk'] = pk + fields_and_pk[pk.name] = pk + fields_and_pk.update(fields) + + # Shortcut that merges both forward and reverse relationships + + relations = SortedDict( + list(forward_relations.items()) + + list(reverse_relations.items()) + ) + + return FieldInfo(pk, fields, forward_relations, reverse_relations, fields_and_pk, relations) diff --git a/tests/models.py b/tests/models.py index fe064b46a..06ec5a221 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from django.db import models from django.utils.translation import ugettext_lazy as _ -from rest_framework import serializers def foobar(): @@ -178,9 +177,3 @@ class NullableOneToOneSource(RESTFrameworkModel): name = models.CharField(max_length=100) target = models.OneToOneField(OneToOneTarget, null=True, blank=True, related_name='nullable_source') - - -# Serializer used to test BasicModel -class BasicModelSerializer(serializers.ModelSerializer): - class Meta: - model = BasicModel diff --git a/tests/test_model_field_mappings.py b/tests/test_model_field_mappings.py index bae63e5a7..6daa574e5 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_field_mappings.py @@ -126,16 +126,16 @@ class TestRelationalFieldMappings(TestCase): expected = dedent(""" TestSerializer(): id = IntegerField(label='ID', read_only=True) - foreign_key = NestedModelSerializer(read_only=True): + foreign_key = NestedSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) - one_to_one = NestedModelSerializer(read_only=True): + one_to_one = NestedSerializer(read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) - many_to_many = NestedModelSerializer(many=True, read_only=True): + many_to_many = NestedSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) - through = NestedModelSerializer(many=True, read_only=True): + through = NestedSerializer(many=True, read_only=True): id = IntegerField(label='ID', read_only=True) name = CharField(max_length=100) """) @@ -165,16 +165,16 @@ class TestRelationalFieldMappings(TestCase): expected = dedent(""" TestSerializer(): url = HyperlinkedIdentityField(view_name='relationalmodel-detail') - foreign_key = NestedModelSerializer(read_only=True): + foreign_key = NestedSerializer(read_only=True): url = HyperlinkedIdentityField(view_name='foreignkeytargetmodel-detail') name = CharField(max_length=100) - one_to_one = NestedModelSerializer(read_only=True): + one_to_one = NestedSerializer(read_only=True): url = HyperlinkedIdentityField(view_name='onetoonetargetmodel-detail') name = CharField(max_length=100) - many_to_many = NestedModelSerializer(many=True, read_only=True): + many_to_many = NestedSerializer(many=True, read_only=True): url = HyperlinkedIdentityField(view_name='manytomanytargetmodel-detail') name = CharField(max_length=100) - through = NestedModelSerializer(many=True, read_only=True): + through = NestedSerializer(many=True, read_only=True): url = HyperlinkedIdentityField(view_name='throughtargetmodel-detail') name = CharField(max_length=100) """) diff --git a/tests/test_response.py b/tests/test_response.py index 67419a718..84c39c1a6 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -2,11 +2,12 @@ from __future__ import unicode_literals from django.conf.urls import patterns, url, include from django.test import TestCase from django.utils import six -from tests.models import BasicModel, BasicModelSerializer +from tests.models import BasicModel from rest_framework.response import Response from rest_framework.views import APIView from rest_framework import generics from rest_framework import routers +from rest_framework import serializers from rest_framework import status from rest_framework.renderers import ( BaseRenderer, @@ -17,6 +18,12 @@ from rest_framework import viewsets from rest_framework.settings import api_settings +# Serializer used to test BasicModel +class BasicModelSerializer(serializers.ModelSerializer): + class Meta: + model = BasicModel + + class MockPickleRenderer(BaseRenderer): media_type = 'application/pickle' diff --git a/tests/test_routers.py b/tests/test_routers.py index b076f134e..c2d595f7f 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -76,9 +76,10 @@ class TestCustomLookupFields(TestCase): def setUp(self): class NoteSerializer(serializers.HyperlinkedModelSerializer): + url = serializers.HyperlinkedIdentityField(view_name='routertestmodel-detail', lookup_field='uuid') + class Meta: model = RouterTestModel - lookup_field = 'uuid' fields = ('url', 'uuid', 'text') class NoteViewSet(viewsets.ModelViewSet): @@ -86,8 +87,6 @@ class TestCustomLookupFields(TestCase): serializer_class = NoteSerializer lookup_field = 'uuid' - RouterTestModel.objects.create(uuid='123', text='foo bar') - self.router = SimpleRouter() self.router.register(r'notes', NoteViewSet) @@ -98,6 +97,8 @@ class TestCustomLookupFields(TestCase): url(r'^', include(self.router.urls)), ) + RouterTestModel.objects.create(uuid='123', text='foo bar') + def test_custom_lookup_field_route(self): detail_route = self.router.urls[-1] detail_url_pattern = detail_route.regex.pattern From 87734be5f41de921ac32ad1f6664db243aab6d07 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 12:17:21 +0100 Subject: [PATCH 093/100] Configuration correctness tests on ModelSerializer --- rest_framework/serializers.py | 30 ++++++- ...d_mappings.py => test_model_serializer.py} | 85 +++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) rename tests/{test_model_field_mappings.py => test_model_serializer.py} (77%) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 99dcc349f..9f3e53fd0 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -10,7 +10,7 @@ python primitives. 2. The process of marshalling between python primitives and request and response content is handled by parsers and renderers. """ -from django.core.exceptions import ValidationError +from django.core.exceptions import ImproperlyConfigured, ValidationError from django.db import models from django.utils import six from django.utils.datastructures import SortedDict @@ -358,6 +358,7 @@ class ModelSerializer(Serializer): model = getattr(self.Meta, 'model') fields = getattr(self.Meta, 'fields', None) depth = getattr(self.Meta, 'depth', 0) + extra_kwargs = getattr(self.Meta, 'extra_kwargs', {}) # Retrieve metadata about fields & relationships on the model class. info = model_meta.get_field_info(model) @@ -405,9 +406,32 @@ class ModelSerializer(Serializer): if not issubclass(field_cls, HyperlinkedRelatedField): kwargs.pop('view_name', None) - else: - assert False, 'Field name `%s` is not valid.' % field_name + elif hasattr(model, field_name): + # Create a read only field for model methods and properties. + field_cls = ReadOnlyField + kwargs = {} + else: + raise ImproperlyConfigured( + 'Field name `%s` is not valid for model `%s`.' % + (field_name, model.__class__.__name__) + ) + + # Check that any fields declared on the class are + # also explicity included in `Meta.fields`. + missing_fields = set(declared_fields.keys()) - set(fields) + if missing_fields: + missing_field = list(missing_fields)[0] + raise ImproperlyConfigured( + 'Field `%s` has been declared on serializer `%s`, but ' + 'is missing from `Meta.fields`.' % + (missing_field, self.__class__.__name__) + ) + + # Populate any kwargs defined in `Meta.extra_kwargs` + kwargs.update(extra_kwargs.get(field_name, {})) + + # Create the serializer field. ret[field_name] = field_cls(**kwargs) return ret diff --git a/tests/test_model_field_mappings.py b/tests/test_model_serializer.py similarity index 77% rename from tests/test_model_field_mappings.py rename to tests/test_model_serializer.py index 6daa574e5..63e364528 100644 --- a/tests/test_model_field_mappings.py +++ b/tests/test_model_serializer.py @@ -5,6 +5,7 @@ shortcuts for automatically creating serializers based on a given model class. These tests deal with ensuring that we correctly map the model fields onto an appropriate set of serializer fields for each case. """ +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.test import TestCase from rest_framework import serializers @@ -37,6 +38,9 @@ class RegularFieldsModel(models.Model): time_field = models.TimeField() url_field = models.URLField(max_length=100) + def method(self): + return 'method' + class TestRegularFieldMappings(TestCase): def test_regular_fields(self): @@ -69,6 +73,87 @@ class TestRegularFieldMappings(TestCase): self.assertEqual(repr(TestSerializer()), expected) + def test_method_field(self): + """ + Properties and methods on the model should be allowed as `Meta.fields` + values, and should map to `ReadOnlyField`. + """ + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + fields = ('auto_field', 'method') + + expected = dedent(""" + TestSerializer(): + auto_field = IntegerField(read_only=True) + method = ReadOnlyField() + """) + self.assertEqual(repr(TestSerializer()), expected) + + def test_pk_fields(self): + """ + Both `pk` and the actual primary key name are valid in `Meta.fields`. + """ + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + fields = ('pk', 'auto_field') + + expected = dedent(""" + TestSerializer(): + pk = IntegerField(label='Auto field', read_only=True) + auto_field = IntegerField(read_only=True) + """) + self.assertEqual(repr(TestSerializer()), expected) + + def test_extra_field_kwargs(self): + """ + Ensure `extra_kwargs` are passed to generated fields. + """ + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + fields = ('pk', 'char_field') + extra_kwargs = {'char_field': {'default': 'extra'}} + + expected = dedent(""" + TestSerializer(): + pk = IntegerField(label='Auto field', read_only=True) + char_field = CharField(default='extra', max_length=100) + """) + self.assertEqual(repr(TestSerializer()), expected) + + def test_invalid_field(self): + """ + Field names that do not map to a model field or relationship should + raise a configuration errror. + """ + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RegularFieldsModel + fields = ('auto_field', 'invalid') + + with self.assertRaises(ImproperlyConfigured) as excinfo: + TestSerializer() + expected = 'Field name `invalid` is not valid for model `ModelBase`.' + assert str(excinfo.exception) == expected + + def test_missing_field(self): + class TestSerializer(serializers.ModelSerializer): + missing = serializers.ReadOnlyField() + + class Meta: + model = RegularFieldsModel + fields = ('auto_field',) + + with self.assertRaises(ImproperlyConfigured) as excinfo: + TestSerializer() + expected = ( + 'Field `missing` has been declared on serializer ' + '`TestSerializer`, but is missing from `Meta.fields`.' + ) + assert str(excinfo.exception) == expected + # Testing relational field mappings From 3bc628edc01a6bd4feeceaefa45168549b79a31a Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 13:07:38 +0100 Subject: [PATCH 094/100] Test for custom fields --- tests/test_model_serializer.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 63e364528..3ee91126f 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -17,6 +17,10 @@ def dedent(blocktext): # Testing regular field mappings +class CustomField(models.Field): + pass + + class RegularFieldsModel(models.Model): auto_field = models.AutoField(primary_key=True) big_integer_field = models.BigIntegerField() @@ -37,6 +41,7 @@ class RegularFieldsModel(models.Model): text_field = models.TextField() time_field = models.TimeField() url_field = models.URLField(max_length=100) + custom_field = CustomField() def method(self): return 'method' @@ -44,6 +49,9 @@ class RegularFieldsModel(models.Model): class TestRegularFieldMappings(TestCase): def test_regular_fields(self): + """ + Model fields should map to their equivelent serializer fields. + """ class TestSerializer(serializers.ModelSerializer): class Meta: model = RegularFieldsModel @@ -69,8 +77,8 @@ class TestRegularFieldMappings(TestCase): text_field = CharField() time_field = TimeField() url_field = URLField(max_length=100) + custom_field = ModelField(model_field=<tests.test_model_serializer.CustomField: custom_field>) """) - self.assertEqual(repr(TestSerializer()), expected) def test_method_field(self): @@ -139,6 +147,10 @@ class TestRegularFieldMappings(TestCase): assert str(excinfo.exception) == expected def test_missing_field(self): + """ + Fields that have been declared on the serializer class must be included + in the `Meta.fields` if it exists. + """ class TestSerializer(serializers.ModelSerializer): missing = serializers.ReadOnlyField() From 9fdb2280d11db126771686d626aa8a0247b8a46c Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 14:23:00 +0100 Subject: [PATCH 095/100] First pass on ManyRelation --- rest_framework/relations.py | 42 +++++++++++++++++++- rest_framework/utils/representation.py | 2 + tests/test_model_serializer.py | 54 ++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 9f44ab633..474d3e757 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -10,7 +10,6 @@ from django.utils.translation import ugettext_lazy as _ class RelatedField(Field): def __init__(self, **kwargs): self.queryset = kwargs.pop('queryset', None) - self.many = kwargs.pop('many', False) assert self.queryset is not None or kwargs.get('read_only', None), ( 'Relational field must provide a `queryset` argument, ' 'or set read_only=`True`.' @@ -21,6 +20,13 @@ class RelatedField(Field): ) super(RelatedField, self).__init__(**kwargs) + def __new__(cls, *args, **kwargs): + # We override this method in order to automagically create + # `ManyRelation` classes instead when `many=True` is set. + if kwargs.pop('many', False): + return ManyRelation(child_relation=cls(*args, **kwargs)) + return super(RelatedField, cls).__new__(cls, *args, **kwargs) + def get_queryset(self): queryset = self.queryset if isinstance(queryset, QuerySet): @@ -216,3 +222,37 @@ class SlugRelatedField(RelatedField): def to_representation(self, obj): return getattr(obj, self.slug_field) + + +class ManyRelation(Field): + """ + Relationships with `many=True` transparently get coerced into instead being + a ManyRelation with a child relationship. + + The `ManyRelation` class is responsible for handling iterating through + the values and passing each one to the child relationship. + + You shouldn't need to be using this class directly yourself. + """ + + def __init__(self, child_relation=None, *args, **kwargs): + self.child_relation = child_relation + assert child_relation is not None, '`child_relation` is a required argument.' + super(ManyRelation, self).__init__(*args, **kwargs) + + def bind(self, field_name, parent, root): + # ManyRelation needs to provide the current context to the child relation. + super(ManyRelation, self).bind(field_name, parent, root) + self.child_relation.bind(field_name, parent, root) + + def to_internal_value(self, data): + return [ + self.child_relation.to_internal_value(item) + for item in data + ] + + def to_representation(self, obj): + return [ + self.child_relation.to_representation(value) + for value in obj.all() + ] diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 71db18863..e64fdd223 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -73,6 +73,8 @@ def serializer_repr(serializer, indent, force_many=None): ret += serializer_repr(field, indent + 1) elif hasattr(field, 'child'): ret += list_repr(field, indent + 1) + elif hasattr(field, 'child_relation'): + ret += field_repr(field.child_relation, force_many=field.child_relation) else: ret += field_repr(field) return ret diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 3ee91126f..b3dae7134 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -199,7 +199,7 @@ class RelationalModel(models.Model): class TestRelationalFieldMappings(TestCase): - def test_flat_relational_fields(self): + def test_pk_relations(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = RelationalModel @@ -214,7 +214,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_nested_relational_fields(self): + def test_nested_relations(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = RelationalModel @@ -238,7 +238,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_flat_hyperlinked_fields(self): + def test_hyperlinked_relations(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = RelationalModel @@ -253,7 +253,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_nested_hyperlinked_fields(self): + def test_nested_hyperlinked_relations(self): class TestSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = RelationalModel @@ -277,7 +277,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_flat_reverse_foreign_key(self): + def test_pk_reverse_foreign_key(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = ForeignKeyTargetModel @@ -291,7 +291,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_flat_reverse_one_to_one(self): + def test_pk_reverse_one_to_one(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = OneToOneTargetModel @@ -305,7 +305,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_flat_reverse_many_to_many(self): + def test_pk_reverse_many_to_many(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = ManyToManyTargetModel @@ -319,7 +319,7 @@ class TestRelationalFieldMappings(TestCase): """) self.assertEqual(repr(TestSerializer()), expected) - def test_flat_reverse_through(self): + def test_pk_reverse_through(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = ThroughTargetModel @@ -332,3 +332,41 @@ class TestRelationalFieldMappings(TestCase): reverse_through = PrimaryKeyRelatedField(many=True, read_only=True) """) self.assertEqual(repr(TestSerializer()), expected) + + +class TestIntegration(TestCase): + def setUp(self): + self.foreign_key_target = ForeignKeyTargetModel.objects.create( + name='foreign_key' + ) + self.one_to_one_target = OneToOneTargetModel.objects.create( + name='one_to_one' + ) + self.many_to_many_targets = [ + ManyToManyTargetModel.objects.create( + name='many_to_many (%d)' % idx + ) for idx in range(3) + ] + self.instance = RelationalModel.objects.create( + foreign_key=self.foreign_key_target, + one_to_one=self.one_to_one_target, + ) + self.instance.many_to_many = self.many_to_many_targets + self.instance.save() + + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RelationalModel + + self.serializer_cls = TestSerializer + + def test_pk_relationship_representations(self): + serializer = self.serializer_cls(self.instance) + expected = { + 'id': self.instance.pk, + 'foreign_key': self.foreign_key_target.pk, + 'one_to_one': self.one_to_one_target.pk, + 'many_to_many': [item.pk for item in self.many_to_many_targets], + 'through': [] + } + self.assertEqual(serializer.data, expected) From 106362b437f45e04faaea759df57a66a8a2d7cfd Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 14:58:08 +0100 Subject: [PATCH 096/100] ModelSerializer.create() to handle many to many by default --- rest_framework/relations.py | 5 +++- rest_framework/serializers.py | 20 ++++++++++++++- tests/test_model_serializer.py | 45 +++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 474d3e757..5aa1f8bd8 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -24,7 +24,10 @@ class RelatedField(Field): # We override this method in order to automagically create # `ManyRelation` classes instead when `many=True` is set. if kwargs.pop('many', False): - return ManyRelation(child_relation=cls(*args, **kwargs)) + return ManyRelation( + child_relation=cls(*args, **kwargs), + read_only=kwargs.get('read_only', False) + ) return super(RelatedField, cls).__new__(cls, *args, **kwargs) def get_queryset(self): diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 9f3e53fd0..03e20df8a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -344,7 +344,25 @@ class ModelSerializer(Serializer): def create(self, attrs): ModelClass = self.Meta.model - return ModelClass.objects.create(**attrs) + + # Remove many-to-many relationships from attrs. + # They are not valid arguments to the default `.create()` method, + # as they require that the instance has already been saved. + info = model_meta.get_field_info(ModelClass) + many_to_many = {} + for key, relation_info in info.relations.items(): + if relation_info.to_many and (key in attrs): + many_to_many[key] = attrs.pop(key) + + instance = ModelClass.objects.create(**attrs) + + # Save many to many relationships after the instance is created. + if many_to_many: + for key, value in many_to_many.items(): + setattr(instance, key, value) + instance.save() + + return instance def update(self, obj, attrs): for attr, value in attrs.items(): diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index b3dae7134..6f207e02c 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -360,7 +360,7 @@ class TestIntegration(TestCase): self.serializer_cls = TestSerializer - def test_pk_relationship_representations(self): + def test_pk_retrival(self): serializer = self.serializer_cls(self.instance) expected = { 'id': self.instance.pk, @@ -370,3 +370,46 @@ class TestIntegration(TestCase): 'through': [] } self.assertEqual(serializer.data, expected) + + def test_pk_create(self): + new_foreign_key = ForeignKeyTargetModel.objects.create( + name='foreign_key' + ) + new_one_to_one = OneToOneTargetModel.objects.create( + name='one_to_one' + ) + new_many_to_many = [ + ManyToManyTargetModel.objects.create( + name='new many_to_many (%d)' % idx + ) for idx in range(3) + ] + data = { + 'foreign_key': new_foreign_key.pk, + 'one_to_one': new_one_to_one.pk, + 'many_to_many': [item.pk for item in new_many_to_many], + } + + # Serializer should validate okay. + serializer = self.serializer_cls(data=data) + assert serializer.is_valid() + + # Creating the instance, relationship attributes should be set. + instance = serializer.save() + assert instance.foreign_key.pk == new_foreign_key.pk + assert instance.one_to_one.pk == new_one_to_one.pk + assert [ + item.pk for item in instance.many_to_many.all() + ] == [ + item.pk for item in new_many_to_many + ] + assert list(instance.through.all()) == [] + + # Representation should be correct. + expected = { + 'id': instance.pk, + 'foreign_key': new_foreign_key.pk, + 'one_to_one': new_one_to_one.pk, + 'many_to_many': [item.pk for item in new_many_to_many], + 'through': [] + } + self.assertEqual(serializer.data, expected) From f90049316a3ecca6c92e10b57bfa5becbceff386 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Thu, 18 Sep 2014 15:47:27 +0100 Subject: [PATCH 097/100] Added a model update integration test --- rest_framework/serializers.py | 13 +++++----- tests/test_model_serializer.py | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 03e20df8a..d2740fc2a 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -350,17 +350,16 @@ class ModelSerializer(Serializer): # as they require that the instance has already been saved. info = model_meta.get_field_info(ModelClass) many_to_many = {} - for key, relation_info in info.relations.items(): - if relation_info.to_many and (key in attrs): - many_to_many[key] = attrs.pop(key) + for field_name, relation_info in info.relations.items(): + if relation_info.to_many and (field_name in attrs): + many_to_many[field_name] = attrs.pop(field_name) instance = ModelClass.objects.create(**attrs) - # Save many to many relationships after the instance is created. + # Save many-to-many relationships after the instance is created. if many_to_many: - for key, value in many_to_many.items(): - setattr(instance, key, value) - instance.save() + for field_name, value in many_to_many.items(): + setattr(instance, field_name, value) return instance diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 6f207e02c..e0120c313 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -413,3 +413,46 @@ class TestIntegration(TestCase): 'through': [] } self.assertEqual(serializer.data, expected) + + def test_pk_update(self): + new_foreign_key = ForeignKeyTargetModel.objects.create( + name='foreign_key' + ) + new_one_to_one = OneToOneTargetModel.objects.create( + name='one_to_one' + ) + new_many_to_many = [ + ManyToManyTargetModel.objects.create( + name='new many_to_many (%d)' % idx + ) for idx in range(3) + ] + data = { + 'foreign_key': new_foreign_key.pk, + 'one_to_one': new_one_to_one.pk, + 'many_to_many': [item.pk for item in new_many_to_many], + } + + # Serializer should validate okay. + serializer = self.serializer_cls(self.instance, data=data) + assert serializer.is_valid() + + # Creating the instance, relationship attributes should be set. + instance = serializer.save() + assert instance.foreign_key.pk == new_foreign_key.pk + assert instance.one_to_one.pk == new_one_to_one.pk + assert [ + item.pk for item in instance.many_to_many.all() + ] == [ + item.pk for item in new_many_to_many + ] + assert list(instance.through.all()) == [] + + # Representation should be correct. + expected = { + 'id': self.instance.pk, + 'foreign_key': new_foreign_key.pk, + 'one_to_one': new_one_to_one.pk, + 'many_to_many': [item.pk for item in new_many_to_many], + 'through': [] + } + self.assertEqual(serializer.data, expected) From 7f758d1cf694c9227a1f55df400ff81e4bce2956 Mon Sep 17 00:00:00 2001 From: Piper Merriam <piper@simpleenergy.com> Date: Thu, 18 Sep 2014 10:30:13 -0600 Subject: [PATCH 098/100] Fix missing CSRF exemption on viewsets --- rest_framework/viewsets.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rest_framework/viewsets.py b/rest_framework/viewsets.py index bb5b304ee..84b4bd8dd 100644 --- a/rest_framework/viewsets.py +++ b/rest_framework/viewsets.py @@ -20,6 +20,7 @@ from __future__ import unicode_literals from functools import update_wrapper from django.utils.decorators import classonlymethod +from django.views.decorators.csrf import csrf_exempt from rest_framework import views, generics, mixins @@ -89,7 +90,7 @@ class ViewSetMixin(object): # resolved URL. view.cls = cls view.suffix = initkwargs.get('suffix', None) - return view + return csrf_exempt(view) def initialize_request(self, request, *args, **kargs): """ From 12ccb0fe8a7dfbefa3687d07ff59a456fbda9ee6 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 19 Sep 2014 09:09:26 +0100 Subject: [PATCH 099/100] Test tweaking --- tests/test_model_serializer.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index e0120c313..ec922b6d5 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -18,10 +18,16 @@ def dedent(blocktext): # Testing regular field mappings class CustomField(models.Field): + """ + A custom model field simply for testing purposes. + """ pass class RegularFieldsModel(models.Model): + """ + A model class for testing regular flat fields. + """ auto_field = models.AutoField(primary_key=True) big_integer_field = models.BigIntegerField() boolean_field = models.BooleanField(default=False) @@ -121,12 +127,12 @@ class TestRegularFieldMappings(TestCase): class TestSerializer(serializers.ModelSerializer): class Meta: model = RegularFieldsModel - fields = ('pk', 'char_field') + fields = ('auto_field', 'char_field') extra_kwargs = {'char_field': {'default': 'extra'}} expected = dedent(""" TestSerializer(): - pk = IntegerField(label='Auto field', read_only=True) + auto_field = IntegerField(read_only=True) char_field = CharField(default='extra', max_length=100) """) self.assertEqual(repr(TestSerializer()), expected) @@ -354,14 +360,12 @@ class TestIntegration(TestCase): self.instance.many_to_many = self.many_to_many_targets self.instance.save() + def test_pk_retrival(self): class TestSerializer(serializers.ModelSerializer): class Meta: model = RelationalModel - self.serializer_cls = TestSerializer - - def test_pk_retrival(self): - serializer = self.serializer_cls(self.instance) + serializer = TestSerializer(self.instance) expected = { 'id': self.instance.pk, 'foreign_key': self.foreign_key_target.pk, @@ -372,6 +376,10 @@ class TestIntegration(TestCase): self.assertEqual(serializer.data, expected) def test_pk_create(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RelationalModel + new_foreign_key = ForeignKeyTargetModel.objects.create( name='foreign_key' ) @@ -390,7 +398,7 @@ class TestIntegration(TestCase): } # Serializer should validate okay. - serializer = self.serializer_cls(data=data) + serializer = TestSerializer(data=data) assert serializer.is_valid() # Creating the instance, relationship attributes should be set. @@ -415,6 +423,10 @@ class TestIntegration(TestCase): self.assertEqual(serializer.data, expected) def test_pk_update(self): + class TestSerializer(serializers.ModelSerializer): + class Meta: + model = RelationalModel + new_foreign_key = ForeignKeyTargetModel.objects.create( name='foreign_key' ) @@ -433,7 +445,7 @@ class TestIntegration(TestCase): } # Serializer should validate okay. - serializer = self.serializer_cls(self.instance, data=data) + serializer = TestSerializer(self.instance, data=data) assert serializer.is_valid() # Creating the instance, relationship attributes should be set. From 6d73b5969a9d415a6d43175e77bc10ba76151e97 Mon Sep 17 00:00:00 2001 From: Tom Christie <tom@tomchristie.com> Date: Fri, 19 Sep 2014 14:02:17 +0100 Subject: [PATCH 100/100] Initial release notes --- docs/topics/3.0-announcement.md | 239 ++++++++++++++++++++++++++++++++ 1 file changed, 239 insertions(+) create mode 100644 docs/topics/3.0-announcement.md diff --git a/docs/topics/3.0-announcement.md b/docs/topics/3.0-announcement.md new file mode 100644 index 000000000..029d98960 --- /dev/null +++ b/docs/topics/3.0-announcement.md @@ -0,0 +1,239 @@ +**THIS DOCUMENT IS CURRENTLY A WORK IN PROGRESS** + +See the [Version 3.0 GitHub issue](https://github.com/tomchristie/django-rest-framework/pull/1800) for more details. + +# REST framework 3.0 + +**Note incremental nature, discuss upgrading.** + +## Motivation + +**TODO** + +--- + +## Request objects + +#### The `request.data` property. + +**TODO** + +#### The parser API. + +**TODO** + +## Serializers + +#### Single-step object creation. + +**TODO**: Drop `.restore_object()`, use `.create()` and `.update()` which should save the instance. + +**TODO**: Drop`.object`, use `.validated_data` or get the instance with `.save()`. + +#### Always use `fields`, not `exclude`. + +The `exclude` option is no longer available. You should use the more explicit `fields` option instead. + +#### The `extra_kwargs` option. + +The `read_only_fields` and `write_only_fields` options have been removed and replaced with a more generic `extra_kwargs`. + + class MySerializer(serializer.ModelSerializer): + class Meta: + model = MyModel + fields = ('id', 'email', 'notes', 'is_admin') + extra_kwargs = { + 'is_admin': {'read_only': True} + } + +Alternatively, specify the field explicitly on the serializer class: + + class MySerializer(serializer.ModelSerializer): + is_admin = serializers.BooleanField(read_only=True) + + class Meta: + model = MyModel + fields = ('id', 'email', 'notes', 'is_admin') + +#### Changes to `HyperlinkedModelSerializer`. + +The `view_name` and `lookup_field` options have been removed. They are no longer required, as you can use the `extra_kwargs` argument instead: + + class MySerializer(serializer.HyperlinkedModelSerializer): + class Meta: + model = MyModel + fields = ('url', 'email', 'notes', 'is_admin') + extra_kwargs = { + 'url': {'lookup_field': 'uuid'} + } + +Alternatively, specify the field explicitly on the serializer class: + + class MySerializer(serializer.HyperlinkedModelSerializer): + url = serializers.HyperlinkedIdentityField( + view_name='mymodel-detail', + lookup_field='uuid' + ) + + class Meta: + model = MyModel + fields = ('url', 'email', 'notes', 'is_admin') + +#### Fields for model methods and properties. + +You can now specify field names in the `fields` option that refer to model methods or properties. For example, suppose you have the following model: + + class Invitation(models.Model): + created = models.DateTimeField() + to_email = models.EmailField() + message = models.CharField(max_length=1000) + + def expiry_date(self): + return self.created + datetime.timedelta(days=30) + +You can include `expiry_date` as a field option on a `ModelSerializer` class. + + class InvitationSerializer(serializers.ModelSerializer): + class Meta: + model = Invitation + fields = ('to_email', 'message', 'expiry_date') + +These fields will be mapped to `serializers.ReadOnlyField()` instances. + + >>> serializer = InvitationSerializer() + >>> print repr(serializer) + InvitationSerializer(): + to_email = EmailField(max_length=75) + message = CharField(max_length=1000) + expiry_date = ReadOnlyField() + + +## Serializer fields + +#### The `Field` and `ReadOnly` field classes. + +**TODO** + +#### Coercing output types. + +**TODO** + +#### The `ListSerializer` class. + +**TODO** + +#### The `MultipleChoiceField` class. + +**TODO** + +#### Changes to the custom field API. + +**TODO** `to_representation`, `to_internal_value`. + +#### Explicit `querysets` required on relational fields. + +**TODO** + +#### Optional argument to `SerializerMethodField`. + +**TODO** + +## Generic views + +#### Simplification of view logic. + +**TODO** + +#### Removal of pre/post save hooks. + +The following method hooks no longer exist on the new, simplified, generic views: `pre_save`, `post_save`, `pre_delete`, `post_delete`. + +If you do need custom behavior, you might choose to instead override the `.save()` method on your serializer class. For example: + + def save(self, *args, **kwargs): + instance = super(MySerializer).save(*args, **kwarg) + send_email(instance.to_email, instance.message) + return instance + +Alternatively write your view logic exlpicitly, or tie your pre/post save behavior into the model class or model manager. + +#### Removal of view attributes. + +The `.object` and `.object_list` attributes are no longer set on the view instance. Treating views as mutable object instances that store state during the processing of the view tends to be poor design, and can lead to obscure flow logic. + +I would personally recommend that developers treat view instances as immutable objects in their application code. + +#### PUT as create. + +**TODO** + +## API style + +There are some improvements in the default style we use in our API responses. + +#### Unicode JSON by default. + +Unicode JSON is now the default. The `UnicodeJSONRenderer` class no longer exists, and the `UNICODE_JSON` setting has been added. To revert this behavior use the new setting: + + REST_FRAMEWORK = { + 'UNICODE_JSON': False + } + +#### Compact JSON by default. + +We now output compact JSON in responses by default. For example, we return: + + {"email":"amy@example.com","is_admin":true} + +Instead of the following: + + {"email": "amy@example.com", "is_admin": true} + +The `COMPACT_JSON` setting has been added, and can be used to revert this behavior if needed: + + REST_FRAMEWORK = { + 'COMPACT_JSON': False + } + +#### Throttle headers using `Retry-After`. + +The custom `X-Throttle-Wait-Second` header has now been dropped in favor of the standard `Retry-After` header. You can revert this behavior if needed by writing a custom exception handler for your application. + +#### Date and time objects as ISO-8859-1 strings in serializer data. + +Date and Time objects are now coerced to strings by default in the serializer output. Previously they were returned as `Date`, `Time` and `DateTime` objects, and later coerced to strings by the renderer. + +You can modify this behavior globally by settings the existing `DATE_FORMAT`, `DATETIME_FORMAT` and `TIME_FORMAT` settings keys. Setting these values to `None` instead of their default value of `'iso-8859-1'` will result in native objects being returned in serializer data. + + REST_FRAMEWORK = { + # Return native `Date` and `Time` objects in `serializer.data` + 'DATETIME_FORMAT': None + 'DATE_FORMAT': None + 'TIME_FORMAT': None + } + +You can also modify serializer fields individually, using the `date_format`, `time_format` and `datetime_format` arguments: + + # Return `DateTime` instances in `serializer.data`, not strings. + created = serializers.DateTimeField(format=None) + +#### Decimals as strings in serializer data. + +Decimals are now coerced to strings by default in the serializer output. Previously they were returned as `Decimal` objects, and later coerced to strings by the renderer. + +You can modify this behavior globally by using the `COERCE_DECIMAL_TO_STRING` settings key. + + REST_FRAMEWORK = { + 'COERCE_DECIMAL_TO_STRING': False + } + +Or modify it on an individual serializer field, using the `corece_to_string` keyword argument. + + # Return `Decimal` instances in `serializer.data`, not strings. + amount = serializers.DecimalField( + max_digits=10, + decimal_places=2, + coerce_to_string=False + ) + +The default JSON renderer will return float objects for uncoerced `Decimal` instances. This allows you to easily switch between string or float representations for decimals depending on your API design needs.