|
@@ -1,13 +1,9 @@
|
|
|
-from abc import ABC, ABCMeta, abstractmethod
|
|
|
|
|
-from functools import cached_property
|
|
|
|
|
-
|
|
|
|
|
from django.template.loader import render_to_string
|
|
from django.template.loader import render_to_string
|
|
|
from django.utils.html import escape
|
|
from django.utils.html import escape
|
|
|
from django.utils.safestring import mark_safe
|
|
from django.utils.safestring import mark_safe
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from netbox.config import get_config
|
|
from netbox.config import get_config
|
|
|
-from utilities.string import title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
#
|
|
@@ -56,10 +52,6 @@ class ObjectAttr(Attr):
|
|
|
self.linkify = linkify
|
|
self.linkify = linkify
|
|
|
self.grouped_by = grouped_by
|
|
self.grouped_by = grouped_by
|
|
|
|
|
|
|
|
- # Derive label from related object if not explicitly set
|
|
|
|
|
- if self.label is None:
|
|
|
|
|
- self.label = title(self.accessor)
|
|
|
|
|
-
|
|
|
|
|
def render(self, obj):
|
|
def render(self, obj):
|
|
|
value = self._resolve_attr(obj, self.accessor)
|
|
value = self._resolve_attr(obj, self.accessor)
|
|
|
if value is None:
|
|
if value is None:
|
|
@@ -132,54 +124,3 @@ class TemplatedAttr(Attr):
|
|
|
'value': self._resolve_attr(obj, self.accessor),
|
|
'value': self._resolve_attr(obj, self.accessor),
|
|
|
}
|
|
}
|
|
|
)
|
|
)
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-#
|
|
|
|
|
-# Components
|
|
|
|
|
-#
|
|
|
|
|
-
|
|
|
|
|
-class Component(ABC):
|
|
|
|
|
-
|
|
|
|
|
- @abstractmethod
|
|
|
|
|
- def render(self):
|
|
|
|
|
- pass
|
|
|
|
|
-
|
|
|
|
|
- def __str__(self):
|
|
|
|
|
- return self.render()
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class ObjectDetailsPanelMeta(ABCMeta):
|
|
|
|
|
-
|
|
|
|
|
- def __new__(mcls, name, bases, attrs):
|
|
|
|
|
- # Collect all declared attributes
|
|
|
|
|
- attrs['_attrs'] = {}
|
|
|
|
|
- for key, val in list(attrs.items()):
|
|
|
|
|
- if isinstance(val, Attr):
|
|
|
|
|
- attrs['_attrs'][key] = val
|
|
|
|
|
- return super().__new__(mcls, name, bases, attrs)
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class ObjectDetailsPanel(Component, metaclass=ObjectDetailsPanelMeta):
|
|
|
|
|
- template_name = 'components/object_details_panel.html'
|
|
|
|
|
-
|
|
|
|
|
- def __init__(self, obj, title=None):
|
|
|
|
|
- self.object = obj
|
|
|
|
|
- self.title = title or obj._meta.verbose_name
|
|
|
|
|
-
|
|
|
|
|
- @cached_property
|
|
|
|
|
- def attributes(self):
|
|
|
|
|
- return [
|
|
|
|
|
- {
|
|
|
|
|
- 'label': attr.label or title(name),
|
|
|
|
|
- 'value': attr.render(self.object),
|
|
|
|
|
- } for name, attr in self._attrs.items()
|
|
|
|
|
- ]
|
|
|
|
|
-
|
|
|
|
|
- def render(self):
|
|
|
|
|
- return render_to_string(self.template_name, {
|
|
|
|
|
- 'title': self.title,
|
|
|
|
|
- 'attrs': self.attributes,
|
|
|
|
|
- })
|
|
|
|
|
-
|
|
|
|
|
- def __str__(self):
|
|
|
|
|
- return self.render()
|
|
|