tables.py 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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/>.

Őry Máté committed
18
from netaddr import EUI
19
from django.utils.translation import ugettext_lazy as _
Őry Máté committed
20
from django.utils.html import format_html
21

22
from django_tables2 import Table, A
23 24
from django_tables2.columns import (LinkColumn, TemplateColumn, Column,
                                    BooleanColumn)
25

26
from firewall.models import Host, Vlan, Domain, Group, Record, Rule, SwitchPort
27 28


Őry Máté committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42
class MACColumn(Column):
    def render(self, value):
        if isinstance(value, basestring):
            try:
                value = EUI(value)
            except:
                return value
        try:
            return format_html('<abbr title="{0}">{1}</abbr>',
                               value.oui.registration().org, value)
        except:
            return value


43
class BlacklistItemTable(Table):
44
    ipv4 = LinkColumn('network.blacklist', args=[A('pk')])
45
    whitelisted = BooleanColumn()
46 47 48 49

    class Meta:
        model = Domain
        attrs = {'class': 'table table-striped table-condensed'}
50 51 52
        fields = ('ipv4', 'host', 'reason', 'whitelisted', 'expires_at',
                  'created_at')
        order_by = ('-expires_at', )
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75


class DomainTable(Table):
    name = LinkColumn('network.domain', args=[A('pk')])

    class Meta:
        model = Domain
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('name', 'owner', 'ttl', )
        order_by = ('name', )


class GroupTable(Table):
    name = LinkColumn('network.group', args=[A('pk')])

    class Meta:
        model = Group
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('name', 'description', 'owner', )
        order_by = ('name', )


class HostTable(Table):
76 77 78 79 80
    hostname = LinkColumn(
        'network.host',
        args=[A('pk')],
        order_by="normalized_hostname",
    )
Őry Máté committed
81
    mac = MACColumn()
82 83 84

    class Meta:
        model = Host
85 86
        attrs = {'class': "table table-striped table-condensed",
                 'id': "network-host-list-table"}
87
        fields = ('hostname', 'vlan', 'mac', 'ipv4', 'ipv6',
Bach Dániel committed
88
                  'external_ipv4', 'created_at', 'owner', )
89
        order_by = ("hostname", )
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107


class SmallRuleTable(Table):
    rule = TemplateColumn(
        template_name="network/columns/host-rule.html",
        attrs={"th": {"style": "display: none;"}}
    )

    action = TemplateColumn(
        template_name="network/columns/host-rule-action.html",
        attrs={
            "th": {"style": "display: none;"},
            "cell": {"style": "text-align: center; vertical-align: middle;"}
        }
    )

    class Meta:
        model = Rule
108 109
        attrs = {'class': 'table table-striped table-bordered table-condensed',
                 'id': "small_rule_table"}
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        fields = ('rule', 'action', )


class SmallGroupRuleTable(Table):
    rule = TemplateColumn(
        template_name="network/columns/host-rule.html",
        attrs={"th": {"style": "display: none;"}}
    )

    class Meta:
        model = Rule
        attrs = {'class': 'table table-striped table-bordered table-condensed'}
        fields = ('rule', )


# inheritance by copy-paste
class SmallHostTable(Table):
    hostname = LinkColumn('network.host', args=[A('pk')])

    class Meta:
        model = Host
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('hostname', 'ipv4')
        order_by = ('vlan', 'hostname', )
134 135 136 137 138 139 140 141 142 143 144 145 146 147
        empty_text = _("No hosts.")


class SmallDhcpTable(Table):
    mac = MACColumn(verbose_name=_("MAC address"))
    hostname = Column(verbose_name=_("hostname"))
    ip = Column(verbose_name=_("requested IP"))
    register = TemplateColumn(
        template_name="network/columns/host-register.html",
        attrs={"th": {"style": "display: none;"}})

    class Meta:
        attrs = {'class': 'table table-striped table-condensed'}
        empty_text = _("No hosts.")
148 149 150 151


class RecordTable(Table):
    fqdn = LinkColumn('network.record', args=[A('pk')], orderable=False)
152 153 154
    address = TemplateColumn(
        template_name="network/columns/records-address.html"
    )
