User group detail

pull/530/head
ibuler 2016-11-15 00:48:48 +08:00
parent 8d358a7a68
commit bd882c8bef
9 changed files with 224 additions and 154 deletions

View File

@ -297,6 +297,7 @@ jumpserver.initDataTable = function (options) {
url: options.i18n_url || "/static/js/plugins/dataTables/i18n/zh-hans.json"
},
order: options.order || [[ 1, 'asc' ]],
select: options.select || 'multi',
buttons: options.buttons || [
{extend: 'excel',
exportOptions: {
@ -323,9 +324,6 @@ jumpserver.initDataTable = function (options) {
}
],
columnDefs: columnDefs,
// select: 'single',
// select: options.select || {style: 'single'},
// select: false,
ajax: {
url: options.ajax_url ,
dataSrc: ""
@ -333,16 +331,16 @@ jumpserver.initDataTable = function (options) {
columns: options.columns || [],
lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]]
});
// table.on('select', function(e, dt, type, indexes) {
// var $node = table[ type ]( indexes ).nodes().to$();
// $node.find('input.ipt_check').prop('checked', true);
// }).on('deselect', function(e, dt, type, indexes) {
// var $node = table[ type ]( indexes ).nodes().to$();
// $node.find('input.ipt_check').prop('checked', false);
// }).on('draw', function(){
// $('#op').html(options.op_html || '');
// $('#uc').html(options.uc_html || '');
// });
table.on('select', function(e, dt, type, indexes) {
var $node = table[ type ]( indexes ).nodes().to$();
$node.find('input.ipt_check').prop('checked', true);
}).on('deselect', function(e, dt, type, indexes) {
var $node = table[ type ]( indexes ).nodes().to$();
$node.find('input.ipt_check').prop('checked', false);
}).on('draw', function(){
$('#op').html(options.op_html || '');
$('#uc').html(options.uc_html || '');
});
$('.ipt_check_all').on('click', function() {
if (!jumpserver.checked) {
$(this).closest('table').find('.ipt_check').prop('checked', true);
@ -354,5 +352,6 @@ jumpserver.initDataTable = function (options) {
table.rows().deselect();
}
});
return table;
};

View File

@ -30,9 +30,9 @@ class UserViewSet(BulkModelViewSet):
permission_classes = (IsSuperUser,)
class UserAndGroupEditApi(generics.RetrieveUpdateAPIView):
class UserUpdateGroupApi(generics.RetrieveUpdateAPIView):
queryset = User.objects.all()
serializer_class = serializers.UserAndGroupSerializer
serializer_class = serializers.UserUpdateGroupSerializer
permission_classes = (IsSuperUser,)
@ -72,7 +72,17 @@ class UserUpdatePKApi(generics.UpdateAPIView):
user.public_key = serializer.validated_data['_public_key']
user.save()
#
class UserGroupViewSet(viewsets.ModelViewSet):
queryset = UserGroup.objects.all()
serializer_class = serializers.UserGroupSerializer
class UserGroupUpdateUserApi(generics.RetrieveUpdateAPIView):
queryset = UserGroup.objects.all()
serializer_class = serializers.UserGroupUpdateMemeberSerializer
permission_classes = (IsSuperUser,)
# class GroupDetailApi(generics.RetrieveUpdateDestroyAPIView):
# queryset = UserGroup.objects.all()
# serializer_class = serializers.GroupDetailSerializer

View File

