tables.py 16 KB

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