2023-03-24 21:25:22 -04:00
|
|
|
import inspect
|
2023-06-30 01:32:39 +05:30
|
|
|
import logging
|
2023-03-24 21:25:22 -04:00
|
|
|
from functools import cached_property
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.urls import reverse
|
2023-08-03 10:41:10 -04:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-03-24 21:25:22 -04:00
|
|
|
|
|
|
|
from core.choices import ManagedFileRootPathChoices
|
|
|
|
from core.models import ManagedFile
|
|
|
|
from extras.utils import is_report
|
2023-03-27 11:43:12 -04:00
|
|
|
from netbox.models.features import JobsMixin, WebhooksMixin
|
2023-03-24 21:25:22 -04:00
|
|
|
from utilities.querysets import RestrictedQuerySet
|
|
|
|
from .mixins import PythonModuleMixin
|
|
|
|
|
2023-06-30 01:32:39 +05:30
|
|
|
logger = logging.getLogger('netbox.reports')
|
|
|
|
|
2023-03-24 21:25:22 -04:00
|
|
|
__all__ = (
|
|
|
|
'Report',
|
|
|
|
'ReportModule',
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-28 15:47:09 -04:00
|
|
|
class Report(WebhooksMixin, models.Model):
|
2023-03-24 21:25:22 -04:00
|
|
|
"""
|
|
|
|
Dummy model used to generate permissions for reports. Does not exist in the database.
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
managed = False
|
|
|
|
|
|
|
|
|
|
|
|
class ReportModuleManager(models.Manager.from_queryset(RestrictedQuerySet)):
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().filter(file_root=ManagedFileRootPathChoices.REPORTS)
|
|
|
|
|
|
|
|
|
2023-03-28 15:47:09 -04:00
|
|
|
class ReportModule(PythonModuleMixin, JobsMixin, ManagedFile):
|
2023-03-24 21:25:22 -04:00
|
|
|
"""
|
|
|
|
Proxy model for report module files.
|
|
|
|
"""
|
|
|
|
objects = ReportModuleManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
2023-08-03 10:41:10 -04:00
|
|
|
verbose_name = _('report module')
|
|
|
|
verbose_name_plural = _('report modules')
|
2023-03-24 21:25:22 -04:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('extras:report_list')
|
|
|
|
|
2023-03-29 16:51:55 -04:00
|
|
|
def __str__(self):
|
|
|
|
return self.python_name
|
|
|
|
|
2023-03-24 21:25:22 -04:00
|
|
|
@cached_property
|
|
|
|
def reports(self):
|
|
|
|
|
|
|
|
def _get_name(cls):
|
|
|
|
# For child objects in submodules use the full import path w/o the root module as the name
|
|
|
|
return cls.full_name.split(".", maxsplit=1)[1]
|
|
|
|
|
2023-03-29 16:51:55 -04:00
|
|
|
try:
|
|
|
|
module = self.get_module()
|
2023-06-30 01:32:39 +05:30
|
|
|
except (ImportError, SyntaxError) as e:
|
|
|
|
logger.error(f"Unable to load report module {self.name}, exception: {e}")
|
2023-03-29 16:51:55 -04:00
|
|
|
return {}
|
2023-03-24 21:25:22 -04:00
|
|
|
reports = {}
|
|
|
|
ordered = getattr(module, 'report_order', [])
|
|
|
|
|
|
|
|
for cls in ordered:
|
|
|
|
reports[_get_name(cls)] = cls
|
|
|
|
for name, cls in inspect.getmembers(module, is_report):
|
|
|
|
if cls not in ordered:
|
|
|
|
reports[_get_name(cls)] = cls
|
|
|
|
|
|
|
|
return reports
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.file_root = ManagedFileRootPathChoices.REPORTS
|
|
|
|
return super().save(*args, **kwargs)
|