mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-27 10:58:37 -06:00
18423 scripts root update
This commit is contained in:
parent
69fcdd23b3
commit
3ade819706
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user