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

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


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

12

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

16

17 18
class HostAdmin(admin.ModelAdmin):
    list_display = ('hostname', 'vlan', 'ipv4', 'ipv6', 'pub_ipv4', 'mac',
19 20
                    'shared_ip', 'owner', 'description', 'reverse',
                    'list_groups')
21 22 23 24 25 26 27 28 29 30 31 32
    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)

33

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

39 40

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

47

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

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


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

93

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

98

99 100 101
class FirewallAdmin(admin.ModelAdmin):
    inlines = (RuleInline, )

102

103 104 105
class DomainAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner')

106

107 108 109 110 111 112 113 114 115 116 117 118 119
class RecordAdmin(admin.ModelAdmin):
    list_display = ('name_', 'type', 'address_', 'ttl', 'host', 'owner')

    @staticmethod
    def address_(instance):
        a = instance.get_data()
        return a['address'] if a else None

    @staticmethod
    def name_(instance):
        a = instance.get_data()
        return a['name'] if a else None

120

121 122 123 124 125 126 127 128 129 130 131 132
class BlacklistAdmin(admin.ModelAdmin):
    list_display = ('ipv4', 'reason', 'created_at', 'modified_at')

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)