[功能] 新增公告管理模块,对GET users API轻微修改,筛管理员添加超级管理员包含

This commit is contained in:
sxw 2015-08-08 17:09:15 +08:00
parent c3fcb119c4
commit 4578426f99
4 changed files with 252 additions and 1 deletions

View File

@ -129,7 +129,7 @@ class UserAPIView(APIView):
admin_type = request.GET.get("admin_type", None)
if admin_type:
try:
user = user.filter(admin_type=int(admin_type))
user = user.filter(admin_type__gte=int(admin_type))
except ValueError:
return error_response(u"参数错误")
keyword = request.GET.get("keyword", None)

View File

@ -0,0 +1,165 @@
require(["jquery", "avalon", "csrf", "bs_alert", "validation"], function ($, avalon, csrfHeader, bs_alert) {
avalon.vmodels.user_list = null;
// avalon:定义模式 user_list
avalon.ready(function () {
var vm = avalon.define({
$id: "user_list",
//通用变量
user_list: [], // 用户列表数据项
previous_page: 0, // 之前的页数
next_page: 0, // 之后的页数
page: 1, // 当前页数
isEditing: 0, // 正在编辑的公告的ID 为零说明未在编辑
page_count: 1, // 总页数
user_type: ["一般用户", "管理员", "超级管理员"],
key_word: "",
showAdminOnly: false,
//编辑区域同步变量
username: "",
real_name: "",
email: "",
admin_type: 0,
id: 0,
last_login: "",
create_time: "",
getNext: function () {
if (!vm.next_page)
return;
getPageData(vm.page + 1);
},
getPrevious: function () {
if (!vm.previous_page)
return;
getPageData(vm.page - 1);
},
getBtnClass: function (btn) { //上一页/下一页按钮启用禁用逻辑
if (btn) {
return vm.next_page ? "btn btn-primary" : "btn btn-primary disabled";
}
else {
return vm.previous_page ? "btn btn-primary" : "btn btn-primary disabled";
}
},
enEdit: function (el) { //点击编辑按钮的事件,显示/隐藏编辑区
vm.username = el.username;
vm.real_name = el.real_name;
vm.admin_type = el.admin_type;
vm.email = el.email;
vm.id = el.id;
if (vm.isEditing == el.id)
vm.isEditing = 0;
else
vm.isEditing = el.id;
},
getPage: function (page_index) {
getPageData(page_index);
}
});
vm.$watch("showAdminOnly", function () {
getPageData(1);
});
avalon.scan();
getPageData(1); //用户列表初始化
//Ajax get数据
function getPageData(page) {
var url = "/api/users/?paging=true&page=" + page + "&page_size=10";
if (vm.showAdminOnly == true)
url += "&admin_type=1";
if (vm.key_word != "")
url += "&keyword=" + vm.key_word;
$.ajax({
beforeSend: csrfHeader,
url: url,
dataType: "json",
method: "get",
success: function (data) {
if (!data.code) {
vm.user_list = data.data.results;
vm.page_count = data.data.total_page;
vm.previous_page = data.data.previous_page;
vm.next_page = data.data.next_page;
vm.page = page;
}
else {
bs_alert(data.data);
}
}
});
}
$("#edit_user-form")
.formValidation({
framework: "bootstrap",
fields: {
username: {
validators: {
notEmpty: {
message: "请填写用户名"
},
stringLength: {
min: 3,
max: 30,
message: '用户名长度必须在3到30位之间'
}
}
},
real_name: {
validators: {
notEmpty: {
message: "请填写真实姓名"
}
}
},
email: {
validators: {
notEmpty: {
message: "请填写电子邮箱邮箱地址"
},
emailAddress: {
message: "请填写有效的邮箱地址"
}
}
},
password: {
validators: {
stringLength: {
min: 6,
max: 30,
message: '密码长度必须在6到30位之间'
}
}
}
}
}
).on('success.form.fv', function (e) {
e.preventDefault();
var data = {
username: vm.username,
real_name: vm.real_name,
email: vm.email,
id: vm.id,
admin_type: vm.admin_type
};
if ($("#password").val() !== "")
data.password = $("#password").val();
$.ajax({
beforeSend: csrfHeader,
url: "/api/admin/user/",
data: data,
dataType: "json",
method: "put",
success: function (data) {
if (!data.code) {
bs_alert("提交成功!");
getPageData(1);
$("#password").val("");
} else {
bs_alert(data.data);
}
}
})
});
});
})

