Эх сурвалжийг харах

HUE-1623 [search] Show import collection error

Errors or warning are now shown
Added 'warn' level to jHueNotify
Enrico Berti 12 жил өмнө
parent
commit
f4518f3618

+ 10 - 2
apps/search/src/search/templates/admin_collections.mako

@@ -206,10 +206,18 @@ ${ commonheader(_('Search'), "search", user, "29px") | n,unicode }
       _btn.button("loading");
     });
 
-    $(document).on("imported", function () {
+    $(document).on("imported", function (e, data) {
       $("#importModal").modal("hide");
       $("#importModalBtn").button("reset");
-      $(document).trigger("info", "${ _("Collections imported successfully.") }"); // Could fail actually
+      if (data.status == 0){
+        $(document).trigger("info", data.message + "<br/>${_('Imported:')}" +  + data.imported.join(", "));
+      }
+      else if (data.status == 1){
+        $(document).trigger("info", data.message + "<br/>${_('Imported:')}" + data.imported.join(", ") + "<br/>${_('Not imported:')}" + data.notImported.join(", "));
+      }
+      else {
+        $(document).trigger("error", data.message+ "<br/>${_('Not imported:')}" + data.notImported.join(", "));
+      }
     });
 
     $(document).on("deleting", function () {

+ 24 - 9
apps/search/src/search/views.py

@@ -124,20 +124,35 @@ def admin_collections(request, is_redirect=False):
 def admin_collections_import(request):
   if request.method == 'POST':
     searcher = SearchController(request.user)
-    status = 0
-    err_message = _('Error')
-    result = {
-      'status': status,
-      'message': err_message
-    }
+    imported = []
+    not_imported = []
+    status = -1
+    message = ""
     importables = json.loads(request.POST["selected"])
     for imp in importables:
       try:
         searcher.add_new_collection(imp)
-        status += 1
+        imported.append(imp['name'])
       except Exception, e:
-        err_message += unicode(str(e), "utf8") + "\n"
-      result['message'] = _('Imported successfully') if status == len(importables) else _('Imported with errors: ') + err_message
+        not_imported.append(imp['name'] + ": " + unicode(str(e), "utf8"))
+
+    if len(imported) == len(importables):
+      status = 0;
+      message = _('Collection(s) or core(s) imported successfully!')
+    elif len(not_imported) == len(importables):
+      status = 2;
+      message = _('There was an error importing the collection(s) or core(s)')
+    else:
+      status = 1;
+      message = _('Collection(s) or core(s) partially imported')
+
+    result = {
+      'status': status,
+      'message': message,
+      'imported': imported,
+      'notImported': not_imported
+    }
+
     return HttpResponse(json.dumps(result), mimetype="application/json")
   else:
     if request.GET.get('format') == 'json':

+ 1 - 1
apps/search/static/js/search.ko.js

@@ -180,7 +180,7 @@ var SearchCollectionsModel = function (props) {
         selected: ko.toJSON(selected)
       },
       function (data) {
-        $(document).trigger("imported");
+        $(document).trigger("imported", data);
         self.updateCollections();
       }, "json");
   };

+ 5 - 0
desktop/core/src/desktop/templates/common_footer.mako

@@ -24,6 +24,9 @@ from django.template.defaultfilters import escape, escapejs
     $(document).on("info", function (e, msg) {
       $.jHueNotify.info(msg);
     });
+    $(document).on("warn", function (e, msg) {
+      $.jHueNotify.warn(msg);
+    });
     $(document).on("error", function (e, msg) {
       $.jHueNotify.error(msg);
     });
@@ -32,6 +35,8 @@ from django.template.defaultfilters import escape, escapejs
       %for message in messages:
         %if message.tags == 'error':
           $(document).trigger('error', '${ escapejs(escape(message)) }');
+        %elif message.tags == 'warn':
+          $(document).trigger('warn', '${ escapejs(escape(message)) }');
         %else:
           $(document).trigger('info', '${ escapejs(escape(message)) }');
         %endif

+ 4 - 0
desktop/core/static/js/jquery.notify.js

@@ -113,6 +113,10 @@
         new Plugin({ level: TYPES.INFO, message: message});
     };
 
+    $[pluginName].warn = function (message) {
+        new Plugin({ level: TYPES.GENERAL, message: message, sticky: true});
+    };
+
     $[pluginName].error = function (message) {
         new Plugin({ level: TYPES.ERROR, message: message, sticky: true});
     };