forms.py 9.58 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/>.

18
from django.forms import ModelForm, widgets
19
from django.core.urlresolvers import reverse_lazy
20
from django.utils.translation import ugettext_lazy as _
21 22 23

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Div, Submit, BaseInput
24
from crispy_forms.bootstrap import FormActions, FieldWithButtons, StrictButton
25

Kálmán Viktor committed
26 27 28 29
from firewall.models import (
    Host, Vlan, Domain, Group, Record, BlacklistItem, Rule, VlanGroup,
    SwitchPort, Firewall
)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48


class LinkButton(BaseInput):
    """
    Used to create a link button descriptor for the {% crispy %} template tag::

        back = LinkButton('back', 'Back', reverse_lazy('index'))

    .. note:: The first argument is also slugified and turned into the id for
              the submit button.
    """
    template = "bootstrap/layout/linkbutton.html"
    field_classes = 'btn btn-default'

    def __init__(self, name, text, url, *args, **kwargs):
        self.href = url
        super(LinkButton, self).__init__(name, text, *args, **kwargs)


49
class BlacklistItemForm(ModelForm):
50 51 52 53 54 55 56
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'ipv4',
                'host',
57 58
                'expires_at',
                'whitelisted',
59
                'reason',
60
                'snort_message',
61 62 63
            )
        ),
        FormActions(
64
            Submit('submit', _('Save changes')),
65 66
            LinkButton('back', _("Back"),
                       reverse_lazy('network.blacklist_list'))
67 68 69 70
        )
    )

    class Meta:
71
        model = BlacklistItem
72 73
        fields = ("ipv4", "host", "expires_at", "whitelisted", "reason",
                  "snort_message", )
74 75 76 77 78 79 80 81 82 83


class DomainForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'name',
                'ttl',
84
                'owner',
85 86 87
            ),
        ),
        FormActions(
88 89
            Submit('submit', _('Save')),
            LinkButton('back', _("Back"), reverse_lazy('network.domain_list'))
90 91 92 93 94
        )
    )

    class Meta:
        model = Domain
95
        fields = ("name", "ttl", "owner", )
96 97


Kálmán Viktor committed
98 99 100
class FirewallForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
Kálmán Viktor committed
101
        Div(Fieldset('', 'name', )),
Kálmán Viktor committed
102 103 104 105 106 107 108 109 110
        FormActions(
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"),
                       reverse_lazy('network.firewall_list'))
        )
    )

    class Meta:
        model = Firewall
111
        fields = ("name", )
Kálmán Viktor committed
112 113


114 115 116 117 118
class GroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
119
                '',
120 121 122 123 124 125
                'name',
                'description',
                'owner',
            ),
        ),
        FormActions(
126 127
            Submit('submit', _('Save')),
            LinkButton('back', _("Back"), reverse_lazy('network.group_list'))
128 129 130 131 132
        )
    )

    class Meta:
        model = Group
133
        fields = ("name", "description", "owner", )
134 135 136 137 138 139 140


class HostForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
141
                '',
142 143 144 145 146
                'hostname',
                'reverse',
                'mac',
            ),
            Fieldset(
147
                _('Network'),
148
                'vlan',
149
                FieldWithButtons('ipv4', StrictButton(
150 151
                    '<i class="fa fa-magic"></i>', css_id="ipv4-magic",
                    title=_("Generate random address."))),
152
                FieldWithButtons('ipv6', StrictButton(
153 154
                    '<i class="fa fa-magic"></i>', css_id="ipv6-magic",
                    title=_("Generate IPv6 pair of IPv4 address."))),
155
                'shared_ip',
Bach Dániel committed
156
                'external_ipv4',
157 158
            ),
            Fieldset(
159
                _('Information'),
160 161 162 163 164 165 166
                'description',
                'location',
                'comment',
                'owner',
            ),
        ),
        FormActions(
167 168
            Submit('submit', _('Save')),
            LinkButton('back', _('Back'), reverse_lazy('network.host_list')))
