重构1 部门添加

pull/6/head
guanghongwei 2015-03-02 18:27:48 +08:00
parent 458ca42f22
commit d90b59191f
9 changed files with 344 additions and 85 deletions

View File

@ -1,15 +1,17 @@
from django.db import models
class UserGroup(models.Model):
GROUP_TYPE_CHOICES = (
('P', 'PrivateGroup'),
('M', 'ManageGroup'),
('A', 'AuthorizeGroup'),
)
class DEPT(models.Model):
name = models.CharField(max_length=80, unique=True)
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):
return self.name
class UserGroup(models.Model):
name = models.CharField(max_length=80, unique=True)
dept = models.ForeignKey(DEPT)
comment = models.CharField(max_length=160, blank=True, null=True)
def __unicode__(self):
@ -19,21 +21,21 @@ class UserGroup(models.Model):
class User(models.Model):
USER_ROLE_CHOICES = (
('SU', 'SuperUser'),
('GA', 'GroupAdmin'),
('DA', 'DeptAdmin'),
('CU', 'CommonUser'),
)
username = models.CharField(max_length=80, unique=True)
password = models.CharField(max_length=100)
name = models.CharField(max_length=80)
email = models.EmailField(max_length=75, null=True, blank=True)
email = models.EmailField(max_length=75)
role = models.CharField(max_length=2, choices=USER_ROLE_CHOICES, default='CU')
user_group = models.ManyToManyField(UserGroup)
dept = models.ForeignKey(DEPT)
group = models.ManyToManyField(UserGroup)
ldap_pwd = models.CharField(max_length=100)
ssh_key_pwd = models.CharField(max_length=100)
ssh_pwd = models.CharField(max_length=100)
is_active = models.BooleanField(default=True)
last_login = models.IntegerField(default=0)
date_joined = models.IntegerField()
last_login = models.DateTimeField(null=True)
date_joined = models.DateTimeField(null=True)
def __unicode__(self):
return self.username

View File

