Debian Codegraph @

adjutant_ui/content/tasks/tables.py

raw
1 # Copyright (c) 2016 Catalyst IT Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from django.utils.translation import gettext_lazy as _
16 from django.utils.translation import ngettext_lazy
17
18 from horizon import exceptions
19 from horizon import tables
20
21 from adjutant_ui.api import adjutant
22
23
24 class CancelTask(tables.DeleteAction):
25 help_text = _("This will cancel all selected tasks.")
26
27 @staticmethod
28 def action_present(count):
29 return ngettext_lazy(
30 u"Cancel Task",
31 u"Cancel Tasks",
32 count
33 )
34
35 @staticmethod
36 def action_past(count):
37 return ngettext_lazy(
38 u"Cancelled Task",
39 u"Cancelled Tasks",
40 count
41 )
42
43 def delete(self, request, obj_id):
44 result = adjutant.task_cancel(request, obj_id)
45 if not result or result.status_code not in [200, 202]:
46 exception = exceptions.NotAvailable()
47 exception._safe_message = False
48 raise exception
49
50 def allowed(self, request, task=None):
51 if task:
52 return not(
53 task.status == "Completed" or task.status == "Cancelled")
54 return True
55
56
57 class ApproveTask(tables.BatchAction):
58 name = "approve"
59 help_text = _("This will approve all of the selected tasks.")
60 action_type = "danger"
61
62 @staticmethod
63 def action_present(count):
64 return ngettext_lazy(
65 u"Approve Task",
66 u"Approve Tasks",
67 count
68 )
69
70 @staticmethod
71 def action_past(count):
72 return ngettext_lazy(
73 u"Approved Task",
74 u"Approved Tasks",
75 count
76 )
77
78 def action(self, request, obj_id):
79 result = adjutant.task_approve(request, obj_id)
80 if not result or result.status_code not in [200, 202]:
81 exception = exceptions.NotAvailable()
82 exception._safe_message = False
83 raise exception
84
85 def allowed(self, request, task=None):
86 if task:
87 return task.valid and not(
88 task.status == "Completed" or task.status == "Cancelled")
89 return True
90
91
92 class ReissueToken(tables.BatchAction):
93 name = "reissue"
94 help_text = _("This will reissue tokens for the selected tasks.")
95
96 @staticmethod
97 def action_present(count):
98 return ngettext_lazy(
99 u"Reissue Token",
100 u"Reissue Tokens",
101 count
102 )
103
104 @staticmethod
105 def action_past(count):
106 return ngettext_lazy(
107 u"Reissued Token",
108 u"Reissued Tokens",
109 count
110 )
111
112 def action(self, request, obj_id):
113 result = adjutant.token_reissue(request, obj_id)
114 if not result or result.status_code not in [200, 202]:
115 exception = exceptions.NotAvailable()
116 exception._safe_message = False
117 raise exception
118
119 def allowed(self, request, task=None):
120 if task:
121 return task.status == "Approved; Incomplete"
122 return True
123
124
125 class RevalidateTask(tables.BatchAction):
126 name = "revalidate"
127 help_text = _("Rerun initial validation for the task.")
128
129 @staticmethod
130 def action_present(count):
131 return ngettext_lazy(
132 u"Rerun Validation",
133 u"Rerun Validation",
134 count
135 )
136
137 @staticmethod
138 def action_past(count):
139 return ngettext_lazy(
140 u"Validation run",
141 u"Validation run",
142 count
143 )
144
145 def action(self, request, obj_id):
146 result = adjutant.task_revalidate(request, obj_id)
147 if not result or result.status_code not in [200, 202]:
148 exception = exceptions.NotAvailable()
149 exception._safe_message = False
150 raise exception
151
152 def allowed(self, request, task=None):
153 if task:
154 return task.status == 'Awaiting Approval' and not task.valid
155 return True
156
157
158 class ReapproveTask(ApproveTask):
159 name = "approve"
160 help_text = _("This will approve all of the selected tasks.")
161 action_type = "danger"
162
163 @staticmethod
164 def action_present(count):
165 return ngettext_lazy(
166 u"Reapprove Task",
167 u"Reapprove Tasks",
168 count
169 )
170
171 @staticmethod
172 def action_past(count):
173 return ngettext_lazy(
174 u"Reapproved Task",
175 u"Reapproved Tasks",
176 count
177 )
178
179
180 class UpdateTask(tables.LinkAction):
181 name = "update"
182 verbose_name = _("Update Task")
183 url = "horizon:management:tasks:update"
184 classes = ("ajax-modal",)
185
186 def allowed(self, request, task=None):
187 if task:
188 return task.status == 'Awaiting Approval'
189 return True
190
191
192 def TaskTypeDisplayFilter(task_type):
193 return task_type.replace("_", " ").title()
194
195
196 class TaskTable(tables.DataTable):
197 uuid = tables.Column('id', verbose_name=_('Task ID'),
198 hidden=True)
199 task_type = tables.Column('task_type', verbose_name=_('Task Type'),
200 filters=[TaskTypeDisplayFilter],
201 link="horizon:management:tasks:detail")
202 status = tables.Column('status', verbose_name=_('Status'))
203 request_by = tables.Column('request_by', verbose_name=_('Requestee'))
204 request_project = tables.Column('request_project',
205 verbose_name=_('Request Project'))
206 valid = tables.Column('valid', verbose_name=_("Actions Valid"))
207 created_on = tables.Column('created_on',
208 verbose_name=_('Request Date'))
209 page = tables.Column('page', hidden=True)
210
211 class Meta(object):
212 name = 'task_table'
213 verbose_name = _('Tasks')
214 table_actions = (ApproveTask, RevalidateTask, CancelTask)
215 row_actions = (ApproveTask, UpdateTask, RevalidateTask, CancelTask, )
216 prev_pagination_param = pagination_param = 'task_page'
217
218 def get_prev_marker(self):
219 return str(int(self.data[0].page) - 1) if self.data else ''
220
221 def get_marker(self):
222 return str(int(self.data[0].page) + 1) if self.data else ''
223
224 def get_object_display(self, obj):
225 task_type = obj.task_type.replace("_", " ").title()
226 return "%s (%s)" % (task_type, obj.id)
227
228
229 class ApprovedTaskTable(TaskTable):
230
231 class Meta(object):
232 name = 'approved_table'
233 verbose_name = _('Tasks')
234 table_actions = (CancelTask, ReapproveTask, ReissueToken)
235 row_actions = (CancelTask, ReapproveTask, ReissueToken)
236 prev_pagination_param = pagination_param = 'approved_page'
237
238
239 class CompletedTaskTable(TaskTable):
240
241 class Meta(object):
242 name = 'completed_table'
243 verbose_name = _('Tasks')
244 prev_pagination_param = pagination_param = 'completed_page'
245
246
247 class CancelledTaskTable(TaskTable):
248
249 class Meta(object):
250 name = 'cancelled_table'
251 verbose_name = _('Tasks')
252 prev_pagination_param = pagination_param = 'cancelled_page'