forms.py 8.31 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 57 58 59 60 61
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'ipv4',
                'host',
                'reason',
                'type',
            )
        ),
        FormActions(
62
            Submit('submit', _('Save changes')),
63 64
            LinkButton('back', _("Back"),
                       reverse_lazy('network.blacklist_list'))
65 66 67 68
        )
    )

    class Meta:
69
        model = BlacklistItem
70 71 72 73 74 75 76 77 78 79


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

    class Meta:
        model = Domain


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

    class Meta:
        model = Firewall


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

    class Meta:
        model = Group


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

    class Meta:
        model = Host


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

    class Meta:
        model = Record


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

    class Meta:
        model = Rule


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

    class Meta:
        model = SwitchPort


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

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


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

    class Meta:
        model = VlanGroup