Introduce proxy models for script & report modules

This commit is contained in:
jeremystretch 2023-03-23 15:31:26 -04:00
parent ab4a38d32d
commit 1daac5f781
4 changed files with 75 additions and 10 deletions

View File

@ -1,7 +1,5 @@
import logging import logging
import os import os
from importlib.machinery import FileFinder
from pkgutil import ModuleInfo, get_importer
from django.conf import settings from django.conf import settings
from django.db import models from django.db import models
@ -73,10 +71,3 @@ class ManagedFile(SyncedDataMixin, models.Model):
'scripts': settings.SCRIPTS_ROOT, 'scripts': settings.SCRIPTS_ROOT,
'reports': settings.REPORTS_ROOT, 'reports': settings.REPORTS_ROOT,
}[self.file_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
)

View File

@ -1,7 +1,8 @@
import pkgutil import pkgutil
from django.conf import settings 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): def create_files(cls, root_name, path):
@ -36,6 +37,31 @@ class Migration(migrations.Migration):
] ]
operations = [ 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( migrations.RunPython(
code=replicate_scripts, code=replicate_scripts,
reverse_code=migrations.RunPython.noop reverse_code=migrations.RunPython.noop

View File

@ -23,8 +23,10 @@ __all__ = (
'JournalEntry', 'JournalEntry',
'ObjectChange', 'ObjectChange',
'Report', 'Report',
'ReportModule',
'SavedFilter', 'SavedFilter',
'Script', 'Script',
'ScriptModule',
'StagedChange', 'StagedChange',
'Tag', 'Tag',
'TaggedItem', 'TaggedItem',

View File

@ -1,5 +1,6 @@
import json import json
import uuid import uuid
from pkgutil import ModuleInfo, get_importer
from django.conf import settings from django.conf import settings
from django.contrib import admin from django.contrib import admin
@ -18,6 +19,7 @@ from django.utils.translation import gettext as _
from rest_framework.utils.encoders import JSONEncoder from rest_framework.utils.encoders import JSONEncoder
import django_rq import django_rq
from core.models import ManagedFile
from extras.choices import * from extras.choices import *
from extras.constants import * from extras.constants import *
from extras.conditions import ConditionSet from extras.conditions import ConditionSet
@ -41,8 +43,10 @@ __all__ = (
'JobResult', 'JobResult',
'JournalEntry', 'JournalEntry',
'Report', 'Report',
'ReportModule',
'SavedFilter', 'SavedFilter',
'Script', 'Script',
'ScriptModule',
'Webhook', 'Webhook',
) )
@ -814,6 +818,16 @@ class ConfigRevision(models.Model):
# Custom scripts & reports # 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): class Script(JobResultsMixin, WebhooksMixin, models.Model):
""" """
Dummy model used to generate permissions for custom scripts. Does not exist in the database. 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 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 # Reports
# #
@ -832,3 +862,19 @@ class Report(JobResultsMixin, WebhooksMixin, models.Model):
""" """
class Meta: class Meta:
managed = False 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