2
2
Define the django models for this plugin.
3
3
"""
4
4
5
- from django .apps import apps
6
5
from django .contrib .postgres .fields import ArrayField
7
6
from django .db import models
8
7
from django .urls import reverse
8
+ from django .utils .translation import gettext_lazy as _
9
9
from netbox .models import NetBoxModel
10
10
11
11
from ..choices import ACLProtocolChoices , ACLRuleActionChoices , ACLTypeChoices
21
21
class ACLRule (NetBoxModel ):
22
22
"""
23
23
Abstract model for ACL Rules.
24
- Inherrited by both ACLStandardRule and ACLExtendedRule.
24
+ Inherited by both ACLStandardRule and ACLExtendedRule.
25
25
"""
26
26
27
27
access_list = models .ForeignKey (
28
- on_delete = models .CASCADE ,
29
28
to = AccessList ,
30
- verbose_name = "Access List" ,
29
+ on_delete = models . CASCADE ,
31
30
related_name = "rules" ,
31
+ verbose_name = _ ("Access List" ),
32
32
)
33
33
index = models .PositiveIntegerField ()
34
34
remark = models .CharField (
35
+ verbose_name = _ ("Remark" ),
35
36
max_length = 500 ,
36
37
blank = True ,
37
38
)
38
39
description = models .CharField (
40
+ verbose_name = _ ("Description" ),
39
41
max_length = 500 ,
40
42
blank = True ,
41
43
)
42
44
action = models .CharField (
43
- choices = ACLRuleActionChoices ,
45
+ verbose_name = _ ( "Action" ) ,
44
46
max_length = 30 ,
47
+ choices = ACLRuleActionChoices ,
45
48
)
46
49
source_prefix = models .ForeignKey (
47
- blank = True ,
48
- null = True ,
50
+ to = "ipam.prefix" ,
49
51
on_delete = models .PROTECT ,
50
52
related_name = "+" ,
51
- to = "ipam.Prefix" ,
52
- verbose_name = "Source Prefix" ,
53
+ verbose_name = _ ("Source Prefix" ),
54
+ blank = True ,
55
+ null = True ,
53
56
)
54
57
55
58
clone_fields = ("access_list" , "action" , "source_prefix" )
56
-
57
- def __str__ (self ):
58
- return f"{ self .access_list } : Rule { self .index } "
59
-
60
- def get_action_color (self ):
61
- return ACLRuleActionChoices .colors .get (self .action )
62
-
63
- @classmethod
64
- def get_prerequisite_models (cls ):
65
- return [apps .get_model ("ipam.Prefix" ), AccessList ]
59
+ prerequisite_models = ("netbox_acls.AccessList" ,)
66
60
67
61
class Meta :
68
62
"""
@@ -76,31 +70,36 @@ class Meta:
76
70
ordering = ["access_list" , "index" ]
77
71
unique_together = ["access_list" , "index" ]
78
72
73
+ def __str__ (self ):
74
+ return f"{ self .access_list } : Rule { self .index } "
75
+
76
+ def get_absolute_url (self ):
77
+ """
78
+ The method is a Django convention; although not strictly required,
79
+ it conveniently returns the absolute URL for any particular object.
80
+ """
81
+ return reverse (
82
+ f"plugins:{ self ._meta .app_label } :{ self ._meta .model_name } " ,
83
+ args = [self .pk ],
84
+ )
85
+
86
+ def get_action_color (self ):
87
+ return ACLRuleActionChoices .colors .get (self .action )
88
+
79
89
80
90
class ACLStandardRule (ACLRule ):
81
91
"""
82
92
Inherits ACLRule.
83
93
"""
84
94
85
95
access_list = models .ForeignKey (
86
- on_delete = models .CASCADE ,
87
96
to = AccessList ,
88
- verbose_name = "Standard Access List" ,
89
- limit_choices_to = {"type" : ACLTypeChoices .TYPE_STANDARD },
97
+ on_delete = models .CASCADE ,
90
98
related_name = "aclstandardrules" ,
99
+ limit_choices_to = {"type" : ACLTypeChoices .TYPE_STANDARD },
100
+ verbose_name = _ ("Standard Access List" ),
91
101
)
92
102
93
- def get_absolute_url (self ):
94
- """
95
- The method is a Django convention; although not strictly required,
96
- it conveniently returns the absolute URL for any particular object.
97
- """
98
- return reverse ("plugins:netbox_acls:aclstandardrule" , args = [self .pk ])
99
-
100
- @classmethod
101
- def get_prerequisite_models (cls ):
102
- return [AccessList ]
103
-
104
103
class Meta (ACLRule .Meta ):
105
104
"""
106
105
Define the model properties adding to or overriding the inherited class:
@@ -109,62 +108,59 @@ class Meta(ACLRule.Meta):
109
108
- verbose name plural (for displaying in the GUI)
110
109
"""
111
110
112
- verbose_name = "ACL Standard Rule"
113
- verbose_name_plural = "ACL Standard Rules"
111
+ verbose_name = _ ( "ACL Standard Rule" )
112
+ verbose_name_plural = _ ( "ACL Standard Rules" )
114
113
115
114
116
115
class ACLExtendedRule (ACLRule ):
117
116
"""
118
117
Inherits ACLRule.
119
- Add ACLExtendedRule specific fields: source_ports, desintation_prefix , destination_ports, and protocol
118
+ Add ACLExtendedRule specific fields: source_ports, destination_prefix , destination_ports, and protocol
120
119
"""
121
120
122
121
access_list = models .ForeignKey (
123
- on_delete = models .CASCADE ,
124
122
to = AccessList ,
125
- verbose_name = "Extended Access List" ,
126
- limit_choices_to = {"type" : "extended" },
123
+ on_delete = models .CASCADE ,
127
124
related_name = "aclextendedrules" ,
125
+ limit_choices_to = {"type" : "extended" },
126
+ verbose_name = _ ("Extended Access List" ),
128
127
)
129
128
source_ports = ArrayField (
130
129
base_field = models .PositiveIntegerField (),
130
+ verbose_name = _ ("Source Ports" ),
131
131
blank = True ,
132
132
null = True ,
133
- verbose_name = "Soure Ports" ,
134
133
)
135
134
destination_prefix = models .ForeignKey (
136
- blank = True ,
137
- null = True ,
135
+ to = "ipam.prefix" ,
138
136
on_delete = models .PROTECT ,
139
137
related_name = "+" ,
140
- to = "ipam.Prefix" ,
141
- verbose_name = "Destination Prefix" ,
138
+ verbose_name = _ ("Destination Prefix" ),
139
+ blank = True ,
140
+ null = True ,
142
141
)
143
142
destination_ports = ArrayField (
144
143
base_field = models .PositiveIntegerField (),
144
+ verbose_name = _ ("Destination Ports" ),
145
145
blank = True ,
146
146
null = True ,
147
- verbose_name = "Destination Ports" ,
148
147
)
149
148
protocol = models .CharField (
150
- blank = True ,
151
- choices = ACLProtocolChoices ,
149
+ verbose_name = _ ("Protocol" ),
152
150
max_length = 30 ,
151
+ choices = ACLProtocolChoices ,
152
+ blank = True ,
153
153
)
154
154
155
- def get_absolute_url (self ):
156
- """
157
- The method is a Django convention; although not strictly required,
158
- it conveniently returns the absolute URL for any particular object.
159
- """
160
- return reverse ("plugins:netbox_acls:aclextendedrule" , args = [self .pk ])
161
-
162
- def get_protocol_color (self ):
163
- return ACLProtocolChoices .colors .get (self .protocol )
164
-
165
- @classmethod
166
- def get_prerequisite_models (cls ):
167
- return [apps .get_model ("ipam.Prefix" ), AccessList ]
155
+ clone_fields = (
156
+ "access_list" ,
157
+ "action" ,
158
+ "source_prefix" ,
159
+ "source_ports" ,
160
+ "destination_prefix" ,
161
+ "destination_ports" ,
162
+ "protocol" ,
163
+ )
168
164
169
165
class Meta (ACLRule .Meta ):
170
166
"""
@@ -174,5 +170,8 @@ class Meta(ACLRule.Meta):
174
170
- verbose name plural (for displaying in the GUI)
175
171
"""
176
172
177
- verbose_name = "ACL Extended Rule"
178
- verbose_name_plural = "ACL Extended Rules"
173
+ verbose_name = _ ("ACL Extended Rule" )
174
+ verbose_name_plural = _ ("ACL Extended Rules" )
175
+
176
+ def get_protocol_color (self ):
177
+ return ACLProtocolChoices .colors .get (self .protocol )
0 commit comments