jumpserver/apps/users/views.py

165 lines
5.6 KiB
Python
Raw Normal View History

2016-08-14 09:21:04 +00:00
# ~*~ coding: utf-8 ~*~
2016-08-08 16:43:11 +00:00
2016-08-23 04:18:19 +00:00
from __future__ import unicode_literals
from django.shortcuts import get_object_or_404, reverse
from django.urls import reverse_lazy
from django.db.models import Q
2016-08-14 09:21:04 +00:00
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
2016-08-16 14:13:06 +00:00
from django.views.generic.detail import DetailView
2016-08-23 04:18:19 +00:00
from django.contrib.messages.views import SuccessMessageMixin
2016-08-20 10:33:18 +00:00
from django.conf import settings
2016-08-14 09:21:04 +00:00
2016-08-24 09:14:21 +00:00
from .models import User, UserGroup
2016-08-18 17:39:08 +00:00
from .forms import UserAddForm, UserUpdateForm, UserGroupForm
2016-08-14 09:21:04 +00:00
class UserListView(ListView):
model = User
2016-08-20 10:33:18 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
2016-08-14 09:21:04 +00:00
context_object_name = 'user_list'
template_name = 'users/user_list.html'
ordering = '-date_joined'
def get_queryset(self):
self.queryset = super(UserListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
2016-08-18 14:29:35 +00:00
self.sort = sort = self.request.GET.get('sort')
if keyword:
self.queryset = self.queryset.filter(Q(username__icontains=keyword) |
Q(name__icontains=keyword))
2016-08-18 14:29:35 +00:00
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
2016-08-14 09:21:04 +00:00
def get_context_data(self, **kwargs):
context = super(UserListView, self).get_context_data(**kwargs)
context.update({'path1': '用户管理', 'path2': '用户列表', 'title': '用户列表', 'keyword': self.keyword})
2016-08-14 09:21:04 +00:00
return context
2016-08-14 11:18:41 +00:00
2016-08-23 04:18:19 +00:00
class UserAddView(SuccessMessageMixin, CreateView):
2016-08-14 11:18:41 +00:00
model = User
2016-08-17 14:17:16 +00:00
form_class = UserAddForm
2016-08-14 11:18:41 +00:00
template_name = 'users/user_add.html'
success_url = reverse_lazy('users:user-list')
2016-08-23 04:18:19 +00:00
success_message = '添加用户 <a href="%s">%s</a> 成功 .'
def get_context_data(self, **kwargs):
context = super(UserAddView, self).get_context_data(**kwargs)
context.update({'path1': '用户管理', 'path2': '用户添加', 'title': '用户添加'})
return context
2016-08-14 11:18:41 +00:00
def form_valid(self, form):
2016-08-24 09:14:21 +00:00
user = form.save(commit=False)
user.created_by = self.request.user.username or 'System'
2016-08-16 14:13:06 +00:00
user.save()
2016-08-24 09:14:21 +00:00
form.save_m2m()
return super(UserAddView, self).form_valid(form)
2016-08-23 04:18:19 +00:00
def get_success_message(self, cleaned_data):
return self.success_message % (
reverse_lazy('users:user-detail', kwargs={'pk': self.object.pk}),
self.object.name,
)
class UserUpdateView(UpdateView):
model = User
2016-08-17 14:17:16 +00:00
form_class = UserUpdateForm
template_name = 'users/user_edit.html'
2016-08-17 14:17:16 +00:00
context_object_name = 'user'
success_url = reverse_lazy('users:user-list')
def form_valid(self, form):
2016-08-17 14:17:16 +00:00
username = self.object.username
user = form.save(commit=False)
user.username = username
user.save()
2016-08-16 14:13:06 +00:00
password = self.request.POST.get('password', '')
if password:
user.set_password(password)
return super(UserUpdateView, self).form_valid(form)
2016-08-16 14:13:06 +00:00
2016-08-22 16:39:07 +00:00
def form_invalid(self, form):
print(form.errors)
return super(UserUpdateView, self).form_invalid(form)
2016-08-16 14:13:06 +00:00
class UserDeleteView(DeleteView):
model = User
success_url = reverse_lazy('users:user-list')
template_name = 'users/user_delete_confirm.html'
2016-08-17 14:17:16 +00:00
class UserDetailView(DetailView):
2016-08-16 14:13:06 +00:00
model = User
template_name = 'users/user_detail.html'
context_object_name = "user"
2016-08-16 16:23:52 +00:00
def get_context_data(self, **kwargs):
context = super(UserDetailView, self).get_context_data(**kwargs)
2016-08-17 16:47:34 +00:00
groups = [group for group in UserGroup.objects.iterator() if group not in self.object.groups.iterator()]
context.update({'path1': '用户管理', 'path2': '用户详情', 'title': '用户详情', 'groups': groups})
2016-08-16 16:23:52 +00:00
return context
2016-08-18 17:39:08 +00:00
class UserGroupListView(ListView):
model = UserGroup
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
2016-08-18 17:39:08 +00:00
context_object_name = 'usergroup_list'
template_name = 'users/usergroup_list.html'
ordering = '-date_added'
def get_queryset(self):
self.queryset = super(UserGroupListView, self).get_queryset()
self.keyword = keyword = self.request.GET.get('keyword', '')
self.sort = sort = self.request.GET.get('sort')
if keyword:
self.queryset = self.queryset.filter(name__icontains=keyword)
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
def get_context_data(self, **kwargs):
context = super(UserGroupListView, self).get_context_data(**kwargs)
context.update({'path1': '用户管理', 'path2': '用户组列表', 'title': '用户组列表', 'keyword': self.keyword})
return context
class UserGroupAddView(CreateView):
model = UserGroup
form_class = UserGroupForm
template_name = 'users/usergroup_add.html'
success_url = reverse_lazy('users:usergroup-list')
def get_context_data(self, **kwargs):
context = super(UserGroupAddView, self).get_context_data(**kwargs)
users = User.objects.all()
context.update({'path1': '用户管理', 'path2': '用户组添加', 'title': '用户组添加', 'users': users})
return context
def form_valid(self, form):
usergroup = form.save()
users_id_list = self.request.POST.getlist('users', [])
users = [get_object_or_404(User, id=user_id) for user_id in users_id_list]
usergroup.created_by = self.request.user.username or 'Admin'
usergroup.user_set.add(*tuple(users))
usergroup.save()
return super(UserGroupAddView, self).form_valid(form)
class UserGroupUpdateView(UpdateView):
pass
class UserGroupDetailView(DetailView):
pass
class UserGroupDeleteView(DeleteView):
pass