169 170 171 172
    )

    class Meta:
        model = Host
173 174 175
        fields = ("hostname", "reverse", "mac", "vlan", "shared_ip", "ipv4",
                  "ipv6", "external_ipv4", "description", "location",
                  "comment", "owner", )
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194


class RecordForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'type',
                'host',
                'name',
                'domain',
                'address',
                'ttl',
                'description',
                'owner',
            )
        ),
        FormActions(
195 196
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.record_list'))
197 198 199 200 201
        )
    )

    class Meta:
        model = Record
202 203
        fields = ("type", "host", "name", "domain", "address", "ttl",
                  "description", "owner", )
204 205 206 207 208 209 210


class RuleForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
211
                '',
212 213 214 215 216
                'direction',
                'description',
                'foreign_network',
                'dport',
                'sport',
217
                'weight',
218 219
                'proto',
                'extra',
220
                'action',
221 222
                'owner',
                'nat',
223 224
                'nat_external_port',
                'nat_external_ipv4',
225 226
            ),
            Fieldset(
227
                _('External'),
228 229 230 231 232 233 234 235
                'vlan',
                'vlangroup',
                'host',
                'hostgroup',
                'firewall'
            )
        ),
        FormActions(
236 237
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.rule_list'))
238 239 240 241 242
        )
    )

    class Meta:
        model = Rule
243 244 245 246
        fields = ("direction", "description", "foreign_network", "dport",
                  "sport", "weight", "proto", "extra", "action", "owner",
                  "nat", "nat_external_port", "nat_external_ipv4", "vlan",
                  "vlangroup", "host", "hostgroup", "firewall", )
247 248


249 250 251 252 253 254 255 256 257 258 259 260
class SwitchPortForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'untagged_vlan',
                'tagged_vlans',
                'description',
            )
        ),
        FormActions(
261 262
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"),
263 264 265 266 267 268
                       reverse_lazy('network.switch_port_list'))
        )
    )

    class Meta:
        model = SwitchPort
269
        fields = ("untagged_vlan", "tagged_vlans", "description", )
270 271


272 273 274 275 276
class VlanForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
277
                '',
278 279
                'name',
                'vid',
280 281
                'network_type',
                'managed',
282 283
            ),
            Fieldset(
284
                _('IPv4'),
285
                'network4',
286 287 288 289 290
                'snat_to',
                'snat_ip',
                'dhcp_pool',
            ),
            Fieldset(
291
                _('IPv6'),
292
                'network6',
293
                FieldWithButtons('ipv6_template', StrictButton(
294 295
                    '<i class="fa fa-magic"></i>', css_id="ipv6-tpl-magic",
                    title=_("Generate sensible template."))),
296
                'host_ipv6_prefixlen',
297 298
            ),
            Fieldset(
299
                _('Domain name service'),
300 301 302 303
                'domain',
                'reverse_domain',
            ),
            Fieldset(
304
                _('Info'),
305 306
                'description',
                'comment',
307
                'owner',
308 309 310 311 312
                # 'created_at',
                # 'modified_at',
            ),
        ),
        FormActions(
313 314
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.vlan_list'))
315 316 317 318 319
        )
    )

    class Meta:
        model = Vlan
320 321 322
        widgets = {
            'ipv6_template': widgets.TextInput,
        }
323 324 325 326
        fields = ("name", "vid", "network_type", "managed", "network4",
                  "snat_to", "snat_ip", "dhcp_pool", "network6",
                  "ipv6_template", "host_ipv6_prefixlen", "domain",
                  "reverse_domain", "description", "comment", "owner", )
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341


class VlanGroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'name',
                'vlans',
                'description',
                'owner',
            )
        ),
        FormActions(
342 343
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy(
344 345 346 347 348 349
                'network.vlan_group_list'))
        )
    )

    class Meta:
        model = VlanGroup
350
        fields = ("name", "vlans", "description", "owner", )