tables.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import django_tables2 as tables
  2. from django.utils.safestring import mark_safe
  3. class BaseTable(tables.Table):
  4. def __init__(self, *args, **kwargs):
  5. super(BaseTable, self).__init__(*args, **kwargs)
  6. # Set default empty_text if none was provided
  7. if self.empty_text is None:
  8. self.empty_text = 'No {} found.'.format(self._meta.model._meta.verbose_name_plural)
  9. class Meta:
  10. attrs = {
  11. 'class': 'table table-hover',
  12. }
  13. class ToggleColumn(tables.CheckBoxColumn):
  14. def __init__(self, *args, **kwargs):
  15. default = kwargs.pop('default', '')
  16. visible = kwargs.pop('visible', False)
  17. super(ToggleColumn, self).__init__(*args, default=default, visible=visible, **kwargs)
  18. @property
  19. def header(self):
  20. return mark_safe('<input type="checkbox" name="_all" title="Select all" />')
  21. class ColorColumn(tables.Column):
  22. def render(self, record):
  23. html = '<label class="label {}">{}</label>'.format(record.color, record.get_color_display())
  24. return mark_safe(html)