From d89d6887d2eb8293348cb1a7a043a05352819cb8 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 4 Oct 2012 11:26:41 +0100 Subject: [PATCH] HyperlinkedModelSerializer with working HyperlinkedIdentityField, but no hyperlinked relations --- rest_framework/fields.py | 9 +++ rest_framework/serializers.py | 37 ++++++++++++ rest_framework/settings.py | 2 +- rest_framework/tests/generics.py | 1 - .../tests/hyperlinkedserializers.py | 60 +++++++++++++++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 rest_framework/tests/hyperlinkedserializers.py diff --git a/rest_framework/fields.py b/rest_framework/fields.py index edc77e1aa..09ccc4ff5 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -9,6 +9,7 @@ from django.conf import settings from django.db import DEFAULT_DB_ALIAS from django.utils.encoding import is_protected_type, smart_unicode from django.utils.translation import ugettext_lazy as _ +from rest_framework.reverse import reverse from rest_framework.compat import parse_date, parse_datetime from rest_framework.compat import timezone @@ -173,6 +174,14 @@ class Field(object): return {} +class HyperlinkedIdentityField(Field): + def field_to_native(self, obj, field_name): + request = self.context.get('request', None) + view_name = self.parent.opts.view_name + view_kwargs = {'pk': obj.pk} + return reverse(view_name, kwargs=view_kwargs, request=request) + + class RelatedField(Field): """ A base class for model related fields or related managers. diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 037638249..990a4f9ab 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -396,3 +396,40 @@ class ModelSerializer(RelatedField, Serializer): """ self.object.save() return self.object.object + + +class HyperlinkedModelSerializerOptions(ModelSerializerOptions): + """ + Options for HyperlinkedModelSerializer + """ + def __init__(self, meta): + super(HyperlinkedModelSerializerOptions, self).__init__(meta) + self.view_name = getattr(meta, 'view_name', None) + + +class HyperlinkedModelSerializer(ModelSerializer): + """ + """ + _options_class = HyperlinkedModelSerializerOptions + _default_view_name = '%(model_name)s-detail' + + url = HyperlinkedIdentityField() + + def __init__(self, *args, **kwargs): + super(HyperlinkedModelSerializer, self).__init__(*args, **kwargs) + if self.opts.view_name is None: + self.opts.view_name = self._get_default_view_name() + + def _get_default_view_name(self): + """ + Return the view name to use if 'view_name' is not specified in 'Meta' + """ + model_meta = self.opts.model._meta + format_kwargs = { + 'app_label': model_meta.app_label, + 'model_name': model_meta.object_name.lower() + } + return self._default_view_name % format_kwargs + + def get_pk_field(self, model_field): + return None diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 2e50e05dc..ccc8f3681 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -46,7 +46,7 @@ DEFAULTS = { 'MODEL_SERIALIZER': 'rest_framework.serializers.ModelSerializer', 'PAGINATION_SERIALIZER': 'rest_framework.pagination.PaginationSerializer', - 'PAGINATE_BY': 20, + 'PAGINATE_BY': None, 'UNAUTHENTICATED_USER': 'django.contrib.auth.models.AnonymousUser', 'UNAUTHENTICATED_TOKEN': None, diff --git a/rest_framework/tests/generics.py b/rest_framework/tests/generics.py index 187465ed2..c0645d6ee 100644 --- a/rest_framework/tests/generics.py +++ b/rest_framework/tests/generics.py @@ -13,7 +13,6 @@ class RootView(generics.ListCreateAPIView): Example description for OPTIONS. """ model = BasicModel - paginate_by = None class InstanceView(generics.RetrieveUpdateDestroyAPIView): diff --git a/rest_framework/tests/hyperlinkedserializers.py b/rest_framework/tests/hyperlinkedserializers.py new file mode 100644 index 000000000..3b4ed7d2e --- /dev/null +++ b/rest_framework/tests/hyperlinkedserializers.py @@ -0,0 +1,60 @@ +from django.conf.urls import patterns, url +from django.test import TestCase +from django.test.client import RequestFactory +from rest_framework import generics, status, serializers +from rest_framework.tests.models import BasicModel + +factory = RequestFactory() + + +class BasicList(generics.ListCreateAPIView): + model = BasicModel + model_serializer_class = serializers.HyperlinkedModelSerializer + + +class BasicDetail(generics.RetrieveUpdateDestroyAPIView): + model = BasicModel + model_serializer_class = serializers.HyperlinkedModelSerializer + + +urlpatterns = patterns('', + url(r'^basic/$', BasicList.as_view(), name='basicmodel-list'), + url(r'^basic/(?P\d+)/$', BasicDetail.as_view(), name='basicmodel-detail'), +) + + +class TestHyperlinkedView(TestCase): + urls = 'rest_framework.tests.hyperlinkedserializers' + + def setUp(self): + """ + Create 3 BasicModel intances. + """ + 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('/') + response = self.list_view(request).render() + self.assertEquals(response.status_code, status.HTTP_200_OK) + self.assertEquals(response.data, self.data) + + def test_get_detail_view(self): + """ + GET requests to ListCreateAPIView should return list of objects. + """ + request = factory.get('/1') + response = self.detail_view(request, pk=1).render() + self.assertEquals(response.status_code, status.HTTP_200_OK) + self.assertEquals(response.data, self.data[0])