tables.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import django_tables2 as tables
  2. from utilities.tables import BaseTable, ButtonsColumn, LinkedCountColumn, MPTTColumn, TagColumn, ToggleColumn
  3. from .models import Tenant, TenantGroup
  4. #
  5. # Table columns
  6. #
  7. class TenantColumn(tables.TemplateColumn):
  8. """
  9. Include the tenant description.
  10. """
  11. template_code = """
  12. {% if record.tenant %}
  13. <a href="{{ record.tenant.get_absolute_url }}" title="{{ record.tenant.description }}">{{ record.tenant }}</a>
  14. {% elif record.vrf.tenant %}
  15. <a href="{{ record.vrf.tenant.get_absolute_url }}" title="{{ record.vrf.tenant.description }}">{{ record.vrf.tenant }}</a>*
  16. {% else %}
  17. &mdash;
  18. {% endif %}
  19. """
  20. def __init__(self, *args, **kwargs):
  21. super().__init__(template_code=self.template_code, *args, **kwargs)
  22. def value(self, value):
  23. return str(value) if value else None
  24. #
  25. # Tenant groups
  26. #
  27. class TenantGroupTable(BaseTable):
  28. pk = ToggleColumn()
  29. name = MPTTColumn(
  30. linkify=True
  31. )
  32. tenant_count = LinkedCountColumn(
  33. viewname='tenancy:tenant_list',
  34. url_params={'group_id': 'pk'},
  35. verbose_name='Tenants'
  36. )
  37. actions = ButtonsColumn(TenantGroup)
  38. class Meta(BaseTable.Meta):
  39. model = TenantGroup
  40. fields = ('pk', 'name', 'tenant_count', 'description', 'slug', 'actions')
  41. default_columns = ('pk', 'name', 'tenant_count', 'description', 'actions')
  42. #
  43. # Tenants
  44. #
  45. class TenantTable(BaseTable):
  46. pk = ToggleColumn()
  47. name = tables.Column(
  48. linkify=True
  49. )
  50. tags = TagColumn(
  51. url_name='tenancy:tenant_list'
  52. )
  53. class Meta(BaseTable.Meta):
  54. model = Tenant
  55. fields = ('pk', 'name', 'slug', 'group', 'description', 'tags')
  56. default_columns = ('pk', 'name', 'group', 'description')