8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 01:16:27 +00:00
re2o/api/serializers.py

66 lines
2.3 KiB
Python
Raw Permalink Normal View History

2018-06-30 01:25:46 +00:00
# -*- mode: python; coding: utf-8 -*-
2018-03-17 17:20:31 +00:00
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
2018-06-17 01:06:58 +00:00
# Copyright © 2018 Maël Kervella
2018-03-17 17:20:31 +00:00
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2018-06-17 01:06:58 +00:00
"""Defines the serializers of the API
2018-03-17 17:20:31 +00:00
"""
2018-03-17 17:50:03 +00:00
from rest_framework import serializers
2018-04-21 22:36:10 +00:00
2018-04-24 15:57:55 +00:00
2018-06-17 01:06:58 +00:00
# The namespace used for the API. It must match the namespace used in the
# urlpatterns to include the API URLs.
API_NAMESPACE = "api"
2018-04-24 15:57:55 +00:00
class NamespacedHRField(serializers.HyperlinkedRelatedField):
2018-06-17 01:06:58 +00:00
"""A `rest_framework.serializers.HyperlinkedRelatedField` subclass to
automatically prefix view names with the API namespace.
"""
2018-04-24 15:57:55 +00:00
def __init__(self, view_name=None, **kwargs):
if view_name is not None:
view_name = "%s:%s" % (API_NAMESPACE, view_name)
2018-04-24 15:57:55 +00:00
super(NamespacedHRField, self).__init__(view_name=view_name, **kwargs)
class NamespacedHIField(serializers.HyperlinkedIdentityField):
2018-06-17 01:06:58 +00:00
"""A `rest_framework.serializers.HyperlinkedIdentityField` subclass to
automatically prefix view names with teh API namespace.
"""
2018-04-24 15:57:55 +00:00
def __init__(self, view_name=None, **kwargs):
if view_name is not None:
view_name = "%s:%s" % (API_NAMESPACE, view_name)
2018-04-24 15:57:55 +00:00
super(NamespacedHIField, self).__init__(view_name=view_name, **kwargs)
class NamespacedHMSerializer(serializers.HyperlinkedModelSerializer):
2018-06-17 01:06:58 +00:00
"""A `rest_framework.serializers.HyperlinkedModelSerializer` subclass to
automatically prefix view names with the API namespace.
"""
2018-04-24 15:57:55 +00:00
serializer_related_field = NamespacedHRField
serializer_url_field = NamespacedHIField
2018-03-17 17:50:03 +00:00
2018-04-22 00:36:06 +00:00
2018-06-10 15:12:42 +00:00