From 3ade819706fdc24c3da73b0eeec72fa5cf217f83 Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 24 Feb 2025 15:03:25 -0800 Subject: [PATCH] 18423 scripts root update --- netbox/extras/forms/scripts.py | 14 ++++++++++++-- netbox/extras/scripts.py | 13 +++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/netbox/extras/forms/scripts.py b/netbox/extras/forms/scripts.py index ca1844cd8..55f5f3b19 100644 --- a/netbox/extras/forms/scripts.py +++ b/netbox/extras/forms/scripts.py @@ -1,9 +1,13 @@ +import os + from django import forms +from django.conf import settings from django.core.files.storage import storages from django.utils.translation import gettext_lazy as _ from core.forms import ManagedFileForm from extras.choices import DurationChoices +from extras.storage import ScriptFileSystemStorage from utilities.forms.widgets import DateTimePicker, NumberWithOptions from utilities.datetime import local_now @@ -69,9 +73,15 @@ class ScriptFileForm(ManagedFileForm): if self.cleaned_data['upload_file']: storage = storages.create_storage(storages.backends["scripts"]) - self.instance.file_path = self.cleaned_data['upload_file'].name + filename = self.cleaned_data['upload_file'].name + if isinstance(storage, ScriptFileSystemStorage): + full_path = filename + else: + full_path = os.path.join(settings.SCRIPTS_ROOT, filename) + + self.instance.file_path = full_path data = self.cleaned_data['upload_file'] - storage.save(self.instance.name, data) + storage.save(full_path, data) # need to skip ManagedFileForm save method return super(ManagedFileForm, self).save(*args, **kwargs) diff --git a/netbox/extras/scripts.py b/netbox/extras/scripts.py index b20e7e170..ea8c75df1 100644 --- a/netbox/extras/scripts.py +++ b/netbox/extras/scripts.py @@ -560,6 +560,12 @@ class BaseScript: # Convenience functions # + def get_storage(self): + if self.storage is None: + self.storage = storages.create_storage(storages.backends["scripts"]) + + return self.storage + def load_yaml(self, filename): """ Return data from a YAML file @@ -569,8 +575,9 @@ class BaseScript: except ImportError: from yaml import Loader + storage = self.get_storage() file_path = os.path.join(settings.SCRIPTS_ROOT, filename) - with open(file_path, 'r') as datafile: + with storage.open(file_path, 'r') as datafile: data = yaml.load(datafile, Loader=Loader) return data @@ -579,8 +586,10 @@ class BaseScript: """ Return data from a JSON file """ + + storage = self.get_storage() file_path = os.path.join(settings.SCRIPTS_ROOT, filename) - with open(file_path, 'r') as datafile: + with storage.open(file_path, 'r') as datafile: data = json.load(datafile) return data