@ -16,8 +16,7 @@ from .models import User, UserGroup
class UserSerializer(BulkSerializerMixin, serializers.ModelSerializer):
group_display = serializers.SerializerMethodField()
active_display = serializers.SerializerMethodField()
groups_display = serializers.SerializerMethodField()
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
class Meta:
@ -27,17 +26,16 @@ class UserSerializer(BulkSerializerMixin, serializers.ModelSerializer):
def get_field_names(self, declared_fields, info):
fields = super(UserSerializer, self).get_field_names(declared_fields, info)
fields.extend(['group_display', 'get_role_display'])
fields.extend(['groups_display', 'get_role_display', 'is_valid'])
return fields
@staticmethod
def get_group_display(obj):
def get_groups_display(obj):
return " ".join([group.name for group in obj.groups.all()])
@staticmethod
def get_active_display(obj):
# TODO: user active state
return not (obj.is_expired and obj.is_active)
# @staticmethod
# def get_active_display(obj):
# return not (obj.is_expired and obj.is_active)
class UserPKUpdateSerializer(serializers.ModelSerializer):
@ -54,7 +52,7 @@ class UserPKUpdateSerializer(serializers.ModelSerializer):
return value
class UserAndGroupSerializer(serializers.ModelSerializer):
class UserUpdateGroupSerializer(serializers.ModelSerializer):
groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all())
class Meta:
@ -62,6 +60,26 @@ class UserAndGroupSerializer(serializers.ModelSerializer):
fields = ['id', 'groups']
class UserGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
user_amount = serializers.SerializerMethodField()
class Meta:
model = UserGroup
list_serializer_class = BulkListSerializer
@staticmethod
def get_user_amount(obj):
return obj.users.count()
class UserGroupUpdateMemeberSerializer(serializers.ModelSerializer):
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all())
class Meta:
model = UserGroup
fields = ['id', 'users']
# class GroupDetailSerializer(serializers.ModelSerializer):
# class Meta:
# model = UserGroup

View File

