修改添加组时添加成员

pull/6/head
guanghongwei 2015-01-28 15:08:16 +08:00
parent 5d38a1996c
commit 5cd09a6503
11 changed files with 152 additions and 184 deletions

View File

@ -2,7 +2,8 @@
import time
from django import template
from juser.models import User, UserGroup
from django.db.models import Q
from juser.models import User
register = template.Library()
@ -24,16 +25,16 @@ def int2str(value):
def get_role(user_id):
user_role = {'SU': u'瓒呯骇绠$悊鍛', 'GA': u'缁勭鐞嗗憳', 'CU': u'鏅氱敤鎴'}
user = User.objects.get(id=user_id)
return user_role.get(user.role)
return user_role.get(str(user.role))
@register.filter(name='groups_str')
def groups_str(username):
groups = []
user = User.objects.get(username=username)
for group in user.user_group.filter(type='M'):
for group in user.user_group.filter(Q(type='A') | Q(type='M')):
groups.append(group.name)
return ','.join(groups)
return ' '.join(groups)
@register.filter(name='get_item')
@ -52,4 +53,15 @@ def bool2str(value):
@register.filter(name='perm_count')
def perm_count(user_id):
user = User.objects.get(id=int(user_id))
return user.perm_set.all().count()
return user.perm_set.all().count()
@register.filter(name='group_type_to_str')
def group_type_to_str(type_name):
group_types = {
'P': '绉佹湁缁',
'M': '绠$悊缁',
'A': '鎺堟潈缁',
}
return group_types.get(type_name)

View File

@ -9,7 +9,7 @@ class UserGroup(models.Model):
)
name = models.CharField(max_length=80, unique=True)
type = models.CharField(max_length=1, choices=GROUP_TYPE_CHOICES, default='U')
type = models.CharField(max_length=1, choices=GROUP_TYPE_CHOICES, default='P')
comment = models.CharField(max_length=160, blank=True, null=True)
def __unicode__(self):

View File

@ -15,6 +15,7 @@ from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q
from juser.models import UserGroup, User
from connect import PyCrypt, KEY
@ -117,37 +118,57 @@ def group_db_add(**kwargs):
group_name = kwargs.get('name')
group = UserGroup.objects.filter(name=group_name)
if group:
raise AddError
raise AddError('Group %s have been exist .' % group_name)
UserGroup.objects.create(**kwargs)
def user_group_add(username, group_name):
user = User.objects.get(username=username)
def add_user_to_group(username, group_name):
try:
user = User.objects.get(username=username)
group = UserGroup.objects.get(name=group_name)
except ObjectDoesNotExist:
raise AddError('User %s or group % does not exit. ' % username, group_name)
else:
groups = [group]
for g in user.user_group.all():
groups.append(g)
user.user_group = groups
def group_add_user(group_name, user_id):
group = UserGroup.objects.get(name=group_name)
groups = [group]
for g in user.user_group.all():
groups.append(g)
user.user_group = groups
user = User.objects.get(id=user_id)
group.user_set.add(user)
def group_add(request):
error = ''
msg = ''
header_title, path1, path2 = '娣诲姞灞炵粍 | Add Group', 'juser', 'group_add'
group_types = {
'P': '绉佹湁缁',
'M': '绠$悊缁',
'A': '鎺堟潈缁',
}
users = User.objects.all()
if request.method == 'POST':
group_name = request.POST.get('group_name', None)
comment = request.POST.get('comment', None)
group_name = request.POST.get('group_name', '')
group_type = request.POST.get('group_type', 'A')
users_selected = request.POST.getlist('users_selected', '')
comment = request.POST.get('comment', '')
try:
if not group_name:
error = u'缁勫悕涓嶈兘涓虹┖'
raise AddError
group_db_add(name=group_name, comment=comment, type='M')
group_db_add(name=group_name, comment=comment, type=group_type)
for user_id in users_selected:
group_add_user(group_name, user_id)
except AddError:
pass
except TypeError:
error = u'淇濆瓨鐢ㄦ埛澶辫触'
error = u'淇濆瓨鐢ㄦ埛澶辫触'
else:
msg = u'娣诲姞缁 %s 鎴愬姛' % group_name
@ -156,7 +177,7 @@ def group_add(request):
def group_list(request):
header_title, path1, path2 = '鏌ョ湅灞炵粍 | Show Group', 'juser', 'group_list'
groups = contact_list = UserGroup.objects.filter(type='M').order_by('id')
groups = contact_list = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('id')
p = paginator = Paginator(contact_list, 10)
try:
@ -467,8 +488,8 @@ def user_add(request):
date_joined=time_now)
server_add_user(username, password, ssh_key_pwd)
group_db_add(name=username, comment=username, type='U')
user_group_add(username=username, group_name=username)
group_db_add(name=username, comment=username, type='P')
add_user_to_group(username=username, group_name=username)
if LDAP_ENABLE:
ldap_add_user(username, ldap_pwd)
msg = u'娣诲姞鐢ㄦ埛 %s 鎴愬姛锛' % username

