Skip to content
Snippets Groups Projects
  • Benjamin Bertrand's avatar
    8471ec5b
    Add button to export items to excel file · 8471ec5b
    Benjamin Bertrand authored
    Retrieving all items from the database takes some time.
    The task is thus handled by the RQ worker.
    We use ajax to periodically monitor the status of the job
    and redirect to the file when ready.
    A progress bar is displayed to let the user see the progress.
    
    The mechanism put in place could be re-used for other use cases.
    
    JIRA INFRA-545 #action In Progress
    8471ec5b
    History
    Add button to export items to excel file
    Benjamin Bertrand authored
    Retrieving all items from the database takes some time.
    The task is thus handled by the RQ worker.
    We use ajax to periodically monitor the status of the job
    and redirect to the file when ready.
    A progress bar is displayed to let the user see the progress.
    
    The mechanism put in place could be re-used for other use cases.
    
    JIRA INFRA-545 #action In Progress
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
csentry.js 3.07 KiB
function flash_alert(message, category) {
  var htmlString = '<div class="alert alert-' + category + ' alert-dismissible" role="alert">'
  htmlString += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">'
  htmlString += '<span aria-hidden="true">&times;</span></button>' + message + '</div>'
  $(htmlString).prependTo("#mainContent").hide().slideDown();
}

function remove_alerts() {
  $(".alert").slideUp("normal", function() {
    $(this).remove();
  });
}

function update_progress_bar(value) {
  // Assume that there is only one progress bar
  $(".progress-bar")
    .css("width", value + "%")
    .attr("aria-valuenow", value)
    .text(value + "%");
}

// Send a request to the server every 500 ms
// until the job is done
function check_job_status(status_url, $modal) {
  // cache shall be set to false for this to work in IE!
  $.ajax({
    dataType: "json",
    url: status_url,
    cache: false,
    success: function(data, status, request) {
      switch (data.status) {
        case "unknown":
          $modal.modal('hide');
          flash_alert("Unknown job id", "danger");
          break;
        case "finished":
          $modal.modal('hide');
          switch (data.func_name) {
            case "generate_items_excel_file":
              window.location.href = $SCRIPT_ROOT + "/static/files/" + data.result;
              break;
          }
          break;
        case "failed":
          $modal.modal('hide');
          flash_alert(data.message, "danger");
          break;
        case "started":
          if( data.progress !== null ) {
            update_progress_bar(data.progress);
          }
        default:
          // queued/started/deferred
          setTimeout(function() {
            check_job_status(status_url, $modal);
          }, 500);
      }
    }
  });
}

// Function to dynamically update a select field
function update_selectfield(field_id, data, selected_value) {
  var $field = $(field_id);
  $field.empty();
  $.map(data, function(option, index) {
    if( option == "None" ) {
      var text = "";
    } else {
      var text = option;
    }
    $field.append($("<option></option>").attr("value", option).text(text));
  });
  $field.val(selected_value);
}

// Function to populate dynamically the stack_member field
// in the create and edit item forms
function update_stack_member() {
  // Retrieve free stack members
  var host_id = $("#host_id").val();
  $.getJSON(
    $SCRIPT_ROOT + "/inventory/_retrieve_free_stack_members/" + host_id,
    {
      ics_id: $("#ics_id").val()
    },
    function(json) {
      update_selectfield("#stack_member", json.data.stack_members, json.data.selected_member);
      $("#stack_member").prop("disabled", json.data.disabled);
    }
  );
}

$(document).ready(function() {

  // When an invalid input was submitted, the server
  // adds the "is-invalid" class to form fields to display
  // them in red with an error message
  // When starting to type again, we want to remove the error
  // message to not confuse the user
  $('input[type="text"]').keyup(function(event) {
    $(this).removeClass("is-invalid");
  });

});