forms.py 8.38 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 74 75 76 77 78 79 80 81


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

    class Meta:
        model = Domain


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

    class Meta:
        model = Firewall


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

    class Meta:
        model = Group


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

    class Meta:
        model = Host


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

    class Meta:
        model = Record


class RuleForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
201
                '',
202 203 204 205 206
                'direction',
                'description',
                'foreign_network',
                'dport',
                'sport',
207
                'weight',
208 209
                'proto',
                'extra',
210
                'action',
211 212
                'owner',
                'nat',
213 214
                'nat_external_port',
                'nat_external_ipv4',
215 216
            ),
            Fieldset(
217
                _('External'),
218 219 220 221 222 223 224 225
                'vlan',
                'vlangroup',
                'host',
                'hostgroup',
                'firewall'
            )
        ),
        FormActions(
226 227
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.rule_list'))
228 229 230 231 232 233 234
        )
    )

    class Meta:
        model = Rule


235 236 237 238 239 240 241 242 243 244 245 246
class SwitchPortForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'untagged_vlan',
                'tagged_vlans',
                'description',
            )
        ),
        FormActions(
247 248
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"),
249 250 251 252 253 254 255 256
                       reverse_lazy('network.switch_port_list'))
        )
    )

    class Meta:
        model = SwitchPort


257 258 259 260 261
class VlanForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
262
                '',
263 264
                'name',
                'vid',
265 266
                'network_type',
                'managed',
267 268
            ),
            Fieldset(
269
                _('IPv4'),
270
                'network4',
271 272 273 274 275
                'snat_to',
                'snat_ip',
                'dhcp_pool',
            ),
            Fieldset(
276
                _('IPv6'),
277
                'network6',
278
                FieldWithButtons('ipv6_template', StrictButton(
279 280
                    '<i class="fa fa-magic"></i>', css_id="ipv6-tpl-magic",
                    title=_("Generate sensible template."))),
281
                'host_ipv6_prefixlen',
282 283
            ),
            Fieldset(
284
                _('Domain name service'),
285 286 287 288
                'domain',
                'reverse_domain',
            ),
            Fieldset(
289
                _('Info'),
290 291
                'description',
                'comment',
292
                'owner',
293 294 295 296 297
                # 'created_at',
                # 'modified_at',
            ),
        ),
        FormActions(
298 299
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.vlan_list'))
300 301 302 303 304
        )
    )

    class Meta:
        model = Vlan
305 306 307
        widgets = {
            'ipv6_template': widgets.TextInput,
        }
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322


class VlanGroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'name',
                'vlans',
                'description',
                'owner',
            )
        ),
        FormActions(
323 324
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy(
325 326 327 328 329 330
                'network.vlan_group_list'))
        )
    )

    class Meta:
        model = VlanGroup