Skip to content

Commit 976542d

Browse files
committed
add docstrings everywhere
1 parent 2abc675 commit 976542d

File tree

8 files changed

+207
-34
lines changed

8 files changed

+207
-34
lines changed

netbox_acls/api/serializers.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@
2929
"ACLExtendedRuleSerializer",
3030
]
3131

32+
# TODO: Check Constants across codebase for consistency.
3233
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
33-
error_message_no_remark = "Action is set to remark, you MUST add a remark."
34+
ERROR_MESSAGE_NO_REMARK = "Action is set to remark, you MUST add a remark."
3435
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
35-
error_message_action_remark_source_prefix_set = (
36+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET = (
3637
"Action is set to remark, Source Prefix CANNOT be set."
3738
)
3839
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
39-
error_message_remark_without_action_remark = (
40+
ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK = (
4041
"CANNOT set remark unless action is set to remark."
4142
)
4243
# Sets a standard error message for ACL rules no associated to an ACL of the same type.
43-
error_message_acl_type = "Provided parent Access List is not of right type."
44+
ERROR_MESSAGE_ACL_TYPE = "Provided parent Access List is not of right type."
4445

4546

4647
class AccessListSerializer(NetBoxModelSerializer):
@@ -83,6 +84,9 @@ class Meta:
8384

8485
@swagger_serializer_method(serializer_or_field=serializers.DictField)
8586
def get_assigned_object(self, obj):
87+
"""
88+
Returns the assigned object for the Access List.
89+
"""
8690
serializer = get_serializer_for_model(
8791
obj.assigned_object,
8892
prefix=NESTED_SERIALIZER_PREFIX,
@@ -166,6 +170,9 @@ class Meta:
166170

167171
@swagger_serializer_method(serializer_or_field=serializers.DictField)
168172
def get_assigned_object(self, obj):
173+
"""
174+
Returns the assigned object for the ACLInterfaceAssignment.
175+
"""
169176
serializer = get_serializer_for_model(
170177
obj.assigned_object,
171178
prefix=NESTED_SERIALIZER_PREFIX,
@@ -272,11 +279,11 @@ def validate(self, data):
272279

273280
# Check if action set to remark, but no remark set.
274281
if data.get("action") == "remark" and data.get("remark") is None:
275-
error_message["remark"] = [error_message_no_remark]
282+
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
276283
# Check if action set to remark, but source_prefix set.
277284
if data.get("source_prefix"):
278285
error_message["source_prefix"] = [
279-
error_message_action_remark_source_prefix_set,
286+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
280287
]
281288

282289
if error_message:
@@ -346,11 +353,11 @@ def validate(self, data):
346353

347354
# Check if action set to remark, but no remark set.
348355
if data.get("action") == "remark" and data.get("remark") is None:
349-
error_message["remark"] = [error_message_no_remark]
356+
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
350357
# Check if action set to remark, but source_prefix set.
351358
if data.get("source_prefix"):
352359
error_message["source_prefix"] = [
353-
error_message_action_remark_source_prefix_set,
360+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
354361
]
355362
# Check if action set to remark, but source_ports set.
356363
if data.get("source_ports"):

netbox_acls/forms/filtersets.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class BaseACLFilterForm(NetBoxModelFilterSetForm):
4444
"""
4545

4646
class Meta:
47+
"""
48+
Sets the parent class as an abstract class to be inherited by other classes.
49+
"""
50+
4751
abstract = True
4852

4953
model = AccessList
@@ -163,6 +167,10 @@ class BaseACLRuleFilterForm(NetBoxModelFilterSetForm):
163167
"""
164168

165169
class Meta:
170+
"""
171+
Sets the parent class as an abstract class to be inherited by other classes.
172+
"""
173+
166174
abstract = True
167175

168176
model = BaseACLRule

netbox_acls/forms/models.py

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@
3737
"<b>*Note:</b> CANNOT be set if action is set to remark.",
3838
)
3939
# Sets a standard help_text value to be used by the various classes for acl action
40-
help_text_acl_action = "Action the rule will take (remark, deny, or allow)."
40+
HELP_TEXT_ACL_ACTION = "Action the rule will take (remark, deny, or allow)."
4141
# Sets a standard help_text value to be used by the various classes for acl index
42-
help_text_acl_rule_index = (
42+
HELP_TEXT_ACL_RULE_INDEX = (
4343
"Determines the order of the rule in the ACL processing. AKA Sequence Number."
4444
)
4545

4646
# Sets a standard error message for ACL rules with an action of remark, but no remark set.
47-
error_message_no_remark = "Action is set to remark, you MUST add a remark."
47+
ERROR_MESSAGE_NO_REMARK = "Action is set to remark, you MUST add a remark."
4848
# Sets a standard error message for ACL rules with an action of remark, but no source_prefix is set.
49-
error_message_action_remark_source_prefix_set = (
49+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET = (
5050
"Action is set to remark, Source Prefix CANNOT be set."
5151
)
5252
# Sets a standard error message for ACL rules with an action not set to remark, but no remark is set.
53-
error_message_remark_without_action_remark = (
53+
ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK = (
5454
"CANNOT set remark unless action is set to remark."
5555
)
5656

@@ -133,6 +133,10 @@ class AccessListForm(NetBoxModelForm):
133133
comments = CommentField()
134134

135135
class Meta:
136+
"""
137+
Defines the Model and fields to be used by the form.
138+
"""
139+
136140
model = AccessList
137141
fields = (
138142
"region",
@@ -156,16 +160,19 @@ class Meta:
156160
}
157161

158162
def __init__(self, *args, **kwargs):
163+
"""
164+
Initializes the form
165+
"""
159166

160167
# Initialize helper selectors
161168
instance = kwargs.get("instance")
162169
initial = kwargs.get("initial", {}).copy()
163170
if instance:
164-
if type(instance.assigned_object) is Device:
171+
if isinstance(instance.assigned_object, Device):
165172
initial["device"] = instance.assigned_object
166-
elif type(instance.assigned_object) is VirtualChassis:
173+
elif isinstance(instance.assigned_object, VirtualChassis):
167174
initial["virtual_chassis"] = instance.assigned_object
168-
elif type(instance.assigned_object) is VirtualMachine:
175+
elif isinstance(instance.assigned_object, VirtualMachine):
169176
initial["virtual_machine"] = instance.assigned_object
170177
kwargs["initial"] = initial
171178

@@ -231,12 +238,16 @@ def clean(self):
231238
host_type: [error_same_acl_name],
232239
"name": [error_same_acl_name],
233240
}
241+
# Check if Access List has no existing rules before change the Access List's type.
234242
if self.instance.pk:
235-
# Check if Access List has no existing rules before change the Access List's type.
236243
if (
237244
acl_type == ACLTypeChoices.TYPE_EXTENDED
238245
and self.instance.aclstandardrules.exists()
239-
) or (
246+
):
247+
error_message["type"] = [
248+
"This ACL has ACL rules associated, CANNOT change ACL type.",
249+
]
250+
elif (
240251
acl_type == ACLTypeChoices.TYPE_STANDARD
241252
and self.instance.aclextendedrules.exists()
242253
):
@@ -250,7 +261,9 @@ def clean(self):
250261
return cleaned_data
251262

252263
def save(self, *args, **kwargs):
253-
# Set assigned object
264+
"""
265+
Set assigned object
266+
"""
254267
self.instance.assigned_object = (
255268
self.cleaned_data.get("device")
256269
or self.cleaned_data.get("virtual_chassis")
@@ -315,22 +328,28 @@ class ACLInterfaceAssignmentForm(NetBoxModelForm):
315328
comments = CommentField()
316329

317330
def __init__(self, *args, **kwargs):
331+
"""
332+
Initialize helper selectors
333+
"""
318334

319-
# Initialize helper selectors
320335
instance = kwargs.get("instance")
321336
initial = kwargs.get("initial", {}).copy()
322337
if instance:
323-
if type(instance.assigned_object) is Interface:
338+
if isinstance(instance.assigned_object, Interface):
324339
initial["interface"] = instance.assigned_object
325340
initial["device"] = "device"
326-
elif type(instance.assigned_object) is VMInterface:
341+
elif isinstance(instance.assigned_object, VMInterface):
327342
initial["vminterface"] = instance.assigned_object
328343
initial["virtual_machine"] = "virtual_machine"
329344
kwargs["initial"] = initial
330345

331346
super().__init__(*args, **kwargs)
332347

333348
class Meta:
349+
"""
350+
Defines the Model and fields to be used by the form.
351+
"""
352+
334353
model = ACLInterfaceAssignment
335354
fields = (
336355
"access_list",
@@ -441,7 +460,9 @@ def clean(self):
441460
return cleaned_data
442461

443462
def save(self, *args, **kwargs):
444-
# Set assigned object
463+
"""
464+
Set assigned object
465+
"""
445466
self.instance.assigned_object = self.cleaned_data.get(
446467
"interface",
447468
) or self.cleaned_data.get("vminterface")
@@ -478,6 +499,10 @@ class ACLStandardRuleForm(NetBoxModelForm):
478499
)
479500

480501
class Meta:
502+
"""
503+
Defines the Model and fields to be used by the form.
504+
"""
505+
481506
model = ACLStandardRule
482507
fields = (
483508
"access_list",
@@ -489,8 +514,8 @@ class Meta:
489514
"description",
490515
)
491516
help_texts = {
492-
"index": help_text_acl_rule_index,
493-
"action": help_text_acl_action,
517+
"index": HELP_TEXT_ACL_RULE_INDEX,
518+
"action": HELP_TEXT_ACL_ACTION,
494519
"remark": mark_safe(
495520
"<b>*Note:</b> CANNOT be set if source prefix OR action is set.",
496521
),
@@ -511,15 +536,15 @@ def clean(self):
511536
if cleaned_data.get("action") == "remark":
512537
# Check if action set to remark, but no remark set.
513538
if not cleaned_data.get("remark"):
514-
error_message["remark"] = [error_message_no_remark]
539+
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
515540
# Check if action set to remark, but source_prefix set.
516541
if cleaned_data.get("source_prefix"):
517542
error_message["source_prefix"] = [
518-
error_message_action_remark_source_prefix_set,
543+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
519544
]
520545
# Check remark set, but action not set to remark.
521546
elif cleaned_data.get("remark"):
522-
error_message["remark"] = [error_message_remark_without_action_remark]
547+
error_message["remark"] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK]
523548

524549
if error_message:
525550
raise forms.ValidationError(error_message)
@@ -574,6 +599,10 @@ class ACLExtendedRuleForm(NetBoxModelForm):
574599
)
575600

576601
class Meta:
602+
"""
603+
Defines the Model and fields to be used by the form.
604+
"""
605+
577606
model = ACLExtendedRule
578607
fields = (
579608
"access_list",
@@ -589,9 +618,9 @@ class Meta:
589618
"description",
590619
)
591620
help_texts = {
592-
"action": help_text_acl_action,
621+
"action": HELP_TEXT_ACL_ACTION,
593622
"destination_ports": help_text_acl_rule_logic,
594-
"index": help_text_acl_rule_index,
623+
"index": HELP_TEXT_ACL_RULE_INDEX,
595624
"protocol": help_text_acl_rule_logic,
596625
"remark": mark_safe(
597626
"<b>*Note:</b> CANNOT be set if action is not set to remark.",
@@ -619,11 +648,11 @@ def clean(self):
619648
if cleaned_data.get("action") == "remark":
620649
# Check if action set to remark, but no remark set.
621650
if not cleaned_data.get("remark"):
622-
error_message["remark"] = [error_message_no_remark]
651+
error_message["remark"] = [ERROR_MESSAGE_NO_REMARK]
623652
# Check if action set to remark, but source_prefix set.
624653
if cleaned_data.get("source_prefix"):
625654
error_message["source_prefix"] = [
626-
error_message_action_remark_source_prefix_set,
655+
ERROR_MESSAGE_ACTION_REMARK_SOURCE_PREFIX_SET,
627656
]
628657
# Check if action set to remark, but source_ports set.
629658
if cleaned_data.get("source_ports"):
@@ -647,7 +676,7 @@ def clean(self):
647676
]
648677
# Check if action not set to remark, but remark set.
649678
elif cleaned_data.get("remark"):
650-
error_message["remark"] = [error_message_remark_without_action_remark]
679+
error_message["remark"] = [ERROR_MESSAGE_REMARK_WITHOUT_ACTION_REMARK]
651680

652681
if error_message:
653682
raise forms.ValidationError(error_message)

netbox_acls/models/access_list_rules.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,22 @@ class BaseACLRule(NetBoxModel):
5555
clone_fields = ("access_list", "action", "source_prefix")
5656

5757
def __str__(self):
58+
"""
59+
Return a string representation of the model.
60+
"""
5861
return f"{self.access_list}: Rule {self.index}"
5962

6063
def get_action_color(self):
64+
"""
65+
Return the color for the action.
66+
"""
6167
return ACLRuleActionChoices.colors.get(self.action)
6268

6369
@classmethod
6470
def get_prerequisite_models(cls):
71+
"""
72+
Return a list of prerequisite models for this model.
73+
"""
6574
return [apps.get_model("ipam.Prefix"), AccessList]
6675

6776
class Meta:
@@ -99,6 +108,9 @@ def get_absolute_url(self):
99108

100109
@classmethod
101110
def get_prerequisite_models(cls):
111+
"""
112+
Return a list of prerequisite models for this model.
113+
"""
102114
return [AccessList]
103115

104116
class Meta(BaseACLRule.Meta):
@@ -160,10 +172,16 @@ def get_absolute_url(self):
160172
return reverse("plugins:netbox_acls:aclextendedrule", args=[self.pk])
161173

162174
def get_protocol_color(self):
175+
"""
176+
Return the color for the protocol.
177+
"""
163178
return ACLProtocolChoices.colors.get(self.protocol)
164179

165180
@classmethod
166181
def get_prerequisite_models(cls):
182+
"""
183+
Return a list of prerequisite models for this model.
184+
xs"""
167185
return [apps.get_model("ipam.Prefix"), AccessList]
168186

169187
class Meta(BaseACLRule.Meta):

0 commit comments

Comments
 (0)