admin.py 4.42 KB
Newer Older
1
# -*- coding: utf-8 -*-
2 3

from django.contrib import admin
4
from firewall.models import (Rule, Host, Vlan, Group, VlanGroup, Firewall,
5 6
                             Domain, Record, Blacklist,
                             SwitchPort, EthernetDevice)
7 8 9 10 11 12
from django import contrib


class RuleInline(contrib.admin.TabularInline):
    model = Rule

13

14 15 16
class RecordInline(contrib.admin.TabularInline):
    model = Record

17

18 19
class HostAdmin(admin.ModelAdmin):
    list_display = ('hostname', 'vlan', 'ipv4', 'ipv6', 'pub_ipv4', 'mac',
20 21
                    'shared_ip', 'owner', 'description', 'reverse',
                    'list_groups')
22 23 24 25 26 27 28 29 30 31 32 33
    ordering = ('hostname', )
    list_filter = ('owner', 'vlan', 'groups')
    search_fields = ('hostname', 'description', 'ipv4', 'ipv6', 'mac')
    filter_horizontal = ('groups', )
    inlines = (RuleInline, RecordInline)

    @staticmethod
    def list_groups(instance):
        """Returns instance's groups' names as a comma-separated list."""
        names = [group.name for group in instance.groups.all()]
        return u', '.join(names)

34

35 36 37
class HostInline(contrib.admin.TabularInline):
    model = Host
    fields = ('hostname', 'ipv4', 'ipv6', 'pub_ipv4', 'mac', 'shared_ip',
38 39
              'owner', 'reverse')

40 41

class VlanAdmin(admin.ModelAdmin):
42
    list_display = ('vid', 'name', 'network4', 'network6',
43
                    'description', 'domain', 'snat_ip', )
44
    search_fields = ('vid', 'name', 'network4', )
45 46 47
    ordering = ('vid', )
    inlines = (RuleInline, )

48

49 50
class RuleAdmin(admin.ModelAdmin):
    list_display = ('r_type', 'color_desc', 'owner', 'extra', 'direction',
51 52
                    'accept', 'proto', 'sport', 'dport', 'nat',
                    'nat_dport', 'used_in')
53
    list_filter = ('vlan', 'owner', 'direction', 'accept',
54
                   'proto', 'nat')
55 56 57

    def color_desc(self, instance):
        """Returns a colorful description of the instance."""
Őry Máté committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        data = {
            'type': instance.r_type,
            'src': (instance.foreign_network.name
                    if instance.direction == '1' else instance.r_type),
            'dst': (instance.r_type if instance.direction == '1'
                    else instance.foreign_network.name),
            'para': (u'<span style="color: #00FF00;">' +
                     (('proto=%s ' % instance.proto)
                      if instance.proto else '') +
                     (('sport=%s ' % instance.sport)
                      if instance.sport else '') +
                     (('dport=%s ' % instance.dport)
                      if instance.dport else '') +
                     '</span>'),
            'desc': instance.description}
73 74
        return (u'<span style="color: #FF0000;">[%(type)s]</span> '
                u'%(src)s<span style="color: #0000FF;"> ▸ </span>%(dst)s '
Őry Máté committed
75
                u'%(para)s %(desc)s') % data
76 77 78 79 80 81 82 83 84 85 86
    color_desc.allow_tags = True

    @staticmethod
    def vlan_l(instance):
        """Returns instance's VLANs' names as a comma-separated list."""
        names = [vlan.name for vlan in instance.foreign_network.vlans.all()]
        return u', '.join(names)

    @staticmethod
    def used_in(instance):
        for field in [instance.vlan, instance.vlangroup, instance.host,
87
                      instance.hostgroup, instance.firewall]:
88 89 90 91 92 93 94
            if field:
                return unicode(field) + ' ' + field._meta.object_name


class AliasAdmin(admin.ModelAdmin):
    list_display = ('alias', 'host')

95

96 97 98 99
class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner', 'description')
    inlines = (RuleInline, )

100

101 102 103
class FirewallAdmin(admin.ModelAdmin):
    inlines = (RuleInline, )

104

105 106 107
class DomainAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner')

108

109
class RecordAdmin(admin.ModelAdmin):
110
    list_display = ('name', 'type', 'address', 'ttl', 'host', 'owner')
111

112

113 114 115
class BlacklistAdmin(admin.ModelAdmin):
    list_display = ('ipv4', 'reason', 'created_at', 'modified_at')

116 117 118 119 120 121 122 123

class SwitchPortAdmin(admin.ModelAdmin):
    list_display = ()


class EthernetDeviceAdmin(admin.ModelAdmin):
    list_display = ('name', )

124 125 126 127 128 129 130 131 132
admin.site.register(Host, HostAdmin)
admin.site.register(Vlan, VlanAdmin)
admin.site.register(Rule, RuleAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(VlanGroup)
admin.site.register(Firewall, FirewallAdmin)
admin.site.register(Domain, DomainAdmin)
admin.site.register(Record, RecordAdmin)
admin.site.register(Blacklist, BlacklistAdmin)
133 134
admin.site.register(SwitchPort)
admin.site.register(EthernetDevice, EthernetDeviceAdmin)