Skip to content

Commit 80c9110

Browse files
committed
Change all uses of custom_object_type.name to use slug in urls or verbose_name/verbose_name_plural in templates
1 parent 7965692 commit 80c9110

File tree

14 files changed

+53
-41
lines changed

14 files changed

+53
-41
lines changed

netbox_custom_objects/api/serializers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def get_url(self, obj):
192192
lookup_value = getattr(obj, "pk")
193193
kwargs = {
194194
"pk": lookup_value,
195-
"custom_object_type": obj.custom_object_type.name.lower(),
195+
"custom_object_type": obj.custom_object_type.slug,
196196
}
197197
request = self.context["request"]
198198
format = self.context.get("format")
@@ -230,7 +230,7 @@ def get_url(self, obj):
230230
lookup_value = getattr(obj, "pk")
231231
kwargs = {
232232
"pk": lookup_value,
233-
"custom_object_type": obj.custom_object_type.name.lower(),
233+
"custom_object_type": obj.custom_object_type.slug,
234234
}
235235
request = self.context["request"]
236236
format = self.context.get("format")

netbox_custom_objects/api/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get(self, request, *args, **kwargs):
5151
# Extra logic to populate roots for custom object type lists
5252
for custom_object_type in CustomObjectType.objects.all():
5353
local_kwargs = deepcopy(kwargs)
54-
cot_name = custom_object_type.name.lower()
54+
cot_name = custom_object_type.slug
5555
url_name = 'customobject-list'
5656
local_kwargs['custom_object_type'] = cot_name
5757
if namespace:

netbox_custom_objects/api/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CustomObjectViewSet(ModelViewSet):
2323

2424
def get_view_name(self):
2525
if self.model:
26-
return self.model.custom_object_type.name
26+
return self.model.custom_object_type.verbose_name or self.model.custom_object_type.name
2727
return 'Custom Object'
2828

2929
def get_serializer_class(self):
@@ -32,7 +32,7 @@ def get_serializer_class(self):
3232
def get_queryset(self):
3333
try:
3434
custom_object_type = CustomObjectType.objects.get(
35-
name__iexact=self.kwargs["custom_object_type"]
35+
slug=self.kwargs["custom_object_type"]
3636
)
3737
except CustomObjectType.DoesNotExist:
3838
raise Http404

netbox_custom_objects/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def get_bound_field(self, form, field_name):
5959
widget.attrs["data-url"] = reverse(
6060
viewname,
6161
kwargs={
62-
"custom_object_type": form.instance.custom_object_type.name.lower()
62+
"custom_object_type": form.instance.custom_object_type.slug
6363
},
6464
)
6565

@@ -70,7 +70,7 @@ def get_bound_field(self, form, field_name):
7070
"url": reverse(
7171
viewname,
7272
kwargs={
73-
"custom_object_type": form.instance.custom_object_type.name.lower()
73+
"custom_object_type": form.instance.custom_object_type.slug
7474
},
7575
),
7676
"params": {},

netbox_custom_objects/models.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ def get_absolute_url(self):
134134
"plugins:netbox_custom_objects:customobject",
135135
kwargs={
136136
"pk": self.pk,
137-
"custom_object_type": self.custom_object_type.name.lower(),
137+
"custom_object_type": self.custom_object_type.slug,
138138
},
139139
)
140140

141141
def get_list_url(self):
142142
return reverse(
143143
"plugins:netbox_custom_objects:customobject_list",
144-
kwargs={"custom_object_type": self.custom_object_type.name.lower()},
144+
kwargs={"custom_object_type": self.custom_object_type.slug},
145145
)
146146

147147
@classmethod
@@ -154,7 +154,7 @@ def _get_viewname(cls, action=None, rest_api=False):
154154
def _get_action_url(cls, action=None, rest_api=False, kwargs=None):
155155
if kwargs is None:
156156
kwargs = {}
157-
kwargs["custom_object_type"] = cls.custom_object_type.name.lower()
157+
kwargs["custom_object_type"] = cls.custom_object_type.slug
158158
return reverse(cls._get_viewname(action, rest_api), kwargs=kwargs)
159159

160160

@@ -188,7 +188,11 @@ class Meta:
188188
]
189189

190190
def __str__(self):
191-
return self.name
191+
return self.display_name
192+
193+
@property
194+
def display_name(self):
195+
return self.verbose_name or self.name
192196

193197
@classmethod
194198
def clear_model_cache(cls, custom_object_type_id=None):
@@ -272,7 +276,7 @@ def get_absolute_url(self):
272276
def get_list_url(self):
273277
return reverse(
274278
"plugins:netbox_custom_objects:customobject_list",
275-
kwargs={"custom_object_type": self.name.lower()},
279+
kwargs={"custom_object_type": self.slug},
276280
)
277281

278282
@classmethod

netbox_custom_objects/navigation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __iter__(self):
3535
add_button.url = reverse_lazy(
3636
f"plugins:{APP_LABEL}:customobject_add",
3737
kwargs={
38-
"custom_object_type": custom_object_type.name.lower()
38+
"custom_object_type": custom_object_type.slug
3939
},
4040
)
4141
menu_item = PluginMenuItem(
@@ -45,7 +45,7 @@ def __iter__(self):
4545
)
4646
menu_item.url = reverse_lazy(
4747
f"plugins:{APP_LABEL}:customobject_list",
48-
kwargs={"custom_object_type": custom_object_type.name.lower()},
48+
kwargs={"custom_object_type": custom_object_type.slug},
4949
)
5050
yield menu_item
5151

netbox_custom_objects/tables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def render(self, record, table, **kwargs):
112112
get_viewname(model, action),
113113
kwargs={
114114
"pk": record.pk,
115-
"custom_object_type": record.custom_object_type.name.lower(),
115+
"custom_object_type": record.custom_object_type.slug,
116116
},
117117
)
118118

netbox_custom_objects/templates/netbox_custom_objects/customobject.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<tr>
9191
<th scope="row">{% trans "Type" %}</th>
9292
<td>
93-
{{ object.custom_object_type|linkify:"name" }}
93+
{{ object.custom_object_type|linkify:"display_name" }}
9494
</td>
9595
</tr>
9696
<tr>

netbox_custom_objects/templates/netbox_custom_objects/customobject_edit.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
{% block title %}
66
{% if object.pk %}
7-
{% trans "Editing" %} {{ object.custom_object_type.get_verbose_name.lower }} {{ object }}
7+
{% trans "Editing" %} {{ object.custom_object_type.display_name.lower }} {{ object }}
88
{% else %}
9-
{% blocktrans trimmed with custom_object_type=object.custom_object_type.name %}
9+
{% blocktrans trimmed with custom_object_type=object.custom_object_type.display_name %}
1010
Add a new {{ custom_object_type }}
1111
{% endblocktrans %}
1212
{% endif %}

netbox_custom_objects/templates/netbox_custom_objects/customobjecttype.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ <h5 class="card-header">{% trans "Custom Object Type" %}</h5>
2525
<th scope="row">{% trans "Name" %}</th>
2626
<td>{{ object.name }}</td>
2727
</tr>
28+
<tr>
29+
<th scope="row">{% trans "Readable name" %}</th>
30+
<td>{{ object.verbose_name }}</td>
31+
</tr>
2832
<tr>
2933
<th scope="row">{% trans "Description" %}</th>
3034
<td>{{ object.description|placeholder }}</td>

0 commit comments

Comments
 (0)