python/netbox-community/netbox/netbox/extras/tables.py

tables.py
import django_tables2 as tables
from django_tables2.utils import Accessor

from utilities.tables import BaseTable, BooleanColumn, ColorColumn, ToggleColumn
from .models import ConfigContext, ObjectChange, Tag, TaggedItem

TAG_ACTIONS = """

    

{% if perms.taggit.change_tag %}
    
{% endif %}
{% if perms.taggit.delete_tag %}
    
{% endif %}
"""

TAGGED_ITEM = """
{% if value.get_absolute_url %}
    {{ value }}
{% else %}
    {{ value }}
{% endif %}
"""

CONFIGCONTEXT_ACTIONS = """
{% if perms.extras.change_configcontext %}
    
{% endif %}
{% if perms.extras.delete_configcontext %}
    
{% endif %}
"""

OBJECTCHANGE_TIME = """
{{ value|date:"SHORT_DATETIME_FORMAT" }}
"""

OBJECTCHANGE_ACTION = """
{% if record.action == 1 %}
    Created
{% elif record.action == 2 %}
    Updated
{% elif record.action == 3 %}
    Deleted
{% endif %}
"""

OBJECTCHANGE_OBJECT = """
{% if record.action != 3 and record.changed_object.get_absolute_url %}
    {{ record.object_repr }}
{% elif record.action != 3 and record.related_object.get_absolute_url %}
    {{ record.object_repr }}
{% else %}
    {{ record.object_repr }}
{% endif %}
"""

OBJECTCHANGE_REQUEST_ID = """
{{ value }}
"""


clast TagTable(BaseTable):
    pk = ToggleColumn()
    name = tables.LinkColumn(
        viewname='extras:tag',
        args=[Accessor('slug')]
    )
    actions = tables.TemplateColumn(
        template_code=TAG_ACTIONS,
        attrs={'td': {'clast': 'text-right noprint'}},
        verbose_name=''
    )
    color = ColorColumn()

    clast Meta(BaseTable.Meta):
        model = Tag
        fields = ('pk', 'name', 'items', 'slug', 'color', 'actions')


clast TaggedItemTable(BaseTable):
    content_object = tables.TemplateColumn(
        template_code=TAGGED_ITEM,
        orderable=False,
        verbose_name='Object'
    )
    content_type = tables.Column(
        verbose_name='Type'
    )

    clast Meta(BaseTable.Meta):
        model = TaggedItem
        fields = ('content_object', 'content_type')


clast ConfigContextTable(BaseTable):
    pk = ToggleColumn()
    name = tables.LinkColumn()
    is_active = BooleanColumn(
        verbose_name='Active'
    )

    clast Meta(BaseTable.Meta):
        model = ConfigContext
        fields = ('pk', 'name', 'weight', 'is_active', 'description')


clast ObjectChangeTable(BaseTable):
    time = tables.TemplateColumn(
        template_code=OBJECTCHANGE_TIME
    )
    action = tables.TemplateColumn(
        template_code=OBJECTCHANGE_ACTION
    )
    changed_object_type = tables.Column(
        verbose_name='Type'
    )
    object_repr = tables.TemplateColumn(
        template_code=OBJECTCHANGE_OBJECT,
        verbose_name='Object'
    )
    request_id = tables.TemplateColumn(
        template_code=OBJECTCHANGE_REQUEST_ID,
        verbose_name='Request ID'
    )

    clast Meta(BaseTable.Meta):
        model = ObjectChange
        fields = ('time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'request_id')