forms.py 7.56 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 19
from django.forms import ModelForm
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, AppendedText
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 126 127 128
                AppendedText('ipv4',
                             '<i class="fa fa-magic" id="ipv4-magic"></i>'),
                AppendedText('ipv6',
                             '<i class="fa fa-magic" id="ipv6-magic"></i>'),
129
                'shared_ip',
Bach Dániel committed
130
                'external_ipv4',
131 132
            ),
            Fieldset(
133
                _('Information'),
134 135 136 137 138 139 140
                'description',
                'location',
                'comment',
                'owner',
            ),
        ),
        FormActions(
141 142
            Submit('submit', _('Save')),
            LinkButton('back', _('Back'), reverse_lazy('network.host_list')))
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    )

    class Meta:
        model = Host


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

    class Meta:
        model = Record


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

    class Meta:
        model = Rule


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

    class Meta:
        model = SwitchPort


236 237 238 239 240
class VlanForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
241
                '',
242 243
                'name',
                'vid',
244 245
                'network_type',
                'managed',
246 247
            ),
            Fieldset(
248
                _('IPv4'),
249
                'network4',
250 251 252 253 254
                'snat_to',
                'snat_ip',
                'dhcp_pool',
            ),
            Fieldset(
255
                _('IPv6'),
256
                'network6',
257
                'ipv6_template',
258
                'host_ipv6_prefixlen',
259 260
            ),
            Fieldset(
261
                _('Domain name service'),
262 263 264 265
                'domain',
                'reverse_domain',
            ),
            Fieldset(
266
                _('Info'),
267 268
                'description',
                'comment',
269
                'owner',
270 271 272 273 274
                # 'created_at',
                # 'modified_at',
            ),
        ),
        FormActions(
275 276
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy('network.vlan_list'))
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        )
    )

    class Meta:
        model = Vlan


class VlanGroupForm(ModelForm):
    helper = FormHelper()
    helper.layout = Layout(
        Div(
            Fieldset(
                '',
                'name',
                'vlans',
                'description',
                'owner',
            )
        ),
        FormActions(
297 298
            Submit('submit', _("Save")),
            LinkButton('back', _("Back"), reverse_lazy(
299 300 301 302 303 304
                'network.vlan_group_list'))
        )
    )

    class Meta:
        model = VlanGroup