tables.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import django_tables2 as tables
  2. from django.conf import settings
  3. from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ToggleColumn
  4. from .models import ConfigContext, ObjectChange, Tag, TaggedItem
  5. TAGGED_ITEM = """
  6. {% if value.get_absolute_url %}
  7. <a href="{{ value.get_absolute_url }}">{{ value }}</a>
  8. {% else %}
  9. {{ value }}
  10. {% endif %}
  11. """
  12. CONFIGCONTEXT_ACTIONS = """
  13. {% if perms.extras.change_configcontext %}
  14. <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>
  15. {% endif %}
  16. {% if perms.extras.delete_configcontext %}
  17. <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>
  18. {% endif %}
  19. """
  20. OBJECTCHANGE_OBJECT = """
  21. {% if record.changed_object.get_absolute_url %}
  22. <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
  23. {% else %}
  24. {{ record.object_repr }}
  25. {% endif %}
  26. """
  27. OBJECTCHANGE_REQUEST_ID = """
  28. <a href="{% url 'extras:objectchange_list' %}?request_id={{ value }}">{{ value }}</a>
  29. """
  30. class TagTable(BaseTable):
  31. pk = ToggleColumn()
  32. color = ColorColumn()
  33. actions = ButtonsColumn(Tag, pk_field='slug')
  34. class Meta(BaseTable.Meta):
  35. model = Tag
  36. fields = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
  37. class TaggedItemTable(BaseTable):
  38. content_object = tables.TemplateColumn(
  39. template_code=TAGGED_ITEM,
  40. orderable=False,
  41. verbose_name='Object'
  42. )
  43. content_type = tables.Column(
  44. verbose_name='Type'
  45. )
  46. class Meta(BaseTable.Meta):
  47. model = TaggedItem
  48. fields = ('content_object', 'content_type')
  49. class ConfigContextTable(BaseTable):
  50. pk = ToggleColumn()
  51. name = tables.LinkColumn()
  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 = tables.Column(
  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')