1import django_tables2 as tables
2from django.conf import settings
3
4from utilities.tables import (
5    BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ContentTypeColumn, ContentTypesColumn,
6    MarkdownColumn, ToggleColumn,
7)
8from .models import *
9
10__all__ = (
11    'ConfigContextTable',
12    'CustomFieldTable',
13    'CustomLinkTable',
14    'ExportTemplateTable',
15    'JournalEntryTable',
16    'ObjectChangeTable',
17    'ObjectJournalTable',
18    'TaggedItemTable',
19    'TagTable',
20    'WebhookTable',
21)
22
23CONFIGCONTEXT_ACTIONS = """
24{% if perms.extras.change_configcontext %}
25    <a href="{% url 'extras:configcontext_edit' pk=record.pk %}" class="btn btn-sm btn-warning"><i class="mdi mdi-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-sm btn-danger"><i class="mdi mdi-trash-can-outline" aria-hidden="true"></i></a>
29{% endif %}
30"""
31
32OBJECTCHANGE_OBJECT = """
33{% if record.changed_object.get_absolute_url %}
34    <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
35{% else %}
36    {{ record.object_repr }}
37{% endif %}
38"""
39
40OBJECTCHANGE_REQUEST_ID = """
41<a href="{% url 'extras:objectchange_list' %}?request_id={{ value }}">{{ value }}</a>
42"""
43
44
45#
46# Custom fields
47#
48
49class CustomFieldTable(BaseTable):
50    pk = ToggleColumn()
51    name = tables.Column(
52        linkify=True
53    )
54    content_types = ContentTypesColumn()
55    required = BooleanColumn()
56
57    class Meta(BaseTable.Meta):
58        model = CustomField
59        fields = (
60            'pk', 'id', 'name', 'content_types', 'label', 'type', 'required', 'weight', 'default',
61            'description', 'filter_logic', 'choices',
62        )
63        default_columns = ('pk', 'name', 'content_types', 'label', 'type', 'required', 'description')
64
65
66#
67# Custom links
68#
69
70class CustomLinkTable(BaseTable):
71    pk = ToggleColumn()
72    name = tables.Column(
73        linkify=True
74    )
75    content_type = ContentTypeColumn()
76    new_window = BooleanColumn()
77
78    class Meta(BaseTable.Meta):
79        model = CustomLink
80        fields = (
81            'pk', 'id', 'name', 'content_type', 'link_text', 'link_url', 'weight', 'group_name',
82            'button_class', 'new_window',
83        )
84        default_columns = ('pk', 'name', 'content_type', 'group_name', 'button_class', 'new_window')
85
86
87#
88# Export templates
89#
90
91class ExportTemplateTable(BaseTable):
92    pk = ToggleColumn()
93    name = tables.Column(
94        linkify=True
95    )
96    content_type = ContentTypeColumn()
97    as_attachment = BooleanColumn()
98
99    class Meta(BaseTable.Meta):
100        model = ExportTemplate
101        fields = (
102            'pk', 'id', 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
103        )
104        default_columns = (
105            'pk', 'name', 'content_type', 'description', 'mime_type', 'file_extension', 'as_attachment',
106        )
107
108
109#
110# Webhooks
111#
112
113class WebhookTable(BaseTable):
114    pk = ToggleColumn()
115    name = tables.Column(
116        linkify=True
117    )
118    content_types = ContentTypesColumn()
119    enabled = BooleanColumn()
120    type_create = BooleanColumn(
121        verbose_name='Create'
122    )
123    type_update = BooleanColumn(
124        verbose_name='Update'
125    )
126    type_delete = BooleanColumn(
127        verbose_name='Delete'
128    )
129    ssl_validation = BooleanColumn(
130        verbose_name='SSL Validation'
131    )
132
133    class Meta(BaseTable.Meta):
134        model = Webhook
135        fields = (
136            'pk', 'id', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
137            'payload_url', 'secret', 'ssl_validation', 'ca_file_path',
138        )
139        default_columns = (
140            'pk', 'name', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'http_method',
141            'payload_url',
142        )
143
144
145#
146# Tags
147#
148
149class TagTable(BaseTable):
150    pk = ToggleColumn()
151    name = tables.Column(
152        linkify=True
153    )
154    color = ColorColumn()
155    actions = ButtonsColumn(Tag)
156
157    class Meta(BaseTable.Meta):
158        model = Tag
159        fields = ('pk', 'id', 'name', 'items', 'slug', 'color', 'description', 'actions')
160        default_columns = ('pk', 'name', 'items', 'slug', 'color', 'description', 'actions')
161
162
163class TaggedItemTable(BaseTable):
164    id = tables.Column(
165        verbose_name='ID',
166        linkify=lambda record: record.content_object.get_absolute_url(),
167        accessor='content_object__id'
168    )
169    content_type = ContentTypeColumn(
170        verbose_name='Type'
171    )
172    content_object = tables.Column(
173        linkify=True,
174        orderable=False,
175        verbose_name='Object'
176    )
177
178    class Meta(BaseTable.Meta):
179        model = TaggedItem
180        fields = ('id', 'content_type', 'content_object')
181
182
183class ConfigContextTable(BaseTable):
184    pk = ToggleColumn()
185    name = tables.Column(
186        linkify=True
187    )
188    is_active = BooleanColumn(
189        verbose_name='Active'
190    )
191
192    class Meta(BaseTable.Meta):
193        model = ConfigContext
194        fields = (
195            'pk', 'id', 'name', 'weight', 'is_active', 'description', 'regions', 'sites', 'roles',
196            'platforms', 'cluster_groups', 'clusters', 'tenant_groups', 'tenants',
197        )
198        default_columns = ('pk', 'name', 'weight', 'is_active', 'description')
199
200
201class ObjectChangeTable(BaseTable):
202    time = tables.DateTimeColumn(
203        linkify=True,
204        format=settings.SHORT_DATETIME_FORMAT
205    )
206    action = ChoiceFieldColumn()
207    changed_object_type = ContentTypeColumn(
208        verbose_name='Type'
209    )
210    object_repr = tables.TemplateColumn(
211        template_code=OBJECTCHANGE_OBJECT,
212        verbose_name='Object'
213    )
214    request_id = tables.TemplateColumn(
215        template_code=OBJECTCHANGE_REQUEST_ID,
216        verbose_name='Request ID'
217    )
218
219    class Meta(BaseTable.Meta):
220        model = ObjectChange
221        fields = ('id', 'time', 'user_name', 'action', 'changed_object_type', 'object_repr', 'request_id')
222
223
224class ObjectJournalTable(BaseTable):
225    """
226    Used for displaying a set of JournalEntries within the context of a single object.
227    """
228    created = tables.DateTimeColumn(
229        linkify=True,
230        format=settings.SHORT_DATETIME_FORMAT
231    )
232    kind = ChoiceFieldColumn()
233    comments = tables.TemplateColumn(
234        template_code='{% load helpers %}{{ value|render_markdown|truncatewords_html:50 }}'
235    )
236    actions = ButtonsColumn(
237        model=JournalEntry
238    )
239
240    class Meta(BaseTable.Meta):
241        model = JournalEntry
242        fields = ('id', 'created', 'created_by', 'kind', 'comments', 'actions')
243
244
245class JournalEntryTable(ObjectJournalTable):
246    pk = ToggleColumn()
247    assigned_object_type = ContentTypeColumn(
248        verbose_name='Object type'
249    )
250    assigned_object = tables.Column(
251        linkify=True,
252        orderable=False,
253        verbose_name='Object'
254    )
255    comments = MarkdownColumn()
256
257    class Meta(BaseTable.Meta):
258        model = JournalEntry
259        fields = (
260            'pk', 'id', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind',
261            'comments', 'actions'
262        )
263        default_columns = (
264            'pk', 'created', 'created_by', 'assigned_object_type', 'assigned_object', 'kind',
265            'comments', 'actions'
266        )
267