forms.py 7.98 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

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


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)


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

    class Meta:
67
        model = BlacklistItem
68 69 70 71 72 73 74 75 76 77


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

    class Meta:
        model = Domain


class GroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
96
                '',
97 98 99 100 101 102
                'name',
                'description',
                'owner',
            ),
        ),
        FormActions(
103 104
            Submit('submit', _('Save')),
            LinkButton('back', _("Back"), reverse_lazy('network.group_list'))
105 106 107 108 109 110 111 112 113 114 115 116
        )
    )

    class Meta:
        model = Group


class HostForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
117
                '',
118 119 120 121 122
                'hostname',
                'reverse',
                'mac',
            ),
            Fieldset(
123
                _('Network'),
124
                'vlan',
125
                FieldWithButtons('ipv4', StrictButton(
126 127
                    '<i class="fa fa-magic"></i>', css_id="ipv4-magic",
                    title=_("Generate random address."))),
128
                FieldWithButtons('ipv6', StrictButton(
129 130
                    '<i class="fa fa-magic"></i>', css_id="ipv6-magic",
                    title=_("Generate IPv6 pair of IPv4 address."))),
131
                'shared_ip',
Bach Dániel committed
132
                'external_ipv4',
133 134
            ),
            Fieldset(
135
                _('Information'),
136 137 138 139 140 141 142
                'description',
                'location',
                'comment',
                'owner',
            ),
        ),
        FormActions(
143 144
            Submit('submit', _('Save')),
            LinkButton('back', _('Back'), reverse_lazy('network.host_list')))
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    )

    class Meta:
        model = Host


class RecordForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'type',
                'host',
                'name',
                'domain',
                'address',
                'ttl',
                'description',
                'owner',
            )
        ),
        FormActions(
168 169
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.record_list'))
170 171 172 173 174 175 176 177 178 179 180 181
        )
    )

    class Meta:
        model = Record


class RuleForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
182
                '',
183 184 185 186 187
                'direction',
                'description',
                'foreign_network',
                'dport',
                'sport',
188
                'weight',
189 190
                'proto',
                'extra',
191
                'action',
192 193
                'owner',
                'nat',
194 195
                'nat_external_port',
                'nat_external_ipv4',
196 197
            ),
            Fieldset(
198
                _('External'),
199 200 201 202 203 204 205 206
                'vlan',
                'vlangroup',
                'host',
                'hostgroup',
                'firewall'
            )
        ),
        FormActions(
207 208
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.rule_list'))
209 210 211 212 213 214 215
        )
    )

    class Meta:
        model = Rule


216 217 218 219 220 221 222 223 224 225 226 227
class SwitchPortForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'untagged_vlan',
                'tagged_vlans',
                'description',
            )
        ),
        FormActions(
228 229
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"),
230 231 232 233 234 235 236 237
                       reverse_lazy('network.switch_port_list'))
        )
    )

    class Meta:
        model = SwitchPort


238 239 240 241 242
class VlanForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
243
                '',
244 245
                'name',
                'vid',
246 247
                'network_type',
                'managed',
248 249
            ),
            Fieldset(
250
                _('IPv4'),
251
                'network4',
252 253 254 255 256
                'snat_to',
                'snat_ip',
                'dhcp_pool',
            ),
            Fieldset(
257
                _('IPv6'),
258
                'network6',
259
                FieldWithButtons('ipv6_template', StrictButton(
260 261
                    '<i class="fa fa-magic"></i>', css_id="ipv6-tpl-magic",
                    title=_("Generate sensible template."))),
262
                'host_ipv6_prefixlen',
263 264
            ),
            Fieldset(
265
                _('Domain name service'),
266 267 268 269
                'domain',
                'reverse_domain',
            ),
            Fieldset(
270
                _('Info'),
271 272
                'description',
                'comment',
273
                'owner',
274 275 276 277 278
                # 'created_at',
                # 'modified_at',
            ),
        ),
        FormActions(
279 280
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.vlan_list'))
281 282 283 284 285
        )
    )

    class Meta:
        model = Vlan
286 287 288
        widgets = {
            'ipv6_template': widgets.TextInput,
        }
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303


class VlanGroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'name',
                'vlans',
                'description',
                'owner',
            )
        ),
        FormActions(
304 305
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy(
306 307 308 309 310 311
                'network.vlan_group_list'))
        )
    )

    class Meta:
        model = VlanGroup