1
1
"""
2
2
Defines each django model's GUI form to add or edit objects for each django model.
3
3
"""
4
- from django . utils . translation import gettext_lazy as _
4
+
5
5
from dcim .models import Device , Interface , Region , Site , SiteGroup , VirtualChassis
6
6
from django .contrib .contenttypes .models import ContentType
7
7
from django .core .exceptions import ValidationError
8
8
from django .utils .safestring import mark_safe
9
+ from django .utils .translation import gettext_lazy as _
9
10
from ipam .models import Prefix
10
11
from netbox .forms import NetBoxModelForm
11
- from utilities .forms .rendering import FieldSet
12
12
from utilities .forms .fields import CommentField , DynamicModelChoiceField
13
+ from utilities .forms .rendering import FieldSet , TabbedGroups
13
14
from virtualization .models import (
14
15
Cluster ,
15
16
ClusterGroup ,
@@ -117,14 +118,41 @@ class AccessListForm(NetBoxModelForm):
117
118
"cluster_group_id" : "$cluster_group" ,
118
119
},
119
120
)
120
-
121
121
comments = CommentField ()
122
+
122
123
fieldsets = (
123
- FieldSet ('region' , 'site_group' , 'site' , 'virtual_machine' , 'virtual_chassis' , 'device' , name = _ ('Assignment' )),
124
- FieldSet ('name' , 'type' , 'default_action' , name = _ ('Access List' )),
125
- FieldSet ('comments' , 'tags' , name = _ ('' )),
124
+ FieldSet (
125
+ "name" ,
126
+ "type" ,
127
+ "default_action" ,
128
+ "tags" ,
129
+ name = _ ("Access List Details" ),
130
+ ),
131
+ FieldSet (
132
+ TabbedGroups (
133
+ FieldSet (
134
+ "region" ,
135
+ "site_group" ,
136
+ "site" ,
137
+ "device" ,
138
+ name = _ ("Device" ),
139
+ ),
140
+ FieldSet (
141
+ "virtual_chassis" ,
142
+ name = _ ("Virtual Chassis" ),
143
+ ),
144
+ FieldSet (
145
+ "cluster_type" ,
146
+ "cluster_group" ,
147
+ "cluster" ,
148
+ "virtual_machine" ,
149
+ name = _ ("Virtual Machine" ),
150
+ ),
151
+ ),
152
+ name = _ ("Host Assignment" ),
153
+ ),
126
154
)
127
-
155
+
128
156
class Meta :
129
157
model = AccessList
130
158
fields = (
@@ -145,7 +173,7 @@ class Meta:
145
173
"default_action" : "The default behavior of the ACL." ,
146
174
"name" : "The name uniqueness per device is case insensitive." ,
147
175
"type" : mark_safe (
148
- "<b>*Note:</b> CANNOT be changed if ACL Rules are assoicated to this Access List." ,
176
+ "<b>*Note:</b> CANNOT be changed if ACL Rules are associated to this Access List." ,
149
177
),
150
178
}
151
179
@@ -200,14 +228,22 @@ def clean(self):
200
228
# Check if more than one host type selected.
201
229
if (device and virtual_chassis ) or (device and virtual_machine ) or (virtual_chassis and virtual_machine ):
202
230
raise ValidationError (
203
- {"__all__" : "Access Lists must be assigned to one host at a time. Either a device, virtual chassis or virtual machine." },
231
+ {
232
+ "__all__" : (
233
+ "Access Lists must be assigned to one host at a time. Either a device, virtual chassis or "
234
+ "virtual machine."
235
+ )
236
+ },
204
237
)
205
238
206
239
# Check if no hosts selected.
207
240
if not device and not virtual_chassis and not virtual_machine :
208
- raise ValidationError ({"__all__" : "Access Lists must be assigned to a device, virtual chassis or virtual machine." })
241
+ raise ValidationError (
242
+ {"__all__" : "Access Lists must be assigned to a device, virtual chassis or virtual machine." }
243
+ )
209
244
210
245
existing_acls = None
246
+ host_type = None
211
247
if device :
212
248
host_type = "device"
213
249
existing_acls = AccessList .objects .filter (name = name , device = device ).exists ()
@@ -233,7 +269,9 @@ def clean(self):
233
269
def save (self , * args , ** kwargs ):
234
270
# Set assigned object
235
271
self .instance .assigned_object = (
236
- self .cleaned_data .get ("device" ) or self .cleaned_data .get ("virtual_chassis" ) or self .cleaned_data .get ("virtual_machine" )
272
+ self .cleaned_data .get ("device" )
273
+ or self .cleaned_data .get ("virtual_chassis" )
274
+ or self .cleaned_data .get ("virtual_machine" )
237
275
)
238
276
239
277
return super ().save (* args , ** kwargs )
@@ -292,10 +330,29 @@ class ACLInterfaceAssignmentForm(NetBoxModelForm):
292
330
),
293
331
)
294
332
comments = CommentField ()
333
+
295
334
fieldsets = (
296
- FieldSet ('device' , 'interface' , 'virtual_machine' , 'vminterface' , name = _ ('Assignment' )),
297
- FieldSet ('access_list' , 'direction' , name = _ ('Access List Details' )),
298
- FieldSet ('comments' , 'tags' , name = _ ('' )),
335
+ FieldSet (
336
+ "access_list" ,
337
+ "direction" ,
338
+ "tags" ,
339
+ name = _ ("Access List Details" ),
340
+ ),
341
+ FieldSet (
342
+ TabbedGroups (
343
+ FieldSet (
344
+ "device" ,
345
+ "interface" ,
346
+ name = _ ("Device" ),
347
+ ),
348
+ FieldSet (
349
+ "virtual_machine" ,
350
+ "vminterface" ,
351
+ name = _ ("Virtual Machine" ),
352
+ ),
353
+ ),
354
+ name = _ ("Interface Assignment" ),
355
+ ),
299
356
)
300
357
301
358
def __init__ (self , * args , ** kwargs ):
@@ -453,9 +510,21 @@ class ACLStandardRuleForm(NetBoxModelForm):
453
510
)
454
511
455
512
fieldsets = (
456
- FieldSet ("access_list" , "description" , "tags" , name = _ ('Access List Details' )),
457
- FieldSet ("index" , "action" , "remark" , "source_prefix" , name = _ ('Rule Definition' ))
513
+ FieldSet (
514
+ "access_list" ,
515
+ "description" ,
516
+ "tags" ,
517
+ name = _ ("Access List Details" ),
518
+ ),
519
+ FieldSet (
520
+ "index" ,
521
+ "action" ,
522
+ "remark" ,
523
+ "source_prefix" ,
524
+ name = _ ("Rule Definition" ),
525
+ ),
458
526
)
527
+
459
528
class Meta :
460
529
model = ACLStandardRule
461
530
fields = (
@@ -468,9 +537,6 @@ class Meta:
468
537
"description" ,
469
538
)
470
539
471
-
472
-
473
-
474
540
help_texts = {
475
541
"index" : help_text_acl_rule_index ,
476
542
"action" : help_text_acl_action ,
@@ -540,9 +606,25 @@ class ACLExtendedRuleForm(NetBoxModelForm):
540
606
label = "Destination Prefix" ,
541
607
)
542
608
fieldsets = (
543
- FieldSet ("access_list" , "description" , "tags" , name = _ ('Access List Details' )),
544
- FieldSet ("index" , "action" , "remark" , "source_prefix" , "source_ports" , "destination_prefix" , "destination_ports" , "protocol" , name = _ ('Rule Definition' ))
609
+ FieldSet (
610
+ "access_list" ,
611
+ "description" ,
612
+ "tags" ,
613
+ name = _ ("Access List Details" ),
614
+ ),
615
+ FieldSet (
616
+ "index" ,
617
+ "action" ,
618
+ "remark" ,
619
+ "source_prefix" ,
620
+ "source_ports" ,
621
+ "destination_prefix" ,
622
+ "destination_ports" ,
623
+ "protocol" ,
624
+ name = _ ("Rule Definition" ),
625
+ ),
545
626
)
627
+
546
628
class Meta :
547
629
model = ACLExtendedRule
548
630
fields = (
0 commit comments