2017-01-17 08:34:47 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.views.generic.base import TemplateView
|
2018-01-09 15:07:53 +00:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView
|
|
|
|
from django.views.generic.detail import DetailView
|
2017-04-07 08:59:09 +00:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
2018-01-09 15:07:53 +00:00
|
|
|
from common.const import create_success_msg, update_success_msg
|
2019-06-19 02:47:26 +00:00
|
|
|
from common.permissions import PermissionsMixin, IsOrgAdmin
|
2018-09-03 03:24:25 +00:00
|
|
|
from orgs.utils import current_org
|
2018-07-25 03:21:12 +00:00
|
|
|
from ..models import User, UserGroup
|
2017-01-17 08:34:47 +00:00
|
|
|
from .. import forms
|
|
|
|
|
|
|
|
__all__ = ['UserGroupListView', 'UserGroupCreateView', 'UserGroupDetailView',
|
2017-12-18 10:38:30 +00:00
|
|
|
'UserGroupUpdateView', 'UserGroupGrantedAssetView']
|
2017-01-17 08:34:47 +00:00
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGroupListView(PermissionsMixin, TemplateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/user_group_list.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-12-18 10:38:30 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User group list')
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGroupCreateView(PermissionsMixin, SuccessMessageMixin, CreateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = UserGroup
|
|
|
|
form_class = forms.UserGroupForm
|
|
|
|
template_name = 'users/user_group_create_update.html'
|
|
|
|
success_url = reverse_lazy('users:user-group-list')
|
2018-01-09 15:07:53 +00:00
|
|
|
success_message = create_success_msg
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-12-18 10:38:30 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('Create user group'),
|
2019-07-11 10:17:47 +00:00
|
|
|
'type': 'create'
|
2017-12-18 10:38:30 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
2017-04-07 08:59:09 +00:00
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGroupUpdateView(PermissionsMixin, SuccessMessageMixin, UpdateView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = UserGroup
|
|
|
|
form_class = forms.UserGroupForm
|
|
|
|
template_name = 'users/user_group_create_update.html'
|
|
|
|
success_url = reverse_lazy('users:user-group-list')
|
2018-01-09 15:07:53 +00:00
|
|
|
success_message = update_success_msg
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-12-18 10:38:30 +00:00
|
|
|
context = {
|
2017-01-17 08:34:47 +00:00
|
|
|
'app': _('Users'),
|
2017-04-07 08:59:09 +00:00
|
|
|
'action': _('Update user group'),
|
2019-07-11 10:17:47 +00:00
|
|
|
'type': 'update'
|
2018-09-03 03:24:25 +00:00
|
|
|
|
2017-12-18 10:38:30 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGroupDetailView(PermissionsMixin, DetailView):
|
2017-01-17 08:34:47 +00:00
|
|
|
model = UserGroup
|
|
|
|
context_object_name = 'user_group'
|
|
|
|
template_name = 'users/user_group_detail.html'
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2019-09-12 10:56:26 +00:00
|
|
|
users = current_org.get_org_members(exclude=('Auditor',)).exclude(
|
|
|
|
groups=self.object)
|
2017-01-17 08:34:47 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
2017-04-08 16:45:28 +00:00
|
|
|
'action': _('User group detail'),
|
2017-01-17 08:34:47 +00:00
|
|
|
'users': users,
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
|
2019-06-19 02:47:26 +00:00
|
|
|
class UserGroupGrantedAssetView(PermissionsMixin, DetailView):
|
2017-12-01 09:00:55 +00:00
|
|
|
model = UserGroup
|
2017-01-17 08:34:47 +00:00
|
|
|
template_name = 'users/user_group_granted_asset.html'
|
|
|
|
context_object_name = 'user_group'
|
2018-02-08 03:34:21 +00:00
|
|
|
object = None
|
2019-06-19 02:47:26 +00:00
|
|
|
permission_classes = [IsOrgAdmin]
|
2017-01-17 08:34:47 +00:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
2018-03-05 09:14:42 +00:00
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('User group granted asset'),
|
2017-01-17 08:34:47 +00:00
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-18 10:38:30 +00:00
|
|
|
return super().get_context_data(**kwargs)
|