Skip to content

Commit 9462874

Browse files
authored
Merge pull request #248 from pheus/housekeeping/247-use-jsonfield-for-assigned-object-serializer
feat(api): Use JSONField for assigned_object serializer
2 parents a9e0d5f + 48a220e commit 9462874

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

netbox_acls/api/serializers.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ class Meta:
7373
"last_updated",
7474
"rule_count",
7575
)
76-
brief_fields = ("id", "url", "name", "display")
76+
brief_fields = ("id", "url", "display", "name")
7777

78-
@extend_schema_field(serializers.DictField())
78+
@extend_schema_field(serializers.JSONField(allow_null=True))
7979
def get_assigned_object(self, obj):
80-
serializer = get_serializer_for_model(
81-
obj.assigned_object,
82-
)
80+
if obj.assigned_object is None:
81+
return None
82+
serializer = get_serializer_for_model(obj.assigned_object)
8383
context = {"request": self.context["request"]}
8484
return serializer(obj.assigned_object, nested=True, context=context).data
8585

@@ -126,6 +126,7 @@ class Meta:
126126
fields = (
127127
"id",
128128
"url",
129+
"display",
129130
"access_list",
130131
"direction",
131132
"assigned_object_type",
@@ -137,10 +138,12 @@ class Meta:
137138
"created",
138139
"last_updated",
139140
)
140-
brief_fields = ("id", "url", "access_list")
141+
brief_fields = ("id", "url", "display", "access_list")
141142

142-
@extend_schema_field(serializers.DictField())
143+
@extend_schema_field(serializers.JSONField(allow_null=True))
143144
def get_assigned_object(self, obj):
145+
if obj.assigned_object is None:
146+
return None
144147
serializer = get_serializer_for_model(obj.assigned_object)
145148
context = {"request": self.context["request"]}
146149
return serializer(obj.assigned_object, nested=True, context=context).data
@@ -203,15 +206,15 @@ class Meta:
203206
"access_list",
204207
"index",
205208
"action",
206-
"tags",
207-
"description",
208209
"remark",
210+
"source_prefix",
211+
"description",
212+
"tags",
209213
"created",
210214
"custom_fields",
211215
"last_updated",
212-
"source_prefix",
213216
)
214-
brief_fields = ("id", "url", "display")
217+
brief_fields = ("id", "url", "display", "access_list", "index")
215218

216219
def validate(self, data):
217220
"""
@@ -274,19 +277,19 @@ class Meta:
274277
"access_list",
275278
"index",
276279
"action",
277-
"tags",
278-
"description",
279-
"created",
280-
"custom_fields",
281-
"last_updated",
280+
"remark",
281+
"protocol",
282282
"source_prefix",
283283
"source_ports",
284284
"destination_prefix",
285285
"destination_ports",
286-
"protocol",
287-
"remark",
286+
"description",
287+
"tags",
288+
"created",
289+
"custom_fields",
290+
"last_updated",
288291
)
289-
brief_fields = ("id", "url", "display")
292+
brief_fields = ("id", "url", "display", "access_list", "index")
290293

291294
def validate(self, data):
292295
"""

netbox_acls/models/access_lists.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
class AccessList(NetBoxModel):
3030
"""
31-
Model defintion for Access Lists.
31+
Model definition for Access Lists.
3232
"""
3333

3434
name = models.CharField(
@@ -89,10 +89,9 @@ def get_type_color(self):
8989

9090
class ACLInterfaceAssignment(NetBoxModel):
9191
"""
92-
Model defintion for Access Lists associations with other Host interfaces:
92+
Model definition for Access Lists associations with other Host interfaces:
9393
- VM interfaces
9494
- device interface
95-
- tbd on more
9695
"""
9796

9897
access_list = models.ForeignKey(
@@ -119,6 +118,7 @@ class ACLInterfaceAssignment(NetBoxModel):
119118
)
120119

121120
clone_fields = ("access_list", "direction")
121+
prerequisite_models = ("netbox_acls.AccessList",)
122122

123123
class Meta:
124124
unique_together = [
@@ -136,6 +136,9 @@ class Meta:
136136
verbose_name = "ACL Interface Assignment"
137137
verbose_name_plural = "ACL Interface Assignments"
138138

139+
def __str__(self):
140+
return f"{self.access_list}: Interface {self.assigned_object}"
141+
139142
def get_absolute_url(self):
140143
"""
141144
The method is a Django convention; although not strictly required,
@@ -146,10 +149,6 @@ def get_absolute_url(self):
146149
args=[self.pk],
147150
)
148151

149-
@classmethod
150-
def get_prerequisite_models(cls):
151-
return [AccessList]
152-
153152
def get_direction_color(self):
154153
return ACLAssignmentDirectionChoices.colors.get(self.direction)
155154

0 commit comments

Comments
 (0)