155
    ttl = Column(verbose_name=_("TTL"))
156 157 158 159 160 161 162

    class Meta:
        model = Record
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('type', 'fqdn', 'host', 'address', 'ttl', 'host',
                  'owner', )
        sequence = ('type', 'fqdn', )
Bach Dániel committed
163
        # order_by = 'name'
164 165 166 167 168 169 170 171 172 173


class SmallRecordTable(Table):
    fqdn = LinkColumn('network.record', args=[A('pk')], orderable=False)

    class Meta:
        model = Record
        attrs = {'class': 'table table-striped table-bordered'}
        fields = ('type', 'fqdn', 'host', 'address', )
        sequence = ('type', 'fqdn', )
Bach Dániel committed
174
        # order_by = '-type'
175 176 177 178
        orderable = False


class RuleTable(Table):
179 180 181 182 183
    r_type = LinkColumn(
        'network.rule', args=[A('pk')],
        verbose_name=_("type"),
        orderable=False,
    )
184
    color_desc = TemplateColumn(
185 186 187 188 189 190
        template_name="network/columns/rule-short-description.html",
        verbose_name=_("Short description"),
        orderable=False,
    )
    nat_external_port = Column(
        verbose_name=_("NAT")
191 192 193 194
    )

    class Meta:
        model = Rule
195 196 197 198
        attrs = {'class': 'table table-striped table-hover table-condensed',
                 'id': "rule-list-table"}
        fields = ('r_type', 'color_desc', 'extra', 'direction',
                  'action', 'proto', 'dport',
199
                  'nat_external_port', )
200 201 202
        order_by = 'direction'


203 204 205 206 207 208 209 210 211 212 213
class SwitchPortTable(Table):
    pk = LinkColumn('network.switch_port', args=[A('pk')],
                    verbose_name="ID")

    class Meta:
        model = SwitchPort
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('pk', 'untagged_vlan', 'tagged_vlans', 'description', )
        order_by = 'pk'


214 215 216 217 218 219
class VlanTable(Table):
    name = LinkColumn('network.vlan', args=[A('vid')])

    class Meta:
        model = Vlan
        attrs = {'class': 'table table-striped table-condensed'}
220
        fields = ('vid', 'name', 'network4', 'network6',
221
                  'domain', )
222 223 224 225 226 227 228 229 230 231 232 233
        order_by = 'vid'


class VlanGroupTable(Table):
    name = LinkColumn('network.vlan_group', args=[A('pk')])
    vlans = TemplateColumn(template_name="network/columns/vlan-list.html")

    class Meta:
        model = Vlan
        attrs = {'class': 'table table-striped table-condensed'}
        fields = ('name', 'vlans', 'description', 'owner', )
        order_by = 'name'
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250


class HostRecordsTable(Table):
    fqdn = LinkColumn(
        "network.record", args=[A("pk")],
        order_by=("name", ),
    )

    class Meta:
        model = Record
        attrs = {
            'class': "table table-striped table-bordered",
            'id': "host-detail-records-table",
        }
        fields = ("type", "fqdn")
        order_by = ("name", )
        empty_text = _("No records.")
Kálmán Viktor committed
251 252 253 254 255 256 257 258 259 260 261


class FirewallTable(Table):
    pk = LinkColumn('network.firewall', args=[A('pk')],
                    verbose_name="ID")

    class Meta:
        model = SwitchPort
        attrs = {'class': 'table table-striped'}
        fields = ('pk', 'name', )
        order_by = 'pk'
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284


class FirewallRuleTable(Table):
    color_desc = TemplateColumn(
        template_name="network/columns/rule-short-description.html",
        verbose_name=_("Short description"),
        orderable=False,
    )
    actions = TemplateColumn(
        template_name="network/columns/firewall-rule-actions.html",
        verbose_name=_("Actions"),
        orderable=False,
    )

    class Meta:
        model = Rule
        template = "django_tables2/table_no_page.html"
        attrs = {'class': 'table table-striped table-hover table-condensed',
                 'id': "rule-list-table"}
        fields = ('color_desc', 'extra', 'direction',
                  'action', 'proto', 'actions')
        order_by = '-pk'
        empty_text = _("No related rules found.")