mirror of
https://github.com/netbox-community/netbox.git
synced 2026-01-23 03:58:45 -06:00
18896 Replace STORAGE_BACKEND with STORAGES and support Script running from S3 (#18680)
This commit is contained in:
@@ -1,11 +1,18 @@
|
||||
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
|
||||
|
||||
__all__ = (
|
||||
'ScriptFileForm',
|
||||
'ScriptForm',
|
||||
)
|
||||
|
||||
@@ -55,3 +62,26 @@ class ScriptForm(forms.Form):
|
||||
self.cleaned_data['_schedule_at'] = local_now()
|
||||
|
||||
return self.cleaned_data
|
||||
|
||||
|
||||
class ScriptFileForm(ManagedFileForm):
|
||||
"""
|
||||
ManagedFileForm with a custom save method to use django-storages.
|
||||
"""
|
||||
def save(self, *args, **kwargs):
|
||||
# If a file was uploaded, save it to disk
|
||||
if self.cleaned_data['upload_file']:
|
||||
storage = storages.create_storage(storages.backends["scripts"])
|
||||
|
||||
filename = self.cleaned_data['upload_file'].name
|
||||
if isinstance(storage, ScriptFileSystemStorage):
|
||||
full_path = os.path.join(settings.SCRIPTS_ROOT, filename)
|
||||
else:
|
||||
full_path = filename
|
||||
|
||||
self.instance.file_path = full_path
|
||||
data = self.cleaned_data['upload_file']
|
||||
storage.save(filename, data)
|
||||
|
||||
# need to skip ManagedFileForm save method
|
||||
return super(ManagedFileForm, self).save(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user