import django_tables2 as tables
__all__ = (
'TenantColumn',
'TenantGroupColumn',
'TenancyColumnsMixin',
)
class TenantColumn(tables.TemplateColumn):
"""
Include the tenant description.
"""
template_code = """
{% if record.tenant %}
{{ record.tenant }}
{% elif record.vrf.tenant %}
{{ record.vrf.tenant }}*
{% else %}
—
{% endif %}
"""
def __init__(self, *args, **kwargs):
super().__init__(template_code=self.template_code, *args, **kwargs)
def value(self, value):
return str(value) if value else None
class TenantGroupColumn(tables.TemplateColumn):
"""
Include the tenant group description.
"""
template_code = """
{% if record.tenant and record.tenant.group %}
{{ record.tenant.group }}
{% elif record.vrf.tenant and record.vrf.tenant.group %}
{{ record.vrf.tenant.group }}*
{% else %}
—
{% endif %}
"""
def __init__(self, accessor=tables.A('tenant__group'), *args, **kwargs):
if 'verbose_name' not in kwargs:
kwargs['verbose_name'] = 'Tenant Group'
super().__init__(template_code=self.template_code, accessor=accessor, *args, **kwargs)
def value(self, value):
return str(value) if value else None
class TenancyColumnsMixin(tables.Table):
tenant_group = TenantGroupColumn()
tenant = TenantColumn()