Update org bulk accept script.
authorLennard de Rijk <ljvderijk@gmail.com>
Mon, 16 Feb 2009 20:38:35 +0000
changeset 1364 b2709805fafe
parent 1363 5667c29be1a3
child 1365 c31e06f66029
Update org bulk accept script. The script now: 1) leaves correctly control to the browser to update the GUI of the progress bar during ajax communication. 2) Displays an error if something went wrong (I've tried in my box stopping the server while doing the requests), and show a retry button to fetch again the list of the applications that are still not accepted and iterate over them again (seems to recover correctly restarting the server after the error in my box) 3) doesn't display any text at the beginning, and the button has the correct CSS class assigned 4) Check if there are applications to accept, if not displays a message stating that there are no orgs to accept. Patch by: Mario Ferraro Reviewed by: Lennard de Rijk
app/soc/content/js/org-accept.js
app/soc/templates/soc/org_app/review_overview.html
--- a/app/soc/content/js/org-accept.js	Mon Feb 16 19:30:33 2009 +0000
+++ b/app/soc/content/js/org-accept.js	Mon Feb 16 20:38:35 2009 +0000
@@ -2,44 +2,93 @@
 	$("#applications_progress_bar").progressBar({showText: false});
 });
 
-function acceptOrgInit() {
-	$("#acceptance_text").fadeOut("slow",
-		function() {
-			$("#applications_progress_bar").fadeIn("slow");
-		}
-	);
-	$.getJSON("{{ bulk_accept_link|safe }}",
+function acceptOrgInit(bulk_accept_link) {
+	// get the JSON object with details of every application for bulk acceptance
+	$.getJSON(bulk_accept_link,
 		function(data){
-			setTimeout(function(){acceptOrg(data)}, 0);
+			// If there are applications to accept...
+			if (data.nr_applications!=0) {
+				//...then fade out the button, show the progress bar and call the function for acceptance
+				$("#button_accept").fadeOut("slow",
+					function() {
+						$("#applications_progress_bar").progressBar(0);
+						$("#button_accept").val("Bulk accept");
+						$("#description_done").html("");
+						$("#applications_progress_bar").fadeIn("slow", acceptOrgs(data));
+					}
+				);
+			}else {
+				$("#description_done").html("<strong>No organizations to accept</strong>");
+			}
 		}
 	);
 }
 
-function acceptOrg(accepted_applications) {
-	var application_index = 0, max_applications=accepted_applications.applications.length;
-	for (application_index; application_index<max_applications; application_index++) {
+function acceptOrgs(data) {
+	// some global constants
+	var GLOBAL_LINK = data.link;
+	var TOTAL_APPLICATIONS = data.nr_applications;
+
+	// some global variables set needed for internal iteration
+	var application_index = 0;
+	// number of iteration is not taken from data.nr_applications
+	// to ensure avoidance of array out of bounds errors
+	var total_index = data.applications.length;
+
+
+	// call immediately the function for acceptance
+	// real iteration is inside
+	setTimeout(function(){
+		var error_happened = false;
+
+		var application = data.applications[application_index];
+		var current_application = application_index+1;
 		// regular expression to find a valid scope path inside matching parenthesis
 		var re = /\((\w*)\)/;
-		var scope_path = accepted_applications.link.match(re)[1];
+		var scope_path = GLOBAL_LINK.match(re)[1];
 		// the URL is obtained by using the scope path found in the matching parenthesis
-		var url_to_call = accepted_applications.link.replace(re,eval("accepted_applications.applications[application_index]."+scope_path));
+		var url_to_call = GLOBAL_LINK.replace(re,eval("application."+scope_path));
 		// now we can call the URL found
 		$.ajax({
 			async: false,
+			cache: false,
 			url: url_to_call,
 			timeout: 10000,
-			complete: function(data) {
+			success: function(data) {
 				if (data) {
 					// update progress bar percentage and description
-					var percentage = Math.floor(100 * (application_index+1) / (accepted_applications.nr_applications));
-					$("#description_progressbar").html(" Processed application "+accepted_applications.applications[application_index].name+" ("+(application_index+1)+"/"+accepted_applications.nr_applications+")");
+					var percentage = Math.floor(100 * (current_application) / (TOTAL_APPLICATIONS));
+					$("#description_progressbar").html(" Processed application "+application.name+" ("+(current_application)+"/"+TOTAL_APPLICATIONS+")");
 					$("#applications_progress_bar").progressBar(percentage);
 				}
+			},
+			error: function(XMLHttpRequest,textStatus,errorThrown) {
+				// if there is an error rename the button to Retry and show an error message
+				error_happened = true;
+				$("#button_accept").val("Retry");
+				$("#button_accept").fadeIn("slow", function() {
+					$("#description_done").html("<strong class='error'> Error encountered, try again</strong>");
+				});
 			}
 		});
-	}
-	// tell the user we are done
-	$("#description_done").html(" <strong>Done!</strong>");
-
+		// if there were no errors, continue the iteration
+		if (!error_happened) {
+			// prepare for new iteration and then recall this function
+			application_index++;
+			if (application_index<total_index) {
+				setTimeout(arguments.callee,0);
+			}
+			else {
+				// all ok, tell the user we are done
+				$("#applications_progress_bar").fadeOut("slow",
+					function() {
+						$("#applications_progress_bar").progressBar(0);
+						$("#button_accept").fadeIn("slow");
+					}
+				);
+				$("#description_progressbar").html("");
+				$("#description_done").html("<strong>Done!</strong>");
+			}
+		}
+	},0);
 }
-
--- a/app/soc/templates/soc/org_app/review_overview.html	Mon Feb 16 19:30:33 2009 +0000
+++ b/app/soc/templates/soc/org_app/review_overview.html	Mon Feb 16 20:38:35 2009 +0000
@@ -14,10 +14,7 @@
 {% endcomment %}
 
 {% block body %}
-<span id="acceptance_text">
-  If you want to accept all pre-accepted organizations please 
-  <button id="button_accept" onclick="javascript:acceptOrgInit();">click here</button>
-</span>
+<input id="button_accept" type="button" class="button" onclick="javascript:acceptOrgInit('{{ bulk_accept_link|safe }}');" value="Bulk accept"/>
 <span class="progressBar" style="display:none;" id="applications_progress_bar"></span>
 <span id="description_progressbar"></span><span id="description_done"></span>