View File

@ -97,6 +97,13 @@
<li class="list-group-item" id="li-contest-add_contest">
<a href="#contest/add_contest">创建比赛</a>
</li>
<li class="list-group-header">用户管理</li>
<li class="list-group-item" id="li-user-user_list">
<a href="#user/user_list">用户列表</a>
</li>
<li class="list-group-item" id="li-user-user_group">
<a href="#user/user_group">用户分组</a>
</li>
</ul>
</div>
<!-- admin left end -->

View File

@ -0,0 +1,79 @@
<div ms-controller="user_list" class="col-md-9">
<h1>User</h1>
<div class="text-right">
<form class="form-inline" onsubmit="return false;">
<div class="form-group-sm">
<label>搜索</label>
<input name="keyWord" class="form-control" placeholder="请输入关键词" ms-duplex="key_word">
<input type="submit" value="搜索" class="btn btn-primary" ms-click="getPage(1)">
</div>
</form>
<br>
</div>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>用户名</th>
<th>注册时间</th>
<th>最近登陆</th>
<th>真实姓名</th>
<th>电子邮箱</th>
<th>用户类型</th>
<th>修改</th>
</tr>
<tr ms-repeat="user_list">
<td>{{el.id}}</td>
<td>{{el.username}}</td>
<td>{{el.create_time|date("yyyy-MM-dd HH:mm:ss")}}</td>
<td>{{el.last_login|date("yyyy-MM-dd HH:mm:ss")}}</td>
<td>{{el.real_name}}</td>
<td>{{el.email}}</td>
<td>{{user_type[el.admin_type]}}</td>
<td>
<button class="btn-sm btn-info" ms-click="enEdit(el)">编辑</button>
</td>
</tr>
</table>
<div class="form-group">
<label>仅显示管理员 <input ms-duplex-checked="showAdminOnly" type="checkbox"/></label>
</div>
<div class="text-right">
页数:{{page}}/{{page_count}}&nbsp;&nbsp;
<botton ms-attr-class="getBtnClass(0)" ms-click="getPrevious">上一页</botton>
<botton ms-attr-class="getBtnClass(1)" ms-click="getNext">下一页</botton>
</div>
<div ms-visible="isEditing">
<h3>修改用户信息</h3>
<form id="edit_user-form">
<div class="row">
<div class="form-group col-md-4"><label>ID</label>
<input name="id" type="number" class="form-control" readonly ms-duplex="id">
</div>
<div class="form-group col-md-4"><label>用户名</label>
<input name="username" type="text" class="form-control" ms-duplex="username">
</div>
<div class="form-group col-md-4"><label>真实姓名</label>
<input name="real_name" type="text" class="form-control" ms-duplex="real_name">
</div>
</div>
<div class="row">
<div class="form-group col-md-4"><label>新密码(留空则保留原密码)</label>
<input name="password" type="password" class="form-control" id="password" ms-duplex="password"
placeholder="此项留空则保留原密码">
</div>
<div class="form-group col-md-4"><label>电子邮箱</label>
<input name="email" type="email" class="form-control" ms-duplex="email">
</div>
<div class="form-group col-md-4"><label>用户类型</label>
<select name="admin_type" class="form-control" ms-duplex="admin_type">
<option ms-repeat="user_type" ms-attr-value="$index">{{el}}</option>
</select>
</div>
</div>
<div class="form-group">
<button type="sbumit" class="btn btn-primary">提交</button>
</div>
</form>
</div>
</div>
<script src="/static/js/app/admin/user/user_list.js"></script>