@ -11,6 +11,8 @@ urlpatterns = patterns('juser.views',
(r'^group_add/$', 'group_add'),
(r'^group_add_ajax/$', 'group_add_ajax'),
(r'^group_list/$', 'group_list'),
(r'^dept_list/$', 'dept_list'),
(r'^dept_add/$', 'dept_add'),
(r'^user_detail/$', 'user_detail'),
(r'^user_del/$', 'user_del'),
(r'^user_edit/$', 'user_edit'),

View File

@ -17,7 +17,7 @@ from django.template import RequestContext
from django.http import HttpResponse
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from juser.models import UserGroup, User
from juser.models import UserGroup, User, DEPT
from connect import PyCrypt, KEY
from connect import BASE_DIR
from connect import CONF
@ -61,12 +61,12 @@ def gen_sha512(salt, password):
return crypt.crypt(password, '$6$%s$' % salt)
def group_db_add(**kwargs):
group_name = kwargs.get('name')
group = UserGroup.objects.filter(name=group_name)
def db_add_group(**kwargs):
name = kwargs.get('name')
group = UserGroup.objects.filter(name=name)
if group:
raise AddError('Group %s have been exist .' % group_name)
UserGroup.objects.create(**kwargs)
raise AddError(u'鐢ㄦ埛缁 %s 宸茬粡瀛樺湪' % name)
UserGroup(**kwargs).save()
def group_add_user(group_name, user_id=None, username=None):
@ -210,32 +210,75 @@ def ldap_del_user(username):
# ldap_conn.add(group_dn, group_attr)
# def group_add_ajax(request):
# group_type = request.POST.get('type', 'A')
# users_all = User.objects.all()
# if group_type == 'A':
# users = users_all
# else:
# users = [user for user in users_all if not user.user_group.filter(type='M')]
#
# return render_to_response('juser/group_add_ajax.html', locals(), context_instance=RequestContext(request))
def dept_add(request):
header_title, path1, path2 = '娣诲姞閮ㄩ棬', '鐢ㄦ埛绠$悊', '娣诲姞閮ㄩ棬'
if request.method == 'POST':
name = request.POST.get('name', '')
comment = request.POST.get('comment', '')
try:
if not name:
raise AddError('閮ㄩ棬鍚嶇О涓嶈兘涓虹┖')
if DEPT.objects.filter(name=name):
raise AddError(u'閮ㄩ棬鍚嶇О %s 宸插瓨鍦' % name)
except AddError, e:
error = e
else:
DEPT(name=name, comment=comment).save()
msg = u'娣诲姞閮ㄩ棬 %s 鎴愬姛' % name
return render_to_response('juser/dept_add.html', locals(), context_instance=RequestContext(request))
def dept_list(request):
header_title, path1, path2 = '鏌ョ湅閮ㄩ棬', '鐢ㄦ埛绠$悊', '鏌ョ湅閮ㄩ棬'
contact_list = DEPT.objects.all()
p = paginator = Paginator(contact_list, 10)
try:
current_page = int(request.GET.get('page', '1'))
except ValueError:
current_page = 1
page_range = page_list_return(len(p.page_range), current_page)
try:
contacts = paginator.page(current_page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(paginator.num_pages)
return render_to_response('juser/dept_list.html', locals(), context_instance=RequestContext(request))
def group_add(request, group_type_select='A'):
error = ''
msg = ''
header_title, path1, path2 = '娣诲姞灞炵粍 | Group Add', '鐢ㄦ埛绠$悊', '娣诲姞鐢ㄦ埛缁'
group_types = {
'M': '閮ㄩ棬',
'A': '鐢ㄦ埛缁',
}
users_all = User.objects.all()
if group_type_select == 'M':
users = [user for user in users_all if not user.user_group.filter(type='M')]
else:
users = users_all
header_title, path1, path2 = '娣诲姞灞炵粍', '鐢ㄦ埛绠$悊', '娣诲姞鐢ㄦ埛缁'
user_all = User.objects.all()
dept_all = DEPT.objects.all()
if request.method == 'POST':
group_name = request.POST.get('group_name', '')
group_type = request.POST.get('group_type', 'A')
dept_id = request.POST.get('dept_id', '')
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=group_type)
if '' in [group_name, dept_id]:
error = u'缁勫悕 鎴 閮ㄩ棬 涓嶈兘涓虹┖'
raise AddError(error)
group_db_add(name=group_name, comment=comment)
for user_id in users_selected:
group_add_user(group_name, user_id=user_id)
@ -249,17 +292,6 @@ def group_add(request, group_type_select='A'):
return render_to_response('juser/group_add.html', locals(), context_instance=RequestContext(request))
def group_add_ajax(request):
group_type = request.POST.get('type', 'A')
users_all = User.objects.all()
if group_type == 'A':
users = users_all
else:
users = [user for user in users_all if not user.user_group.filter(type='M')]
return render_to_response('juser/group_add_ajax.html', locals(), context_instance=RequestContext(request))
def group_list(request):
header_title, path1, path2 = '鏌ョ湅灞炵粍 | Show Group', '鐢ㄦ埛绠$悊', '鏌ョ湅鐢ㄦ埛缁'
groups = contact_list = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('type')
@ -442,21 +474,17 @@ def user_add(request):
error = ''
msg = ''
header_title, path1, path2 = '娣诲姞鐢ㄦ埛 | User Add', '鐢ㄦ埛绠$悊', '娣诲姞鐢ㄦ埛'
user_role = {'SU': u'瓒呯骇绠$悊鍛', 'GA': u'绠$悊鍛', 'CU': u'鏅氱敤鎴'}
manage_groups = UserGroup.objects.filter(type='M')
auth_groups = UserGroup.objects.filter(type='A')
user_role = {'SU': u'瓒呯骇绠$悊鍛', 'DA': u'閮ㄩ棬绠$悊鍛', 'CU': u'鏅氱敤鎴'}
dept_all = DEPT.objects.all()
if request.method == 'POST':
username = request.POST.get('username', None)
password = request.POST.get('password', '')
name = request.POST.get('name', None)
email = request.POST.get('email', '')
manage_group_id = request.POST.get('manage_group')
dept_id = request.POST.get('dept_id')
auth_groups = request.POST.getlist('groups', None)
groups = auth_groups
groups.append(manage_group_id)
groups_str = ' '.join(auth_groups)
role_post = request.POST.get('role', 'CU')
ssh_pwd = request.POST.get('ssh_pwd', '')
ssh_key_pwd = request.POST.get('ssh_key_pwd', '')
is_active = request.POST.get('is_active', '1')
ldap_pwd = gen_rand_pwd(16)

BIN
static/img/user.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -0,0 +1,133 @@
{% extends 'base.html' %}
{% 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>濉啓鍩烘湰淇℃伅</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">
<form id="deptForm" method="post" class="form-horizontal" action="">
{% if error %}
<div class="alert alert-warning text-center">{{ error }}</div>
{% endif %}
{% if msg %}
<div class="alert alert-success text-center">{{ msg }}</div>
{% endif %}
<div class="form-group">
<label for="name" class="col-sm-2 control-label">閮ㄩ棬鍚嶇О<span class="red-fonts">*</span></label>
<div class="col-sm-8">
{% if error %}
<input id="name" name="name" placeholder="Dept name" type="text" class="form-control" value="{{ name }}">
{% else %}
<input id="name" name="name" placeholder="Dept name" type="text" class="form-control">
{% endif %}
</div>
</div>
{# <div class="hr-line-dashed"></div>#}
{# <div class="form-group">#}
{# <label for="users" 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">
{% if error %}
<input id="comment" name="comment" placeholder="Comment" type="text" class="form-control" value="{{ comment }}">
{% else %}
<input id="comment" name="comment" placeholder="Comment" type="text" class="form-control">
{% endif %}
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-white" type="reset">鍙栨秷</button>
<button id="submit_button" class="btn btn-primary" type="submit">纭淇濆瓨</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$('#deptForm').validator({
timely: 2,
theme: "yellow_right_effect",
fields: {
"name": {
rule: "required",
tip: "杈撳叆閮ㄩ棬鍚嶇О",
ok: "",
msg: {required: "蹇呴』濉啓!"}
}
},
valid: function(form) {
form.submit();
}
});
function change_type(type){
$.post('/juser/group_add_ajax/',
{'type': type},
function(data){
$('#users').html(data)
})
}
$(document).ready(function(){
$("#submit_button").click(function(){
$('#users_selected option').each(function(){
$(this).prop('selected', true)
})
})
})
</script>
{% endblock %}

View File

@ -0,0 +1,110 @@
{% 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> 鏌ョ湅閮ㄩ棬 </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/dept_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">閮ㄩ棬鍚嶇О</th>
<th class="text-center">鎴愬憳鏁伴噺</th>
<th class="text-center">澶囨敞</th>
<th class="text-center">鎿嶄綔</th>
</tr>
</thead>
<tbody>
{% for dept in contacts.object_list %}
<tr class="gradeX">
<td class="text-center"> {{ dept.name }} </td>
<td class="text-center"> {{ dept.name }} </td>
<td class="text-center"> {{ dept.comment }} </td>
<td class="text-center">
<a title="[ {{ dept.name }} ] 鎴愬憳淇℃伅" href="../dept_detail/?id={{ group.id }}" class="iframe btn btn-xs btn-primary">鎴愬憳</a>
<a href="../dept_edit/?id={{ group.id }}" class="btn btn-xs btn-info">缂栬緫</a>
<a href="../dept_del/?id={{ group.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>
{% endblock %}

View File

@ -7,7 +7,7 @@
<div class="col-lg-10">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>濉啓鍩烘湰淇℃伅 <small> Fill group info.</small></h5>
<h5>濉啓鍩烘湰淇℃伅</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
@ -42,15 +42,11 @@
</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>
<label for="dept_id" 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" onchange="change_type(this.value)">
{% for t, type_name in group_types.items %}
{% ifequal t group_type_select %}
<option value="{{ t }}" selected>{{ type_name }}</option>
{% else %}
<option value="{{ t }}">{{ type_name }}</option>
{% endifequal %}
<select id="dept_id" name="dept_id" class="form-control m-b">
{% for dept in dept_all %}
<option value="{{ dept.id }}" selected>{{ dept.name }}</option>
{% endfor %}
</select>
</div>
@ -60,7 +56,7 @@
<label for="users" 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 %}
{% for user in user_all %}
<option value="{{ user.id }}">{{ user.name }}</option>
{% endfor %}
</select>

View File

@ -9,7 +9,7 @@
<div class="col-lg-10">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>濉啓鍩烘湰淇℃伅 <small> Add user info.</small></h5>
<h5>濉啓鍩烘湰淇℃伅</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
@ -74,12 +74,8 @@
<label for="manage_group" class="col-lg-2 control-label">閮ㄩ棬<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<select id="manage_group" name="manage_group" class="form-control m-b">
{% for group in manage_groups %}
{% ifequal group.id manage_group_id %}
<option value="{{ group.id }}" selected>{{ group.name }}</option>
{% else %}
<option value="{{ group.id }}">{{ group.name }}</option>
{% endifequal %}
{% for dept in dept_all %}
<option value="{{ dept.id }}">{{ dept.name }}</option>
{% endfor %}
</select>
</div>
@ -89,7 +85,7 @@
<label for="groups" class="col-lg-2 control-label">灏忕粍</label>
<div class="col-sm-8">
<select id="groups" name="groups" class="form-control m-b" multiple size="12">
{% for group in auth_groups %}
{% for group in groups %}
{% if groups_str %}
{% if group.id|int2str in groups_str %}
<option value="{{ group.id }}" selected>{{ group.name }}</option>
@ -123,16 +119,6 @@
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="ssh_pwd" class="col-sm-2 control-label">SSH瀵嗙爜</label>
<div class="col-sm-8">
<input id="ssh_pwd" name="ssh_pwd" type="password" placeholder="SSH Password" class="form-control" value="{{ ssh_pwd }}">
<span class="help-block m-b-none">
濡傛灉浣跨敤password鏂瑰紡璇ュ瘑鐮佹槸鐢ㄦ埛鍦ㄥ悗绔湇鍔″櫒鐨勫瘑鐮
</span>
</div>
</div>
<div class="hr-line-dashed"></div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email<span class="red-fonts">*</span></label>
<div class="col-sm-8">

View File

@ -16,8 +16,10 @@
<ul class="nav nav-second-level">
<li id="user_list"><a href="/juser/user_list/">鏌ョ湅鐢ㄦ埛<span class="label label-primary pull-right">16/24</span></a></li>
<li id="user_add"><a href="/juser/user_add/">娣诲姞鐢ㄦ埛</a></li>
<li id="group_list"><a href="/juser/group_list/">鏌ョ湅灞炵粍</a></li>
<li id="group_add"><a href="/juser/group_add/">娣诲姞灞炵粍</a></li>
<li id="dept_list"><a href="/juser/dept_list/">鏌ョ湅閮ㄩ棬</a></li>
<li id="dept_add"><a href="/juser/dept_add/">娣诲姞閮ㄩ棬</a></li>
<li id="group_list"><a href="/juser/group_list/">鏌ョ湅灏忕粍</a></li>
<li id="group_add"><a href="/juser/group_add/">娣诲姞灏忕粍</a></li>
</ul>
</li>
<li id="jasset">