Exemplo n.º 1
0
class NodeDetailSerializer(ExtensibleNodeSerializer):
    """ node detail """
    
    if HSTORE_ENABLED:
        data = HStoreDictionaryField(required=False,
                                     label=_('extra data'),
                                     help_text=_('store extra attributes in JSON string'))
    
    class Meta:
        model = Node
        primary_fields = [
            'name', 'slug', 'status', 'user',
            'geometry', 'elev', 'address', 'description',
        ]
        
        if HSTORE_ENABLED:
            primary_fields += ['data']
            
        secondary_fields = [
            'access_level', 'layer', 'layer_name',
            'added', 'updated', 'relationships'
        ]
        
        fields = primary_fields + secondary_fields
        
        read_only_fields = ('added', 'updated')
        geo_field = 'geometry'
Exemplo n.º 2
0
class InterfaceSerializer(serializers.ModelSerializer):
    """ Base Interface Serializer Class """
    access_level = serializers.Field(source='get_access_level_display')
    mac = MacAddressField(label=_('mac address'))
    type = serializers.Field(source='get_type_display', label=_('type'))

    tx_rate = serializers.Field()
    rx_rate = serializers.Field()

    ip = serializers.SerializerMethodField('get_ip_addresses')
    ip_url = serializers.HyperlinkedIdentityField(view_name='api_interface_ip')

    if HSTORE_ENABLED:
        data = HStoreDictionaryField(
            required=False,
            label=_('extra data'),
            help_text=_('store extra attributes in JSON string'))

    def get_ip_addresses(self, obj):
        user = self.context['request'].user
        interfaces = Ip.objects.filter(interface=obj.id).accessible_to(user)
        return IpSerializer(interfaces, many=True, context=self.context).data

    class Meta:
        model = Interface
        fields = [
            'id',
            'access_level',
            'type',
            'name',
            'mac',
            'mtu',
            'tx_rate',
            'rx_rate',
            'added',
            'updated',
            'ip_url',
            'ip',
        ]
        read_only_fields = ['added', 'updated']

        if HSTORE_ENABLED:
            fields.insert(fields.index('added'), 'data')
Exemplo n.º 3
0
class DeviceDetailSerializer(DeviceListSerializer):

    access_level = serializers.Field(source='get_access_level_display')

    routing_protocols_named = serializers.RelatedField(
        source='routing_protocols', many=True)

    ethernet = serializers.SerializerMethodField('get_ethernet_interfaces')
    ethernet_url = serializers.HyperlinkedIdentityField(
        view_name='api_device_ethernet')

    wireless = serializers.SerializerMethodField('get_wireless_interfaces')
    wireless_url = serializers.HyperlinkedIdentityField(
        view_name='api_device_wireless')

    bridge = serializers.SerializerMethodField('get_bridge_interfaces')
    bridge_url = serializers.HyperlinkedIdentityField(
        view_name='api_device_bridge')

    tunnel = serializers.SerializerMethodField('get_tunnel_interfaces')
    tunnel_url = serializers.HyperlinkedIdentityField(
        view_name='api_device_tunnel')

    vlan = serializers.SerializerMethodField('get_vlan_interfaces')
    vlan_url = serializers.HyperlinkedIdentityField(
        view_name='api_device_vlan')

    if HSTORE_ENABLED:
        data = HStoreDictionaryField(
            required=False,
            label=_('extra data'),
            help_text=_('store extra attributes in JSON string'))

    def get_ethernet_interfaces(self, obj):
        user = self.context['request'].user
        interfaces = Ethernet.objects.filter(device=obj.id).accessible_to(user)
        return EthernetSerializer(interfaces, many=True,
                                  context=self.context).data

    def get_wireless_interfaces(self, obj):
        user = self.context['request'].user
        interfaces = Wireless.objects.filter(device=obj.id).accessible_to(user)
        return WirelessSerializer(interfaces, many=True,
                                  context=self.context).data

    def get_bridge_interfaces(self, obj):
        user = self.context['request'].user
        interfaces = Bridge.objects.filter(device=obj.id).accessible_to(user)
        return BridgeSerializer(interfaces, many=True,
                                context=self.context).data

    def get_tunnel_interfaces(self, obj):
        user = self.context['request'].user
        interfaces = Tunnel.objects.filter(device=obj.id).accessible_to(user)
        return TunnelSerializer(interfaces, many=True,
                                context=self.context).data

    def get_vlan_interfaces(self, obj):
        user = self.context['request'].user
        interfaces = Vlan.objects.filter(device=obj.id).accessible_to(user)
        return VlanSerializer(interfaces, many=True, context=self.context).data

    class Meta:
        model = Device
        primary_fields = [
            'id', 'access_level', 'node', 'name', 'type', 'status', 'location',
            'elev', 'os', 'os_version', 'description', 'routing_protocols',
            'routing_protocols_named', 'first_seen', 'last_seen', 'added',
            'updated'
        ]

        if HSTORE_ENABLED:
            primary_fields.insert(primary_fields.index('added'), 'data')

        secondary_fields = [
            'ethernet', 'ethernet_url', 'wireless', 'wireless_url', 'bridge',
            'bridge_url', 'tunnel', 'tunnel_url', 'vlan', 'vlan_url'
        ]

        fields = primary_fields + secondary_fields
        read_only_fields = [
            'added',
            'updated',
            'first_seen',
            'last_seen',
        ]