Bläddra i källkod

HUE-6312 [useradmin] Add error notifications on Hue 4 for edit group

Enrico Berti 8 år sedan
förälder
incheckning
16389c6

+ 7 - 4
apps/useradmin/src/useradmin/templates/edit_group.mako

@@ -68,12 +68,12 @@ ${layout.menubar(section='groups')}
 
       <div class="form-actions">
         % if name:
-            <input type="submit" class="btn btn-primary" value="${_('Update group')}"/>
+            <input type="submit" class="btn btn-primary disable-feedback" value="${_('Update group')}"/>
         % else:
           % if ldap:
-              <input type="submit" class="btn btn-primary" value="${_('Add/Sync group')}"/>
+              <input type="submit" class="btn btn-primary disable-feedback" value="${_('Add/Sync group')}"/>
           % else:
-              <input type="submit" class="btn btn-primary" value="${_('Add group')}"/>
+              <input type="submit" class="btn btn-primary disable-feedback" value="${_('Add group')}"/>
           % endif
         % endif
         % if is_embeddable:
@@ -107,7 +107,10 @@ ${layout.menubar(section='groups')}
     $editGroupComponents.find('#editForm').ajaxForm({
       dataType:  'json',
       success: function(data) {
-        if (data && data.url){
+        if (data && data.status == -1) {
+          renderUseradminErrors(data.errors);
+        }
+        else if (data && data.url) {
           huePubSub.publish('open.link', data.url);
           $.jHueNotify.info("${ _('Group information updated correctly') }");
         }

+ 1 - 19
apps/useradmin/src/useradmin/templates/edit_user.mako

@@ -135,29 +135,11 @@ $(document).ready(function(){
 
   % if is_embeddable:
   $editUserComponents.find('#editForm').attr('action', window.location.pathname.substr(4).replace(/\/$/, ''));
-
-  function renderErrors(errors) {
-    $('.control-group').removeClass('error');
-    $('.errorlist').remove();
-    if (errors && errors.length > 0){
-      errors.forEach(function(e){
-        var $el = $('#' + e.id);
-        $el.closest('.control-group').addClass('error');
-        var html = '<span class="help-inline"><ul class="errorlist">';
-        e.message.forEach(function(message){
-          html += '<li>' + message + '</li>';
-        });
-        html += '</ul></span>';
-        $el.after(html);
-      });
-    }
-  }
-
   $editUserComponents.find('#editForm').ajaxForm({
     dataType:  'json',
     success: function(data) {
       if (data && data.status == -1) {
-        renderErrors(data.errors);
+        renderUseradminErrors(data.errors);
       }
       else if (data && data.url) {
         huePubSub.publish('open.link', data.url);

+ 23 - 0
apps/useradmin/src/useradmin/templates/layout.mako

@@ -78,4 +78,27 @@ def is_selected(section, matcher):
 
 <%def name="commons()">
   <link href="${ static('useradmin/css/useradmin.css') }" rel="stylesheet">
+  <script>
+    function renderUseradminErrors(errors) {
+      $('.control-group').removeClass('error');
+      $('.errorlist').remove();
+      if (errors && errors.length > 0) {
+        errors.forEach(function (e, idx) {
+          var $el = $('#' + e.id);
+          $el.closest('.control-group').addClass('error');
+          var html = '<span class="help-inline"><ul class="errorlist">';
+          e.message.forEach(function (message) {
+            html += '<li>' + message + '</li>';
+          });
+          html += '</ul></span>';
+          $el.after(html);
+          if (idx === 0) {
+            $('.page-content').animate({
+              scrollTop: $el.offset().top
+            }, 200);
+          }
+        });
+      }
+    }
+  </script>
 </%def>

+ 10 - 6
apps/useradmin/src/useradmin/views.py

@@ -460,12 +460,16 @@ def edit_group(request, name=None):
   else:
     form = GroupEditForm(instance=instance)
 
-  return render('edit_group.mako', request, {
-    'form': form,
-    'action': request.path,
-    'name': name,
-    'is_embeddable': is_embeddable,
-  })
+  if request.method == 'POST' and is_embeddable:
+    return JsonResponse(
+      {'status': -1, 'errors': [{'id': f.id_for_label, 'message': f.errors} for f in form if f.errors]})
+  else:
+    return render('edit_group.mako', request, {
+      'form': form,
+      'action': request.path,
+      'name': name,
+      'is_embeddable': is_embeddable,
+    })
 
 
 def edit_permission(request, app=None, priv=None):