From 74cdd2d0f3fda2657549a008bb42e372526f8f8c Mon Sep 17 00:00:00 2001 From: "xiaokong1937@gmail.com" <763691951@qq.com> Date: Mon, 26 Sep 2016 19:39:43 +0800 Subject: [PATCH] user list groups bulk update --- apps/users/models.py | 5 ++-- apps/users/serializers.py | 1 + apps/users/templates/users/user_list.html | 28 +++++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/apps/users/models.py b/apps/users/models.py index 8099ed025..b6d40dfb3 100644 --- a/apps/users/models.py +++ b/apps/users/models.py @@ -156,8 +156,9 @@ class User(AbstractUser): super(User, self).save(*args, **kwargs) # Add the current user to the default group. - group = UserGroup.initial() - self.groups.add(group) + if not self.groups.count(): + group = UserGroup.initial() + self.groups.add(group) @property def private_token(self): diff --git a/apps/users/serializers.py b/apps/users/serializers.py index c5e8f3926..06caa0e45 100644 --- a/apps/users/serializers.py +++ b/apps/users/serializers.py @@ -73,6 +73,7 @@ class UserPKUpdateSerializer(serializers.ModelSerializer): class UserBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer): group_display = serializers.SerializerMethodField() active_display = serializers.SerializerMethodField() + groups = serializers.PrimaryKeyRelatedField(many=True, queryset=UserGroup.objects.all()) class Meta(object): model = User diff --git a/apps/users/templates/users/user_list.html b/apps/users/templates/users/user_list.html index 0ac352f2b..a1369ee73 100644 --- a/apps/users/templates/users/user_list.html +++ b/apps/users/templates/users/user_list.html @@ -80,7 +80,8 @@ $(document).ready(function(){ {targets: 0, orderable: false, createdCell: function(td) { $(td).html('
'); - }}, + } + }, {className: 'text-center', targets: [0, 1, 2, 3, 4, 5, 6, 7]}, {targets: 7, createdCell: function (td, cellData, rowData) { @@ -247,7 +248,30 @@ $(document).ready(function(){ if (json_data.groups != undefined) { body.groups = json_data.groups; } - console.log(body) + if (typeof body.groups === 'string') { + body.groups = [parseInt(body.groups)] + } else if(typeof body.groups === 'array') { + new_groups = body.groups.map(Number); + body.groups = new_groups; + } + var $data_table = $('#user_list_table').DataTable() + var post_list = []; + $data_table.rows({selected: true}).every(function(){ + var content = Object.assign({id: this.data().id}, body); + post_list.push(content); + }); + if (post_list === []) { + return false; + }; + var the_url = "{% url 'users:user-bulk-update-api' %}"; + var success = function() { + var msg = "{% trans 'The selected users has been updated successfully.' %}"; + swal("{% trans 'User Updated' %}", msg, "success"); + $('#user_list_table').DataTable().ajax.reload(); + jumpserver.checked = false; + } + APIUpdateAttr({url: the_url, method: 'PATCH', body: JSON.stringify(post_list), success: success}); + $('#user_bulk_update_modal').modal('hide'); }) {% endblock %}