18423 scripts root update

This commit is contained in:
Arthur 2025-02-24 15:03:25 -08:00
parent 69fcdd23b3
commit 3ade819706
2 changed files with 23 additions and 4 deletions

View File

@ -1,9 +1,13 @@
import os
from django import forms from django import forms
from django.conf import settings
from django.core.files.storage import storages from django.core.files.storage import storages
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from core.forms import ManagedFileForm from core.forms import ManagedFileForm
from extras.choices import DurationChoices from extras.choices import DurationChoices
from extras.storage import ScriptFileSystemStorage
from utilities.forms.widgets import DateTimePicker, NumberWithOptions from utilities.forms.widgets import DateTimePicker, NumberWithOptions
from utilities.datetime import local_now from utilities.datetime import local_now
@ -69,9 +73,15 @@ class ScriptFileForm(ManagedFileForm):
if self.cleaned_data['upload_file']: if self.cleaned_data['upload_file']:
storage = storages.create_storage(storages.backends["scripts"]) 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'] data = self.cleaned_data['upload_file']
storage.save(self.instance.name, data) storage.save(full_path, data)
# need to skip ManagedFileForm save method # need to skip ManagedFileForm save method
return super(ManagedFileForm, self).save(*args, **kwargs) return super(ManagedFileForm, self).save(*args, **kwargs)

View File

@ -560,6 +560,12 @@ class BaseScript:
# Convenience functions # 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): def load_yaml(self, filename):
""" """
Return data from a YAML file Return data from a YAML file
@ -569,8 +575,9 @@ class BaseScript:
except ImportError: except ImportError:
from yaml import Loader from yaml import Loader
storage = self.get_storage()
file_path = os.path.join(settings.SCRIPTS_ROOT, filename) 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) data = yaml.load(datafile, Loader=Loader)
return data return data
@ -579,8 +586,10 @@ class BaseScript:
""" """
Return data from a JSON file Return data from a JSON file
""" """
storage = self.get_storage()
file_path = os.path.join(settings.SCRIPTS_ROOT, filename) 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) data = json.load(datafile)
return data return data