fix error propagation in userhandler 75/7475/1
authorlombardofr <lombardo@everyup.it>
Sun, 19 May 2019 11:24:36 +0000 (13:24 +0200)
committerlombardofr <lombardo@everyup.it>
Sun, 19 May 2019 11:24:36 +0000 (13:24 +0200)
Change-Id: I39d846373f0068b548a6721a9e277efcf258c6a9
Signed-off-by: lombardofr <lombardo@everyup.it>
static/src/userhandler/user_list.js
userhandler/templates/user_list.html
userhandler/views.py

index 615f118..9a52249 100644 (file)
@@ -84,7 +84,7 @@ function deleteUser(user_id, name) {
                 success: function (result) {
                     //$('#modal_show_vim_body').empty();
                     dialog.modal('hide');
-                    location.reload();
+                    table.ajax.reload();
                 },
                 error: function (result) {
                     dialog.modal('hide');
index 87753c6..99393b5 100644 (file)
@@ -74,8 +74,9 @@
     <script src="/static/bower_components/select2/dist/js/select2.js"></script>
     <script src="/static/src/userhandler/user_list.js"></script>
     <script>
+        var table;
         $(document).ready(function () {
-            var table = $('#users_table').DataTable({
+            table = $('#users_table').DataTable({
                 responsive: true,
                 "ajax": {
                     "url": "/admin/users/list",
                 table.ajax.reload();
             }, 10000);
 
+            $("#formCreateUser").submit(function (event) {
+                event.preventDefault(); //prevent default action
+                var post_url = $(this).attr("action"); //get form action url
+                var request_method = $(this).attr("method");
+                var form_data = new FormData(this); //Encode form elements for submission
+                
+                $.ajax({
+                    url: post_url,
+                    type: request_method,
+                    data: form_data,
+                    headers: {
+                        "Accept": 'application/json'
+                    },
+                    contentType: false,
+                    processData: false
+                }).done(function (response, textStatus, jqXHR) {
+                    $('#modal_new_user').modal('hide');
+                    table.ajax.reload();
+                    bootbox.alert({
+                        title: "Result",
+                        message: "User successfully created."
+                    });
+
+                }).fail(function (result) {
+                    var data = result.responseJSON;
+                    var title = "Error " + (data.code ? data.code : 'unknown');
+                    var message = data.detail ? data.detail : 'No detail available.';
+                    bootbox.alert({
+                        title: title,
+                        message: message
+                    });
+                });
+            });
+
             $("#formEditUser").submit(function (event) {
                 event.preventDefault(); //prevent default action
                 var post_url = $(this).attr("action"); //get form action url
                     table.ajax.reload();
                     bootbox.alert({
                         title: "Result",
-                        message: "User modified successfully."
+                        message: "User successfully modified."
                     });
 
                 }).fail(function (result) {
                     });
                 });
             });
+        
         });
     </script>
 
index 47e4302..0c12a84 100644 (file)
@@ -46,10 +46,12 @@ def create(request):
         "password": request.POST['password'],
         "projects": request.POST.getlist('projects')
     }
-
     result = client.user_create(user.get_token(), user_data)
-
-    return __response_handler(request, result, 'users:list', to_redirect=True)
+    if result['error']:
+        return __response_handler(request, result['data'], url=None,
+                                  status=result['data']['status'] if 'status' in result['data'] else 500)
+    else:
+        return __response_handler(request, {}, url=None, status=200)
 
 
 @login_required
@@ -57,10 +59,15 @@ def delete(request, user_id=None):
     user = osmutils.get_user(request)
     try:
         client = Client()
-        del_res = client.user_delete(user.get_token(), user_id)
+        result = client.user_delete(user.get_token(), user_id)
     except Exception as e:
         log.exception(e)
-    return __response_handler(request, {}, 'users:list', to_redirect=True, )
+        result = {'error': True, 'data': str(e)}
+    if result['error']:
+        return __response_handler(request, result['data'], url=None,
+                                  status=result['data']['status'] if 'status' in result['data'] else 500)
+    else:
+        return __response_handler(request, {}, url=None, status=200)
 
 @login_required
 def update(request, user_id=None):