Update import/export for protocol templates [SCI-7986] (#5032)

This commit is contained in:
ajugo 2023-02-27 13:22:01 +01:00 committed by GitHub
parent 33bb7294f1
commit 0328a32e09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 22 additions and 168 deletions

View file

@ -34,7 +34,6 @@ var ProtocolsIndex = (function() {
initProtocolPreviewModal();
initLinkedChildrenModal();
initModals();
initImport();
initVersionsModal();
}
@ -257,6 +256,7 @@ var ProtocolsIndex = (function() {
let protocolFilters = $($('#protocolFilters').html());
$(protocolFilters).prependTo('.protocols-container .protocol-filters');
initLocalFileImport();
initProtocolsFilters();
},
stateLoadCallback: function() {
@ -892,7 +892,7 @@ var ProtocolsIndex = (function() {
*/
function initImport() {
function initLocalFileImport() {
// Some templating code duplication. I know, I hate myself
function newElement(name, values) {
var template = $("[data-template='" + name + "']").clone();
@ -913,7 +913,6 @@ var ProtocolsIndex = (function() {
parentEl.find("[data-hold='" + name + "']").append(childEl);
}
let importResultsModal = $('#import-results-modal');
let fileInput = $("[data-role='import-file-input']");
// Make sure multiple selections of same file
@ -936,89 +935,23 @@ var ProtocolsIndex = (function() {
false,
function(datas) {
var nrSuccessful = 0;
var failed = [];
var unchanged = [];
var renamed = [];
_.each(datas, function(data) {
if (data.status === 'ok') {
nrSuccessful += 1;
if (data.name === data.new_name) {
unchanged.push(data);
} else {
renamed.push(data);
}
} else {
failed.push(data);
}
});
animateSpinner(null, false);
// Display the results modal by cloning
// templates and populating them
let modalBody = importResultsModal.find('.modal-body');
if (failed.length > 0) {
let failedMessageEl = newElement(
'import-result-message-error',
{
message: I18n.t('protocols.index.import_results.message_failed', { nr: failed.length })
}
);
modalBody.append(failedMessageEl);
animateSpinner(null, false);
if (nrSuccessful) {
HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_ok_html', { count: nrSuccessful }), 'success');
reloadTable();
} else {
HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_failed'), 'danger');
}
if (nrSuccessful > 0) {
let successMessageEl = newElement(
'import-result-message-success',
{
message: I18n.t('protocols.index.import_results.message_ok', { nr: nrSuccessful })
}
);
modalBody.append(successMessageEl);
}
let resultsListEl = newElement('import-result-list');
modalBody.append(resultsListEl);
if (unchanged.length > 0) {
_.each(unchanged, function(pr) {
var itemEl = newElement(
'import-result-unchanged-item',
{ message: pr.name }
);
addChildToElement(resultsListEl, 'items', itemEl);
});
}
if (renamed.length > 0) {
_.each(renamed, function(pr) {
var itemEl = newElement(
'import-result-renamed-item',
{ message: I18n.t('protocols.index.row_renamed_html', { old_name: pr.name, new_name: pr.new_name }) }
);
addChildToElement(resultsListEl, 'items', itemEl);
});
}
if (failed.length > 0) {
_.each(failed, function(pr) {
var itemEl = newElement(
'import-result-failed-item',
{
message: pr.name,
message2: (pr.status === 'size_too_large' ? I18n.t('protocols.index.import_results.row_file_too_large') : '')
}
);
addChildToElement(resultsListEl, 'items', itemEl);
});
}
importResultsModal.modal('show');
}
);
$(this).val('');
});
importResultsModal.on('hidden.bs.modal', function() {
importResultsModal.find('.modal-body').html('');
// Also reload table
reloadTable();
});
}
init();

View file

@ -638,23 +638,16 @@ class ProtocolsController < ApplicationController
respond_to do |format|
transaction_error = false
Protocol.transaction do
protocol =
@importer.import_new_protocol(@protocol_json, @type)
protocol = @importer.import_new_protocol(@protocol_json)
rescue StandardError => e
Rails.logger.error e.backtrace.join("\n")
transaction_error = true
raise ActiveRecord::Rollback
end
p_name =
if @protocol_json['name'].present?
escape_input(@protocol_json['name'])
else
t('protocols.index.no_protocol_name')
end
if transaction_error
format.json do
render json: { name: p_name, status: :bad_request }, status: :bad_request
render json: { status: :bad_request }, status: :bad_request
end
else
Activities::CreateActivityService
@ -667,10 +660,7 @@ class ProtocolsController < ApplicationController
})
format.json do
render json: {
name: escape_input(p_name), new_name: escape_input(protocol.name), status: :ok
},
status: :ok
render json: { status: :ok }, status: :ok
end
end
end
@ -769,9 +759,7 @@ class ProtocolsController < ApplicationController
@protocolsio_general_error = false
Protocol.transaction do
begin
protocol = @importer.import_new_protocol(
@db_json, params[:type].to_sym
)
protocol = @importer.import_new_protocol(@db_json)
rescue Exception
transaction_error = true
raise ActiveRecord:: Rollback
@ -1285,12 +1273,7 @@ class ProtocolsController < ApplicationController
def check_import_permissions
@protocol_json = params[:protocol]
@team = Team.find(params[:team_id])
@type = params[:type] ? params[:type].to_sym : nil
unless @protocol_json.present? && @team.present? &&
(@type == :public || @type == :private) &&
can_create_protocols_in_repository?(@team)
render_403
end
render_403 unless @protocol_json.present? && @team.present? && can_create_protocols_in_repository?(@team)
end
def check_export_permissions

View file

@ -27,6 +27,7 @@ module ProtocolsExporter
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
"version=\"1.0\">\n"
protocols.each do |protocol|
protocol = protocol.latest_published_version || protocol
protocol_name = get_protocol_name(protocol)
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \

View file

@ -9,6 +9,7 @@ module ProtocolsExporterV2
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
"version=\"1.1\">\n"
protocols.each do |protocol|
protocol = protocol.latest_published_version || protocol
protocol_name = get_protocol_name(protocol)
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \

View file

@ -8,13 +8,12 @@ class ProtocolsImporter
@team = team
end
def import_new_protocol(protocol_json, type)
def import_new_protocol(protocol_json)
remove_empty_inputs(protocol_json)
protocol = Protocol.new(
name: protocol_json['name'],
authors: protocol_json['authors'],
protocol_type: (type == :public ? :in_repository_public : :in_repository_private),
published_on: (type == :public ? Time.now : nil),
protocol_type: :in_repository_draft,
added_by: @user,
team: @team
)

View file

@ -8,13 +8,12 @@ class ProtocolsImporterV2
@team = team
end
def import_new_protocol(protocol_json, type)
def import_new_protocol(protocol_json)
remove_empty_inputs(protocol_json)
protocol = Protocol.new(
name: protocol_json['name'],
authors: protocol_json['authors'],
protocol_type: (type == :public ? :in_repository_public : :in_repository_private),
published_on: (type == :public ? Time.now : nil),
protocol_type: :in_repository_draft,
added_by: @user,
team: @team
)

View file

@ -40,7 +40,6 @@
<%= render partial: "protocols/index/confirm_archive_modal.html.erb" %>
<%= render partial: "protocols/index/archive_results_modal.html.erb" %>
<%= render partial: "protocols/index/delete_draft_modal.html.erb" %>
<%= render partial: "protocols/index/import_results_modal.html.erb" %>
<%= render partial: "protocols/index/linked_children_modal.html.erb" %>
<%= render partial: "protocols/index/protocolsio_modal.html.erb" %>
<%= render partial: "protocols/index/new_protocol_modal.html.erb", locals: {type: 'new'} %>

View file

@ -22,8 +22,7 @@
<a class="btn-link-alt btn-default-link btn-open-file" <%= can_create_protocols_in_repository?(@current_team) ? 'data-action="import"' : 'disabled="disabled"' %>>
<span><%= t("protocols.index.import_alt") %></span>
<input type="file" value="" accept=".eln" data-role="import-file-input"
data-team-id="<%= @current_team.id %>"
data-type="<%= @type %>" data-import-url="<%= import_protocols_path %>"
data-team-id="<%= @current_team.id %>" data-import-url="<%= import_protocols_path %>"
<%= 'disabled="disabled"' unless can_create_protocols_in_repository?(@current_team) %>>
</a>
</li>

View file

@ -1,60 +0,0 @@
<div class="modal" id="import-results-modal" tabindex="-1" role="dialog" aria-labelledby="import-results-modal-label">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="import-results-modal-label">
<%= t("protocols.index.import_results.title") %>
</h4>
</div>
<div class="modal-body"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><%=t "general.close" %></button>
</div>
</div>
</div>
</div>
<!-- Bunch of templates for rendering import results -->
<!-- (THIS IS UGLY) -->
<div data-template="import-result-message-error" style="display: none;">
<div class="alert alert-danger" style="margin-bottom: 15px;" role="alert">
<span class="fas fa-exclamation-triangle"></span>
<span data-val="message"></span>
</div>
</div>
<div data-template="import-result-message-success" style="display: none;">
<div data-val="message">
</div>
</div>
<div data-template="import-result-list" style="display: none;">
<div class="well well-sm well-protocols-results">
<ul data-hold="items">
</ul>
</div>
</div>
<div data-template="import-result-unchanged-item" style="display: none;">
<li>
<span class="label label-success"><%= t("protocols.index.import_results.row_success") %></span>
<span data-val="message"></span>
</li>
</div>
<div data-template="import-result-renamed-item" style="display: none;">
<li>
<span class="label label-warning"><%= t("protocols.index.import_results.row_renamed") %></span>
<span data-val="message"></span>
</li>
</div>
<div data-template="import-result-failed-item" style="display: none;">
<li>
<span class="label label-danger"><%= t("protocols.index.import_results.row_failed") %></span>
<span data-val="message"></span>
&nbsp;
<span data-val="message2"></span>
</li>
</div>

View file

@ -2793,8 +2793,8 @@ en:
row_failed: "Failed"
import_results:
title: "Import results"
message_failed: "Failed to import %{nr} protocol/s."
message_ok: "Successfully imported %{nr} protocol/s."
message_failed: "Failed to import protocol template(s)."
message_ok_html: "<strong>%{count} protocol</strong> template(s) successfully imported."
message_ok_pio: "Successfully imported protocol from protocols.io file."
message_warn_truncated: "Successfully imported protocol from protocols.io file. However, text in some fields was too long so we had to cut it off."
row_success: "Imported"