| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- from dataclasses import dataclass
- from typing import Optional
- import django_tables2 as tables
- from django.conf import settings
- from django.contrib.auth.models import AnonymousUser
- from django.template import Context, Template
- from django.urls import reverse
- from django.utils.safestring import mark_safe
- from django_tables2.utils import Accessor
- from extras.choices import CustomFieldTypeChoices
- from utilities.utils import content_type_identifier, content_type_name
- __all__ = (
- 'ActionsColumn',
- 'BooleanColumn',
- 'ChoiceFieldColumn',
- 'ColorColumn',
- 'ColoredLabelColumn',
- 'ContentTypeColumn',
- 'ContentTypesColumn',
- 'CustomFieldColumn',
- 'CustomLinkColumn',
- 'LinkedCountColumn',
- 'MarkdownColumn',
- 'MPTTColumn',
- 'TagColumn',
- 'TemplateColumn',
- 'ToggleColumn',
- 'UtilizationColumn',
- )
- class ToggleColumn(tables.CheckBoxColumn):
- """
- Extend CheckBoxColumn to add a "toggle all" checkbox in the column header.
- """
- def __init__(self, *args, **kwargs):
- default = kwargs.pop('default', '')
- visible = kwargs.pop('visible', False)
- if 'attrs' not in kwargs:
- kwargs['attrs'] = {
- 'td': {
- 'class': 'min-width',
- },
- 'input': {
- 'class': 'form-check-input'
- }
- }
- super().__init__(*args, default=default, visible=visible, **kwargs)
- @property
- def header(self):
- return mark_safe('<input type="checkbox" class="toggle form-check-input" title="Toggle All" />')
- class BooleanColumn(tables.Column):
- """
- Custom implementation of BooleanColumn to render a nicely-formatted checkmark or X icon instead of a Unicode
- character.
- """
- def render(self, value):
- if value:
- rendered = '<span class="text-success"><i class="mdi mdi-check-bold"></i></span>'
- elif value is None:
- rendered = '<span class="text-muted">—</span>'
- else:
- rendered = '<span class="text-danger"><i class="mdi mdi-close-thick"></i></span>'
- return mark_safe(rendered)
- def value(self, value):
- return str(value)
- class TemplateColumn(tables.TemplateColumn):
- """
- Overrides the stock TemplateColumn to render a placeholder if the returned value is an empty string.
- """
- PLACEHOLDER = mark_safe('—')
- def render(self, *args, **kwargs):
- ret = super().render(*args, **kwargs)
- if not ret.strip():
- return self.PLACEHOLDER
- return ret
- def value(self, **kwargs):
- ret = super().value(**kwargs)
- if ret == self.PLACEHOLDER:
- return ''
- return ret
- @dataclass
- class ActionsItem:
- title: str
- icon: str
- permission: Optional[str] = None
- class ActionsColumn(tables.Column):
- """
- A dropdown menu which provides edit, delete, and changelog links for an object. Can optionally include
- additional buttons rendered from a template string.
- :param sequence: The ordered list of dropdown menu items to include
- :param extra_buttons: A Django template string which renders additional buttons preceding the actions dropdown
- """
- attrs = {'td': {'class': 'text-end text-nowrap noprint'}}
- empty_values = ()
- actions = {
- 'edit': ActionsItem('Edit', 'pencil', 'change'),
- 'delete': ActionsItem('Delete', 'trash-can-outline', 'delete'),
- 'changelog': ActionsItem('Changelog', 'history'),
- }
- def __init__(self, *args, sequence=('edit', 'delete', 'changelog'), extra_buttons='', **kwargs):
- super().__init__(*args, **kwargs)
- self.extra_buttons = extra_buttons
- # Determine which actions to enable
- self.actions = {
- name: self.actions[name] for name in sequence
- }
- def header(self):
- return ''
- def render(self, record, table, **kwargs):
- # Skip dummy records (e.g. available VLANs) or those with no actions
- if not getattr(record, 'pk', None) or not self.actions:
- return ''
- model = table.Meta.model
- viewname_base = f'{model._meta.app_label}:{model._meta.model_name}'
- request = getattr(table, 'context', {}).get('request')
- url_appendix = f'?return_url={request.path}' if request else ''
- links = []
- user = getattr(request, 'user', AnonymousUser())
- for action, attrs in self.actions.items():
- permission = f'{model._meta.app_label}.{attrs.permission}_{model._meta.model_name}'
- if attrs.permission is None or user.has_perm(permission):
- url = reverse(f'{viewname_base}_{action}', kwargs={'pk': record.pk})
- links.append(f'<li><a class="dropdown-item" href="{url}{url_appendix}">'
- f'<i class="mdi mdi-{attrs.icon}"></i> {attrs.title}</a></li>')
- if not links:
- return ''
- menu = f'<span class="dropdown">' \
- f'<a class="btn btn-sm btn-secondary dropdown-toggle" href="#" type="button" data-bs-toggle="dropdown">' \
- f'<i class="mdi mdi-wrench"></i></a>' \
- f'<ul class="dropdown-menu">{"".join(links)}</ul></span>'
- # Render any extra buttons from template code
- if self.extra_buttons:
- template = Template(self.extra_buttons)
- context = getattr(table, "context", Context())
- context.update({'record': record})
- menu = template.render(context) + menu
- return mark_safe(menu)
- class ChoiceFieldColumn(tables.Column):
- """
- Render a ChoiceField value inside a <span> indicating a particular CSS class. This is useful for displaying colored
- choices. The CSS class is derived by calling .get_FOO_class() on the row record.
- """
- def render(self, record, bound_column, value):
- if value:
- name = bound_column.name
- css_class = getattr(record, f'get_{name}_class')()
- label = getattr(record, f'get_{name}_display')()
- return mark_safe(
- f'<span class="badge bg-{css_class}">{label}</span>'
- )
- return self.default
- def value(self, value):
- return value
- class ContentTypeColumn(tables.Column):
- """
- Display a ContentType instance.
- """
- def render(self, value):
- if value is None:
- return None
- return content_type_name(value)
- def value(self, value):
- if value is None:
- return None
- return content_type_identifier(value)
- class ContentTypesColumn(tables.ManyToManyColumn):
- """
- Display a list of ContentType instances.
- """
- def __init__(self, separator=None, *args, **kwargs):
- # Use a line break as the default separator
- if separator is None:
- separator = mark_safe('<br />')
- super().__init__(separator=separator, *args, **kwargs)
- def transform(self, obj):
- return content_type_name(obj)
- def value(self, value):
- return ','.join([
- content_type_identifier(ct) for ct in self.filter(value)
- ])
- class ColorColumn(tables.Column):
- """
- Display a color (#RRGGBB).
- """
- def render(self, value):
- return mark_safe(
- f'<span class="color-label" style="background-color: #{value}"> </span>'
- )
- def value(self, value):
- return f'#{value}'
- class ColoredLabelColumn(tables.TemplateColumn):
- """
- Render a colored label (e.g. for DeviceRoles).
- """
- template_code = """
- {% load helpers %}
- {% if value %}
- <span class="badge" style="color: {{ value.color|fgcolor }}; background-color: #{{ value.color }}">
- <a href="{{ value.get_absolute_url }}">{{ value }}</a>
- </span>
- {% else %}
- —
- {% endif %}
- """
- def __init__(self, *args, **kwargs):
- super().__init__(template_code=self.template_code, *args, **kwargs)
- def value(self, value):
- return str(value)
- class LinkedCountColumn(tables.Column):
- """
- Render a count of related objects linked to a filtered URL.
- :param viewname: The view name to use for URL resolution
- :param view_kwargs: Additional kwargs to pass for URL resolution (optional)
- :param url_params: A dict of query parameters to append to the URL (e.g. ?foo=bar) (optional)
- """
- def __init__(self, viewname, *args, view_kwargs=None, url_params=None, default=0, **kwargs):
- self.viewname = viewname
- self.view_kwargs = view_kwargs or {}
- self.url_params = url_params
- super().__init__(*args, default=default, **kwargs)
- def render(self, record, value):
- if value:
- url = reverse(self.viewname, kwargs=self.view_kwargs)
- if self.url_params:
- url += '?' + '&'.join([
- f'{k}={getattr(record, v) or settings.FILTERS_NULL_CHOICE_VALUE}'
- for k, v in self.url_params.items()
- ])
- return mark_safe(f'<a href="{url}">{value}</a>')
- return value
- def value(self, value):
- return value
- class TagColumn(tables.TemplateColumn):
- """
- Display a list of tags assigned to the object.
- """
- template_code = """
- {% load helpers %}
- {% for tag in value.all %}
- {% tag tag url_name=url_name %}
- {% empty %}
- <span class="text-muted">—</span>
- {% endfor %}
- """
- def __init__(self, url_name=None):
- super().__init__(
- template_code=self.template_code,
- extra_context={'url_name': url_name}
- )
- def value(self, value):
- return ",".join([tag.name for tag in value.all()])
- class CustomFieldColumn(tables.Column):
- """
- Display custom fields in the appropriate format.
- """
- def __init__(self, customfield, *args, **kwargs):
- self.customfield = customfield
- kwargs['accessor'] = Accessor(f'custom_field_data__{customfield.name}')
- if 'verbose_name' not in kwargs:
- kwargs['verbose_name'] = customfield.label or customfield.name
- super().__init__(*args, **kwargs)
- def render(self, value):
- if isinstance(value, list):
- return ', '.join(v for v in value)
- elif self.customfield.type == CustomFieldTypeChoices.TYPE_URL:
- # Linkify custom URLs
- return mark_safe(f'<a href="{value}">{value}</a>')
- if value is not None:
- return value
- return self.default
- class CustomLinkColumn(tables.Column):
- """
- Render a custom links as a table column.
- """
- def __init__(self, customlink, *args, **kwargs):
- self.customlink = customlink
- kwargs['accessor'] = Accessor('pk')
- if 'verbose_name' not in kwargs:
- kwargs['verbose_name'] = customlink.name
- super().__init__(*args, **kwargs)
- def render(self, record):
- try:
- rendered = self.customlink.render({'obj': record})
- if rendered:
- return mark_safe(f'<a href="{rendered["link"]}"{rendered["link_target"]}>{rendered["text"]}</a>')
- except Exception as e:
- return mark_safe(f'<span class="text-danger" title="{e}"><i class="mdi mdi-alert"></i> Error</span>')
- return ''
- def value(self, record):
- try:
- rendered = self.customlink.render({'obj': record})
- if rendered:
- return rendered['link']
- except Exception:
- pass
- return None
- class MPTTColumn(tables.TemplateColumn):
- """
- Display a nested hierarchy for MPTT-enabled models.
- """
- template_code = """
- {% load helpers %}
- {% for i in record.level|as_range %}<i class="mdi mdi-circle-small"></i>{% endfor %}
- <a href="{{ record.get_absolute_url }}">{{ record.name }}</a>
- """
- def __init__(self, *args, **kwargs):
- super().__init__(
- template_code=self.template_code,
- orderable=False,
- attrs={'td': {'class': 'text-nowrap'}},
- *args,
- **kwargs
- )
- def value(self, value):
- return value
- class UtilizationColumn(tables.TemplateColumn):
- """
- Display a colored utilization bar graph.
- """
- template_code = """{% load helpers %}{% if record.pk %}{% utilization_graph value %}{% endif %}"""
- def __init__(self, *args, **kwargs):
- super().__init__(template_code=self.template_code, *args, **kwargs)
- def value(self, value):
- return f'{value}%'
- class MarkdownColumn(tables.TemplateColumn):
- """
- Render a Markdown string.
- """
- template_code = """
- {% load helpers %}
- {% if value %}
- {{ value|render_markdown }}
- {% else %}
- —
- {% endif %}
- """
- def __init__(self):
- super().__init__(
- template_code=self.template_code
- )
- def value(self, value):
- return value
|