Fixes 18555: Fix model URL generator for plugins (#18607)

* Fix model URL generator for plugins

* Fix reverse accessor warning

* Revert "Fix reverse accessor warning"

This reverts commit f07642bb99.

* Add URL test case for regular models

* Split dummy models

Instead of using a single model for testing, one is used for testing the
plugin API and a dedicated one is used for testing the NetBox plugin
model features.

* Fix filterset test case error

* Rename test module

---------

Co-authored-by: Jeremy Stretch <jstretch@netboxlabs.com>
This commit is contained in:
Alexander Haase 2025-02-18 15:11:32 +01:00 committed by GitHub
parent 11514bfb21
commit 6c6cb321bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 82 additions and 2 deletions

View File

@ -1118,6 +1118,7 @@ class TagTestCase(TestCase, ChangeLoggedFilterSetTests):
'devicerole',
'devicetype',
'dummymodel', # From dummy_plugin
'dummynetboxmodel', # From dummy_plugin
'eventrule',
'fhrpgroup',
'frontport',

View File

@ -10,6 +10,7 @@ from mptt.models import MPTTModel, TreeForeignKey
from netbox.models.features import *
from utilities.mptt import TreeManager
from utilities.querysets import RestrictedQuerySet
from utilities.views import get_viewname
__all__ = (
@ -42,7 +43,7 @@ class NetBoxFeatureSet(
return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/'
def get_absolute_url(self):
return reverse(f'{self._meta.app_label}:{self._meta.model_name}', args=[self.pk])
return reverse(get_viewname(self), args=[self.pk])
#

View File

@ -0,0 +1,30 @@
import taggit.managers
import utilities.json
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dummy_plugin', '0001_initial'),
('extras', '0122_charfield_null_choices'),
]
operations = [
migrations.CreateModel(
name='DummyNetBoxModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False)),
('created', models.DateTimeField(auto_now_add=True, null=True)),
('last_updated', models.DateTimeField(auto_now=True, null=True)),
(
'custom_field_data',
models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
],
options={
'abstract': False,
},
),
]

View File

@ -1,5 +1,7 @@
from django.db import models
from netbox.models import NetBoxModel
class DummyModel(models.Model):
name = models.CharField(
@ -11,3 +13,7 @@ class DummyModel(models.Model):
class Meta:
ordering = ['name']
class DummyNetBoxModel(NetBoxModel):
pass

View File

@ -6,4 +6,6 @@ from . import views
urlpatterns = (
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
path('netboxmodel/<int:pk>/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel'),
)

View File

@ -5,12 +5,17 @@ from django.http import HttpResponse
from django.views.generic import View
from dcim.models import Site
from netbox.views import generic
from utilities.views import register_model_view
from .models import DummyModel
from .models import DummyModel, DummyNetBoxModel
# Trigger registration of custom column
from .tables import mycol # noqa: F401
#
# DummyModel
#
class DummyModelsView(View):
def get(self, request):
@ -32,6 +37,18 @@ class DummyModelAddView(View):
return HttpResponse("Instance created")
#
# DummyNetBoxModel
#
class DummyNetBoxModelView(generic.ObjectView):
queryset = DummyNetBoxModel.objects.all()
#
# API
#
@register_model_view(Site, 'extra', path='other-stuff')
class ExtraCoreModelView(View):

View File

@ -0,0 +1,23 @@
from unittest import skipIf
from django.conf import settings
from django.test import TestCase
from core.models import ObjectChange
from netbox.tests.dummy_plugin.models import DummyNetBoxModel
class ModelTest(TestCase):
def test_get_absolute_url(self):
m = ObjectChange()
m.pk = 123
self.assertEqual(m.get_absolute_url(), f'/core/changelog/{m.pk}/')
@skipIf('netbox.tests.dummy_plugin' not in settings.PLUGINS, "dummy_plugin not in settings.PLUGINS")
def test_get_absolute_url_plugin(self):
m = DummyNetBoxModel()
m.pk = 123
self.assertEqual(m.get_absolute_url(), f'/plugins/dummy-plugin/netboxmodel/{m.pk}/')