Debian Codegraph @

adjutant_ui/content/notifications/tabs.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
17 from horizon import exceptions
18 from horizon import tabs
19
20 from adjutant_ui.api import adjutant
21 from adjutant_ui.api.adjutant import AdjutantApiError
22 from adjutant_ui.content.notifications import tables as notification_tables
23
24
25 class UnacknowledgedNotificationsTab(tabs.TableTab):
26 table_classes = (notification_tables.NotificationTable,)
27 template_name = 'horizon/common/_detail_table.html'
28 page_title = _("Unacknowledged Notifications")
29 name = _('Unacknowledged')
30 slug = 'unacknowledged'
31 filters = {'acknowledged': {'exact': False}}
32 _prev = False
33 _more = False
34 _page = 1
35
36 def get_notification_table_data(self):
37 notifications = []
38 self._page = self.request.GET.get(
39 self.table_classes[0]._meta.pagination_param, 1)
40 try:
41 notifications, self._prev, self._more = adjutant.notification_list(
42 self.request, filters=self.filters, page=self._page)
43 except AdjutantApiError as e:
44 if e.message != "Empty Page":
45 raise
46 try:
47 self._page = 1
48 notifications, self._prev, self._more = \
49 adjutant.notification_list(
50 self.request, filters=self.filters,
51 page=self._page)
52 except Exception:
53 exceptions.handle(
54 self.request, _('Failed to list notifications.'))
55 except Exception:
56 exceptions.handle(self.request, _('Failed to list notifications.'))
57 return notifications
58
59 def has_prev_data(self, table):
60 table.page = self._page
61 return self._prev
62
63 def has_more_data(self, table):
64 table.page = self._page
65 return self._more
66
67
68 class AcknowlededNotificationsTab(UnacknowledgedNotificationsTab):
69 table_classes = (notification_tables.AcknowlegedNotificationTable,)
70 page_title = _("Acknowleged Notifications")
71 name = _('Acknowleged')
72 slug = 'acknowledged'
73 filters = {'acknowledged': {'exact': True}}
74
75 def get_acknowleged_table_data(self):
76 notifications = []
77 self._page = self.request.GET.get(
78 self.table_classes[0]._meta.pagination_param, 1)
79 try:
80 notifications, self._prev, self._more = adjutant.notification_list(
81 self.request, filters=self.filters, page=self._page)
82 except Exception:
83 exceptions.handle(self.request, _('Failed to list notifications.'))
84 return notifications
85
86
87 class NotificationTabGroup(tabs.TabGroup):
88 slug = "notifications"
89 tabs = (UnacknowledgedNotificationsTab, AcknowlededNotificationsTab, )
90 sticky = True
91
92 def get_selected_tab(self):
93 super(NotificationTabGroup, self).get_selected_tab()
94 if not self._selected:
95 for tab in self.tabs:
96 param = tab.table_classes[0]._meta.pagination_param
97 if self.request.GET.get(param):
98 self._selected = self.get_tab(tab.slug)
99 return self._selected