From 908e74d9744e748e1bbb70fafa5eaec017934287 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 19 Oct 2015 16:19:05 +0200 Subject: [PATCH] Add NamespacedSerializer class It is able to prepend the view names with a namespace --- api/serializers.py | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 200ce20..3764d05 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -1,14 +1,35 @@ # -*- coding: utf-8 -*- +from django.core.exceptions import ImproperlyConfigured from rest_framework import serializers from booking.models import Duck -class DuckSerializer(serializers.HyperlinkedModelSerializer): +class NamespacedSerializer(serializers.HyperlinkedModelSerializer): + def __init__(self, *args, **kwargs): + if not hasattr(self.Meta, 'url_namespace') or self.Meta.url_namespace is None: + raise ImproperlyConfigured("namespace must be set!") + + self.url_namespace = self.Meta.url_namespace + + if not self.url_namespace.endswith(':'): + self.url_namespace += ':' + + return super(NamespacedSerializer, self).__init__(*args, **kwargs) + + def build_url_field(self, field_name, model_class): + field_class, field_kwargs = super(NamespacedSerializer, self) \ + .build_url_field(field_name, + model_class) + + view_name = field_kwargs.get('view_name') + + if not view_name.startswith(self.url_namespace): + field_kwargs['view_name'] = self.url_namespace + view_name + + return field_class, field_kwargs + +class DuckSerializer(NamespacedSerializer): class Meta: + url_namespace = 'api' model = Duck fields = ('url', 'name', 'color',) - extra_kwargs = { - 'url': { - 'view_name': 'api:duck-detail', - } - }