2016-09-10 13:08:10 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2016-08-08 16:43:11 +00:00
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
from __future__ import unicode_literals, absolute_import
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2016-09-11 01:50:42 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-07-27 08:21:55 +00:00
|
|
|
from django.views.generic import ListView, CreateView, UpdateView, DetailView, TemplateView
|
2018-04-11 07:24:12 +00:00
|
|
|
from django.views.generic.edit import DeleteView, SingleObjectMixin
|
2016-09-11 01:50:42 +00:00
|
|
|
from django.urls import reverse_lazy
|
2018-04-07 16:16:37 +00:00
|
|
|
from django.conf import settings
|
2016-09-10 13:08:10 +00:00
|
|
|
|
2018-07-20 10:42:01 +00:00
|
|
|
from common.permissions import AdminUserRequiredMixin
|
2018-07-26 10:25:44 +00:00
|
|
|
from orgs.utils import current_org
|
2018-04-11 07:24:12 +00:00
|
|
|
from .hands import Node, Asset, SystemUser, User, UserGroup
|
2018-04-10 12:29:06 +00:00
|
|
|
from .models import AssetPermission
|
2016-09-11 14:45:24 +00:00
|
|
|
from .forms import AssetPermissionForm
|
2016-09-10 13:08:10 +00:00
|
|
|
|
|
|
|
|
2018-07-27 08:21:55 +00:00
|
|
|
class AssetPermissionListView(AdminUserRequiredMixin, TemplateView):
|
2016-09-11 14:45:24 +00:00
|
|
|
template_name = 'perms/asset_permission_list.html'
|
2018-04-07 16:16:37 +00:00
|
|
|
|
2016-09-10 13:08:10 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2016-09-11 01:50:42 +00:00
|
|
|
'app': _('Perms'),
|
2016-09-11 14:45:24 +00:00
|
|
|
'action': _('Asset permission list'),
|
2016-09-10 13:08:10 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-19 11:38:09 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class AssetPermissionCreateView(AdminUserRequiredMixin, CreateView):
|
2018-04-07 16:16:37 +00:00
|
|
|
model = AssetPermission
|
2016-09-11 14:45:24 +00:00
|
|
|
form_class = AssetPermissionForm
|
2016-09-11 15:20:14 +00:00
|
|
|
template_name = 'perms/asset_permission_create_update.html'
|
2016-09-11 14:45:24 +00:00
|
|
|
success_url = reverse_lazy('perms:asset-permission-list')
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
def get_form(self, form_class=None):
|
|
|
|
form = super().get_form(form_class=form_class)
|
2018-04-10 12:45:01 +00:00
|
|
|
nodes_id = self.request.GET.get("nodes")
|
|
|
|
assets_id = self.request.GET.get("assets")
|
2018-04-10 12:29:06 +00:00
|
|
|
|
|
|
|
if nodes_id:
|
2018-04-10 12:45:01 +00:00
|
|
|
nodes_id = nodes_id.split(",")
|
2018-05-14 06:21:32 +00:00
|
|
|
nodes = Node.objects.filter(id__in=nodes_id).exclude(id=Node.root().id)
|
2018-04-10 12:29:06 +00:00
|
|
|
form['nodes'].initial = nodes
|
|
|
|
if assets_id:
|
2018-04-10 12:45:01 +00:00
|
|
|
assets_id = assets_id.split(",")
|
2018-04-10 12:29:06 +00:00
|
|
|
assets = Asset.objects.filter(id__in=assets_id)
|
|
|
|
form['assets'].initial = assets
|
|
|
|
return form
|
|
|
|
|
2016-09-11 08:59:19 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Perms'),
|
2016-09-11 14:45:24 +00:00
|
|
|
'action': _('Create asset permission'),
|
2016-09-11 08:59:19 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-19 11:38:09 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-02-06 15:13:27 +00:00
|
|
|
|
2016-09-11 15:20:14 +00:00
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class AssetPermissionUpdateView(AdminUserRequiredMixin, UpdateView):
|
2018-04-07 16:16:37 +00:00
|
|
|
model = AssetPermission
|
2016-09-11 15:20:14 +00:00
|
|
|
form_class = AssetPermissionForm
|
|
|
|
template_name = 'perms/asset_permission_create_update.html'
|
2017-05-15 15:39:54 +00:00
|
|
|
success_url = reverse_lazy("perms:asset-permission-list")
|
2016-09-11 15:20:14 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Perms'),
|
|
|
|
'action': _('Update asset permission')
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-19 11:38:09 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2016-09-11 15:20:14 +00:00
|
|
|
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
class AssetPermissionDetailView(AdminUserRequiredMixin, DetailView):
|
|
|
|
model = AssetPermission
|
|
|
|
form_class = AssetPermissionForm
|
|
|
|
template_name = 'perms/asset_permission_detail.html'
|
|
|
|
success_url = reverse_lazy("perms:asset-permission-list")
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Perms'),
|
2018-04-11 07:24:12 +00:00
|
|
|
'action': _('Update asset permission'),
|
|
|
|
'system_users_remain': SystemUser.objects.exclude(
|
|
|
|
granted_by_permissions=self.object
|
|
|
|
),
|
2018-04-10 12:29:06 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
2016-09-11 15:20:14 +00:00
|
|
|
class AssetPermissionDeleteView(AdminUserRequiredMixin, DeleteView):
|
|
|
|
model = AssetPermission
|
2017-12-14 13:27:14 +00:00
|
|
|
template_name = 'delete_confirm.html'
|
2016-09-11 15:20:14 +00:00
|
|
|
success_url = reverse_lazy('perms:asset-permission-list')
|
2016-09-12 16:37:24 +00:00
|
|
|
|
|
|
|
|
2018-04-11 07:24:12 +00:00
|
|
|
class AssetPermissionUserView(AdminUserRequiredMixin,
|
|
|
|
SingleObjectMixin,
|
|
|
|
ListView):
|
|
|
|
template_name = 'perms/asset_permission_user.html'
|
|
|
|
context_object_name = 'asset_permission'
|
2018-11-22 04:27:27 +00:00
|
|
|
paginate_by = settings.DISPLAY_PER_PAGE
|
2018-04-11 07:24:12 +00:00
|
|
|
object = None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2018-07-26 10:25:44 +00:00
|
|
|
self.object = self.get_object(queryset=AssetPermission.objects.all())
|
2018-04-11 07:24:12 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-04-19 02:34:17 +00:00
|
|
|
queryset = list(self.object.get_all_users())
|
2018-04-11 07:24:12 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2018-07-26 10:25:44 +00:00
|
|
|
|
2018-04-11 07:24:12 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Perms'),
|
|
|
|
'action': _('Asset permission user list'),
|
2018-07-26 10:25:44 +00:00
|
|
|
'users_remain': current_org.get_org_users().exclude(
|
|
|
|
asset_permissions=self.object
|
|
|
|
),
|
2018-04-11 07:24:12 +00:00
|
|
|
'user_groups_remain': UserGroup.objects.exclude(
|
|
|
|
asset_permissions=self.object
|
|
|
|
)
|
|
|
|
}
|
|
|
|
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'
|
2018-11-22 04:27:27 +00:00
|
|
|
paginate_by = settings.DISPLAY_PER_PAGE
|
2018-04-11 07:24:12 +00:00
|
|
|
object = None
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2018-07-14 16:55:05 +00:00
|
|
|
self.object = self.get_object(queryset = AssetPermission.objects.all())
|
2018-04-11 07:24:12 +00:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-04-19 02:34:17 +00:00
|
|
|
queryset = list(self.object.get_all_assets())
|
2018-04-11 07:24:12 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
assets_granted = self.get_queryset()
|
|
|
|
context = {
|
|
|
|
'app': _('Perms'),
|
|
|
|
'action': _('Asset permission asset list'),
|
|
|
|
'assets_remain': Asset.objects.exclude(id__in=[a.id for a in assets_granted]),
|
|
|
|
'nodes_remain': Node.objects.exclude(granted_by_permissions=self.object),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|