admin.py 4.57 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
class HostAdmin(admin.ModelAdmin):
19
    list_display = ('hostname', 'vlan', 'ipv4', 'ipv6', 'external_ipv4', 'mac',
20 21
                    'shared_ip', 'owner', 'description', 'reverse',
                    'list_groups')
22 23 24 25 26 27
    ordering = ('hostname', )
    list_filter = ('owner', 'vlan', 'groups')
    search_fields = ('hostname', 'description', 'ipv4', 'ipv6', 'mac')
    filter_horizontal = ('groups', )
    inlines = (RuleInline, RecordInline)

28 29 30 31
    def queryset(self, request):
        qs = super(HostAdmin, self).queryset(request)
        return qs.prefetch_related('groups')

32 33 34 35 36 37
    @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)

38

39 40 41
class HostInline(contrib.admin.TabularInline):
    model = Host
    fields = ('hostname', 'ipv4', 'ipv6', 'pub_ipv4', 'mac', 'shared_ip',
42 43
              'owner', 'reverse')

44 45

class VlanAdmin(admin.ModelAdmin):
46
    list_display = ('vid', 'name', 'network4', 'network6',
47
                    'description', 'domain', 'snat_ip', )
48
    search_fields = ('vid', 'name', 'network4', )
49 50 51
    ordering = ('vid', )
    inlines = (RuleInline, )

52

53 54
class RuleAdmin(admin.ModelAdmin):
    list_display = ('r_type', 'color_desc', 'owner', 'extra', 'direction',
55
                    'action', 'proto', 'sport', 'dport', 'nat',
56
                    'nat_external_port', 'used_in')
57
    list_filter = ('vlan', 'owner', 'direction', 'action',
58
                   'proto', 'nat')
59 60 61

    def color_desc(self, instance):
        """Returns a colorful description of the instance."""
Őry Máté committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        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}
77 78
        return (u'<span style="color: #FF0000;">[%(type)s]</span> '
                u'%(src)s<span style="color: #0000FF;"> ▸ </span>%(dst)s '
Őry Máté committed
79
                u'%(para)s %(desc)s') % data
80 81 82 83 84 85 86 87 88 89 90
    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,
91
                      instance.hostgroup, instance.firewall]:
92 93 94 95 96 97 98
            if field:
                return unicode(field) + ' ' + field._meta.object_name


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

99

100 101 102 103
class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner', 'description')
    inlines = (RuleInline, )

104

105 106 107
class FirewallAdmin(admin.ModelAdmin):
    inlines = (RuleInline, )

108

109 110 111
class DomainAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner')

112

113
class RecordAdmin(admin.ModelAdmin):
114
    list_display = ('name', 'type', 'address', 'ttl', 'host', 'owner')
115

116

117 118 119
class BlacklistAdmin(admin.ModelAdmin):
    list_display = ('ipv4', 'reason', 'created_at', 'modified_at')

120 121 122 123 124 125 126 127

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


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

128 129 130 131 132 133 134 135 136
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)
137 138
admin.site.register(SwitchPort)
admin.site.register(EthernetDevice, EthernetDeviceAdmin)