Debian Codegraph @

adjutant_ui/content/notifications/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.urls import reverse
16 from django.utils.translation import gettext_lazy as _
17 from django.utils.translation import ngettext_lazy
18
19 from horizon import exceptions
20 from horizon import tables
21
22 from adjutant_ui.api import adjutant
23
24
25 class AcknowlegeNotifcation(tables.BatchAction):
26 name = 'acknowlege'
27 help_text = _("This will acknowlege all selected tasks.")
28
29 @staticmethod
30 def action_present(count):
31 return ngettext_lazy(
32 u"Acknowlege Notification",
33 u"Acknowlege Notifications",
34 count
35 )
36
37 @staticmethod
38 def action_past(count):
39 return ngettext_lazy(
40 u"Acknowleged Notification",
41 u"Acknowleged Notifications",
42 count
43 )
44
45 def action(self, request, obj_id):
46 result = adjutant.notifications_acknowlege(request, obj_id)
47 if not result or result.status_code not in [200, 202]:
48 exception = exceptions.NotAvailable()
49 exception._safe_message = False
50 raise exception
51
52 def allowed(self, request, notification=None):
53 if notification:
54 return not(notification.acknowledged)
55 return True
56
57
58 def get_task_link(datum):
59 return reverse("horizon:management:tasks:detail",
60 args=(datum.task,))
61
62
63 class ErrorRow(tables.Row):
64
65 def __init__(self, *args, **kwargs):
66 super(ErrorRow, self).__init__(*args, **kwargs)
67 if not self.datum:
68 return
69 if self.datum.error:
70 self.classes.append('danger')
71
72
73 class NotificationTable(tables.DataTable):
74 uuid = tables.Column('uuid', verbose_name=_('Notification ID'),
75 link="horizon:management:notifications:detail")
76 task = tables.Column('task', verbose_name=_('Task ID'),
77 link=get_task_link)
78 error = tables.Column('error', verbose_name=_('Error'))
79 created_on = tables.Column('created_on',
80 verbose_name=_('Created On'))
81 notes = tables.Column('notes')
82
83 class Meta(object):
84 template = 'notifications/table_override.html'
85 name = 'notification_table'
86 verbose_name = _('Unacknowledged Notifications')
87 table_actions = (AcknowlegeNotifcation, )
88 row_actions = (AcknowlegeNotifcation, )
89 row_class = ErrorRow
90 prev_pagination_param = pagination_param = 'task_page'
91
92 def get_prev_marker(self):
93 return str(int(self.page) - 1) if self.data else ''
94
95 def get_marker(self):
96 return str(int(self.page) + 1) if self.data else ''
97
98 def get_object_display(self, obj):
99 return obj.uuid
100
101 def get_object_id(self, obj):
102 return obj.uuid
103
104
105 class AcknowlegedNotificationTable(NotificationTable):
106
107 class Meta(object):
108 name = 'acknowleged_table'
109 verbose_name = _('Acknowleged Notifications')
110 prev_pagination_param = pagination_param = 'acknowledged_page'
111 table_actions = ()