mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 19:08:38 -06:00
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.
This commit is contained in:
parent
8b0af50019
commit
2a6702e88b
@ -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,34 +0,0 @@
|
|||||||
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.AddField(
|
|
||||||
model_name='dummymodel',
|
|
||||||
name='created',
|
|
||||||
field=models.DateTimeField(auto_now_add=True, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='dummymodel',
|
|
||||||
name='custom_field_data',
|
|
||||||
field=models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='dummymodel',
|
|
||||||
name='last_updated',
|
|
||||||
field=models.DateTimeField(auto_now=True, null=True),
|
|
||||||
),
|
|
||||||
migrations.AddField(
|
|
||||||
model_name='dummymodel',
|
|
||||||
name='tags',
|
|
||||||
field=taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag'),
|
|
||||||
),
|
|
||||||
]
|
|
@ -3,7 +3,7 @@ from django.db import models
|
|||||||
from netbox.models import NetBoxModel
|
from netbox.models import NetBoxModel
|
||||||
|
|
||||||
|
|
||||||
class DummyModel(NetBoxModel):
|
class DummyModel(models.Model):
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
max_length=20
|
max_length=20
|
||||||
)
|
)
|
||||||
@ -13,3 +13,7 @@ class DummyModel(NetBoxModel):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ['name']
|
ordering = ['name']
|
||||||
|
|
||||||
|
|
||||||
|
class DummyNetBoxModel(NetBoxModel):
|
||||||
|
pass
|
||||||
|
@ -6,5 +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('models/<int:pk>/', views.DummyModelView.as_view(), name='dummymodel'),
|
|
||||||
|
path('netboxmodel/<int:pk>/', views.DummyNetBoxModelView.as_view(), name='dummynetboxmodel'),
|
||||||
)
|
)
|
||||||
|
@ -7,11 +7,15 @@ from django.views.generic import View
|
|||||||
from dcim.models import Site
|
from dcim.models import Site
|
||||||
from netbox.views import generic
|
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):
|
||||||
@ -19,10 +23,6 @@ class DummyModelsView(View):
|
|||||||
return HttpResponse(f"Instances: {instance_count}")
|
return HttpResponse(f"Instances: {instance_count}")
|
||||||
|
|
||||||
|
|
||||||
class DummyModelView(generic.ObjectView):
|
|
||||||
queryset = DummyModel.objects.all()
|
|
||||||
|
|
||||||
|
|
||||||
class DummyModelAddView(View):
|
class DummyModelAddView(View):
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
@ -37,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):
|
||||||
|
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
|
from unittest import skipIf
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from core.models import ObjectChange
|
from core.models import ObjectChange
|
||||||
from netbox.tests.dummy_plugin.models import DummyModel
|
from netbox.tests.dummy_plugin.models import DummyNetBoxModel
|
||||||
|
|
||||||
|
|
||||||
class ModelTest(TestCase):
|
class ModelTest(TestCase):
|
||||||
@ -12,9 +15,9 @@ class ModelTest(TestCase):
|
|||||||
|
|
||||||
self.assertEqual(m.get_absolute_url(), f'/core/changelog/{m.pk}/')
|
self.assertEqual(m.get_absolute_url(), f'/core/changelog/{m.pk}/')
|
||||||
|
|
||||||
def test_absolute_url(self):
|
@skipIf('netbox.tests.dummy_plugin' not in settings.PLUGINS, "dummy_plugin not in settings.PLUGINS")
|
||||||
m = DummyModel(name='Foo')
|
def test_get_absolute_url_plugin(self):
|
||||||
m.full_clean()
|
m = DummyNetBoxModel()
|
||||||
m.save()
|
m.pk = 123
|
||||||
|
|
||||||
self.assertEqual(m.get_absolute_url(), f"/plugins/dummy-plugin/models/{m.pk}/")
|
self.assertEqual(m.get_absolute_url(), f'/plugins/dummy-plugin/netboxmodel/{m.pk}/')
|
||||||
|
Loading…
Reference in New Issue
Block a user