tables.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import django_tables2 as tables
  2. from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, ColorColumn, ToggleColumn
  3. from .models import ConfigContext, ObjectChange, Tag, TaggedItem
  4. TAGGED_ITEM = """
  5. {% if value.get_absolute_url %}
  6. <a href="{{ value.get_absolute_url }}">{{ value }}</a>
  7. {% else %}
  8. {{ value }}
  9. {% endif %}
  10. """
  11. CONFIGCONTEXT_ACTIONS = """
  12. {% if perms.extras.change_configcontext %}
  13. <a href="{% url 'extras:configcontext_edit' pk=record.pk %}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
  14. {% endif %}
  15. {% if perms.extras.delete_configcontext %}
  16. <a href="{% url 'extras:configcontext_delete' pk=record.pk %}" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash" aria-hidden="true"></i></a>
  17. {% endif %}
  18. """
  19. OBJECTCHANGE_TIME = """
  20. <a href="{{ record.get_absolute_url }}">{{ value|date:"SHORT_DATETIME_FORMAT" }}</a>
  21. """
  22. OBJECTCHANGE_ACTION = """
  23. {% if record.action == 'create' %}
  24. <span class="label label-success">Created</span>
  25. {% elif record.action == 'update' %}
  26. <span class="label label-primary">Updated</span>
  27. {% elif record.action == 'delete' %}
  28. <span class="label label-danger">Deleted</span>
  29. {% endif %}
  30. """
  31. OBJECTCHANGE_OBJECT = """
  32. {% if record.action != 3 and record.changed_object.get_absolute_url %}
  33. <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
  34. {% elif record.action != 3 and record.related_object.get_absolute_url %}
  35. <a href="{{ record.related_object.get_absolute_url }}">{{ record.object_repr }}</a>
  36. {% else %}
  37. {{ record.object_repr }}
  38. {% endif %}
  39. """
  40. OBJECTCHANGE_REQUEST_ID = """
  41. <a href="{% url 'extras:objectchange_list' %}?request_id={{ value }}">{{ value }}</a>
  42. """
  43. class TagTable(BaseTable):
  44. pk = ToggleColumn()
  45. color = ColorColumn()
  46. actions = ButtonsColumn(Tag, pk_field='slug')
  47. class Meta(BaseTable.Meta):
  48. model = Tag
  49. fields = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
  50. class TaggedItemTable(BaseTable):
  51. content_object = tables.TemplateColumn(
  52. template_code=TAGGED_ITEM,
  53. orderable=False,
  54. verbose_name='Object'
  55. )
  56. content_type = tables.Column(
  57. verbose_name='Type'
  58. )
  59. class Meta(BaseTable.Meta):
  60. model = TaggedItem
  61. fields = ('content_object', 'content_type')
  62. class ConfigContextTable(BaseTable):
  63. pk = ToggleColumn()
  64. name = tables.LinkColumn()
  65. is_active = BooleanColumn(
  66. verbose_name='Active'
  67. )
  68. class Meta(BaseTable.Meta):
  69. model = ConfigContext
  70. fields = (
  71. 'pk', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'roles', 'platforms',
  72. 'cluster_groups', 'clusters', 'tenant_groups', 'tenants',
  73. )
  74. default_columns = ('pk', 'name', 'weight', 'is_active', 'description')
  75. class ObjectChangeTable(BaseTable):
  76. time = tables.TemplateColumn(
  77. template_code=OBJECTCHANGE_TIME
  78. )
  79. action = tables.TemplateColumn(
  80. template_code=OBJECTCHANGE_ACTION
  81. )
  82. changed_object_type = tables.Column(
  83. verbose_name='Type'
  84. )
  85. object_repr = tables.TemplateColumn(
  86. template_code=OBJECTCHANGE_OBJECT,
  87. verbose_name='Object'
  88. )
  89. request_id = tables.TemplateColumn(
  90. template_code=OBJECTCHANGE_REQUEST_ID,
  91. verbose_name='Request ID'
  92. )
  93. class Meta(BaseTable.Meta):
  94. model = ObjectChange
  95. fields = ('time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'request_id')