@ -224,9 +224,8 @@
jumpserver.selected_groups = {};
function updateUserGroups(user_groups) {
var the_url = "{% url 'users:group-user-edit-api' pk=user.id %}";
var the_url = "{% url 'users:api-user-update-group' pk=user.id %}";
var body = {
id: {{ user.id }},
groups: Object.assign([], user_groups)
};
var success = function(data) {
@ -245,13 +244,11 @@ function updateUserGroups(user_groups) {
});
// clear jumpserver.selected_groups
jumpserver.selected_groups = {};
toastr.success('{% trans "UserGroup Update Success!" %}')
};
APIUpdateAttr({
url: the_url,
body: JSON.stringify(body),
success: success,
method: 'PUT'
success: success
});
}
$(document).ready(function() {

View File

@ -11,52 +11,52 @@
<script src="{% static "js/plugins/sweetalert/sweetalert.min.js" %}"></script>
<script src="{% static "js/plugins/dataTables/dataTables.min.js" %}"></script>
<style>
.user_div {
color: #5e5e5e;
font-family: "Open Sans";
padding: 3px 8px;
text-shadow: none;
}
.user_div .ui_container {
background-color: #d1dade;
width: 100%;
}
.user_div a {
color: #5e5e5e;
}
.user_div .remove {
color: #fff;
}
dl {
width: 100%;
overflow: hidden;
padding: 0;
margin: 10px;
border-bottom: 1px solid #e7eaec;
}
dt {
float: left;
width: 30%;
padding: 0;
margin: 0
}
dd {
float: left;
width: 70%;
padding: 0;
margin: 0
}
#group_user_row dt {
border-bottom: 1px solid #e7eaec;
width: 100%;
margin-bottom: 20px;
}
#group_user_row dd {
width: 100%;
overflow: auto;
}
.user_div {
color: #5e5e5e;
font-family: "Open Sans";
padding: 3px 8px;
text-shadow: none;
}
.user_div .ui_container {
background-color: #d1dade;
width: 100%;
}
.user_div a {
color: #5e5e5e;
}
.user_div .remove {
color: #fff;
}
dl {
width: 100%;
overflow: hidden;
padding: 0;
margin: 10px;
border-bottom: 1px solid #e7eaec;
}
dt {
float: left;
width: 30%;
padding: 0;
margin: 0
}
dd {
float: left;
width: 70%;
padding: 0;
margin: 0
}
#group_user_row dt {
border-bottom: 1px solid #e7eaec;
width: 100%;
margin-bottom: 20px;
}
#group_user_row dd {
width: 100%;
overflow: auto;
}
</style>
{% endblock %}
{% block content %}
@ -67,7 +67,7 @@ dd {
<div class="panel-options">
<ul class="nav nav-tabs">
<li class="active">
<a href="{% url 'users:user-group-detail' pk=object.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User Group Detail' %} </a>
<a href="{% url 'users:user-group-detail' pk=user_group.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'User Group Detail' %} </a>
</li>
</ul>
</div>
@ -75,7 +75,7 @@ dd {
<div class="col-sm-7" style="padding-left: 0">
<div class="ibox float-e-margins">
<div class="ibox-title">
<span class="label"><b>{{ object.name }}</b></span>
<span class="label"><b>{{ user_group.name }}</b></span>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
@ -86,53 +86,62 @@ dd {
</div>
</div>
<div class="ibox-content">
<dl>
<dt width="20%">{% trans 'Name' %}:</dt>
<dd><b>{{ object.name }}</b></dd>
</dl>
<dl>
<dt>{% trans 'Comment' %}:</dt>
<dd><b>{{ object.comment }}</b></dd>
</dl>
<dl>
<dt>{% trans 'Created at' %}:</dt>
<dd><b>{{ object.date_created }}</b></dd>
</dl>
<dl id="group_user_row">
<dt>{% trans 'Users' %}:</dt>
<dd style="line-height: 2" id="group_user_container">
{% for user in object.users.all %}
<div class="col-sm-4 user_div"><div class="ui_container row"><div class="col-xs-9"><a href="{{ user.get_absolute_url }}" title="{{ user.name }}" data-toggle="tooltip">{{ user.name|truncatechars:15 }}</a></div><div class="col-xs-3"><a data-uid="{{ user.id }}" class="btn_remove m-l-5"><i class="remove fa fa-times-circle"></i></a></div></div></div>
{% endfor %}
</dd>
</dl>
<table class="table">
<tbody>
<tr class="no-borders-tr">
<td width="20%">{% trans 'Name' %}:</td>
<td><b>{{ user_group.name }}</b></td>
</tr>
<tr>
<td>{% trans 'Create by' %}:</td>
<td><b>{{ user_group.created_by }}</b></td>
</tr>
<tr>
<td>{% trans 'Date created' %}:</td>
<td><b>{{ user_group.date_created }}</b></td>
</tr>
<tr>
<td>{% trans 'Comment' %}:</td>
<td><b>{{ user_group.comment }}</b></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-sm-5" style="padding-left: 0;padding-right: 0">
<div class="panel panel-primary">
<div class="panel panel-info">
<div class="panel-heading">
<i class="fa fa-info-circle"></i> {% trans 'Quick modify' %}
<i class="fa fa-info-circle"></i> {% trans 'User' %}
</div>
<div class="panel-body">
<table class="table">
<table class="table user_edit">
<tbody>
<form>
<tr>
<td>{% trans 'Add User' %}:</td>
<td>
<span class="pull-right">
<button type="button" class="btn btn-primary btn-xs" id="btn_group_add_user" style="width: 54px" data-toggle="modal" data-target="#select_user_modal">{% trans 'Add' %}</button>
</span>
<td colspan="2" class="no-borders">
<select data-placeholder="{% trans 'Add user' %}" id="slct_groups" class="select2" style="width: 100%" multiple="" tabindex="4">
{% for user in users %}
<option value="{{ user.id }}" id="opt_{{ user.id }}">{{ user.name }}</option>
{% endfor %}
</select>
</td>
</tr>
<tr>
<td>{% trans 'Delete' %}:</td>
<td>
<span class="pull-right">
<button type="button" class="btn btn-danger btn-xs" id="btn_group_delete" style="width: 54px">{% trans 'Delete' %}</button>
</span>
<td colspan="2" class="no-borders">
<button type="button" class="btn btn-info btn-small" id="btn_add_user_group">{% trans 'Add' %}</button>
</td>
</tr>
</form>
{% for user in user_group.users.all %}
<tr>
<td ><b class="bdg_user" data-uid={{ user.id }}>{{ user.name }}</b></td>
<td>
<button class="btn btn-danger pull-right btn-xs btn_delete_user" type="button"><i class="fa fa-minus"></i></button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
@ -147,35 +156,63 @@ dd {
{% endblock %}
{% block custom_foot_js %}
<script>
$(document).on('click', '.btn_remove', function(){
jumpserver.selected_groups = {};
function updateGroupMember(users) {
var the_url = "{% url 'users:api-user-group-update-user' pk=user_group.id %}";
var body = {
groups: Object.assign([], users)
};
var success = function(data) {
// remove all the selected groups from select > option and rendered ul element;
$('.select2-selection__rendered').empty();
$('#slct_users').val('');
$.map(jumpserver.selected_groups, function(user_name, index) {
$('#opt_' + index).remove();
// change tr html of users
$('.user_edit tbody').append(
'<tr>' +
'<td><b class="bdg_user" data-uid="' + index + '">' + user_name + '</b></td>' +
'<td><button class="btn btn-danger btn-xs pull-right btn_delete_user" type="button"><i class="fa fa-minus"></i></button></td>' +
'</tr>'
)
});
// clear jumpserver.selected_groups
jumpserver.selected_users = {};
};
APIUpdateAttr({
url: the_url,
body: JSON.stringify(body),
success: success
});
}
$(document).ready(function () {
$('.select2').select2();
}).on('click', '.btn_remove', function(){
var $this = $(this);
var uid = $this.data('uid');
var the_url = '{% url "users:delete-user-from-group-api" pk=object.id uid=99991937 %}'.replace('99991937', uid);
var the_url = '{% url "users:api-user-group-detail" pk=user_group.id %}'.replace('99991937', uid);
var success = function(){
$this.closest('.user_div').remove();
};
var error = function(){};
APIUpdateAttr({url: the_url, body: "{}", method: "DELETE", success: success, error: error});
return false;
}).on('click', '#btn_group_delete', function() {
function doDelete() {
var the_url = '{% url "users:user-group-detail-api" pk=object.id %}';
var success = function() {
window.location.href = '{% url "users:user-group-list" %}';
};
APIUpdateAttr({url: the_url, body: "{}", method: "DELETE", success: success});
}
swal({
title: "{% trans 'Are you sure?' %}",
text: "{% trans 'This will delete the current group, but will not delete any user of it.' %}",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "{% trans 'Confirm' %}",
closeOnConfirm: false
}, function() {
doDelete();
});
}).on('click', '.btn_delete_user', function() {
var $this = $(this);
var $tr = $this.closest('tr');
var $badge = $tr.find('.bdg_user_group');
var gid = $badge.data('gid');
var group_name = $badge.html() || $badge.text();
$('#slct_groups').append(
'<option value="' + gid + '" id="opt_' + gid + '">' + group_name + '</option>'
);
$tr.remove();
var user_groups = $('.bdg_user_group').map(function() {
return $(this).data('gid');
}).get();
updateUserGroups(user_groups)
}).on('shown.bs.modal', '#select_user_modal', function() {
if ($.fn.dataTable.isDataTable('#select_user_table')) {
return true;
@ -197,7 +234,7 @@ $(document).on('click', '.btn_remove', function(){
$(td).html('<a href="javascript:void(0);" data-toggle="tooltip" title="' + cellData + '">' + innerHtml + '</a>');
}}
],
ajax_url: '{% url "users:user-bulk-update-api" %}',
ajax_url: '{% url "users:api-user-list" %}',
columns: [{data: function(){return ""}}, {data: "username" }, {data: "name" }, {data: "get_role_display" }, {data: "group_display" },
{data: function(){return 999}}, {data: "active_display" }],
};
@ -214,11 +251,11 @@ $(document).on('click', '.btn_remove', function(){
return false;
};
var body = {
id: {{ object.id }},
id: {{ user_group.id }},
users: plain_id_list.map(Number)
};
$('#select_user_modal').modal('hide');
var the_url = "{% url 'users:user-group-detail-api' pk=object.id %}";
var the_url = "{% url 'users:api-user-group-detail' pk=user_group.id %}";
var success = function() {
toastr.success('{% trans "The selected users has been added to current group." %}');
var html = "";
@ -236,7 +273,7 @@ $(document).on('click', '.btn_remove', function(){
].join("");
});
$(html).appendTo($('#group_user_container'));
}
};
APIUpdateAttr({url: the_url, body: JSON.stringify(body), success: success});
})
</script>

View File

@ -11,7 +11,6 @@
</th>
<th class="text-center">{% trans 'Name' %}</th>
<th class="text-center">{% trans 'User Amount' %}</th>
<th class="text-center">{% trans 'Asset Amount' %}</th>
<th class="text-center">{% trans 'Comment' %}</th>
<th class="text-center">{% trans 'Action' %}</th>
</tr>
@ -43,11 +42,11 @@ $(document).ready(function() {
var detail_btn = '<a href="{% url "users:user-group-detail" pk=99991937 %}">' + cellData + '</a>';
$(td).html(detail_btn.replace('99991937', rowData.id));
}},
{targets: 4, createdCell: function (td, cellData) {
var innerHtml = cellData.length > 18 ? cellData.substring(0, 18) + '...': cellData;
$(td).html('<a href="javascript:void(0);" data-toggle="tooltip" title="' + cellData + '">' + innerHtml + '</a>');
{targets: 3, createdCell: function (td, cellData) {
var innerHtml = cellData.length > 30 ? cellData.substring(0, 30) + '...': cellData;
$(td).html('<span href="javascript:void(0);" data-toggle="tooltip" title="' + cellData + '">' + innerHtml + '</span>');
}},
{targets: 5, createdCell: function (td, cellData, rowData) {
{targets: 4, createdCell: function (td, cellData, rowData) {
var update_btn = '<a href="{% url "users:user-group-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace('99991937', cellData);
var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_delete_user_group" data-uid="99991937">{% trans "Delete" %}</a>'.replace('99991937', cellData);
if (rowData.id === 1) {
@ -56,9 +55,9 @@ $(document).ready(function() {
$(td).html(update_btn + del_btn)
}
}}],
ajax_url: '{% url "users:user-group-bulk-update-api" %}',
ajax_url: '{% url "users:api-user-group-list" %}',
columns: [{data: function(){return ""}}, {data: "name" }, {data: "user_amount"},
{data: function(){return 999}}, {data: "comment"}, {data: "id" }],
{data: "comment"}, {data: "id" }],
op_html: $('#actions').html()
};
jumpserver.initDataTable(options);
@ -66,7 +65,7 @@ $(document).ready(function() {
var $this = $(this);
function doDelete() {
var group_id = $this.data('gid');
var the_url = "{% url 'users:user-group-detail-api' 99991937 %}".replace('99991937', group_id);
var the_url = "{% url 'users:api-user-group-detail' pk=99991937 %}".replace('99991937', group_id);
var body = {};
var success = function() {
var msg = "{% trans 'Group Deleted.' %}";
@ -76,7 +75,7 @@ $(document).ready(function() {
var fail = function() {
var msg = "{% trans 'Group Deleting failed.' %}";
swal("{% trans 'Group Delete' %}", msg, "error");
}
};
APIUpdateAttr({
url: the_url,
body: JSON.stringify(body),
@ -105,8 +104,8 @@ $(document).ready(function() {
});
if (plain_id_list === []) {
return false;
};
var the_url = "{% url 'users:user-group-bulk-update-api' %}";
}
var the_url = "{% url 'users:api-user-group-list' %}";
function doDelete() {
swal({
title: "{% trans 'Are you sure?' %}",

View File

@ -15,7 +15,6 @@
<th class="text-center">{% trans 'Username' %}</th>
<th class="text-center">{% trans 'Role' %}</th>
<th class="text-center">{% trans 'User group' %}</th>
<th class="text-center">{% trans 'Asset num' %}</th>
<th class="text-center">{% trans 'Active' %}</th>
<th class="text-center">{% trans 'Action' %}</th>
</tr>
@ -56,14 +55,14 @@ $(document).ready(function(){
var innerHtml = cellData.length > 20 ? cellData.substring(0, 20) + '...': cellData;
$(td).html('<span href="javascript:void(0);" data-toggle="tooltip" title="' + cellData + '">' + innerHtml + '</span>');
}},
{targets: 6, createdCell: function (td, cellData) {
{targets: 5, createdCell: function (td, cellData) {
if (!cellData) {
$(td).html('<i class="fa fa-times text-danger"></i>')
} else {
$(td).html('<i class="fa fa-check text-navy"></i>')
}
}},
{targets: 7, createdCell: function (td, cellData, rowData) {
{targets: 6, createdCell: function (td, cellData, rowData) {
var update_btn = '<a href="{% url "users:user-update" pk=99991937 %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace('99991937', cellData);
var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn_user_delete" data-uid="99991937">{% trans "Delete" %}</a>'.replace('99991937', cellData);
if (rowData.id === 1 || rowData.username == "admin") {
@ -73,8 +72,8 @@ $(document).ready(function(){
}
}}],
ajax_url: '{% url "users:api-user-list" %}',
columns: [{data: function(){return ""}}, {data: "username" }, {data: "name" }, {data: "get_role_display" }, {data: "group_display" },
{data: function(){return 999}}, {data: "active_display" }, {data: "id" }],
columns: [{data: function(){return ""}}, {data: "username" }, {data: "name" }, {data: "get_role_display" },
{data: "groups_display" }, {data: "is_valid" }, {data: "id" }],
op_html: $('#actions').html()
};
jumpserver.initDataTable(options);
@ -224,6 +223,8 @@ $(document).ready(function(){
$form.ajaxSubmit({success: success});
}).on('change', '#id_excel', function() {
$(this).siblings('.help-block').remove();
}).on('click', '.ipt_check', function () {
console.log('Hello')
})
</script>
{% endblock %}

View File

@ -39,6 +39,7 @@ urlpatterns = [
router = BulkRouter()
router.register(r'v1/users', api.UserViewSet, 'api-user')
router.register(r'v1/user-groups', api.UserGroupViewSet, 'api-user-group')
# router.register(r'v1/user-groups', api.AssetViewSet, 'api-groups')
@ -53,7 +54,9 @@ urlpatterns += [
# url(r'^v1/user-groups/(?P<pk>\d+)/user/(?P<uid>\d+)/$',
# api.DeleteUserFromGroupApi.as_view(), name='delete-user-from-group-api'),
url(r'^v1/users/(?P<pk>\d+)/groups/$',
api.UserAndGroupEditApi.as_view(), name='group-user-edit-api'),
api.UserUpdateGroupApi.as_view(), name='api-user-update-group'),
url(r'^v1/user-groups/(?P<pk>\d+)/users/$',
api.UserGroupUpdateUserApi.as_view(), name='api-user-group-update-user'),
]
urlpatterns += router.urls

View File

@ -216,10 +216,16 @@ class UserGroupUpdateView(AdminUserRequiredMixin, UpdateView):
class UserGroupDetailView(AdminUserRequiredMixin, DetailView):
model = UserGroup
context_object_name = 'user_group'
template_name = 'users/user_group_detail.html'
def get_context_data(self, **kwargs):
context = {'app': _('Users'), 'action': _('User Group Detail')}
users = User.objects.exclude(id__in=self.object.users.all())
context = {
'app': _('Users'),
'action': _('User Group Detail'),
'users': users,
}
kwargs.update(context)
return super(UserGroupDetailView, self).get_context_data(**kwargs)