From 8d04f31314797ef90b3ea710ab5f83d0f0f52607 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 30 Mar 2026 16:45:13 -0700 Subject: [PATCH] cleanup --- netbox/extras/api/serializers_/scripts.py | 14 ++++++-------- netbox/extras/tests/test_api.py | 8 ++++++-- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/netbox/extras/api/serializers_/scripts.py b/netbox/extras/api/serializers_/scripts.py index 3dba5bf86..60ff86d09 100644 --- a/netbox/extras/api/serializers_/scripts.py +++ b/netbox/extras/api/serializers_/scripts.py @@ -50,7 +50,7 @@ class ScriptModuleSerializer(ValidatedModelSerializer): if upload_file and has_data_file: raise serializers.ValidationError( - _("Cannot upload a file and sync from an existing file") + _("Cannot upload a file and sync from an existing file.") ) if upload_file and has_data_source: raise serializers.ValidationError( @@ -62,7 +62,7 @@ class ScriptModuleSerializer(ValidatedModelSerializer): ) if self.instance is None and not upload_file and not has_data_file: raise serializers.ValidationError( - _("Must upload a file or select a data file to sync") + _("Must upload a file or select a data file to sync.") ) # ScriptModule.save() sets file_root; inject it here so full_clean() succeeds @@ -101,16 +101,14 @@ class ScriptModuleSerializer(ValidatedModelSerializer): try: return super().create(validated_data) except IntegrityError: + # Clean up the file written to disk before the failed DB insert + if file_path := validated_data.get('file_path'): + storage = storages.create_storage(storages.backends["scripts"]) + storage.delete(file_path) raise serializers.ValidationError( _("A script module with this file name already exists.") ) - def update(self, instance, validated_data): - upload_file = validated_data.pop('upload_file', None) - if upload_file: - self._save_upload(upload_file, validated_data) - return super().update(instance, validated_data) - class ScriptSerializer(ValidatedModelSerializer): description = serializers.SerializerMethodField(read_only=True) diff --git a/netbox/extras/tests/test_api.py b/netbox/extras/tests/test_api.py index b49330cae..069d50d93 100644 --- a/netbox/extras/tests/test_api.py +++ b/netbox/extras/tests/test_api.py @@ -1390,6 +1390,10 @@ class NotificationTest(APIViewTestCases.APIViewTestCase): class ScriptUploadTest(APITestCase): + @classmethod + def setUpTestData(cls): + cls.data_source = DataSource.objects.create(name='Test Source', type='local', source_url='/tmp') + def setUp(self): super().setUp() self.url_list = reverse('extras-api:script-list') @@ -1433,7 +1437,7 @@ class ScriptUploadTest(APITestCase): upload_file = SimpleUploadedFile('test_upload.py', script_content, content_type='text/plain') response = self.client.post( self.url_list, - {'upload_file': upload_file, 'data_source': 1}, + {'upload_file': upload_file, 'data_source': self.data_source.pk}, format='multipart', **self.header, ) @@ -1444,7 +1448,7 @@ class ScriptUploadTest(APITestCase): self.add_permissions('extras.add_scriptmodule', 'core.add_managedfile') response = self.client.post( self.url_list, - {'data_source': 1}, + {'data_source': self.data_source.pk}, format='multipart', **self.header, )