mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 04:22:01 -06:00
Update Tag.object_types to reference ObjectType
This commit is contained in:
parent
5f43eabab1
commit
570f64784f
@ -111,7 +111,7 @@ class TagType(ObjectType):
|
|||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Tag
|
model = models.Tag
|
||||||
exclude = ('extras_taggeditem_items',)
|
exclude = ('object_types', 'extras_taggeditem_items',)
|
||||||
filterset_class = filtersets.TagFilterSet
|
filterset_class = filtersets.TagFilterSet
|
||||||
|
|
||||||
|
|
||||||
|
17
netbox/extras/migrations/0112_tag_update_object_types.py
Normal file
17
netbox/extras/migrations/0112_tag_update_object_types.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('core', '0010_gfk_indexes'),
|
||||||
|
('extras', '0111_rename_content_types'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='tag',
|
||||||
|
name='object_types',
|
||||||
|
field=models.ManyToManyField(blank=True, related_name='+', to='core.objecttype'),
|
||||||
|
),
|
||||||
|
]
|
@ -34,7 +34,7 @@ class Tag(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel, TagBase):
|
|||||||
blank=True,
|
blank=True,
|
||||||
)
|
)
|
||||||
object_types = models.ManyToManyField(
|
object_types = models.ManyToManyField(
|
||||||
to='contenttypes.ContentType',
|
to='core.ObjectType',
|
||||||
related_name='+',
|
related_name='+',
|
||||||
blank=True,
|
blank=True,
|
||||||
help_text=_("The object type(s) to which this this tag can be applied.")
|
help_text=_("The object type(s) to which this this tag can be applied.")
|
||||||
|
@ -8,6 +8,7 @@ from django.dispatch import receiver, Signal
|
|||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
||||||
|
|
||||||
|
from core.models import ObjectType
|
||||||
from core.signals import job_end, job_start
|
from core.signals import job_end, job_start
|
||||||
from extras.constants import EVENT_JOB_END, EVENT_JOB_START
|
from extras.constants import EVENT_JOB_END, EVENT_JOB_START
|
||||||
from extras.events import process_event_rules
|
from extras.events import process_event_rules
|
||||||
@ -240,8 +241,8 @@ def validate_assigned_tags(sender, instance, action, model, pk_set, **kwargs):
|
|||||||
"""
|
"""
|
||||||
if action != 'pre_add':
|
if action != 'pre_add':
|
||||||
return
|
return
|
||||||
ct = ContentType.objects.get_for_model(instance)
|
ct = ObjectType.objects.get_for_model(instance)
|
||||||
# Retrieve any applied Tags that are restricted to certain object_types
|
# Retrieve any applied Tags that are restricted to certain object types
|
||||||
for tag in model.objects.filter(pk__in=pk_set, object_types__isnull=False).prefetch_related('object_types'):
|
for tag in model.objects.filter(pk__in=pk_set, object_types__isnull=False).prefetch_related('object_types'):
|
||||||
if ct not in tag.object_types.all():
|
if ct not in tag.object_types.all():
|
||||||
raise AbortRequest(f"Tag {tag} cannot be assigned to {ct.model} objects.")
|
raise AbortRequest(f"Tag {tag} cannot be assigned to {ct.model} objects.")
|
||||||
|
@ -1114,9 +1114,9 @@ class TagTestCase(TestCase, ChangeLoggedFilterSetTests):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def setUpTestData(cls):
|
def setUpTestData(cls):
|
||||||
content_types = {
|
object_types = {
|
||||||
'site': ContentType.objects.get_by_natural_key('dcim', 'site'),
|
'site': ObjectType.objects.get_by_natural_key('dcim', 'site'),
|
||||||
'provider': ContentType.objects.get_by_natural_key('circuits', 'provider'),
|
'provider': ObjectType.objects.get_by_natural_key('circuits', 'provider'),
|
||||||
}
|
}
|
||||||
|
|
||||||
tags = (
|
tags = (
|
||||||
@ -1125,8 +1125,8 @@ class TagTestCase(TestCase, ChangeLoggedFilterSetTests):
|
|||||||
Tag(name='Tag 3', slug='tag-3', color='0000ff'),
|
Tag(name='Tag 3', slug='tag-3', color='0000ff'),
|
||||||
)
|
)
|
||||||
Tag.objects.bulk_create(tags)
|
Tag.objects.bulk_create(tags)
|
||||||
tags[0].object_types.add(content_types['site'])
|
tags[0].object_types.add(object_types['site'])
|
||||||
tags[1].object_types.add(content_types['provider'])
|
tags[1].object_types.add(object_types['provider'])
|
||||||
|
|
||||||
# Apply some tags so we can filter by content type
|
# Apply some tags so we can filter by content type
|
||||||
site = Site.objects.create(name='Site 1', slug='site-1')
|
site = Site.objects.create(name='Site 1', slug='site-1')
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from django.contrib.contenttypes.models import ContentType
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from core.models import ObjectType
|
||||||
from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Region, Site, SiteGroup
|
from dcim.models import Device, DeviceRole, DeviceType, Location, Manufacturer, Platform, Region, Site, SiteGroup
|
||||||
from extras.models import ConfigContext, Tag
|
from extras.models import ConfigContext, Tag
|
||||||
from tenancy.models import Tenant, TenantGroup
|
from tenancy.models import Tenant, TenantGroup
|
||||||
@ -22,7 +22,7 @@ class TagTest(TestCase):
|
|||||||
|
|
||||||
# Create a Tag that can only be applied to Regions
|
# Create a Tag that can only be applied to Regions
|
||||||
tag = Tag.objects.create(name='Tag 1', slug='tag-1')
|
tag = Tag.objects.create(name='Tag 1', slug='tag-1')
|
||||||
tag.object_types.add(ContentType.objects.get_by_natural_key('dcim', 'region'))
|
tag.object_types.add(ObjectType.objects.get_by_natural_key('dcim', 'region'))
|
||||||
|
|
||||||
# Apply the Tag to a Region
|
# Apply the Tag to a Region
|
||||||
region.tags.add(tag)
|
region.tags.add(tag)
|
||||||
|
Loading…
Reference in New Issue
Block a user