You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/perms/views.py

159 lines
5.8 KiB

8 years ago
# ~*~ coding: utf-8 ~*~
8 years ago
from __future__ import unicode_literals, absolute_import
from django.utils.translation import ugettext as _
8 years ago
from django.conf import settings
from django.views.generic import ListView, CreateView, UpdateView
7 years ago
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
from django.views.generic.detail import DetailView, SingleObjectMixin
8 years ago
from common.utils import get_object_or_none
from .hands import AdminUserRequiredMixin, User, UserGroup, SystemUser, \
Asset, AssetGroup, Node
7 years ago
from .models import AssetPermission, NodePermission
from .forms import AssetPermissionForm
8 years ago
class AssetPermissionListView(AdminUserRequiredMixin, ListView):
7 years ago
model = NodePermission
context_object_name = 'asset_permission_list'
template_name = 'perms/asset_permission_list.html'
8 years ago
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Asset permission list'),
8 years ago
}
kwargs.update(context)
return super().get_context_data(**kwargs)
7 years ago
class AssetPermissionCreateView(AdminUserRequiredMixin, CreateView):
model = NodePermission
form_class = AssetPermissionForm
template_name = 'perms/asset_permission_create_update.html'
success_url = reverse_lazy('perms:asset-permission-list')
def get_form(self, form_class=None):
form = super().get_form(form_class=form_class)
node_id = self.request.GET.get("node_id")
node = get_object_or_none(Node, id=node_id)
if not node:
node = Node.root()
form['node'].initial = node
return form
8 years ago
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Create asset permission'),
8 years ago
}
kwargs.update(context)
return super().get_context_data(**kwargs)
7 years ago
class AssetPermissionUpdateView(AdminUserRequiredMixin, UpdateView):
model = NodePermission
form_class = AssetPermissionForm
template_name = 'perms/asset_permission_create_update.html'
success_url = reverse_lazy("perms:asset-permission-list")
def get_form(self, form_class=None):
form = super().get_form(form_class=form_class)
form['node'].initial = form.instance.node
return form
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Update asset permission')
}
kwargs.update(context)
return super().get_context_data(**kwargs)
# class AssetPermissionDetailView(AdminUserRequiredMixin, DetailView):
# template_name = 'perms/asset_permission_detail.html'
# context_object_name = 'asset_permission'
# model = AssetPermission
#
# def get_context_data(self, **kwargs):
# context = {
# 'app': _('Perms'),
# 'action': _('Asset permission detail'),
# 'system_users_remain': [
# system_user for system_user in SystemUser.objects.all()
# if system_user not in self.object.system_users.all()],
# 'system_users': self.object.system_users.all(),
# }
# kwargs.update(context)
# return super().get_context_data(**kwargs)
class AssetPermissionDeleteView(AdminUserRequiredMixin, DeleteView):
model = AssetPermission
template_name = 'delete_confirm.html'
success_url = reverse_lazy('perms:asset-permission-list')
# class AssetPermissionUserView(AdminUserRequiredMixin,
# SingleObjectMixin,
# ListView):
# template_name = 'perms/asset_permission_user.html'
# context_object_name = 'asset_permission'
# paginate_by = settings.DISPLAY_PER_PAGE
# object = None
#
# def get(self, request, *args, **kwargs):
# self.object = self.get_object(queryset=AssetPermission.objects.all())
# return super().get(request, *args, **kwargs)
#
# def get_queryset(self):
# queryset = self.object.get_granted_users()
# return queryset
#
# def get_context_data(self, **kwargs):
# users_granted = self.get_queryset()
# groups_granted = self.object.user_groups.all()
# context = {
# 'app': _('Perms'),
# 'action': _('Asset permission user list'),
# 'users_remain': User.objects.exclude(id__in=[user.id for user in users_granted]),
# 'user_groups': self.object.user_groups.all(),
# 'user_groups_remain': UserGroup.objects.exclude(id__in=[group.id for group in groups_granted])
# }
# kwargs.update(context)
# return super().get_context_data(**kwargs)
# class AssetPermissionAssetView(AdminUserRequiredMixin,
# SingleObjectMixin,
# ListView):
# template_name = 'perms/asset_permission_asset.html'
# context_object_name = 'asset_permission'
# paginate_by = settings.DISPLAY_PER_PAGE
# object = None
#
# def get(self, request, *args, **kwargs):
# self.object = self.get_object(queryset=AssetPermission.objects.all())
# return super().get(request, *args, **kwargs)
#
# def get_queryset(self):
# queryset = self.object.get_granted_assets()
# return queryset
#
# def get_context_data(self, **kwargs):
# assets_granted = self.get_queryset()
# groups_granted = self.object.asset_groups.all()
# context = {
# 'app': _('Perms'),
# 'action': _('Asset permission asset list'),
# 'assets_remain': Asset.objects.exclude(id__in=[asset.id for asset in assets_granted]),
# 'asset_groups': self.object.asset_groups.all(),
# 'asset_groups_remain': AssetGroup.objects.exclude(id__in=[group.id for group in groups_granted])
# }
# kwargs.update(context)
# return super().get_context_data(**kwargs)