View File

@ -4,19 +4,6 @@
{% block content %}
<script type="text/javascript">
function move(from, to) {
$("#"+from+" option").each(function(){
if ( $(this).prop("selected") == true ) {
$("#"+to).append(this);
}
});
}
function move_all(from, to){
$("#"+from).children().each(function(){
$("#"+to).append(this);
});
}
function search_host(text){
$("#host_unperm").children().each(function(){$(this).remove();});

View File

@ -27,7 +27,7 @@
</div>
</div>
<div class="ibox-content">
<form method="post" class="form-horizontal" action="">
<form id="groupForm" method="post" class="form-horizontal" action="">
{% if error %}
<div class="alert alert-warning text-center">{{ error }}</div>
{% endif %}
@ -47,6 +47,44 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="group_type" class="col-sm-2 control-label">绫诲瀷<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select id="group_type" name="group_type" class="form-control m-b">
{% for t, type_name in group_types.items %}
{% ifequal t type_name %}
<option value="{{ t }}" selected>{{ type_name }}</option>
{% else %}
<option value="{{ t }}">{{ type_name }}</option>
{% endifequal %}
{% endfor %}
</select>
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="groups" class="col-lg-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 %}
<option value="{{ user.id }}">{{ user.name }}</option>
{% endfor %}
</select>
</div>
<div class="col-sm-1">
<div class="btn-group" style="margin-top: 50px;">
<button type="button" class="btn btn-white" onclick="move('users', 'users_selected')"><i class="fa fa-chevron-right"></i></button>
<button type="button" class="btn btn-white" onclick="move('users_selected', 'users')"><i class="fa fa-chevron-left"></i> </button>
</div>
</div>
<div class="col-sm-3">
<div>
<select id="users_selected" name="users_selected" class="form-control m-b" size="12" multiple>
</select>
</div>
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">澶囨敞</label>
<div class="col-sm-8">
@ -67,4 +105,28 @@
</div>
</div>
</div>
<script>
$('#groupForm').validator({
timely: 2,
theme: "yellow_right_effect",
fields: {
"group_name": {
rule: "required",
tip: "杈撳叆缁勫悕",
ok: "",
msg: {required: "蹇呴』濉啓!"}
},
"group_type": {
rule: "checked",
tip: "閫夋嫨缁勭被鍨",
ok: "",
msg: {required: "鑷冲皯閫夋嫨涓涓粍!"}
}
},
valid: function(form) {
form.submit();
}
});
</script>
{% endblock %}

View File

@ -20,23 +20,22 @@
<table class="table table-striped table-bordered table-hover " id="editable" >
<thead>
<tr>
<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>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr class="gradeX">
<td class="text-center">ID</td>
<td class="text-center">{{ group.id }}</td>
</tr>
<tr class="gradeX">
<td class="text-center">缁勫悕</td>
<td class="text-center">{{ group.name }}</td>
</tr>
<tr class="gradeX">
<td class="text-center">澶囨敞</td>
<td class="text-center">{{ group_comment }}</td>
<td class="text-center">{{ user.username }}</td>
<td class="text-center">{{ user.name }}</td>
<td class="text-center">{{ user.id|get_role }}</td>
</tr>
{% endfor %}
</tbody>
</tbody>
</table>
</div>
</div>

View File

@ -39,6 +39,7 @@
<th class="text-center"><input type="checkbox" class="i-checks" name=""></th>
<th class="text-center">ID</th>
<th class="text-center">缁勫悕</th>
<th class="text-center">绫诲瀷</th>
<th class="text-center">澶囨敞</th>
<th class="text-center">鎿嶄綔</th>
</tr>
@ -49,9 +50,10 @@
<td class="text-center"><input type="checkbox" class="i-checks" name=""></td>
<td class="text-center"> {{ group.id }} </td>
<td class="text-center"> {{ group.name }} </td>
<td class="text-center"> {{ group.type|group_type_to_str }} </td>
<td class="text-center"> {{ group.comment }} </td>
<td class="text-center">
<a href="../group_detail/?id={{ group.id }}" class="iframe btn btn-xs btn-primary">璇︽儏</a>
<a href="../group_detail/?id={{ group.id }}" class="iframe btn btn-xs btn-primary">鎴愬憳</a>
<a href="../group_edit/?id={{ group.id }}" class="btn btn-xs btn-info">缂栬緫</a>
<a href="../group_del/?id={{ group.id }}" class="btn btn-xs btn-danger">鍒犻櫎</a>
</td>

View File

@ -54,9 +54,9 @@
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="ssh_key_pwd1" class="col-sm-2 control-label">瀵嗛挜瀵嗙爜<span class="red-fonts">*</span></label>
<label for="ssh_key_pwd" class="col-sm-2 control-label">瀵嗛挜瀵嗙爜<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input id="ssh_key_pwd1" name="ssh_key_pwd" placeholder="SSH Key Password" type="password" class="form-control" value="{{ ssh_key_pwd1 }}">
<input id="ssh_key_pwd" name="ssh_key_pwd" placeholder="SSH Key Password" type="password" class="form-control" value="{{ ssh_key_pwd }}">
<span class="help-block m-b-none">
鐧婚檰 Jumpserver 浣跨敤鐨凷SH瀵嗛挜鐨勫瘑鐮
</span>
@ -173,7 +173,7 @@ $('#userForm').validator({
ok: "",
msg: {required: "蹇呴』濉啓!"}
},
"ssh_key_pwd1": {
"ssh_key_pwd": {
rule: "required;length[6~50]",
tip: "ssh绉侀挜瀵嗙爜",
ok: "",

View File

@ -44,7 +44,6 @@
<th class="text-center">濮撳悕</th>
<th class="text-center">灞炵粍</th>
<th class="text-center">瑙掕壊</th>
<th class="text-center">Email</th>
<th class="text-center">婵娲</th>
<th class="text-center">鎿嶄綔</th>
</tr>
@ -60,7 +59,6 @@
<td class="text-center"> {{ user.name }} </td>
<td class="text-center"> {{ user.username|groups_str }}</td>
<td class="text-center">{{ user.id|get_role }}</td>
<td class="text-center">{{ user.email }}</td>
<td class="text-center">{{ user.is_active|bool2str }}</td>
<td class="text-center">
<a href="../user_detail/?id={{ user.id }}" class="iframe btn btn-xs btn-primary">璇︽儏</a>

View File

@ -1,130 +0,0 @@
{% extends 'base.html' %}
{% load mytags %}
{% block content %}
{% include 'nav_cat_bar.html' %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-lg-10">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5> 鏌ョ湅鐢ㄦ埛 <small> show user info.</small> </h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<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>
</div>
</div>
<div class="ibox-content">
<div class="">
<a target="_blank" href="/juser/user_add/" class="btn btn-sm btn-primary "> 娣诲姞 </a>
</div>
<table class="table table-striped table-bordered table-hover " id="editable" >
<thead>
<tr>
<th class="text-center"><input type="checkbox" class="i-checks" name=""></th>
<th class="text-center">ID</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">Email</th>
<th class="text-center">婵娲</th>
<th class="text-center">鎿嶄綔</th>
</tr>
</thead>
<tbody>
{% for user in contacts.object_list %}
<tr class="gradeX">
<td class="text-center"><input type="checkbox" class="i-checks" name=""></td>
<td class="text-center"> {{ user.id }} </td>
<td class="text-center"> {{ user.username }} </td>
<td class="text-center"> {{ user.name }} </td>
<td class="text-center"> {{ user.username|groups_str }}</td>
<td class="text-center">{{ user.id|get_role }}</td>
<td class="text-center">{{ user.email }}</td>
<td class="text-center">{{ user.is_active|bool2str }}</td>
<td class="text-center">
<a href="../user_detail/?id={{ user.id }}" class="iframe btn btn-xs btn-primary">璇︽儏</a>
<a href="../user_edit/?id={{ user.id }}" class="btn btn-xs btn-info">缂栬緫</a>
<a href="../user_del/?id={{ user.id }}" class="btn btn-xs btn-danger">鍒犻櫎</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="row">
<div class="col-sm-6">
<div class="dataTables_info" id="editable_info" role="status" aria-live="polite">
Showing {{ contacts.start_index }} to {{ contacts.end_index }} of {{ p.count }} entries
</div>
</div>
<div class="col-sm-6">
<div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
<ul class="pagination" style="margin-top: 0; float: right">
{% if contacts.has_previous %}
<li class="paginate_button previous" aria-controls="editable" tabindex="0" id="editable_previous">
<a href="?page={{ contacts.previous_page_number }}">Previous</a>
</li>
{% else %}
<li class="paginate_button previous disabled" aria-controls="editable" tabindex="0" id="editable_previous">
<a href="#">Previous</a>
</li>
{% endif %}
{% for page in p.page_range %}
{% ifequal offset1 page %}
<li class="paginate_button active" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="绗瑊{ page }}椤">{{ page }}</a></li>
{% else %}
<li class="paginate_button" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="绗瑊{ page }}椤">{{ page }}</a></li>
{% endifequal %}
{% endfor %}
{% if contacts.has_next %}
<li class="paginate_button next" aria-controls="editable" tabindex="0" id="editable_next">
<a href="?page={{ contacts.next_page_number }}">Next</a>
</li>
{% else %}
<li class="paginate_button next disabled" aria-controls="editable" tabindex="0" id="editable_next">
<a href="#">Next</a>
</li>
{% endif %}
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){
$(".iframe").colorbox({iframe:true, width:"70%", height:"70%"});
});
</script>
<script>
$(document).ready(function(){
$('input').iCheck({
checkboxClass: 'icheckbox_square',
radioClass: 'iradio_square',
increaseArea: '20%' // optional
});
});
</script>
{% endblock %}

View File

@ -21,7 +21,7 @@
$(document).ready(function(){
$('.i-checks').iCheck({
checkboxClass: 'icheckbox_square-green',
radioClass: 'iradio_square-green',
radioClass: 'iradio_square-green'
});
});
@ -42,9 +42,26 @@
}
function move(from, to) {
$("#"+from+" option").each(function(){
if ( $(this).prop("selected") == true ) {
$("#"+to).append(this);
}
});
}
function move_all(from, to){
$("#"+from).children().each(function(){
$("#"+to).append(this);
});
}
</script>
<!-- pop windows -->
<script src="/static/js/jquery.colorbox.js"></script>