diff --git a/netbox/core/models/files.py b/netbox/core/models/files.py index 029df0ef4..7fe98f64f 100644 --- a/netbox/core/models/files.py +++ b/netbox/core/models/files.py @@ -1,7 +1,5 @@ import logging import os -from importlib.machinery import FileFinder -from pkgutil import ModuleInfo, get_importer from django.conf import settings from django.db import models @@ -73,10 +71,3 @@ class ManagedFile(SyncedDataMixin, models.Model): 'scripts': settings.SCRIPTS_ROOT, 'reports': settings.REPORTS_ROOT, }[self.file_root] - - def get_module_info(self): - return ModuleInfo( - module_finder=get_importer(self.file_root), - name=self.file_path.split('.py')[0], - ispkg=False - ) diff --git a/netbox/extras/migrations/0091_create_managedfiles.py b/netbox/extras/migrations/0091_create_managedfiles.py index eefdc06a0..0bd569dd8 100644 --- a/netbox/extras/migrations/0091_create_managedfiles.py +++ b/netbox/extras/migrations/0091_create_managedfiles.py @@ -1,7 +1,8 @@ import pkgutil from django.conf import settings -from django.db import migrations +from django.db import migrations, models +import extras.models.models def create_files(cls, root_name, path): @@ -36,6 +37,31 @@ class Migration(migrations.Migration): ] operations = [ + # Create proxy models + migrations.CreateModel( + name='ReportModule', + fields=[ + ], + options={ + 'proxy': True, + 'indexes': [], + 'constraints': [], + }, + bases=(extras.models.models.PythonModuleMixin, 'core.managedfile', models.Model), + ), + migrations.CreateModel( + name='ScriptModule', + fields=[ + ], + options={ + 'proxy': True, + 'indexes': [], + 'constraints': [], + }, + bases=(extras.models.models.PythonModuleMixin, 'core.managedfile', models.Model), + ), + + # Instantiate ManagedFiles to represent scripts & reports migrations.RunPython( code=replicate_scripts, reverse_code=migrations.RunPython.noop diff --git a/netbox/extras/models/__init__.py b/netbox/extras/models/__init__.py index 14e23366f..b89b958f5 100644 --- a/netbox/extras/models/__init__.py +++ b/netbox/extras/models/__init__.py @@ -23,8 +23,10 @@ __all__ = ( 'JournalEntry', 'ObjectChange', 'Report', + 'ReportModule', 'SavedFilter', 'Script', + 'ScriptModule', 'StagedChange', 'Tag', 'TaggedItem', diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index b0c6d7fe3..6ddd30ebb 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -1,5 +1,6 @@ import json import uuid +from pkgutil import ModuleInfo, get_importer from django.conf import settings from django.contrib import admin @@ -18,6 +19,7 @@ from django.utils.translation import gettext as _ from rest_framework.utils.encoders import JSONEncoder import django_rq +from core.models import ManagedFile from extras.choices import * from extras.constants import * from extras.conditions import ConditionSet @@ -41,8 +43,10 @@ __all__ = ( 'JobResult', 'JournalEntry', 'Report', + 'ReportModule', 'SavedFilter', 'Script', + 'ScriptModule', 'Webhook', ) @@ -814,6 +818,16 @@ class ConfigRevision(models.Model): # Custom scripts & reports # +class PythonModuleMixin: + + def get_module_info(self): + return ModuleInfo( + module_finder=get_importer(self.file_root), + name=self.file_path.split('.py')[0], + ispkg=False + ) + + class Script(JobResultsMixin, WebhooksMixin, models.Model): """ Dummy model used to generate permissions for custom scripts. Does not exist in the database. @@ -822,6 +836,22 @@ class Script(JobResultsMixin, WebhooksMixin, models.Model): managed = False +class ScriptModuleManager(models.Manager): + + def get_queryset(self): + return super().get_queryset().filter(file_root='scripts') + + +class ScriptModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFile): + """ + Proxy model for script module files. + """ + objects = ScriptModuleManager() + + class Meta: + proxy = True + + # # Reports # @@ -832,3 +862,19 @@ class Report(JobResultsMixin, WebhooksMixin, models.Model): """ class Meta: managed = False + + +class ReportModuleManager(models.Manager): + + def get_queryset(self): + return super().get_queryset().filter(file_root='reports') + + +class ReportModule(JobResultsMixin, WebhooksMixin, PythonModuleMixin, ManagedFile): + """ + Proxy model for report module files. + """ + objects = ReportModuleManager() + + class Meta: + proxy = True