2017-04-04 03:37:52 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2017-12-13 09:21:08 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.shortcuts import redirect, reverse
|
2017-04-04 03:37:52 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.db import transaction
|
2017-12-13 09:21:08 +00:00
|
|
|
from django.views.generic import TemplateView, ListView, FormView
|
2017-07-24 14:42:01 +00:00
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
2017-04-04 03:37:52 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
|
|
from django.views.generic.detail import DetailView, SingleObjectMixin
|
|
|
|
|
2017-12-13 09:21:08 +00:00
|
|
|
from ..forms import SystemUserForm, SystemUserUpdateForm, SystemUserAuthForm
|
|
|
|
from ..models import SystemUser, Cluster
|
2017-04-04 03:37:52 +00:00
|
|
|
from ..hands import AdminUserRequiredMixin
|
2017-07-19 23:55:24 +00:00
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
__all__ = ['SystemUserCreateView', 'SystemUserUpdateView',
|
|
|
|
'SystemUserDetailView', 'SystemUserDeleteView',
|
|
|
|
'SystemUserAssetView', 'SystemUserListView',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserListView(AdminUserRequiredMixin, TemplateView):
|
|
|
|
template_name = 'assets/system_user_list.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('System 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 SystemUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
|
|
|
|
model = SystemUser
|
2017-12-11 09:08:43 +00:00
|
|
|
form_class = SystemUserForm
|
2017-04-04 03:37:52 +00:00
|
|
|
template_name = 'assets/system_user_create.html'
|
|
|
|
success_url = reverse_lazy('assets:system-user-list')
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
return super(SystemUserCreateView, self).post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Create system user'),
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
def get_success_message(self, cleaned_data):
|
2017-12-13 09:21:08 +00:00
|
|
|
url = reverse('assets:system-user-detail', kwargs={'pk': self.object.pk})
|
2017-04-07 08:59:09 +00:00
|
|
|
success_message = _(
|
|
|
|
'Create system user <a href="{url}">{name}</a> '
|
|
|
|
'successfully.'.format(url=url, name=self.object.name)
|
|
|
|
)
|
2017-04-04 03:37:52 +00:00
|
|
|
|
|
|
|
return success_message
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserUpdateView(AdminUserRequiredMixin, UpdateView):
|
|
|
|
model = SystemUser
|
2017-12-11 09:08:43 +00:00
|
|
|
form_class = SystemUserUpdateForm
|
2017-04-04 03:37:52 +00:00
|
|
|
template_name = 'assets/system_user_update.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Assets'),
|
|
|
|
'action': _('Update system user')
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-13 09:21:08 +00:00
|
|
|
return super().get_context_data(**kwargs)
|
2017-07-19 23:55:24 +00:00
|
|
|
|
2017-04-04 03:37:52 +00:00
|
|
|
def get_success_url(self):
|
2017-04-07 08:59:09 +00:00
|
|
|
success_url = reverse_lazy('assets:system-user-detail',
|
|
|
|
kwargs={'pk': self.object.pk})
|
2017-04-04 03:37:52 +00:00
|
|
|
return success_url
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserDetailView(AdminUserRequiredMixin, DetailView):
|
|
|
|
template_name = 'assets/system_user_detail.html'
|
|
|
|
context_object_name = 'system_user'
|
|
|
|
model = SystemUser
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-12-13 09:21:08 +00:00
|
|
|
cluster_remain = Cluster.objects.exclude(systemuser=self.object)
|
2017-04-04 03:37:52 +00:00
|
|
|
context = {
|
|
|
|
'app': _('Assets'),
|
2017-12-13 09:21:08 +00:00
|
|
|
'action': _('System user detail'),
|
|
|
|
'cluster_remain': cluster_remain,
|
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 SystemUserDeleteView(AdminUserRequiredMixin, DeleteView):
|
|
|
|
model = SystemUser
|
2017-12-14 13:27:14 +00:00
|
|
|
template_name = 'delete_confirm.html'
|
2017-04-04 03:37:52 +00:00
|
|
|
success_url = reverse_lazy('assets:system-user-list')
|
|
|
|
|
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
class SystemUserAssetView(AdminUserRequiredMixin, DetailView):
|
|
|
|
model = SystemUser
|
2017-04-04 03:37:52 +00:00
|
|
|
template_name = 'assets/system_user_asset.html'
|
|
|
|
context_object_name = 'system_user'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': 'assets',
|
|
|
|
'action': 'System user asset',
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
2017-12-13 09:21:08 +00:00
|
|
|
return super().get_context_data(**kwargs)
|