mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
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:
parent
11514bfb21
commit
6c6cb321bf
@ -1118,6 +1118,7 @@ class TagTestCase(TestCase, ChangeLoggedFilterSetTests):
|
|||||||
'devicerole',
|
'devicerole',
|
||||||
'devicetype',
|
'devicetype',
|
||||||
'dummymodel', # From dummy_plugin
|
'dummymodel', # From dummy_plugin
|
||||||
|
'dummynetboxmodel', # From dummy_plugin
|
||||||
'eventrule',
|
'eventrule',
|
||||||
'fhrpgroup',
|
'fhrpgroup',
|
||||||
'frontport',
|
'frontport',
|
||||||
|
@ -10,6 +10,7 @@ from mptt.models import MPTTModel, TreeForeignKey
|
|||||||
from netbox.models.features import *
|
from netbox.models.features import *
|
||||||
from utilities.mptt import TreeManager
|
from utilities.mptt import TreeManager
|
||||||
from utilities.querysets import RestrictedQuerySet
|
from utilities.querysets import RestrictedQuerySet
|
||||||
|
from utilities.views import get_viewname
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -42,7 +43,7 @@ class NetBoxFeatureSet(
|
|||||||
return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/'
|
return f'{settings.STATIC_URL}docs/models/{self._meta.app_label}/{self._meta.model_name}/'
|
||||||
|
|
||||||
def get_absolute_url(self):
|
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])
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -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,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
@ -1,5 +1,7 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from netbox.models import NetBoxModel
|
||||||
|
|
||||||
|
|
||||||
class DummyModel(models.Model):
|
class DummyModel(models.Model):
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
@ -11,3 +13,7 @@ class DummyModel(models.Model):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['name']
|
ordering = ['name']
|
||||||
|
|
||||||
|
|
||||||
|
class DummyNetBoxModel(NetBoxModel):
|
||||||
|
pass
|
||||||
|
@ -6,4 +6,6 @@ from . import views
|
|||||||
urlpatterns = (
|
urlpatterns = (
|
||||||
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
|
path('models/', views.DummyModelsView.as_view(), name='dummy_model_list'),
|
||||||
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
|
path('models/add/', views.DummyModelAddView.as_view(), name='dummy_model_add'),
|
||||||
|
|
||||||
|
path('netboxmodel/<int:pk>/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel'),
|
||||||
)
|
)
|
||||||
|
@ -5,12 +5,17 @@ from django.http import HttpResponse
|
|||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
|
|
||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
|
from netbox.views import generic
|
||||||
from utilities.views import register_model_view
|
from utilities.views import register_model_view
|
||||||
from .models import DummyModel
|
from .models import DummyModel, DummyNetBoxModel
|
||||||
# Trigger registration of custom column
|
# Trigger registration of custom column
|
||||||
from .tables import mycol # noqa: F401
|
from .tables import mycol # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# DummyModel
|
||||||
|
#
|
||||||
|
|
||||||
class DummyModelsView(View):
|
class DummyModelsView(View):
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
@ -32,6 +37,18 @@ class DummyModelAddView(View):
|
|||||||
return HttpResponse("Instance created")
|
return HttpResponse("Instance created")
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# DummyNetBoxModel
|
||||||
|
#
|
||||||
|
|
||||||
|
class DummyNetBoxModelView(generic.ObjectView):
|
||||||
|
queryset = DummyNetBoxModel.objects.all()
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# API
|
||||||
|
#
|
||||||
|
|
||||||
@register_model_view(Site, 'extra', path='other-stuff')
|
@register_model_view(Site, 'extra', path='other-stuff')
|
||||||
class ExtraCoreModelView(View):
|
class ExtraCoreModelView(View):
|
||||||
|
|
||||||
|
23
netbox/netbox/tests/test_models.py
Normal file
23
netbox/netbox/tests/test_models.py
Normal 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}/')
|
Loading…
Reference in New Issue
Block a user