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

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

20
from django.contrib import admin
21
from firewall.models import (Rule, Host, Vlan, Group, VlanGroup, Firewall,
22
                             Domain, Record, BlacklistItem,
23
                             SwitchPort, EthernetDevice)
24 25 26 27 28 29
from django import contrib


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

30

31 32 33
class RecordInline(contrib.admin.TabularInline):
    model = Record

34

35
class HostAdmin(admin.ModelAdmin):
36
    list_display = ('hostname', 'vlan', 'ipv4', 'ipv6', 'external_ipv4', 'mac',
37 38
                    'shared_ip', 'owner', 'description', 'reverse',
                    'list_groups')
39 40 41 42 43 44
    ordering = ('hostname', )
    list_filter = ('owner', 'vlan', 'groups')
    search_fields = ('hostname', 'description', 'ipv4', 'ipv6', 'mac')
    filter_horizontal = ('groups', )
    inlines = (RuleInline, RecordInline)

45 46 47 48
    def queryset(self, request):
        qs = super(HostAdmin, self).queryset(request)
        return qs.prefetch_related('groups')

49 50 51 52 53 54
    @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)

55

56 57
class HostInline(contrib.admin.TabularInline):
    model = Host
Bach Dániel committed
58
    fields = ('hostname', 'ipv4', 'ipv6', 'external_ipv4', 'mac', 'shared_ip',
59 60
              'owner', 'reverse')

61 62

class VlanAdmin(admin.ModelAdmin):
63
    list_display = ('vid', 'name', 'network4', 'network6',
64
                    'description', 'domain', 'snat_ip', )
65
    search_fields = ('vid', 'name', 'network4', )
66 67 68
    ordering = ('vid', )
    inlines = (RuleInline, )

69

70 71
class RuleAdmin(admin.ModelAdmin):
    list_display = ('r_type', 'color_desc', 'owner', 'extra', 'direction',
72
                    'action', 'proto', 'sport', 'dport', 'nat',
73
                    'nat_external_port', 'used_in')
74
    list_filter = ('vlan', 'owner', 'direction', 'action',
75
                   'proto', 'nat')
76 77 78

    def color_desc(self, instance):
        """Returns a colorful description of the instance."""
Őry Máté committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        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}
94 95
        return (u'<span style="color: #FF0000;">[%(type)s]</span> '
                u'%(src)s<span style="color: #0000FF;"> ▸ </span>%(dst)s '
Őry Máté committed
96
                u'%(para)s %(desc)s') % data
97 98 99 100 101 102 103 104 105 106 107
    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,
108
                      instance.hostgroup, instance.firewall]:
109 110 111 112 113 114 115
            if field:
                return unicode(field) + ' ' + field._meta.object_name


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

116

117 118 119 120
class GroupAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner', 'description')
    inlines = (RuleInline, )

121

122 123 124
class FirewallAdmin(admin.ModelAdmin):
    inlines = (RuleInline, )

125

126 127 128
class DomainAdmin(admin.ModelAdmin):
    list_display = ('name', 'owner')

129

130
class RecordAdmin(admin.ModelAdmin):
131
    list_display = ('name', 'type', 'address', 'ttl', 'host', 'owner')
132

133

134 135
class BlacklistItemAdmin(admin.ModelAdmin):
    list_display = ('ipv4', 'type', 'reason', 'created_at', 'modified_at')
136

137 138 139 140 141 142 143 144

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


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

145 146 147 148 149 150 151 152
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)
153
admin.site.register(BlacklistItem, BlacklistItemAdmin)
154 155
admin.site.register(SwitchPort)
admin.site.register(EthernetDevice, EthernetDeviceAdmin)