From 8b12a9b5600453e1e6f8c2f2abe163bfde3f8b9e Mon Sep 17 00:00:00 2001 From: lombardofr Date: Wed, 3 Oct 2018 11:52:15 +0200 Subject: [PATCH] user upadate: add/revoke project access Change-Id: I81565801160e91fc10bb68ce92431bdab9383477 --- lib/osm/osmclient/clientv2.py | 7 +-- static/src/userhandler/user_list.js | 63 +++++++--------------- userhandler/templates/modal/user_edit.html | 5 +- userhandler/templates/user_list.html | 63 ++++++++++++++++++++-- userhandler/urls.py | 2 +- userhandler/views.py | 27 +++++++--- 6 files changed, 105 insertions(+), 62 deletions(-) diff --git a/lib/osm/osmclient/clientv2.py b/lib/osm/osmclient/clientv2.py index ad7072e..414019e 100644 --- a/lib/osm/osmclient/clientv2.py +++ b/lib/osm/osmclient/clientv2.py @@ -117,14 +117,15 @@ class Client(object): _url = "{0}/admin/v1/users/{1}".format(self._base_path, id) try: - r = requests.post(_url, json=user_data, verify=False, headers=headers) + r = requests.patch(_url, json=user_data, verify=False, headers=headers) except Exception as e: log.exception(e) result['data'] = str(e) return result - if r.status_code == requests.codes.created: + if r.status_code == requests.codes.no_content: result['error'] = False - result['data'] = Util.json_loads_byteified(r.text) + else: + result['data'] = Util.json_loads_byteified(r.text) return result def user_delete(self, token, id): diff --git a/static/src/userhandler/user_list.js b/static/src/userhandler/user_list.js index 0c53b9e..afa1ae3 100644 --- a/static/src/userhandler/user_list.js +++ b/static/src/userhandler/user_list.js @@ -26,59 +26,36 @@ function openModalCreateUser(args) { } function openModalEditUser(args) { - var url = '/admin/users/'+args.user_id; - console.log(url) + var url = '/admin/users/' + args.user_id; + var user_projects = args.projects ? args.projects.split(',') : []; $("#formEditUser").attr("action", url); - select2_groups = $('#projects_edit').select2({ - placeholder: 'Select Projects', - width: '100%', - ajax: { - url: '/projects/list', - dataType: 'json', - processResults: function (data) { - projects = []; - if (data['projects']) { - for (d in data['projects']) { - var project = data['projects'][d]; - projects.push({id: project['_id'], text: project['name']}) - } - } + $("#projects_old").val(user_projects.toString()); + $('#projects_edit').val(null).trigger('change'); + $('#default_project_edit').val(null).trigger('change'); - return { - results: projects - }; - } + if (user_projects.length > 0) { + // Create a DOM Option and pre-select by default + var newOption = new Option(user_projects[0], user_projects[0], true, true); + // Append it to the select + $('#default_project_edit').append(newOption).trigger('change'); + + for (var d in user_projects) { + var project = user_projects[d]; + // Create a DOM Option and pre-select by default + var newOption = new Option(project, project, true, true); + // Append it to the select + $('#projects_edit').append(newOption).trigger('change'); } - }); - select2_single = $('#default_project_edit').select2({ - placeholder: 'Select Default Project', - width: '100%', - ajax: { - url: '/projects/list', - dataType: 'json', - processResults: function (data) { - projects = []; - if (data['projects']) { - for (d in data['projects']) { - var project = data['projects'][d]; - projects.push({id: project['_id'], text: project['name']}) - } - } + } - return { - results: projects - }; - } - } - }); $('#modal_edit_user').modal('show'); } function deleteUser(user_id, name) { - var delete_url = '/admin/users/'+user_id+'/delete'; - bootbox.confirm("Are you sure want to delete "+name+"?", function (confirm) { + var delete_url = '/admin/users/' + user_id + '/delete'; + bootbox.confirm("Are you sure want to delete " + name + "?", function (confirm) { if (confirm) { var dialog = bootbox.dialog({ message: '
Loading...
', diff --git a/userhandler/templates/modal/user_edit.html b/userhandler/templates/modal/user_edit.html index d3c3d38..caeeb3a 100644 --- a/userhandler/templates/modal/user_edit.html +++ b/userhandler/templates/modal/user_edit.html @@ -10,14 +10,15 @@ class="form-horizontal" method="post" enctype="multipart/form-data"> {% csrf_token %} +