tables.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import django_tables2 as tables
  2. from django_tables2.utils import Accessor
  3. from utilities.tables import BaseTable, BooleanColumn, ColorColumn, ToggleColumn
  4. from .models import ConfigContext, ObjectChange, Tag, TaggedItem
  5. TAG_ACTIONS = """
  6. <a href="{% url 'extras:tag_changelog' slug=record.slug %}" class="btn btn-default btn-xs" title="Change log">
  7. <i class="fa fa-history"></i>
  8. </a>
  9. {% if perms.taggit.change_tag %}
  10. <a href="{% url 'extras:tag_edit' slug=record.slug %}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
  11. {% endif %}
  12. {% if perms.taggit.delete_tag %}
  13. <a href="{% url 'extras:tag_delete' slug=record.slug %}" class="btn btn-xs btn-danger"><i class="glyphicon glyphicon-trash" aria-hidden="true"></i></a>
  14. {% endif %}
  15. """
  16. TAGGED_ITEM = """
  17. {% if value.get_absolute_url %}
  18. <a href="{{ value.get_absolute_url }}">{{ value }}</a>
  19. {% else %}
  20. {{ value }}
  21. {% endif %}
  22. """
  23. CONFIGCONTEXT_ACTIONS = """
  24. {% if perms.extras.change_configcontext %}
  25. <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>
  26. {% endif %}
  27. {% if perms.extras.delete_configcontext %}
  28. <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>
  29. {% endif %}
  30. """
  31. OBJECTCHANGE_TIME = """
  32. <a href="{{ record.get_absolute_url }}">{{ value|date:"SHORT_DATETIME_FORMAT" }}</a>
  33. """
  34. OBJECTCHANGE_ACTION = """
  35. {% if record.action == 'create' %}
  36. <span class="label label-success">Created</span>
  37. {% elif record.action == 'update' %}
  38. <span class="label label-primary">Updated</span>
  39. {% elif record.action == 'delete' %}
  40. <span class="label label-danger">Deleted</span>
  41. {% endif %}
  42. """
  43. OBJECTCHANGE_OBJECT = """
  44. {% if record.action != 3 and record.changed_object.get_absolute_url %}
  45. <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
  46. {% elif record.action != 3 and record.related_object.get_absolute_url %}
  47. <a href="{{ record.related_object.get_absolute_url }}">{{ record.object_repr }}</a>
  48. {% else %}
  49. {{ record.object_repr }}
  50. {% endif %}
  51. """
  52. OBJECTCHANGE_REQUEST_ID = """
  53. <a href="{% url 'extras:objectchange_list' %}?request_id={{ value }}">{{ value }}</a>
  54. """
  55. class TagTable(BaseTable):
  56. pk = ToggleColumn()
  57. name = tables.LinkColumn(
  58. viewname='extras:tag',
  59. args=[Accessor('slug')]
  60. )
  61. actions = tables.TemplateColumn(
  62. template_code=TAG_ACTIONS,
  63. attrs={'td': {'class': 'text-right noprint'}},
  64. verbose_name=''
  65. )
  66. color = ColorColumn()
  67. class Meta(BaseTable.Meta):
  68. model = Tag
  69. fields = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
  70. class TaggedItemTable(BaseTable):
  71. content_object = tables.TemplateColumn(
  72. template_code=TAGGED_ITEM,
  73. orderable=False,
  74. verbose_name='Object'
  75. )
  76. content_type = tables.Column(
  77. verbose_name='Type'
  78. )
  79. class Meta(BaseTable.Meta):
  80. model = TaggedItem
  81. fields = ('content_object', 'content_type')
  82. class ConfigContextTable(BaseTable):
  83. pk = ToggleColumn()
  84. name = tables.LinkColumn()
  85. is_active = BooleanColumn(
  86. verbose_name='Active'
  87. )
  88. class Meta(BaseTable.Meta):
  89. model = ConfigContext
  90. fields = ('pk', 'name', 'weight', 'is_active', 'description')
  91. class ObjectChangeTable(BaseTable):
  92. time = tables.TemplateColumn(
  93. template_code=OBJECTCHANGE_TIME
  94. )
  95. action = tables.TemplateColumn(
  96. template_code=OBJECTCHANGE_ACTION
  97. )
  98. changed_object_type = tables.Column(
  99. verbose_name='Type'
  100. )
  101. object_repr = tables.TemplateColumn(
  102. template_code=OBJECTCHANGE_OBJECT,
  103. verbose_name='Object'
  104. )
  105. request_id = tables.TemplateColumn(
  106. template_code=OBJECTCHANGE_REQUEST_ID,
  107. verbose_name='Request ID'
  108. )
  109. class Meta(BaseTable.Meta):
  110. model = ObjectChange
  111. fields = ('time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'request_id')