2017-01-15 23:01:18 +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.
|
|
|
|
#
|
|
|
|
# Copyright © 2017 Gabriel Détraz
|
|
|
|
# Copyright © 2017 Goulven Kermarec
|
|
|
|
# Copyright © 2017 Augustin Lemesle
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2016-11-12 15:12:24 +00:00
|
|
|
#Augustin Lemesle
|
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
from rest_framework import serializers
|
2016-12-24 19:04:53 +00:00
|
|
|
from machines.models import Interface, IpType, Extension, IpList, MachineType, Domain, Mx, Ns
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class IpTypeField(serializers.RelatedField):
|
|
|
|
def to_representation(self, value):
|
|
|
|
return value.type
|
|
|
|
|
|
|
|
class IpListSerializer(serializers.ModelSerializer):
|
|
|
|
ip_type = IpTypeField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IpList
|
|
|
|
fields = ('ipv4', 'ip_type')
|
|
|
|
|
|
|
|
class InterfaceSerializer(serializers.ModelSerializer):
|
|
|
|
ipv4 = IpListSerializer(read_only=True)
|
2016-12-19 22:59:54 +00:00
|
|
|
mac_address = serializers.SerializerMethodField('get_macaddress')
|
2016-12-25 23:01:48 +00:00
|
|
|
domain = serializers.SerializerMethodField('get_dns')
|
2016-12-26 23:48:32 +00:00
|
|
|
extension = serializers.SerializerMethodField('get_interface_extension')
|
2016-12-19 22:59:54 +00:00
|
|
|
|
2016-07-18 21:22:59 +00:00
|
|
|
class Meta:
|
|
|
|
model = Interface
|
2016-12-26 17:00:43 +00:00
|
|
|
fields = ('ipv4', 'mac_address', 'domain', 'extension')
|
2016-12-24 19:04:53 +00:00
|
|
|
|
|
|
|
def get_dns(self, obj):
|
2016-12-26 17:00:43 +00:00
|
|
|
return obj.domain.name
|
|
|
|
|
2016-12-26 23:48:32 +00:00
|
|
|
def get_interface_extension(self, obj):
|
2017-08-01 04:57:56 +00:00
|
|
|
return obj.domain.get_extension().name
|
2016-11-16 18:30:48 +00:00
|
|
|
|
2016-12-19 22:59:54 +00:00
|
|
|
def get_macaddress(self, obj):
|
|
|
|
return str(obj.mac_address)
|
|
|
|
|
2016-11-16 18:30:48 +00:00
|
|
|
class ExtensionNameField(serializers.RelatedField):
|
2016-11-18 19:16:40 +00:00
|
|
|
def to_representation(self, value):
|
2016-12-19 19:55:35 +00:00
|
|
|
return value.name
|
2016-07-19 19:43:24 +00:00
|
|
|
|
2016-11-20 03:39:10 +00:00
|
|
|
class TypeSerializer(serializers.ModelSerializer):
|
|
|
|
extension = ExtensionNameField(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IpType
|
|
|
|
fields = ('type', 'extension', 'domaine_ip', 'domaine_range')
|
|
|
|
|
2016-11-20 04:12:36 +00:00
|
|
|
class ExtensionSerializer(serializers.ModelSerializer):
|
|
|
|
origin = serializers.SerializerMethodField('get_origin_ip')
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Extension
|
|
|
|
fields = ('name', 'origin')
|
|
|
|
|
|
|
|
def get_origin_ip(self, obj):
|
|
|
|
return obj.origin.ipv4
|
|
|
|
|
2016-11-20 03:29:16 +00:00
|
|
|
class MxSerializer(serializers.ModelSerializer):
|
2016-12-26 23:48:32 +00:00
|
|
|
name = serializers.SerializerMethodField('get_mx_name')
|
2016-11-20 03:29:16 +00:00
|
|
|
zone = serializers.SerializerMethodField('get_zone_name')
|
2016-11-16 18:30:48 +00:00
|
|
|
|
2016-11-12 15:12:24 +00:00
|
|
|
class Meta:
|
2016-11-20 03:29:16 +00:00
|
|
|
model = Mx
|
|
|
|
fields = ('zone', 'priority', 'name')
|
|
|
|
|
2016-12-26 23:48:32 +00:00
|
|
|
def get_mx_name(self, obj):
|
|
|
|
return str(obj.name)
|
2016-11-20 03:29:16 +00:00
|
|
|
|
|
|
|
def get_zone_name(self, obj):
|
|
|
|
return obj.zone.name
|
|
|
|
|
|
|
|
class NsSerializer(serializers.ModelSerializer):
|
|
|
|
zone = serializers.SerializerMethodField('get_zone_name')
|
2016-12-26 23:48:32 +00:00
|
|
|
ns = serializers.SerializerMethodField('get_domain_name')
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-18 17:15:57 +00:00
|
|
|
class Meta:
|
2016-11-20 03:29:16 +00:00
|
|
|
model = Ns
|
2016-12-26 16:43:41 +00:00
|
|
|
fields = ('zone', 'ns')
|
2016-11-20 03:29:16 +00:00
|
|
|
|
|
|
|
def get_zone_name(self, obj):
|
|
|
|
return obj.zone.name
|
|
|
|
|
2016-12-26 23:48:32 +00:00
|
|
|
def get_domain_name(self, obj):
|
|
|
|
return str(obj.ns)
|
2016-11-18 17:15:57 +00:00
|
|
|
|
2016-12-24 19:04:53 +00:00
|
|
|
class DomainSerializer(serializers.ModelSerializer):
|
2016-11-20 03:29:16 +00:00
|
|
|
extension = serializers.SerializerMethodField('get_zone_name')
|
2016-12-26 23:48:32 +00:00
|
|
|
cname = serializers.SerializerMethodField('get_cname_name')
|
2016-07-18 21:22:59 +00:00
|
|
|
|
2016-11-18 17:15:57 +00:00
|
|
|
class Meta:
|
2016-12-24 19:04:53 +00:00
|
|
|
model = Domain
|
2016-12-25 23:01:48 +00:00
|
|
|
fields = ('name', 'extension', 'cname')
|
2016-11-20 03:29:16 +00:00
|
|
|
|
|
|
|
def get_zone_name(self, obj):
|
2016-12-25 23:01:48 +00:00
|
|
|
return obj.extension.name
|
2016-11-20 03:29:16 +00:00
|
|
|
|
2016-12-26 23:48:32 +00:00
|
|
|
def get_cname_name(self, obj):
|
|
|
|
return str(obj.cname)
|
2016-12-24 19:04:53 +00:00
|
|
|
|