mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-12-17 14:19:05 +08:00
Update import/export for protocol templates [SCI-7986] (#5032)
This commit is contained in:
parent
33bb7294f1
commit
0328a32e09
10 changed files with 22 additions and 168 deletions
|
|
@ -34,7 +34,6 @@ var ProtocolsIndex = (function() {
|
||||||
initProtocolPreviewModal();
|
initProtocolPreviewModal();
|
||||||
initLinkedChildrenModal();
|
initLinkedChildrenModal();
|
||||||
initModals();
|
initModals();
|
||||||
initImport();
|
|
||||||
initVersionsModal();
|
initVersionsModal();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -257,6 +256,7 @@ var ProtocolsIndex = (function() {
|
||||||
let protocolFilters = $($('#protocolFilters').html());
|
let protocolFilters = $($('#protocolFilters').html());
|
||||||
$(protocolFilters).prependTo('.protocols-container .protocol-filters');
|
$(protocolFilters).prependTo('.protocols-container .protocol-filters');
|
||||||
|
|
||||||
|
initLocalFileImport();
|
||||||
initProtocolsFilters();
|
initProtocolsFilters();
|
||||||
},
|
},
|
||||||
stateLoadCallback: function() {
|
stateLoadCallback: function() {
|
||||||
|
|
@ -892,7 +892,7 @@ var ProtocolsIndex = (function() {
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
function initImport() {
|
function initLocalFileImport() {
|
||||||
// Some templating code duplication. I know, I hate myself
|
// Some templating code duplication. I know, I hate myself
|
||||||
function newElement(name, values) {
|
function newElement(name, values) {
|
||||||
var template = $("[data-template='" + name + "']").clone();
|
var template = $("[data-template='" + name + "']").clone();
|
||||||
|
|
@ -913,7 +913,6 @@ var ProtocolsIndex = (function() {
|
||||||
parentEl.find("[data-hold='" + name + "']").append(childEl);
|
parentEl.find("[data-hold='" + name + "']").append(childEl);
|
||||||
}
|
}
|
||||||
|
|
||||||
let importResultsModal = $('#import-results-modal');
|
|
||||||
let fileInput = $("[data-role='import-file-input']");
|
let fileInput = $("[data-role='import-file-input']");
|
||||||
|
|
||||||
// Make sure multiple selections of same file
|
// Make sure multiple selections of same file
|
||||||
|
|
@ -936,89 +935,23 @@ var ProtocolsIndex = (function() {
|
||||||
false,
|
false,
|
||||||
function(datas) {
|
function(datas) {
|
||||||
var nrSuccessful = 0;
|
var nrSuccessful = 0;
|
||||||
var failed = [];
|
|
||||||
var unchanged = [];
|
|
||||||
var renamed = [];
|
|
||||||
_.each(datas, function(data) {
|
_.each(datas, function(data) {
|
||||||
if (data.status === 'ok') {
|
if (data.status === 'ok') {
|
||||||
nrSuccessful += 1;
|
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
|
if (nrSuccessful) {
|
||||||
// templates and populating them
|
HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_ok_html', { count: nrSuccessful }), 'success');
|
||||||
let modalBody = importResultsModal.find('.modal-body');
|
reloadTable();
|
||||||
if (failed.length > 0) {
|
} else {
|
||||||
let failedMessageEl = newElement(
|
HelperModule.flashAlertMsg(I18n.t('protocols.index.import_results.message_failed'), 'danger');
|
||||||
'import-result-message-error',
|
|
||||||
{
|
|
||||||
message: I18n.t('protocols.index.import_results.message_failed', { nr: failed.length })
|
|
||||||
}
|
|
||||||
);
|
|
||||||
modalBody.append(failedMessageEl);
|
|
||||||
animateSpinner(null, false);
|
|
||||||
}
|
}
|
||||||
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('');
|
$(this).val('');
|
||||||
});
|
});
|
||||||
importResultsModal.on('hidden.bs.modal', function() {
|
|
||||||
importResultsModal.find('.modal-body').html('');
|
|
||||||
|
|
||||||
// Also reload table
|
|
||||||
reloadTable();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
|
||||||
|
|
@ -638,23 +638,16 @@ class ProtocolsController < ApplicationController
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
transaction_error = false
|
transaction_error = false
|
||||||
Protocol.transaction do
|
Protocol.transaction do
|
||||||
protocol =
|
protocol = @importer.import_new_protocol(@protocol_json)
|
||||||
@importer.import_new_protocol(@protocol_json, @type)
|
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
Rails.logger.error e.backtrace.join("\n")
|
Rails.logger.error e.backtrace.join("\n")
|
||||||
transaction_error = true
|
transaction_error = true
|
||||||
raise ActiveRecord::Rollback
|
raise ActiveRecord::Rollback
|
||||||
end
|
end
|
||||||
|
|
||||||
p_name =
|
|
||||||
if @protocol_json['name'].present?
|
|
||||||
escape_input(@protocol_json['name'])
|
|
||||||
else
|
|
||||||
t('protocols.index.no_protocol_name')
|
|
||||||
end
|
|
||||||
if transaction_error
|
if transaction_error
|
||||||
format.json do
|
format.json do
|
||||||
render json: { name: p_name, status: :bad_request }, status: :bad_request
|
render json: { status: :bad_request }, status: :bad_request
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
Activities::CreateActivityService
|
Activities::CreateActivityService
|
||||||
|
|
@ -667,10 +660,7 @@ class ProtocolsController < ApplicationController
|
||||||
})
|
})
|
||||||
|
|
||||||
format.json do
|
format.json do
|
||||||
render json: {
|
render json: { status: :ok }, status: :ok
|
||||||
name: escape_input(p_name), new_name: escape_input(protocol.name), status: :ok
|
|
||||||
},
|
|
||||||
status: :ok
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -769,9 +759,7 @@ class ProtocolsController < ApplicationController
|
||||||
@protocolsio_general_error = false
|
@protocolsio_general_error = false
|
||||||
Protocol.transaction do
|
Protocol.transaction do
|
||||||
begin
|
begin
|
||||||
protocol = @importer.import_new_protocol(
|
protocol = @importer.import_new_protocol(@db_json)
|
||||||
@db_json, params[:type].to_sym
|
|
||||||
)
|
|
||||||
rescue Exception
|
rescue Exception
|
||||||
transaction_error = true
|
transaction_error = true
|
||||||
raise ActiveRecord:: Rollback
|
raise ActiveRecord:: Rollback
|
||||||
|
|
@ -1285,12 +1273,7 @@ class ProtocolsController < ApplicationController
|
||||||
def check_import_permissions
|
def check_import_permissions
|
||||||
@protocol_json = params[:protocol]
|
@protocol_json = params[:protocol]
|
||||||
@team = Team.find(params[:team_id])
|
@team = Team.find(params[:team_id])
|
||||||
@type = params[:type] ? params[:type].to_sym : nil
|
render_403 unless @protocol_json.present? && @team.present? && can_create_protocols_in_repository?(@team)
|
||||||
unless @protocol_json.present? && @team.present? &&
|
|
||||||
(@type == :public || @type == :private) &&
|
|
||||||
can_create_protocols_in_repository?(@team)
|
|
||||||
render_403
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_export_permissions
|
def check_export_permissions
|
||||||
|
|
|
||||||
|
|
@ -27,6 +27,7 @@ module ProtocolsExporter
|
||||||
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
|
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
|
||||||
"version=\"1.0\">\n"
|
"version=\"1.0\">\n"
|
||||||
protocols.each do |protocol|
|
protocols.each do |protocol|
|
||||||
|
protocol = protocol.latest_published_version || protocol
|
||||||
protocol_name = get_protocol_name(protocol)
|
protocol_name = get_protocol_name(protocol)
|
||||||
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
|
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
|
||||||
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \
|
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ module ProtocolsExporterV2
|
||||||
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
|
envelope_xml = "<envelope xmlns=\"http://www.scinote.net\" " \
|
||||||
"version=\"1.1\">\n"
|
"version=\"1.1\">\n"
|
||||||
protocols.each do |protocol|
|
protocols.each do |protocol|
|
||||||
|
protocol = protocol.latest_published_version || protocol
|
||||||
protocol_name = get_protocol_name(protocol)
|
protocol_name = get_protocol_name(protocol)
|
||||||
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
|
envelope_xml << "<protocol id=\"#{protocol.id}\" " \
|
||||||
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \
|
"guid=\"#{get_guid(protocol.id)}\">#{protocol_name}" \
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,12 @@ class ProtocolsImporter
|
||||||
@team = team
|
@team = team
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_new_protocol(protocol_json, type)
|
def import_new_protocol(protocol_json)
|
||||||
remove_empty_inputs(protocol_json)
|
remove_empty_inputs(protocol_json)
|
||||||
protocol = Protocol.new(
|
protocol = Protocol.new(
|
||||||
name: protocol_json['name'],
|
name: protocol_json['name'],
|
||||||
authors: protocol_json['authors'],
|
authors: protocol_json['authors'],
|
||||||
protocol_type: (type == :public ? :in_repository_public : :in_repository_private),
|
protocol_type: :in_repository_draft,
|
||||||
published_on: (type == :public ? Time.now : nil),
|
|
||||||
added_by: @user,
|
added_by: @user,
|
||||||
team: @team
|
team: @team
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,12 @@ class ProtocolsImporterV2
|
||||||
@team = team
|
@team = team
|
||||||
end
|
end
|
||||||
|
|
||||||
def import_new_protocol(protocol_json, type)
|
def import_new_protocol(protocol_json)
|
||||||
remove_empty_inputs(protocol_json)
|
remove_empty_inputs(protocol_json)
|
||||||
protocol = Protocol.new(
|
protocol = Protocol.new(
|
||||||
name: protocol_json['name'],
|
name: protocol_json['name'],
|
||||||
authors: protocol_json['authors'],
|
authors: protocol_json['authors'],
|
||||||
protocol_type: (type == :public ? :in_repository_public : :in_repository_private),
|
protocol_type: :in_repository_draft,
|
||||||
published_on: (type == :public ? Time.now : nil),
|
|
||||||
added_by: @user,
|
added_by: @user,
|
||||||
team: @team
|
team: @team
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,6 @@
|
||||||
<%= render partial: "protocols/index/confirm_archive_modal.html.erb" %>
|
<%= render partial: "protocols/index/confirm_archive_modal.html.erb" %>
|
||||||
<%= render partial: "protocols/index/archive_results_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/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/linked_children_modal.html.erb" %>
|
||||||
<%= render partial: "protocols/index/protocolsio_modal.html.erb" %>
|
<%= render partial: "protocols/index/protocolsio_modal.html.erb" %>
|
||||||
<%= render partial: "protocols/index/new_protocol_modal.html.erb", locals: {type: 'new'} %>
|
<%= render partial: "protocols/index/new_protocol_modal.html.erb", locals: {type: 'new'} %>
|
||||||
|
|
|
||||||
|
|
@ -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"' %>>
|
<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>
|
<span><%= t("protocols.index.import_alt") %></span>
|
||||||
<input type="file" value="" accept=".eln" data-role="import-file-input"
|
<input type="file" value="" accept=".eln" data-role="import-file-input"
|
||||||
data-team-id="<%= @current_team.id %>"
|
data-team-id="<%= @current_team.id %>" data-import-url="<%= import_protocols_path %>"
|
||||||
data-type="<%= @type %>" data-import-url="<%= import_protocols_path %>"
|
|
||||||
<%= 'disabled="disabled"' unless can_create_protocols_in_repository?(@current_team) %>>
|
<%= 'disabled="disabled"' unless can_create_protocols_in_repository?(@current_team) %>>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
|
||||||
|
|
@ -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">×</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>
|
|
||||||
|
|
||||||
<span data-val="message2"></span>
|
|
||||||
</li>
|
|
||||||
</div>
|
|
||||||
|
|
@ -2793,8 +2793,8 @@ en:
|
||||||
row_failed: "Failed"
|
row_failed: "Failed"
|
||||||
import_results:
|
import_results:
|
||||||
title: "Import results"
|
title: "Import results"
|
||||||
message_failed: "Failed to import %{nr} protocol/s."
|
message_failed: "Failed to import protocol template(s)."
|
||||||
message_ok: "Successfully imported %{nr} protocol/s."
|
message_ok_html: "<strong>%{count} protocol</strong> template(s) successfully imported."
|
||||||
message_ok_pio: "Successfully imported protocol from protocols.io file."
|
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."
|
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"
|
row_success: "Imported"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue