Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions netbox/extras/forms/scripts.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import os

from core.choices import JobIntervalChoices
from core.forms import ManagedFileForm
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.choices import JobIntervalChoices
from core.forms import ManagedFileForm
from extras.storage import ScriptFileSystemStorage
from utilities.datetime import local_now
from utilities.forms.widgets import DateTimePicker, NumberWithOptions

Expand Down Expand Up @@ -74,12 +69,7 @@ def save(self, *args, **kwargs):
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
self.instance.file_path = filename
data = self.cleaned_data['upload_file']
storage.save(filename, data)

Expand Down
56 changes: 56 additions & 0 deletions netbox/extras/migrations/0129_fix_script_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from django.conf import settings
from django.core.files.storage import storages
from django.db import migrations
from urllib.parse import urlparse

from extras.storage import ScriptFileSystemStorage


def normalize(url):
parsed_url = urlparse(url)
if not parsed_url.path.endswith('/'):
return url + '/'
return url


def fix_script_paths(apps, schema_editor):
"""
Fix script paths for scripts that had incorrect path from NB 4.3.
"""
storage = storages.create_storage(storages.backends["scripts"])
if not isinstance(storage, ScriptFileSystemStorage):
return

ScriptModule = apps.get_model('extras', 'ScriptModule')
script_root_path = normalize(settings.SCRIPTS_ROOT)
for script in ScriptModule.objects.filter(file_path__startswith=script_root_path):
script.file_path = script.file_path[len(script_root_path):]
script.save()


class Migration(migrations.Migration):

dependencies = [
('extras', '0128_tableconfig'),
]

operations = [
migrations.RunPython(code=fix_script_paths, reverse_code=migrations.RunPython.noop),
]


def oc_fix_script_paths(objectchange, reverting):
script_root_path = normalize(settings.SCRIPTS_ROOT)

for data in (objectchange.prechange_data, objectchange.postchange_data):
if data is None:
continue

if file_path := data.get('file_path'):
if file_path.startswith(script_root_path):
data['file_path'] = file_path[len(script_root_path):]


objectchange_migrators = {
'extras.scriptmodule': oc_fix_script_paths,
}