8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-09 03:16:25 +00:00
re2o/machines/serializers.py

86 lines
2.5 KiB
Python
Raw Normal View History

2016-11-12 15:12:24 +00:00
#Augustin Lemesle
2016-07-18 21:22:59 +00:00
from rest_framework import serializers
from machines.models import Interface, IpType, Extension, IpList, MachineType, Alias, 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-07-18 21:22:59 +00:00
class Meta:
model = Interface
2016-11-16 18:30:48 +00:00
fields = ('ipv4', 'mac_address', 'dns')
class ExtensionNameField(serializers.RelatedField):
def to_representation(self, value):
return value.alias
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')
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
class MxSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField('get_alias_name')
zone = serializers.SerializerMethodField('get_zone_name')
2016-11-16 18:30:48 +00:00
2016-11-12 15:12:24 +00:00
class Meta:
model = Mx
fields = ('zone', 'priority', 'name')
def get_alias_name(self, obj):
return obj.name.alias + obj.name.extension.name
def get_zone_name(self, obj):
return obj.zone.name
class NsSerializer(serializers.ModelSerializer):
zone = serializers.SerializerMethodField('get_zone_name')
interface = serializers.SerializerMethodField('get_interface_name')
2016-07-18 21:22:59 +00:00
2016-11-18 17:15:57 +00:00
class Meta:
model = Ns
fields = ('zone', 'interface')
def get_zone_name(self, obj):
return obj.zone.name
def get_interface_name(self, obj):
return obj.interface.dns + obj.interface.ipv4.ip_type.extension.name
2016-11-18 17:15:57 +00:00
class AliasSerializer(serializers.ModelSerializer):
interface_parent = serializers.SerializerMethodField('get_interface_name')
extension = serializers.SerializerMethodField('get_zone_name')
2016-07-18 21:22:59 +00:00
2016-11-18 17:15:57 +00:00
class Meta:
model = Alias
fields = ('interface_parent', 'alias', 'extension')
def get_zone_name(self, obj):
return obj.extension.name
def get_interface_name(self, obj):
return obj.interface_parent.dns + obj.interface_parent.ipv4.ip_type.extension.name