tables.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import django_tables2 as tables
  2. from django.conf import settings
  3. from utilities.tables import (
  4. BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ContentTypeColumn, ToggleColumn,
  5. )
  6. from .models import ConfigContext, JournalEntry, ObjectChange, Tag, TaggedItem
  7. CONFIGCONTEXT_ACTIONS = """
  8. {% if perms.extras.change_configcontext %}
  9. <a href="{% url 'extras:configcontext_edit' pk=record.pk %}" class="btn btn-xs btn-warning"><i class="mdi mdi-pencil" aria-hidden="true"></i></a>
  10. {% endif %}
  11. {% if perms.extras.delete_configcontext %}
  12. <a href="{% url 'extras:configcontext_delete' pk=record.pk %}" class="btn btn-xs btn-danger"><i class="mdi mdi-trash-can-outline" aria-hidden="true"></i></a>
  13. {% endif %}
  14. """
  15. OBJECTCHANGE_OBJECT = """
  16. {% if record.changed_object.get_absolute_url %}
  17. <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
  18. {% else %}
  19. {{ record.object_repr }}
  20. {% endif %}
  21. """
  22. OBJECTCHANGE_REQUEST_ID = """
  23. <a href="{% url 'extras:objectchange_list' %}?request_id={{ value }}">{{ value }}</a>
  24. """
  25. class TagTable(BaseTable):
  26. pk = ToggleColumn()
  27. name = tables.Column(
  28. linkify=True
  29. )
  30. color = ColorColumn()
  31. actions = ButtonsColumn(Tag)
  32. class Meta(BaseTable.Meta):
  33. model = Tag
  34. fields = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
  35. class TaggedItemTable(BaseTable):
  36. content_type = ContentTypeColumn(
  37. verbose_name='Type'
  38. )
  39. content_object = tables.Column(
  40. linkify=True,
  41. orderable=False,
  42. verbose_name='Object'
  43. )
  44. class Meta(BaseTable.Meta):
  45. model = TaggedItem
  46. fields = ('content_type', 'content_object')
  47. class ConfigContextTable(BaseTable):
  48. pk = ToggleColumn()
  49. name = tables.Column(
  50. linkify=True
  51. )
  52. is_active = BooleanColumn(
  53. verbose_name='Active'
  54. )
  55. class Meta(BaseTable.Meta):
  56. model = ConfigContext
  57. fields = (
  58. 'pk', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'roles', 'platforms',
  59. 'cluster_groups', 'clusters', 'tenant_groups', 'tenants',
  60. )
  61. default_columns = ('pk', 'name', 'weight', 'is_active', 'description')
  62. class ObjectChangeTable(BaseTable):
  63. time = tables.DateTimeColumn(
  64. linkify=True,
  65. format=settings.SHORT_DATETIME_FORMAT
  66. )
  67. action = ChoiceFieldColumn()
  68. changed_object_type = ContentTypeColumn(
  69. verbose_name='Type'
  70. )
  71. object_repr = tables.TemplateColumn(
  72. template_code=OBJECTCHANGE_OBJECT,
  73. verbose_name='Object'
  74. )
  75. request_id = tables.TemplateColumn(
  76. template_code=OBJECTCHANGE_REQUEST_ID,
  77. verbose_name='Request ID'
  78. )
  79. class Meta(BaseTable.Meta):
  80. model = ObjectChange
  81. fields = ('time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'request_id')
  82. class ObjectJournalTable(BaseTable):
  83. """
  84. Used for displaying a set of JournalEntries within the context of a single object.
  85. """
  86. created = tables.DateTimeColumn(
  87. linkify=True,
  88. format=settings.SHORT_DATETIME_FORMAT
  89. )
  90. kind = ChoiceFieldColumn()
  91. comments = tables.TemplateColumn(
  92. template_code='{% load helpers %}{{ value|render_markdown|truncatewords_html:50 }}'
  93. )
  94. actions = ButtonsColumn(
  95. model=JournalEntry
  96. )
  97. class Meta(BaseTable.Meta):
  98. model = JournalEntry
  99. fields = ('created', 'created_by', 'kind', 'comments', 'actions')
  100. class JournalEntryTable(ObjectJournalTable):
  101. pk = ToggleColumn()
  102. assigned_object_type = ContentTypeColumn(
  103. verbose_name='Object type'
  104. )
  105. assigned_object = tables.Column(
  106. linkify=True,
  107. orderable=False,
  108. verbose_name='Object'
  109. )
  110. class Meta(BaseTable.Meta):
  111. model = JournalEntry
  112. fields = (
  113. 'pk', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind', 'comments', 'actions'
  114. )