From e0165539b31119d3ef0f670e7636d7255bfc3a11 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 1 Mar 2024 16:34:52 -0500 Subject: [PATCH] Rename ImageAttachment.content_type to object_type --- netbox/extras/api/serializers.py | 8 ++++---- netbox/extras/filtersets.py | 4 ++-- netbox/extras/forms/filtersets.py | 6 +++--- .../migrations/0111_rename_content_types.py | 15 +++++++++++++++ netbox/extras/models/models.py | 12 ++++++------ netbox/extras/tables/tables.py | 8 ++++---- netbox/extras/tests/test_api.py | 6 +++--- netbox/extras/tests/test_filtersets.py | 16 ++++++++-------- netbox/extras/utils.py | 2 +- netbox/netbox/models/features.py | 4 +++- netbox/tenancy/forms/bulk_import.py | 4 ++-- 11 files changed, 51 insertions(+), 34 deletions(-) diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index d2e296ffa..46189cf4e 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -312,7 +312,7 @@ class TagSerializer(ValidatedModelSerializer): class ImageAttachmentSerializer(ValidatedModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='extras-api:imageattachment-detail') - content_type = ContentTypeField( + object_type = ContentTypeField( queryset=ObjectType.objects.all() ) parent = serializers.SerializerMethodField(read_only=True) @@ -320,7 +320,7 @@ class ImageAttachmentSerializer(ValidatedModelSerializer): class Meta: model = ImageAttachment fields = [ - 'id', 'url', 'display', 'content_type', 'object_id', 'parent', 'name', 'image', 'image_height', + 'id', 'url', 'display', 'object_type', 'object_id', 'parent', 'name', 'image', 'image_height', 'image_width', 'created', 'last_updated', ] brief_fields = ('id', 'url', 'display', 'name', 'image') @@ -329,10 +329,10 @@ class ImageAttachmentSerializer(ValidatedModelSerializer): # Validate that the parent object exists try: - data['content_type'].get_object_for_this_type(id=data['object_id']) + data['object_type'].get_object_for_this_type(id=data['object_id']) except ObjectDoesNotExist: raise serializers.ValidationError( - "Invalid parent object: {} ID {}".format(data['content_type'], data['object_id']) + "Invalid parent object: {} ID {}".format(data['object_type'], data['object_id']) ) # Enforce model validation diff --git a/netbox/extras/filtersets.py b/netbox/extras/filtersets.py index fb5f972d1..797c76b51 100644 --- a/netbox/extras/filtersets.py +++ b/netbox/extras/filtersets.py @@ -318,11 +318,11 @@ class ImageAttachmentFilterSet(BaseFilterSet): label=_('Search'), ) created = django_filters.DateTimeFilter() - content_type = ContentTypeFilter() + object_type = ContentTypeFilter() class Meta: model = ImageAttachment - fields = ['id', 'content_type_id', 'object_id', 'name'] + fields = ['id', 'object_type_id', 'object_id', 'name'] def search(self, queryset, name, value): if not value.strip(): diff --git a/netbox/extras/forms/filtersets.py b/netbox/extras/forms/filtersets.py index 42ba5618c..75724b108 100644 --- a/netbox/extras/forms/filtersets.py +++ b/netbox/extras/forms/filtersets.py @@ -179,10 +179,10 @@ class ExportTemplateFilterForm(SavedFiltersMixin, FilterForm): class ImageAttachmentFilterForm(SavedFiltersMixin, FilterForm): fieldsets = ( (None, ('q', 'filter_id')), - (_('Attributes'), ('content_type_id', 'name',)), + (_('Attributes'), ('object_type_id', 'name',)), ) - content_type_id = ContentTypeChoiceField( - label=_('Content type'), + object_type_id = ContentTypeChoiceField( + label=_('Object type'), queryset=ObjectType.objects.with_feature('image_attachments'), required=False ) diff --git a/netbox/extras/migrations/0111_rename_content_types.py b/netbox/extras/migrations/0111_rename_content_types.py index df347dffd..3d6529692 100644 --- a/netbox/extras/migrations/0111_rename_content_types.py +++ b/netbox/extras/migrations/0111_rename_content_types.py @@ -74,4 +74,19 @@ class Migration(migrations.Migration): name='object_types', field=models.ManyToManyField(related_name='saved_filters', to='core.objecttype'), ), + + # Image attachments + migrations.RemoveIndex( + model_name='imageattachment', + name='extras_imag_content_94728e_idx', + ), + migrations.RenameField( + model_name='imageattachment', + old_name='content_type', + new_name='object_type', + ), + migrations.AddIndex( + model_name='imageattachment', + index=models.Index(fields=['object_type', 'object_id'], name='extras_imag_object__96bebc_idx'), + ), ] diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index c5e35c9c7..4a57c6ada 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -598,13 +598,13 @@ class ImageAttachment(ChangeLoggedModel): """ An uploaded image which is associated with an object. """ - content_type = models.ForeignKey( + object_type = models.ForeignKey( to='contenttypes.ContentType', on_delete=models.CASCADE ) object_id = models.PositiveBigIntegerField() parent = GenericForeignKey( - ct_field='content_type', + ct_field='object_type', fk_field='object_id' ) image = models.ImageField( @@ -626,12 +626,12 @@ class ImageAttachment(ChangeLoggedModel): objects = RestrictedQuerySet.as_manager() - clone_fields = ('content_type', 'object_id') + clone_fields = ('object_type', 'object_id') class Meta: ordering = ('name', 'pk') # name may be non-unique indexes = ( - models.Index(fields=('content_type', 'object_id')), + models.Index(fields=('object_type', 'object_id')), ) verbose_name = _('image attachment') verbose_name_plural = _('image attachments') @@ -646,9 +646,9 @@ class ImageAttachment(ChangeLoggedModel): super().clean() # Validate the assigned object type - if self.content_type not in ObjectType.objects.with_feature('image_attachments'): + if self.object_type not in ObjectType.objects.with_feature('image_attachments'): raise ValidationError( - _("Image attachments cannot be assigned to this object type ({type}).").format(type=self.content_type) + _("Image attachments cannot be assigned to this object type ({type}).").format(type=self.object_type) ) def delete(self, *args, **kwargs): diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index 479cb568e..5bf4f1892 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -174,8 +174,8 @@ class ImageAttachmentTable(NetBoxTable): verbose_name=_('ID'), linkify=False ) - content_type = columns.ContentTypeColumn( - verbose_name=_('Content Type'), + object_type = columns.ContentTypeColumn( + verbose_name=_('Object Type'), ) parent = tables.Column( verbose_name=_('Parent'), @@ -193,10 +193,10 @@ class ImageAttachmentTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = ImageAttachment fields = ( - 'pk', 'content_type', 'parent', 'image', 'name', 'image_height', 'image_width', 'size', 'created', + 'pk', 'object_type', 'parent', 'image', 'name', 'image_height', 'image_width', 'size', 'created', 'last_updated', ) - default_columns = ('content_type', 'parent', 'image', 'name', 'size', 'created') + default_columns = ('object_type', 'parent', 'image', 'name', 'size', 'created') class SavedFilterTable(NetBoxTable): diff --git a/netbox/extras/tests/test_api.py b/netbox/extras/tests/test_api.py index eaa031837..53d981123 100644 --- a/netbox/extras/tests/test_api.py +++ b/netbox/extras/tests/test_api.py @@ -548,7 +548,7 @@ class ImageAttachmentTest( image_attachments = ( ImageAttachment( - content_type=ct, + object_type=ct, object_id=site.pk, name='Image Attachment 1', image='http://example.com/image1.png', @@ -556,7 +556,7 @@ class ImageAttachmentTest( image_width=100 ), ImageAttachment( - content_type=ct, + object_type=ct, object_id=site.pk, name='Image Attachment 2', image='http://example.com/image2.png', @@ -564,7 +564,7 @@ class ImageAttachmentTest( image_width=100 ), ImageAttachment( - content_type=ct, + object_type=ct, object_id=site.pk, name='Image Attachment 3', image='http://example.com/image3.png', diff --git a/netbox/extras/tests/test_filtersets.py b/netbox/extras/tests/test_filtersets.py index a45da8e20..762818d6d 100644 --- a/netbox/extras/tests/test_filtersets.py +++ b/netbox/extras/tests/test_filtersets.py @@ -693,7 +693,7 @@ class ImageAttachmentTestCase(TestCase, BaseFilterSetTests): image_attachments = ( ImageAttachment( - content_type=site_ct, + object_type=site_ct, object_id=sites[0].pk, name='Image Attachment 1', image='http://example.com/image1.png', @@ -701,7 +701,7 @@ class ImageAttachmentTestCase(TestCase, BaseFilterSetTests): image_width=100 ), ImageAttachment( - content_type=site_ct, + object_type=site_ct, object_id=sites[1].pk, name='Image Attachment 2', image='http://example.com/image2.png', @@ -709,7 +709,7 @@ class ImageAttachmentTestCase(TestCase, BaseFilterSetTests): image_width=100 ), ImageAttachment( - content_type=rack_ct, + object_type=rack_ct, object_id=racks[0].pk, name='Image Attachment 3', image='http://example.com/image3.png', @@ -717,7 +717,7 @@ class ImageAttachmentTestCase(TestCase, BaseFilterSetTests): image_width=100 ), ImageAttachment( - content_type=rack_ct, + object_type=rack_ct, object_id=racks[1].pk, name='Image Attachment 4', image='http://example.com/image4.png', @@ -735,13 +735,13 @@ class ImageAttachmentTestCase(TestCase, BaseFilterSetTests): params = {'name': ['Image Attachment 1', 'Image Attachment 2']} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) - def test_content_type(self): - params = {'content_type': 'dcim.site'} + def test_object_type(self): + params = {'object_type': 'dcim.site'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) - def test_content_type_id_and_object_id(self): + def test_object_type_id_and_object_id(self): params = { - 'content_type_id': ContentType.objects.get(app_label='dcim', model='site').pk, + 'object_type_id': ContentType.objects.get(app_label='dcim', model='site').pk, 'object_id': [Site.objects.first().pk], } self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) diff --git a/netbox/extras/utils.py b/netbox/extras/utils.py index 4464af718..e67b9b50c 100644 --- a/netbox/extras/utils.py +++ b/netbox/extras/utils.py @@ -24,7 +24,7 @@ def image_upload(instance, filename): elif instance.name: filename = instance.name - return '{}{}_{}_{}'.format(path, instance.content_type.name, instance.object_id, filename) + return '{}{}_{}_{}'.format(path, instance.object_type.name, instance.object_id, filename) def is_script(obj): diff --git a/netbox/netbox/models/features.py b/netbox/netbox/models/features.py index fb6cf8498..c8137ec66 100644 --- a/netbox/netbox/models/features.py +++ b/netbox/netbox/models/features.py @@ -329,7 +329,9 @@ class ImageAttachmentsMixin(models.Model): Enables the assignments of ImageAttachments. """ images = GenericRelation( - to='extras.ImageAttachment' + to='extras.ImageAttachment', + content_type_field='object_type', + object_id_field='object_id' ) class Meta: diff --git a/netbox/tenancy/forms/bulk_import.py b/netbox/tenancy/forms/bulk_import.py index f38b3293d..f37317549 100644 --- a/netbox/tenancy/forms/bulk_import.py +++ b/netbox/tenancy/forms/bulk_import.py @@ -91,7 +91,7 @@ class ContactImportForm(NetBoxModelImportForm): class ContactAssignmentImportForm(NetBoxModelImportForm): - content_type = CSVContentTypeField( + object_type = CSVContentTypeField( queryset=ContentType.objects.all(), help_text=_("One or more assigned object types") ) @@ -108,4 +108,4 @@ class ContactAssignmentImportForm(NetBoxModelImportForm): class Meta: model = ContactAssignment - fields = ('content_type', 'object_id', 'contact', 'priority', 'role') + fields = ('object_type', 'object_id', 'contact', 'priority', 'role')