mirror of
https://github.com/netbox-community/netbox.git
synced 2026-04-20 07:51:35 +02:00
cleanup
This commit is contained in:
@@ -50,7 +50,7 @@ class ScriptModuleSerializer(ValidatedModelSerializer):
|
|||||||
|
|
||||||
if upload_file and has_data_file:
|
if upload_file and has_data_file:
|
||||||
raise serializers.ValidationError(
|
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:
|
if upload_file and has_data_source:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
@@ -62,7 +62,7 @@ class ScriptModuleSerializer(ValidatedModelSerializer):
|
|||||||
)
|
)
|
||||||
if self.instance is None and not upload_file and not has_data_file:
|
if self.instance is None and not upload_file and not has_data_file:
|
||||||
raise serializers.ValidationError(
|
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
|
# ScriptModule.save() sets file_root; inject it here so full_clean() succeeds
|
||||||
@@ -101,16 +101,14 @@ class ScriptModuleSerializer(ValidatedModelSerializer):
|
|||||||
try:
|
try:
|
||||||
return super().create(validated_data)
|
return super().create(validated_data)
|
||||||
except IntegrityError:
|
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(
|
raise serializers.ValidationError(
|
||||||
_("A script module with this file name already exists.")
|
_("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):
|
class ScriptSerializer(ValidatedModelSerializer):
|
||||||
description = serializers.SerializerMethodField(read_only=True)
|
description = serializers.SerializerMethodField(read_only=True)
|
||||||
|
|||||||
@@ -1390,6 +1390,10 @@ class NotificationTest(APIViewTestCases.APIViewTestCase):
|
|||||||
|
|
||||||
class ScriptUploadTest(APITestCase):
|
class ScriptUploadTest(APITestCase):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def setUpTestData(cls):
|
||||||
|
cls.data_source = DataSource.objects.create(name='Test Source', type='local', source_url='/tmp')
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
self.url_list = reverse('extras-api:script-list')
|
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')
|
upload_file = SimpleUploadedFile('test_upload.py', script_content, content_type='text/plain')
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.url_list,
|
self.url_list,
|
||||||
{'upload_file': upload_file, 'data_source': 1},
|
{'upload_file': upload_file, 'data_source': self.data_source.pk},
|
||||||
format='multipart',
|
format='multipart',
|
||||||
**self.header,
|
**self.header,
|
||||||
)
|
)
|
||||||
@@ -1444,7 +1448,7 @@ class ScriptUploadTest(APITestCase):
|
|||||||
self.add_permissions('extras.add_scriptmodule', 'core.add_managedfile')
|
self.add_permissions('extras.add_scriptmodule', 'core.add_managedfile')
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
self.url_list,
|
self.url_list,
|
||||||
{'data_source': 1},
|
{'data_source': self.data_source.pk},
|
||||||
format='multipart',
|
format='multipart',
|
||||||
**self.header,
|
**self.header,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user