jumpserver/apps/assets/views/admin_user.py

123 lines
3.9 KiB
Python
Raw Normal View History

2017-04-04 03:37:52 +00:00
# coding:utf-8
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext as _
from django.conf import settings
from django.urls import reverse_lazy
from django.views.generic import TemplateView, ListView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
2017-04-04 03:37:52 +00:00
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.detail import DetailView, SingleObjectMixin
from common.const import create_success_msg, update_success_msg
2017-04-04 03:37:52 +00:00
from .. import forms
2018-02-09 07:24:44 +00:00
from ..models import AdminUser, Node
from common.permissions import PermissionsMixin, IsOrgAdmin
2017-04-04 03:37:52 +00:00
__all__ = [
'AdminUserCreateView', 'AdminUserDetailView',
'AdminUserDeleteView', 'AdminUserListView',
'AdminUserUpdateView', 'AdminUserAssetsView',
]
2017-04-04 03:37:52 +00:00
class AdminUserListView(PermissionsMixin, TemplateView):
2017-04-04 03:37:52 +00:00
model = AdminUser
template_name = 'assets/admin_user_list.html'
permission_classes = [IsOrgAdmin]
2017-04-04 03:37:52 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Assets'),
'action': _('Admin user list'),
}
kwargs.update(context)
2017-12-13 09:21:08 +00:00
return super().get_context_data(**kwargs)
2017-04-04 03:37:52 +00:00
class AdminUserCreateView(PermissionsMixin,
2017-04-04 03:37:52 +00:00
SuccessMessageMixin,
CreateView):
model = AdminUser
form_class = forms.AdminUserForm
template_name = 'assets/admin_user_create_update.html'
success_url = reverse_lazy('assets:admin-user-list')
success_message = create_success_msg
permission_classes = [IsOrgAdmin]
2017-04-04 03:37:52 +00:00
def get_context_data(self, **kwargs):
context = {
2018-01-01 07:08:33 +00:00
'app': _('Assets'),
'action': _('Create admin user')
2017-04-04 03:37:52 +00:00
}
kwargs.update(context)
2017-12-13 09:21:08 +00:00
return super().get_context_data(**kwargs)
2017-04-04 03:37:52 +00:00
class AdminUserUpdateView(PermissionsMixin, SuccessMessageMixin, UpdateView):
2017-04-04 03:37:52 +00:00
model = AdminUser
form_class = forms.AdminUserForm
template_name = 'assets/admin_user_create_update.html'
success_url = reverse_lazy('assets:admin-user-list')
success_message = update_success_msg
permission_classes = [IsOrgAdmin]
2017-04-04 03:37:52 +00:00
def get_context_data(self, **kwargs):
context = {
2018-01-01 07:08:33 +00:00
'app': _('Assets'),
'action': _('Update admin user'),
2017-04-04 03:37:52 +00:00
}
kwargs.update(context)
2017-12-13 09:21:08 +00:00
return super().get_context_data(**kwargs)
2017-04-04 03:37:52 +00:00
class AdminUserDetailView(PermissionsMixin, DetailView):
2017-12-13 09:21:08 +00:00
model = AdminUser
2017-04-04 03:37:52 +00:00
template_name = 'assets/admin_user_detail.html'
context_object_name = 'admin_user'
2017-12-13 09:21:08 +00:00
object = None
permission_classes = [IsOrgAdmin]
2017-04-04 03:37:52 +00:00
def get_context_data(self, **kwargs):
context = {
2018-01-01 07:08:33 +00:00
'app': _('Assets'),
'action': _('Admin user detail'),
2018-02-09 07:24:44 +00:00
'nodes': Node.objects.all()
2017-04-04 03:37:52 +00:00
}
kwargs.update(context)
2017-12-13 09:21:08 +00:00
return super().get_context_data(**kwargs)
2017-04-04 03:37:52 +00:00
class AdminUserAssetsView(PermissionsMixin, SingleObjectMixin, ListView):
2018-01-12 07:43:26 +00:00
paginate_by = settings.DISPLAY_PER_PAGE
2017-12-10 16:29:25 +00:00
template_name = 'assets/admin_user_assets.html'
context_object_name = 'admin_user'
2017-12-13 09:21:08 +00:00
object = None
permission_classes = [IsOrgAdmin]
2017-12-10 16:29:25 +00:00
def get(self, request, *args, **kwargs):
2018-07-14 16:55:05 +00:00
self.object = self.get_object(queryset=AdminUser.objects.all())
2017-12-10 16:29:25 +00:00
return super().get(request, *args, **kwargs)
def get_queryset(self):
self.queryset = self.object.asset_set.all()
return self.queryset
2017-12-10 16:29:25 +00:00
def get_context_data(self, **kwargs):
context = {
2018-01-01 07:08:33 +00:00
'app': _('Assets'),
'action': _('Admin user detail'),
2017-12-10 16:29:25 +00:00
"total_amount": len(self.queryset),
'unreachable_amount': len([asset for asset in self.queryset if asset.connectivity is False])
2017-12-10 16:29:25 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class AdminUserDeleteView(PermissionsMixin, DeleteView):
2017-04-04 03:37:52 +00:00
model = AdminUser
template_name = 'delete_confirm.html'
2017-04-04 03:37:52 +00:00
success_url = reverse_lazy('assets:admin-user-list')
permission_classes = [IsOrgAdmin]
2017-04-04 03:37:52 +00:00