修改用户组视图

pull/26/head
ibuler 2015-08-28 00:33:54 +08:00
parent fd5041965b
commit 10d96a9767
11 changed files with 127 additions and 186 deletions

View File

@ -152,35 +152,35 @@ def page_list_return(total, current=1):
return range(min_page, max_page+1)
def pages(posts, r):
def pages(post_objects, request):
"""
page public function , return page's object tuple
分页公用函数返回分页的对象元组
"""
contact_list = posts
p = paginator = Paginator(contact_list, 10)
paginator = Paginator(post_objects, 10)
try:
current_page = int(r.GET.get('page', '1'))
current_page = int(request.GET.get('page', '1'))
except ValueError:
current_page = 1
page_range = page_list_return(len(p.page_range), current_page)
page_range = page_list_return(len(paginator.page_range), current_page)
try:
contacts = paginator.page(current_page)
page_objects = paginator.page(current_page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
page_objects = paginator.page(paginator.num_pages)
if current_page >= 5:
show_first = 1
else:
show_first = 0
if current_page <= (len(p.page_range) - 3):
if current_page <= (len(paginator.page_range) - 3):
show_end = 1
else:
show_end = 0
return contact_list, p, contacts, page_range, current_page, show_first, show_end
return post_objects, paginator, page_objects, page_range, current_page, show_first, show_end
class Jtty(object):

View File

@ -116,10 +116,13 @@ def bool2str(value):
# return True
#
@register.filter(name='member_count')
def member_count(group_id):
group = UserGroup.objects.get(id=group_id)
@register.filter(name='members_count')
def members_count(group_id):
group = get_object(UserGroup, id=group_id)
if group:
return group.user_set.count()
else:
return 0
@register.filter(name='group_user_count')

View File

@ -13,6 +13,11 @@ class UserGroup(models.Model):
def get_user(self):
return self.user_set.all()
def update(self, **kwargs):
for key, value in kwargs.items():
self.__setattr__(key, value)
self.save()
class User(models.Model):
USER_ROLE_CHOICES = (

View File

@ -8,10 +8,9 @@ urlpatterns = patterns('juser.views',
# url(r'^blog/', include('blog.urls')),
(r'^group_add/$', group_add),
(r'^group_list/$', view_splitter, {'su': group_list, 'adm': group_list_adm}),
(r'^group_detail/$', 'group_detail'),
(r'^group_del/$', view_splitter, {'su': group_del, 'adm': group_del_adm}),
(r'^group_del_ajax/$', 'group_del_ajax'),
(r'^group_list/$', group_list),
(r'^group_del/$', group_del),
(r'^group_del_ajax', group_del_ajax),
(r'^group_edit/$', view_splitter, {'su': group_edit, 'adm': group_edit_adm}),
(r'^user_add/$', view_splitter, {'su': user_add, 'adm': user_add_adm}),
(r'^user_list/$', view_splitter, {'su': user_list, 'adm': user_list_adm}),

View File

@ -35,6 +35,20 @@ def db_add_group(**kwargs):
group_add_user(group, user_id)
def group_update_member(group_id, users_id_list):
"""
user group update member
用户组更新成员
"""
group = get_object(UserGroup, id=group_id)
if group:
group.user_set.clear()
for user_id in users_id_list:
user = get_object(UserGroup, id=user_id)
if isinstance(user, UserGroup):
group.user_set.add(user)
def db_add_user(**kwargs):
"""
add a user in database

View File

@ -24,6 +24,10 @@ def chg_role(request):
@require_role(role='super')
def group_add(request):
"""
group add view for route
添加用户组的视图
"""
error = ''
msg = ''
header_title, path1, path2 = '添加用户组', '用户管理', '添加用户组'
@ -55,51 +59,27 @@ def group_add(request):
@require_role(role='super')
def group_list(request):
header_title, path1, path2 = '查看小组', '用户管理', '查看小组'
"""
list user group
用户组列表
"""
header_title, path1, path2 = '查看用户组', '用户管理', '查看用户组'
keyword = request.GET.get('search', '')
did = request.GET.get('did', '')
contact_list = UserGroup.objects.all().order_by('name')
if did:
dept = DEPT.objects.filter(id=did)
if dept:
dept = dept[0]
contact_list = dept.usergroup_set.all()
user_group_list = UserGroup.objects.all().order_by('name')
if keyword:
contact_list = contact_list.filter(Q(name__icontains=keyword) | Q(comment__icontains=keyword))
user_group_list = user_group_list.filter(Q(name__icontains=keyword) | Q(comment__icontains=keyword))
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(contact_list, request)
contacts, p, contacts, page_range, current_page, show_first, show_end = pages(user_group_list, request)
return render_to_response('juser/group_list.html', locals(), context_instance=RequestContext(request))
@require_role(role='admin')
def group_list_adm(request):
header_title, path1, path2 = '查看部门小组', '用户管理', '查看小组'
keyword = request.GET.get('search', '')
did = request.GET.get('did', '')
user, dept = get_session_user_dept(request)
contact_list = dept.usergroup_set.all().order_by('name')
if keyword:
contact_list = contact_list.filter(Q(name__icontains=keyword) | Q(comment__icontains=keyword))
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(contact_list, request)
return render_to_response('juser/group_list.html', locals(), context_instance=RequestContext(request))
@require_role(role='admin')
def group_detail(request):
group_id = request.GET.get('id', None)
if not group_id:
return HttpResponseRedirect('/')
group = UserGroup.objects.get(id=group_id)
users = group.user_set.all()
return render_to_response('juser/group_detail.html', locals(), context_instance=RequestContext(request))
@require_role(role='super')
def group_del(request):
"""
del a group
删除用户组
"""
group_id = request.GET.get('id', '')
if not group_id:
return HttpResponseRedirect('/')
@ -107,59 +87,56 @@ def group_del(request):
return HttpResponseRedirect('/juser/group_list/')
@require_role(role='admin')
def group_del_adm(request):
group_id = request.GET.get('id', '')
if not validate(request, user_group=[group_id]):
return HttpResponseRedirect('/juser/group_list/')
if not group_id:
return HttpResponseRedirect('/')
UserGroup.objects.filter(id=group_id).delete()
return HttpResponseRedirect('/juser/group_list/')
@require_role(role='admin')
@require_role(role='super')
def group_del_ajax(request):
group_ids = request.POST.get('group_ids')
group_ids = group_ids.split(',')
if request.session.get('role_id') == 1:
if not validate(request, user_group=group_ids):
return "error"
for group_id in group_ids:
UserGroup.objects.filter(id=group_id).delete()
return HttpResponse('删除成功')
# @require_role(role='admin')
# def group_list_adm(request):
# header_title, path1, path2 = '查看部门小组', '用户管理', '查看小组'
# keyword = request.GET.get('search', '')
# did = request.GET.get('did', '')
# user, dept = get_session_user_dept(request)
# contact_list = dept.usergroup_set.all().order_by('name')
#
# if keyword:
# contact_list = contact_list.filter(Q(name__icontains=keyword) | Q(comment__icontains=keyword))
#
# contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(contact_list, request)
# return render_to_response('juser/group_list.html', locals(), context_instance=RequestContext(request))
def group_update_member(group_id, users_id_list):
group = UserGroup.objects.filter(id=group_id)
if group:
group = group[0]
group.user_set.clear()
for user_id in users_id_list:
user = User.objects.get(id=user_id)
group.user_set.add(user)
#
# @require_role(role='admin')
# def group_detail(request):
# group_id = request.GET.get('id', None)
# if not group_id:
# return HttpResponseRedirect('/')
# group = UserGroup.objects.get(id=group_id)
# users = group.user_set.all()
# return render_to_response('juser/group_detail.html', locals(), context_instance=RequestContext(request))
@require_role(role='super')
def group_edit(request):
error = ''
msg = ''
header_title, path1, path2 = '修改小组信息', '用户管理', '编辑小组'
header_title, path1, path2 = '编辑用户组', '用户管理', '编辑用户组'
if request.method == 'GET':
group_id = request.GET.get('id', '')
group = UserGroup.objects.filter(id=group_id)
if group:
group = group[0]
dept_all = DEPT.objects.all()
user_group = get_object(UserGroup, id=group_id)
if user_group:
users_all = User.objects.all()
users_selected = group.user_set.all()
users = [user for user in users_all if user not in users_selected]
users_selected = user_group.user_set.all()
users_remain = [user for user in users_all if user not in users_selected]
return render_to_response('juser/group_edit.html', locals(), context_instance=RequestContext(request))
else:
group_id = request.POST.get('group_id', '')
group_name = request.POST.get('group_name', '')
dept_id = request.POST.get('dept_id', '')
comment = request.POST.get('comment', '')
users_selected = request.POST.getlist('users_selected')
@ -167,25 +144,31 @@ def group_edit(request):
try:
if '' in [group_id, group_name]:
raise ServerError('组名不能为空')
dept = DEPT.objects.filter(id=dept_id)
if dept:
dept = dept[0]
else:
raise ServerError('部门不存在')
user_group = get_object(UserGroup, id=group_id)
other_group = get_object(UserGroup, name=group_name)
if other_group and other_group.id != int(group_id):
raise ServerError(u'%s 用户组已存在' % group_name)
for user_id in users_selected:
users.extend(User.objects.filter(id=user_id))
user_group = UserGroup.objects.filter(id=group_id)
if user_group:
user_group.update(name=group_name, comment=comment, dept=dept)
user_group = user_group[0]
user_group.update(name=group_name, comment=comment)
user_group.user_set.clear()
user_group.user_set = users
except ServerError, e:
error = e
if not error:
return HttpResponseRedirect('/juser/group_list/')
else:
users_all = User.objects.all()
users_selected = user_group.user_set.all()
users_remain = [user for user in users_all if user not in users_selected]
return render_to_response('juser/group_edit.html', locals(), context_instance=RequestContext(request))
@require_role(role='admin')

View File

@ -3,6 +3,6 @@
Version <strong>2.0.0</strong> GPL.
</div>
<div>
<strong>Copyright</strong> Jumpserver.org Organization &copy; 2014-2015
<strong>Copyright</strong> Jumpserver.org Team &copy; 2014-2015
</div>
</div>

View File

@ -20,6 +20,7 @@
</a>
</div>
</div>
<div class="ibox-content">
<form id="groupForm" method="post" class="form-horizontal" action="">
{% if error %}
@ -106,23 +107,6 @@ $('#groupForm').validator({
}
});
function change_type(type){
$.post('/juser/group_add_ajax/',
{'type': type},
function(data){
$('#users').html(data)
})
}
function change_dept(dept_id){
$.get('/juser/dept_user_ajax/',
{'id': dept_id},
function(data){
$('#users').html(data)
})
}
$(document).ready(function(){
$("#submit_button").click(function(){

View File

@ -1,4 +0,0 @@
{% for user in users %}
<option value="{{ user.id }}">{{ user.name }}</option>
{% endfor %}

View File

@ -4,7 +4,7 @@
{% include 'nav_cat_bar.html' %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-10">
<div class="col-sm-10">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>填写基本信息</h5>
@ -15,12 +15,6 @@
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="#">未启用 1</a>
</li>
<li><a href="#">未启用 2</a>
</li>
</ul>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
@ -35,35 +29,19 @@
<div class="alert alert-success text-center">{{ msg }}</div>
{% endif %}
<div class="form-group">
<label for="group_name" class="col-sm-2 control-label">组名<span class="red-fonts">*</span></label>
<label for="group_name" class="col-sm-2 control-label">用户组名<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input name="group_id" type="text" class="form-control" value="{{ group.id }}" style="display: none">
<input id="group_name" name="group_name" placeholder="Group name" type="text" class="form-control" value="{{ group.name }}">
<input name="group_id" type="text" class="form-control" value="{{ user_group.id }}" style="display: none">
<input id="group_name" name="group_name" placeholder="Group name" type="text" class="form-control" value="{{ user_group.name }}">
</div>
</div>
{% ifequal session_role_id 2 %}
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="dept_id" class="col-sm-2 control-label">部门<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select id="dept_id" name="dept_id" class="form-control m-b" onchange="change_dept(this.value)">
{% for dept in dept_all %}
{% ifequal group.dept.id dept.id %}
<option value="{{ dept.id }}" selected>{{ dept.name }}</option>
{% else %}
<option value="{{ dept.id }}">{{ dept.name }}</option>
{% endifequal %}
{% endfor %}
</select>
</div>
</div>
{% endifequal %}
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="users" class="col-lg-2 control-label">用户</label>
<label for="users" class="col-sm-2 control-label">用户</label>
<div class="col-sm-3">
<select id="users" name="users" size="12" class="form-control m-b" multiple>
{% for user in users %}
{% for user in users_remain %}
<option value="{{ user.id }}">{{ user.name }}</option>
{% endfor %}
</select>
@ -88,7 +66,7 @@
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">备注</label>
<div class="col-sm-8">
<input id="comment" name="comment" placeholder="Comment" type="text" class="form-control" value="{{ group.comment }}">
<input id="comment" name="comment" placeholder="Comment" type="text" class="form-control" value="{{ user_group.comment }}">
</div>
</div>
@ -105,7 +83,9 @@
</div>
</div>
</div>
{% endblock %}
{% block self_footer_js %}
<script>
$('#groupForm').validator({
timely: 2,
@ -129,13 +109,6 @@ $('#groupForm').validator({
}
});
function change_type(type){
$.post('/juser/group_add_ajax/',
{'type': type},
function(data){
$('#users').html(data)
})
}
$(document).ready(function(){
$("#submit_button").click(function(){
@ -143,18 +116,6 @@ $(document).ready(function(){
$(this).prop('selected', true)
})
})
})
function change_dept(dept_id){
$.get('/juser/dept_user_ajax/',
{'id': dept_id},
function(data){
$('#users').html(data);
$('#users_selected').html('')
})
}
});
</script>
{% endblock %}

View File

@ -7,8 +7,9 @@
<div class="row">
<div class="col-lg-10">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5> 查看</h5>
<h5> 查看用户</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
@ -16,12 +17,6 @@
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<ul class="dropdown-menu dropdown-user">
<li><a href="#">未启用 1</a>
</li>
<li><a href="#">未启用 2</a>
</li>
</ul>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
@ -30,14 +25,14 @@
<div class="ibox-content">
<div class="">
<a target="_blank" href="/juser/group_add/" class="btn btn-sm btn-primary "> 添加</a>
<a id="del_btn" class="btn btn-sm btn-danger "> 删除所选 </a>
<a target="_blank" href="/juser/group_add/" class="btn btn-sm btn-primary "> 添加用户</a>
<a id="del_btn" class="btn btn-sm btn-danger "> 删除所选 </a>
<form id="search_form" method="get" action="" class="pull-right mail-search">
<div class="input-group">
<input type="text" class="form-control input-sm" id="search_input" name="search" placeholder="Search">
<div class="input-group-btn">
<button id='search_btn' type="submit" class="btn btn-sm btn-primary">
Search
—搜索—
</button>
</div>
</div>
@ -51,7 +46,6 @@
<input type="checkbox" id="select_all" onclick="selectAll()" name="select_all">
</th>
<th class="text-center">组名</th>
<th class="text-center">所属部门</th>
<th class="text-center">成员数目</th>
<th class="text-center">备注</th>
<th class="text-center">操作</th>
@ -64,8 +58,7 @@
<input type="checkbox" name="selected" value="{{ group.id }}">
</td>
<td class="text-center"> {{ group.name }} </td>
<td class="text-center"> {{ group.dept.name }} </td>
<td class="text-center"><a href="/juser/user_list/?gid={{ group.id }}"> {{ group.id | member_count }}</a> </td>
<td class="text-center"><a href="/juser/user_list/?gid={{ group.id }}"> {{ group.id | members_count }}</a> </td>
<td class="text-center"> {{ group.comment }} </td>
<td class="text-center">
<a href="../group_edit/?id={{ group.id }}" class="btn btn-xs btn-info">编辑</a>
@ -88,25 +81,28 @@
</div>
</div>
</div>
{% endblock %}
{% block self_footer_js %}
<script>
$(document).ready(function(){
var check_array = []
var check_array = [];
$('#del_btn').click(function(){
if (confirm("确定删除")) {
$(".gradeX input:checked").each(function() {check_array.push($(this).attr("value")) })
$(".gradeX input:checked").closest("tr").remove()
$(".gradeX input:checked").each(function() {
check_array.push($(this).attr("value"))
});
$.post("/juser/group_del_ajax/",
{group_ids: check_array.join(",")},
function(data){
function(result){
alert(result);
$(".gradeX input:checked").closest("tr").remove();
}
)
}
})
});
</script>
{% endblock %}