From d8143a67cd433079d0ee61b298b8f615cf10bb0b Mon Sep 17 00:00:00 2001 From: "xiaokong1937@gmail.com" <763691951@qq.com> Date: Thu, 29 Sep 2016 21:14:55 +0800 Subject: [PATCH] user-group-detail: user-adding frontend integration --- apps/users/api.py | 10 +- .../templates/users/user_group_detail.html | 112 ++++++++++++------ 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/apps/users/api.py b/apps/users/api.py index b896fa8be..361f532a4 100644 --- a/apps/users/api.py +++ b/apps/users/api.py @@ -93,14 +93,16 @@ class GroupEditApi(generics.RetrieveUpdateDestroyAPIView): serializer_class = GroupEditSerializer def perform_update(self, serializer): - partial = serializer.validated_data.get('partial', False) users = serializer.validated_data.get('users') - if partial and users: + if users: group = self.get_object() - group.users.add(users) + # Note: use `list` method to force hitting the db. + group_users = list(group.users.all()) + serializer.save() + group.users.set(users + group_users) group.save() return - return super(GroupEditApi, self).perform_update(serializer) + serializer.save() class UserBulkUpdateApi(ListBulkCreateUpdateDestroyAPIView): diff --git a/apps/users/templates/users/user_group_detail.html b/apps/users/templates/users/user_group_detail.html index 0bc02a85e..878ed3050 100644 --- a/apps/users/templates/users/user_group_detail.html +++ b/apps/users/templates/users/user_group_detail.html @@ -11,15 +11,48 @@ {% endblock %} @@ -50,29 +83,26 @@
- - - - - - - - - - - - - - - - - - -
{% trans 'Name' %}:{{ object.name }}
{% trans 'Comment' %}:{{ object.comment }}
{% trans 'Created at' %}:{{ object.date_created }}
{% trans 'Users' %}: +
+
{% trans 'Name' %}:
+
{{ object.name }}
+
+
+
{% trans 'Comment' %}:
+
{{ object.comment }}
+
+
+
{% trans 'Created at' %}:
+
{{ object.date_created }}
+
+
+
{% trans 'Users' %}:
+
{% for user in object.users.all %} - {{ user.name }} + {% endfor %} -
+ +
@@ -119,7 +149,7 @@ $(document).on('click', '.btn_remove', function(){ var uid = $this.data('uid'); var the_url = '{% url "users:delete-user-from-group-api" pk=object.id uid=99991937 %}'.replace('99991937', uid); var success = function(){ - $this.closest('.label').remove(); + $this.closest('.user_div').remove(); }; var error = function(){}; APIUpdateAttr({url: the_url, body: "{}", method: "DELETE", success: success, error: error}); @@ -167,25 +197,39 @@ $(document).on('click', '.btn_remove', function(){ jumpserver.initDataTable(options); }).on('click', '#btn_select_user', function() { var $data_table = $('#select_user_table').DataTable(); - var id_list = []; var plain_id_list = []; + var selected_users = []; $data_table.rows({selected: true}).every(function(){ - id_list.push({id: this.data().id}); plain_id_list.push(this.data().id); + selected_users.push({id: this.data().id, name: this.data().name}); }); - if (id_list === []) { + console.log(selected_users) + if (plain_id_list === []) { return false; }; - console.log(id_list); - console.log(plain_id_list); var body = { id: {{ object.id }}, users: plain_id_list.map(Number) }; - console.log(body); $('#select_user_modal').modal('hide'); var the_url = "{% url 'users:user-group-edit-api' pk=object.id %}"; - APIUpdateAttr({url: the_url, body: JSON.stringify(body)}); + var success = function() { + toastr.success('{% trans "The selected users has been added to current group." %}'); + var html = ""; + $.each(selected_users, function(index, user) { + html += [ + '
', + user.name, + '
\n', + ].join(""); + }); + $(html).appendTo($('#group_user_container')); + } + APIUpdateAttr({url: the_url, body: JSON.stringify(body), success: success}); }) {% endblock %}