tables.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. import django_tables2 as tables
  2. from django.conf import settings
  3. from django.contrib.auth.models import AnonymousUser
  4. from django.contrib.contenttypes.fields import GenericForeignKey
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.core.exceptions import FieldDoesNotExist
  7. from django.db.models.fields.related import RelatedField
  8. from django.urls import reverse
  9. from django.utils.safestring import mark_safe
  10. from django_tables2 import RequestConfig
  11. from django_tables2.data import TableQuerysetData
  12. from django_tables2.utils import Accessor
  13. from extras.choices import CustomFieldTypeChoices
  14. from extras.models import CustomField, CustomLink
  15. from .utils import content_type_identifier, content_type_name
  16. from .paginator import EnhancedPaginator, get_paginate_count
  17. class BaseTable(tables.Table):
  18. """
  19. Default table for object lists
  20. :param user: Personalize table display for the given user (optional). Has no effect if AnonymousUser is passed.
  21. """
  22. id = tables.Column(
  23. linkify=True,
  24. verbose_name='ID'
  25. )
  26. class Meta:
  27. attrs = {
  28. 'class': 'table table-hover object-list',
  29. }
  30. def __init__(self, *args, user=None, extra_columns=None, **kwargs):
  31. if extra_columns is None:
  32. extra_columns = []
  33. # Add custom field columns
  34. obj_type = ContentType.objects.get_for_model(self._meta.model)
  35. cf_columns = [
  36. (f'cf_{cf.name}', CustomFieldColumn(cf)) for cf in CustomField.objects.filter(content_types=obj_type)
  37. ]
  38. cl_columns = [
  39. (f'cl_{cl.name}', CustomLinkColumn(cl)) for cl in CustomLink.objects.filter(content_type=obj_type)
  40. ]
  41. extra_columns.extend([*cf_columns, *cl_columns])
  42. super().__init__(*args, extra_columns=extra_columns, **kwargs)
  43. # Set default empty_text if none was provided
  44. if self.empty_text is None:
  45. self.empty_text = f"No {self._meta.model._meta.verbose_name_plural} found"
  46. # Hide non-default columns
  47. default_columns = getattr(self.Meta, 'default_columns', list())
  48. if default_columns:
  49. for column in self.columns:
  50. if column.name not in default_columns:
  51. self.columns.hide(column.name)
  52. # Apply custom column ordering for user
  53. if user is not None and not isinstance(user, AnonymousUser):
  54. selected_columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
  55. if selected_columns:
  56. # Show only persistent or selected columns
  57. for name, column in self.columns.items():
  58. if name in ['pk', 'actions', *selected_columns]:
  59. self.columns.show(name)
  60. else:
  61. self.columns.hide(name)
  62. # Rearrange the sequence to list selected columns first, followed by all remaining columns
  63. # TODO: There's probably a more clever way to accomplish this
  64. self.sequence = [
  65. *[c for c in selected_columns if c in self.columns.names()],
  66. *[c for c in self.columns.names() if c not in selected_columns]
  67. ]
  68. # PK column should always come first
  69. if 'pk' in self.sequence:
  70. self.sequence.remove('pk')
  71. self.sequence.insert(0, 'pk')
  72. # Actions column should always come last
  73. if 'actions' in self.sequence:
  74. self.sequence.remove('actions')
  75. self.sequence.append('actions')
  76. # Dynamically update the table's QuerySet to ensure related fields are pre-fetched
  77. if isinstance(self.data, TableQuerysetData):
  78. prefetch_fields = []
  79. for column in self.columns:
  80. if column.visible:
  81. model = getattr(self.Meta, 'model')
  82. accessor = column.accessor
  83. prefetch_path = []
  84. for field_name in accessor.split(accessor.SEPARATOR):
  85. try:
  86. field = model._meta.get_field(field_name)
  87. except FieldDoesNotExist:
  88. break
  89. if isinstance(field, RelatedField):
  90. # Follow ForeignKeys to the related model
  91. prefetch_path.append(field_name)
  92. model = field.remote_field.model
  93. elif isinstance(field, GenericForeignKey):
  94. # Can't prefetch beyond a GenericForeignKey
  95. prefetch_path.append(field_name)
  96. break
  97. if prefetch_path:
  98. prefetch_fields.append('__'.join(prefetch_path))
  99. self.data.data = self.data.data.prefetch_related(None).prefetch_related(*prefetch_fields)
  100. def _get_columns(self, visible=True):
  101. columns = []
  102. for name, column in self.columns.items():
  103. if column.visible == visible and name not in ['pk', 'actions']:
  104. columns.append((name, column.verbose_name))
  105. return columns
  106. @property
  107. def available_columns(self):
  108. return self._get_columns(visible=False)
  109. @property
  110. def selected_columns(self):
  111. return self._get_columns(visible=True)
  112. @property
  113. def objects_count(self):
  114. """
  115. Return the total number of real objects represented by the Table. This is useful when dealing with
  116. prefixes/IP addresses/etc., where some table rows may represent available address space.
  117. """
  118. if not hasattr(self, '_objects_count'):
  119. self._objects_count = sum(1 for obj in self.data if hasattr(obj, 'pk'))
  120. return self._objects_count
  121. #
  122. # Table columns
  123. #
  124. class ToggleColumn(tables.CheckBoxColumn):
  125. """
  126. Extend CheckBoxColumn to add a "toggle all" checkbox in the column header.
  127. """
  128. def __init__(self, *args, **kwargs):
  129. default = kwargs.pop('default', '')
  130. visible = kwargs.pop('visible', False)
  131. if 'attrs' not in kwargs:
  132. kwargs['attrs'] = {
  133. 'td': {
  134. 'class': 'min-width',
  135. },
  136. 'input': {
  137. 'class': 'form-check-input'
  138. }
  139. }
  140. super().__init__(*args, default=default, visible=visible, **kwargs)
  141. @property
  142. def header(self):
  143. return mark_safe('<input type="checkbox" class="toggle form-check-input" title="Toggle All" />')
  144. class BooleanColumn(tables.Column):
  145. """
  146. Custom implementation of BooleanColumn to render a nicely-formatted checkmark or X icon instead of a Unicode
  147. character.
  148. """
  149. def render(self, value):
  150. if value:
  151. rendered = '<span class="text-success"><i class="mdi mdi-check-bold"></i></span>'
  152. elif value is None:
  153. rendered = '<span class="text-muted">&mdash;</span>'
  154. else:
  155. rendered = '<span class="text-danger"><i class="mdi mdi-close-thick"></i></span>'
  156. return mark_safe(rendered)
  157. def value(self, value):
  158. return str(value)
  159. class TemplateColumn(tables.TemplateColumn):
  160. """
  161. Overrides the stock TemplateColumn to render a placeholder if the returned value is an empty string.
  162. """
  163. PLACEHOLDER = mark_safe('&mdash;')
  164. def render(self, *args, **kwargs):
  165. ret = super().render(*args, **kwargs)
  166. if not ret.strip():
  167. return self.PLACEHOLDER
  168. return ret
  169. def value(self, **kwargs):
  170. ret = super().value(**kwargs)
  171. if ret == self.PLACEHOLDER:
  172. return ''
  173. return ret
  174. class ButtonsColumn(tables.TemplateColumn):
  175. """
  176. Render edit, delete, and changelog buttons for an object.
  177. :param model: Model class to use for calculating URL view names
  178. :param prepend_content: Additional template content to render in the column (optional)
  179. """
  180. buttons = ('changelog', 'edit', 'delete')
  181. attrs = {'td': {'class': 'text-end text-nowrap noprint'}}
  182. # Note that braces are escaped to allow for string formatting prior to template rendering
  183. template_code = """
  184. {{% if "changelog" in buttons %}}
  185. <a href="{{% url '{app_label}:{model_name}_changelog' pk=record.pk %}}" class="btn btn-outline-dark btn-sm" title="Change log">
  186. <i class="mdi mdi-history"></i>
  187. </a>
  188. {{% endif %}}
  189. {{% if "edit" in buttons and perms.{app_label}.change_{model_name} %}}
  190. <a href="{{% url '{app_label}:{model_name}_edit' pk=record.pk %}}?return_url={{{{ request.path }}}}" class="btn btn-sm btn-warning" title="Edit">
  191. <i class="mdi mdi-pencil"></i>
  192. </a>
  193. {{% endif %}}
  194. {{% if "delete" in buttons and perms.{app_label}.delete_{model_name} %}}
  195. <a href="{{% url '{app_label}:{model_name}_delete' pk=record.pk %}}?return_url={{{{ request.path }}}}" class="btn btn-sm btn-danger" title="Delete">
  196. <i class="mdi mdi-trash-can-outline"></i>
  197. </a>
  198. {{% endif %}}
  199. """
  200. def __init__(self, model, *args, buttons=None, prepend_template=None, **kwargs):
  201. if prepend_template:
  202. prepend_template = prepend_template.replace('{', '{{')
  203. prepend_template = prepend_template.replace('}', '}}')
  204. self.template_code = prepend_template + self.template_code
  205. template_code = self.template_code.format(
  206. app_label=model._meta.app_label,
  207. model_name=model._meta.model_name,
  208. buttons=buttons
  209. )
  210. super().__init__(template_code=template_code, *args, **kwargs)
  211. # Exclude from export by default
  212. if 'exclude_from_export' not in kwargs:
  213. self.exclude_from_export = True
  214. self.extra_context.update({
  215. 'buttons': buttons or self.buttons,
  216. })
  217. def header(self):
  218. return ''
  219. class ChoiceFieldColumn(tables.Column):
  220. """
  221. Render a ChoiceField value inside a <span> indicating a particular CSS class. This is useful for displaying colored
  222. choices. The CSS class is derived by calling .get_FOO_class() on the row record.
  223. """
  224. def render(self, record, bound_column, value):
  225. if value:
  226. name = bound_column.name
  227. css_class = getattr(record, f'get_{name}_class')()
  228. label = getattr(record, f'get_{name}_display')()
  229. return mark_safe(
  230. f'<span class="badge bg-{css_class}">{label}</span>'
  231. )
  232. return self.default
  233. def value(self, value):
  234. return value
  235. class ContentTypeColumn(tables.Column):
  236. """
  237. Display a ContentType instance.
  238. """
  239. def render(self, value):
  240. if value is None:
  241. return None
  242. return content_type_name(value)
  243. def value(self, value):
  244. if value is None:
  245. return None
  246. return content_type_identifier(value)
  247. class ContentTypesColumn(tables.ManyToManyColumn):
  248. """
  249. Display a list of ContentType instances.
  250. """
  251. def __init__(self, separator=None, *args, **kwargs):
  252. # Use a line break as the default separator
  253. if separator is None:
  254. separator = mark_safe('<br />')
  255. super().__init__(separator=separator, *args, **kwargs)
  256. def transform(self, obj):
  257. return content_type_name(obj)
  258. def value(self, value):
  259. return ','.join([
  260. content_type_identifier(ct) for ct in self.filter(value)
  261. ])
  262. class ColorColumn(tables.Column):
  263. """
  264. Display a color (#RRGGBB).
  265. """
  266. def render(self, value):
  267. return mark_safe(
  268. f'<span class="color-label" style="background-color: #{value}">&nbsp;</span>'
  269. )
  270. def value(self, value):
  271. return f'#{value}'
  272. class ColoredLabelColumn(tables.TemplateColumn):
  273. """
  274. Render a colored label (e.g. for DeviceRoles).
  275. """
  276. template_code = """
  277. {% load helpers %}
  278. {% if value %}
  279. <span class="badge" style="color: {{ value.color|fgcolor }}; background-color: #{{ value.color }}">
  280. {{ value }}
  281. </span>
  282. {% else %}
  283. &mdash;
  284. {% endif %}
  285. """
  286. def __init__(self, *args, **kwargs):
  287. super().__init__(template_code=self.template_code, *args, **kwargs)
  288. def value(self, value):
  289. return str(value)
  290. class LinkedCountColumn(tables.Column):
  291. """
  292. Render a count of related objects linked to a filtered URL.
  293. :param viewname: The view name to use for URL resolution
  294. :param view_kwargs: Additional kwargs to pass for URL resolution (optional)
  295. :param url_params: A dict of query parameters to append to the URL (e.g. ?foo=bar) (optional)
  296. """
  297. def __init__(self, viewname, *args, view_kwargs=None, url_params=None, default=0, **kwargs):
  298. self.viewname = viewname
  299. self.view_kwargs = view_kwargs or {}
  300. self.url_params = url_params
  301. super().__init__(*args, default=default, **kwargs)
  302. def render(self, record, value):
  303. if value:
  304. url = reverse(self.viewname, kwargs=self.view_kwargs)
  305. if self.url_params:
  306. url += '?' + '&'.join([
  307. f'{k}={getattr(record, v) or settings.FILTERS_NULL_CHOICE_VALUE}'
  308. for k, v in self.url_params.items()
  309. ])
  310. return mark_safe(f'<a href="{url}">{value}</a>')
  311. return value
  312. def value(self, value):
  313. return value
  314. class TagColumn(tables.TemplateColumn):
  315. """
  316. Display a list of tags assigned to the object.
  317. """
  318. template_code = """
  319. {% for tag in value.all %}
  320. {% include 'utilities/templatetags/tag.html' %}
  321. {% empty %}
  322. <span class="text-muted">&mdash;</span>
  323. {% endfor %}
  324. """
  325. def __init__(self, url_name=None):
  326. super().__init__(
  327. template_code=self.template_code,
  328. extra_context={'url_name': url_name}
  329. )
  330. def value(self, value):
  331. return ",".join([tag.name for tag in value.all()])
  332. class CustomFieldColumn(tables.Column):
  333. """
  334. Display custom fields in the appropriate format.
  335. """
  336. def __init__(self, customfield, *args, **kwargs):
  337. self.customfield = customfield
  338. kwargs['accessor'] = Accessor(f'custom_field_data__{customfield.name}')
  339. if 'verbose_name' not in kwargs:
  340. kwargs['verbose_name'] = customfield.label or customfield.name
  341. super().__init__(*args, **kwargs)
  342. def render(self, value):
  343. if isinstance(value, list):
  344. return ', '.join(v for v in value)
  345. elif self.customfield.type == CustomFieldTypeChoices.TYPE_URL:
  346. # Linkify custom URLs
  347. return mark_safe(f'<a href="{value}">{value}</a>')
  348. if value is not None:
  349. return value
  350. return self.default
  351. class CustomLinkColumn(tables.Column):
  352. """
  353. Render a custom links as a table column.
  354. """
  355. def __init__(self, customlink, *args, **kwargs):
  356. self.customlink = customlink
  357. kwargs['accessor'] = Accessor('pk')
  358. if 'verbose_name' not in kwargs:
  359. kwargs['verbose_name'] = customlink.name
  360. super().__init__(*args, **kwargs)
  361. def render(self, record):
  362. try:
  363. rendered = self.customlink.render({'obj': record})
  364. if rendered:
  365. return mark_safe(f'<a href="{rendered["link"]}"{rendered["link_target"]}>{rendered["text"]}</a>')
  366. except Exception as e:
  367. return mark_safe(f'<span class="text-danger" title="{e}"><i class="mdi mdi-alert"></i> Error</span>')
  368. return ''
  369. def value(self, record):
  370. try:
  371. rendered = self.customlink.render({'obj': record})
  372. if rendered:
  373. return rendered['link']
  374. except Exception:
  375. pass
  376. return None
  377. class MPTTColumn(tables.TemplateColumn):
  378. """
  379. Display a nested hierarchy for MPTT-enabled models.
  380. """
  381. template_code = """
  382. {% load helpers %}
  383. {% for i in record.level|as_range %}<i class="mdi mdi-circle-small"></i>{% endfor %}
  384. <a href="{{ record.get_absolute_url }}">{{ record.name }}</a>
  385. """
  386. def __init__(self, *args, **kwargs):
  387. super().__init__(
  388. template_code=self.template_code,
  389. orderable=False,
  390. attrs={'td': {'class': 'text-nowrap'}},
  391. *args,
  392. **kwargs
  393. )
  394. def value(self, value):
  395. return value
  396. class UtilizationColumn(tables.TemplateColumn):
  397. """
  398. Display a colored utilization bar graph.
  399. """
  400. template_code = """{% load helpers %}{% if record.pk %}{% utilization_graph value %}{% endif %}"""
  401. def __init__(self, *args, **kwargs):
  402. super().__init__(template_code=self.template_code, *args, **kwargs)
  403. def value(self, value):
  404. return f'{value}%'
  405. class MarkdownColumn(tables.TemplateColumn):
  406. """
  407. Render a Markdown string.
  408. """
  409. template_code = """
  410. {% load helpers %}
  411. {% if value %}
  412. {{ value|render_markdown }}
  413. {% else %}
  414. &mdash;
  415. {% endif %}
  416. """
  417. def __init__(self):
  418. super().__init__(
  419. template_code=self.template_code
  420. )
  421. def value(self, value):
  422. return value
  423. #
  424. # Pagination
  425. #
  426. def paginate_table(table, request):
  427. """
  428. Paginate a table given a request context.
  429. """
  430. paginate = {
  431. 'paginator_class': EnhancedPaginator,
  432. 'per_page': get_paginate_count(request)
  433. }
  434. RequestConfig(request, paginate).configure(table)
  435. #
  436. # Callables
  437. #
  438. def linkify_email(value):
  439. if value is None:
  440. return None
  441. return f"mailto:{value}"
  442. def linkify_phone(value):
  443. if value is None:
  444. return None
  445. return f"tel:{value}"