forms.py 7.37 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 24 25

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Div, Submit, BaseInput
from crispy_forms.bootstrap import FormActions

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 125 126 127
                'vlan',
                'ipv4',
                'ipv6',
                'shared_ip',
Bach Dániel committed
128
                'external_ipv4',
129 130
            ),
            Fieldset(
131
                _('Information'),
132 133 134 135 136 137 138
                'description',
                'location',
                'comment',
                'owner',
            ),
        ),
        FormActions(
139 140
            Submit('submit', _('Save')),
            LinkButton('back', _('Back'), reverse_lazy('network.host_list')))
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    )

    class Meta:
        model = Host


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

    class Meta:
        model = Record


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

    class Meta:
        model = Rule


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

    class Meta:
        model = SwitchPort


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

    class Meta:
        model = Vlan


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

    class Meta:
        model = VlanGroup