Debian Codegraph @

adjutant_ui/content/project_users/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 collections import defaultdict
16
17 from django.utils.translation import gettext_lazy as _
18 from django.utils.translation import ngettext_lazy
19
20 from horizon import exceptions
21 from horizon import tables
22
23 from adjutant_ui.api import adjutant
24
25
26 class InviteUser(tables.LinkAction):
27 name = "invite"
28 verbose_name = _("Invite User")
29 url = "horizon:management:project_users:invite"
30 classes = ("ajax-modal",)
31 icon = "plus"
32
33 @staticmethod
34 def action_past(count):
35 return ngettext_lazy(
36 u"Invited User",
37 u"Invited Users",
38 count
39 )
40
41
42 class ResendInvitation(tables.BatchAction):
43 name = "resend"
44
45 @staticmethod
46 def action_present(count):
47 return ngettext_lazy(
48 u"Resend Invitation",
49 u"Resend Invitations",
50 count
51 )
52
53 @staticmethod
54 def action_past(count):
55 return ngettext_lazy(
56 u"Invitation re-sent",
57 u"Invitations re-sent",
58 count
59 )
60
61 def allowed(self, request, user=None):
62 # Only invited users can be re-invited
63 return user.cohort == 'Invited'
64
65 def action(self, request, datum_id):
66 user = self.table.get_object_by_id(datum_id)
67 adjutant.user_invitation_resend(request, user.id)
68
69
70 class UpdateUser(tables.LinkAction):
71 name = "update"
72 verbose_name = _("Update User")
73 url = "horizon:management:project_users:update"
74 classes = ("ajax-modal",)
75 icon = "pencil"
76
77 def allowed(self, request, user=None):
78 # Currently, we only support updating existing users
79 return user.cohort == 'Member'
80
81
82 class RevokeUser(tables.DeleteAction):
83 help_text = _("This will remove the selected user(s) from the current "
84 "project.")
85
86 @staticmethod
87 def action_present(count):
88 return ngettext_lazy(
89 u"Revoke User",
90 u"Revoke Users",
91 count
92 )
93
94 @staticmethod
95 def action_past(count):
96 return ngettext_lazy(
97 u"Revoked User",
98 u"Revoked Users",
99 count
100 )
101
102 def delete(self, request, obj_id):
103 user = self.table.get_object_by_id(obj_id)
104
105 # NOTE(dale): There is a different endpoint to revoke an invited user
106 # than an Active user.
107 result = None
108 if user.cohort == 'Invited':
109 # Revoke invite for the user
110 result = adjutant.user_revoke(request, user.id)
111 else:
112 # Revoke all roles from the user.
113 # That'll get them out of our project; they keep their account.
114 result = adjutant.user_roles_remove(
115 request, user.id, user.roles)
116 if not result or result.status_code not in [200, 202]:
117 exception = exceptions.NotAvailable()
118 exception._safe_message = False
119 raise exception
120
121
122 class UpdateUserRow(tables.Row):
123 ajax = True
124
125 def get_data(self, request, user_id):
126 user = adjutant.user_get(request, user_id)
127 return user
128
129 def load_cells(self, user=None):
130 super(UpdateUserRow, self).load_cells(user)
131 user = self.datum
132 self.classes.append('category-' + user.cohort)
133
134
135 class CohortFilter(tables.FixedFilterAction):
136 def get_fixed_buttons(self):
137 def make_dict(text, value, icon):
138 return dict(text=text, value=value, icon=icon)
139
140 buttons = []
141 buttons.append(make_dict(_('Project Users'),
142 'Member',
143 'fa-user-md'))
144 buttons.append(make_dict(_('Invited Users'),
145 'Invited',
146 'fa-user'))
147 return buttons
148
149 def categorize(self, table, users):
150 categorized_users = defaultdict(list)
151 for u in users:
152 categorized_users[u.cohort].append(u)
153 return categorized_users
154
155
156 def UserRoleDisplayFilter(role_list):
157 roles = [adjutant.get_role_text(r) for r in role_list]
158 return ', '.join(roles)
159
160
161 class UsersTable(tables.DataTable):
162 uid = tables.Column('id', verbose_name=_('User ID'))
163 name = tables.Column('name', verbose_name=_('Name'))
164 email = tables.Column('email', verbose_name=_('Email'))
165 roles = tables.Column('roles',
166 verbose_name=_('Roles'),
167 filters=[UserRoleDisplayFilter])
168 status = tables.Column('status', verbose_name=_('Status'))
169 cohort = tables.Column('cohort',
170 verbose_name=_('Member Type'),
171 hidden=True)
172
173 class Meta(object):
174 name = 'users'
175 row_class = UpdateUserRow
176 verbose_name = _('Users')
177 columns = ('id', 'name', 'email', 'roles', 'status', 'cohort')
178 table_actions = (CohortFilter, InviteUser, RevokeUser)
179 row_actions = (UpdateUser, ResendInvitation, RevokeUser)
180 multi_select = True