| 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.urls import reverse_lazy |
| 17 |
from django.utils.translation import gettext_lazy as _ |
| 18 |
|
| 19 |
from horizon import exceptions |
| 20 |
from horizon import forms |
| 21 |
from horizon import tables |
| 22 |
from horizon.utils import memoized |
| 23 |
|
| 24 |
from adjutant_ui.content.project_users import tables as users_tables |
| 25 |
|
| 26 |
from adjutant_ui.content.project_users import forms as users_forms |
| 27 |
|
| 28 |
from adjutant_ui.api import adjutant |
| 29 |
|
| 30 |
|
| 31 |
class InviteUserView(forms.ModalFormView): |
| 32 |
form_class = users_forms.InviteUserForm |
| 33 |
form_id = "invite_user_form" |
| 34 |
modal_header = _("Invite User") |
| 35 |
submit_label = _("Invite User") |
| 36 |
submit_url = reverse_lazy('horizon:management:project_users:invite') |
| 37 |
template_name = 'management/project_users/invite.html' |
| 38 |
context_object_name = 'project_users' |
| 39 |
success_url = reverse_lazy("horizon:management:project_users:index") |
| 40 |
page_title = _("Invite User") |
| 41 |
|
| 42 |
|
| 43 |
class UpdateUserView(forms.ModalFormView): |
| 44 |
form_class = users_forms.UpdateUserForm |
| 45 |
form_id = "update_user_form" |
| 46 |
modal_header = _("Update User") |
| 47 |
submit_label = _("Update User") |
| 48 |
submit_url = 'horizon:management:project_users:update' |
| 49 |
template_name = 'management/project_users/update.html' |
| 50 |
context_object_name = 'project_users' |
| 51 |
success_url = reverse_lazy("horizon:management:project_users:index") |
| 52 |
page_title = _("Update User") |
| 53 |
|
| 54 |
@memoized.memoized_method |
| 55 |
def get_object(self): |
| 56 |
try: |
| 57 |
return adjutant.user_get(self.request, |
| 58 |
self.kwargs['user_id']) |
| 59 |
except Exception: |
| 60 |
msg = _('Unable to retrieve user.') |
| 61 |
url = reverse('horizon:management:project_users:index') |
| 62 |
exceptions.handle(self.request, msg, redirect=url) |
| 63 |
|
| 64 |
def get_context_data(self, **kwargs): |
| 65 |
context = super(UpdateUserView, self).get_context_data(**kwargs) |
| 66 |
context['user'] = self.get_object() |
| 67 |
args = (self.kwargs['user_id'],) |
| 68 |
context['submit_url'] = reverse(self.submit_url, args=args) |
| 69 |
return context |
| 70 |
|
| 71 |
def get_initial(self): |
| 72 |
user = self.get_object() |
| 73 |
data = {'id': self.kwargs['user_id'], |
| 74 |
'name': user['username'], |
| 75 |
'roles': user['roles'], |
| 76 |
} |
| 77 |
return data |
| 78 |
|
| 79 |
|
| 80 |
class UsersView(tables.DataTableView): |
| 81 |
table_class = users_tables.UsersTable |
| 82 |
template_name = 'management/project_users/index.html' |
| 83 |
page_title = _("Project Users") |
| 84 |
|
| 85 |
def get_data(self): |
| 86 |
try: |
| 87 |
return adjutant.user_list(self.request) |
| 88 |
except Exception: |
| 89 |
exceptions.handle(self.request, _('Failed to list users.')) |
| 90 |
return [] |