mirror of
https://github.com/netbox-community/netbox.git
synced 2026-03-31 14:43:36 +02:00
cleanup
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user