mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 17:26:10 -06:00
Support automatic migration for submodules
This commit is contained in:
parent
c20eeb8114
commit
bf6cfd22b4
@ -56,9 +56,6 @@ class ManagedFile(SyncedDataMixin, models.Model):
|
|||||||
models.Index(fields=('file_root', 'file_path'), name='core_managedfile_root_path'),
|
models.Index(fields=('file_root', 'file_path'), name='core_managedfile_root_path'),
|
||||||
]
|
]
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return f'{self.get_file_root_display()}: {self.file_path}'
|
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('core:managedfile', args=[self.pk])
|
return reverse('core:managedfile', args=[self.pk])
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import pkgutil
|
import pkgutil
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
@ -5,28 +6,40 @@ from django.db import migrations, models
|
|||||||
import extras.models.models
|
import extras.models.models
|
||||||
|
|
||||||
|
|
||||||
def create_files(cls, root_name, path):
|
def create_files(cls, root_name, root_path):
|
||||||
|
|
||||||
modules = list(pkgutil.iter_modules([path]))
|
path_tree = [
|
||||||
filenames = [f'{m.name}.py' for m in modules]
|
path for path, _, _ in os.walk(root_path)
|
||||||
|
if os.path.basename(path)[0] not in ('_', '.')
|
||||||
|
]
|
||||||
|
|
||||||
|
modules = list(pkgutil.iter_modules(path_tree))
|
||||||
|
filenames = []
|
||||||
|
for importer, module_name, is_pkg in modules:
|
||||||
|
if is_pkg:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
module = importer.find_module(module_name).load_module(module_name)
|
||||||
|
rel_path = os.path.relpath(module.__file__, root_path)
|
||||||
|
filenames.append(rel_path)
|
||||||
|
except ImportError:
|
||||||
|
pass
|
||||||
|
|
||||||
managed_files = [
|
managed_files = [
|
||||||
cls(
|
cls(file_root=root_name, file_path=filename)
|
||||||
file_root=root_name,
|
for filename in filenames
|
||||||
file_path=filename
|
|
||||||
) for filename in filenames
|
|
||||||
]
|
]
|
||||||
cls.objects.bulk_create(managed_files)
|
cls.objects.bulk_create(managed_files)
|
||||||
|
|
||||||
|
|
||||||
def replicate_scripts(apps, schema_editor):
|
def replicate_scripts(apps, schema_editor):
|
||||||
ManagedFile = apps.get_model('core', 'ManagedFile')
|
ScriptModule = apps.get_model('extras', 'ScriptModule')
|
||||||
create_files(ManagedFile, 'scripts', settings.SCRIPTS_ROOT)
|
create_files(ScriptModule, 'scripts', settings.SCRIPTS_ROOT)
|
||||||
|
|
||||||
|
|
||||||
def replicate_reports(apps, schema_editor):
|
def replicate_reports(apps, schema_editor):
|
||||||
ManagedFile = apps.get_model('core', 'ManagedFile')
|
ReportModule = apps.get_model('extras', 'ReportModule')
|
||||||
create_files(ManagedFile, 'reports', settings.REPORTS_ROOT)
|
create_files(ReportModule, 'reports', settings.REPORTS_ROOT)
|
||||||
|
|
||||||
|
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import inspect
|
import inspect
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import uuid
|
import uuid
|
||||||
from functools import cached_property
|
from functools import cached_property
|
||||||
from pkgutil import ModuleInfo, get_importer
|
from pkgutil import ModuleInfo, get_importer
|
||||||
@ -824,9 +825,11 @@ class ConfigRevision(models.Model):
|
|||||||
class PythonModuleMixin:
|
class PythonModuleMixin:
|
||||||
|
|
||||||
def get_module_info(self):
|
def get_module_info(self):
|
||||||
|
path = os.path.dirname(self.full_path)
|
||||||
|
module_name = os.path.splitext(os.path.basename(self.file_path))[0]
|
||||||
return ModuleInfo(
|
return ModuleInfo(
|
||||||
module_finder=get_importer(self.file_root),
|
module_finder=get_importer(path),
|
||||||
name=self.file_path.split('.py')[0],
|
name=module_name,
|
||||||
ispkg=False
|
ispkg=False
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -859,9 +862,8 @@ class ScriptModule(PythonModuleMixin, ManagedFile):
|
|||||||
class Meta:
|
class Meta:
|
||||||
proxy = True
|
proxy = True
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def __str__(self):
|
||||||
self.file_root = SCRIPTS_ROOT_NAME
|
return self.file_path
|
||||||
return super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('extras:script_list')
|
return reverse('extras:script_list')
|
||||||
@ -882,6 +884,10 @@ class ScriptModule(PythonModuleMixin, ManagedFile):
|
|||||||
|
|
||||||
return scripts
|
return scripts
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
self.file_root = SCRIPTS_ROOT_NAME
|
||||||
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Reports
|
# Reports
|
||||||
@ -910,9 +916,8 @@ class ReportModule(PythonModuleMixin, ManagedFile):
|
|||||||
class Meta:
|
class Meta:
|
||||||
proxy = True
|
proxy = True
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def __str__(self):
|
||||||
self.file_root = REPORTS_ROOT_NAME
|
return self.file_path
|
||||||
return super().save(*args, **kwargs)
|
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('extras:report_list')
|
return reverse('extras:report_list')
|
||||||
@ -932,3 +937,7 @@ class ReportModule(PythonModuleMixin, ManagedFile):
|
|||||||
reports[child_name] = cls
|
reports[child_name] = cls
|
||||||
|
|
||||||
return reports
|
return reports
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
self.file_root = REPORTS_ROOT_NAME
|
||||||
|
return super().save(*args, **kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user