adds import records to repositories [fixes SCI-1336]

This commit is contained in:
zmagod 2017-06-21 14:45:50 +02:00
parent f64e1ac713
commit f66fb49b56
5 changed files with 35 additions and 47 deletions

View file

@ -232,15 +232,19 @@ class RepositoriesController < ApplicationController
def import_records
import_records = repostiory_import_actions
status = import_records.import!
respond_to do |format|
format.json do
if status[:status] == :ok
flash[:success] = t('repositories.import_records.success_flash',
number_of_rows: status[:nr_of_added])
render json: {}, status: :ok
else
flash[:alert] = t('repositories.import_records.error_flash',
message: status[:errors])
render json: {}, status: :unprocessable_entity
end
end
if status[:status] == :ok
flash[:success] = t('repositories.import_records.success_flash',
number_of_rows: status[:nr_of_added])
head :ok
else
flash[:alert] = t('repositories.import_records.error_flash',
message: status[:errors])
head :unprocessable_entity
end
end

View file

@ -29,7 +29,7 @@ class Repository < ActiveRecord::Base
def available_repository_fields
fields = {}
# First and foremost add sample name
fields['-1'] = I18n.t('samples.table.sample_name')
fields['-1'] = I18n.t('repositories.default_column')
# Add all other custom columns
repository_columns.order(:created_at).each do |rc|
fields[rc.id] = rc.name
@ -69,10 +69,7 @@ class Repository < ActiveRecord::Base
errors = []
custom_fields = []
name_index = -1
total_nr = 0
nr_of_added = 0
header = sheet.row(1)
generate_new_columns(header)
mappings.each.with_index do |(k, v), i|
if v == '-1'
@ -80,8 +77,7 @@ class Repository < ActiveRecord::Base
custom_fields << nil
name_index = i
else
cf = repository_columns.find_by_name(header[i])
cf = repository_columns.find_by_id(v)
custom_fields << cf
end
end
@ -89,19 +85,15 @@ class Repository < ActiveRecord::Base
# Now we can iterate through sample data and save stuff into db
(2..sheet.last_row).each do |i|
error = []
total_nr += 1
nr_of_added += 1
record_row = RepositoryRow.new(name: sheet.row(i)[name_index],
repository: self,
created_by: user,
last_modified_by: user)
if record_row.save
sheet.row(i).each.with_index do |value, index|
# We need to have sample saved before messing with custom fields (they
# need sample id)
if custom_fields[index]
nr_of_added += 1
# we're working with CustomField
rep_column = RepositoryTextValue.new(
data: value,
@ -125,30 +117,20 @@ class Repository < ActiveRecord::Base
end
if errors.count > 0
return {
status: :error,
errors: errors,
nr_of_added: nr_of_added,
total_nr: total_nr
}
else
return {
status: :ok,
nr_of_added: nr_of_added,
total_nr: total_nr
}
return { status: :error, errors: errors, nr_of_added: nr_of_added }
end
{ status: :ok, nr_of_added: nr_of_added }
end
private
def generate_new_columns(header)
rep_columns_names = self.repository_columns.pluck(:name).push('Name')
header.each do |cname|
next if rep_columns_names.include? cname
RepositoryColumn.create(repository: self, name: cname, data_type: 0)
end
end
# def generate_new_columns(header)
# rep_columns_names = self.repository_columns.pluck(:name).push('Name')
# header.each do |cname|
# next if rep_columns_names.include? cname
# RepositoryColumn.create(repository: self, name: cname, data_type: 0)
# end
# end
def generate_file(filename, file_path)
case File.extname(filename)

View file

@ -9,7 +9,6 @@ module ImportRepository
end
def import!
status = run_import_actions
@temp_file.destroy
status

View file

@ -6,7 +6,7 @@
<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="parse-modal-title"><%= t('samples.modal_import.title') %></h4>
<h4 class="modal-title" id="parse-modal-title"><%= t('repositories.modal_parse.title') %></h4>
</div>
<%= bootstrap_form_tag(url: import_records_repository_path(@import_data.repository, format: :json),
html: {'data-type' => 'json', id: 'form-import'},
@ -63,7 +63,7 @@
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t('general.cancel')%></button>
<input type="submit" class="btn btn-primary" value="<%= t('teams.parse_sheet.import_samples') %>"</input>
<input type="submit" class="btn btn-primary" value="<%= t('repositories.modal_parse.title') %>"</input>
</div>
<% end %>
</div>

View file

@ -892,11 +892,11 @@ en:
success_flash: "%{number_of_rows} new record(s) successfully imported."
error_flash: "Something went wrong: %{message}"
error_message:
temp_file_not_found: 'This file could not be found. Your session might expire.'
session_expired: 'Your session expired. Please try again.'
no_data_to_parse: 'There's nothing to be parsed.'
no_column_name: 'Name column is required!'
duplicated_values: 'Two or more columns have the same mapping.'
temp_file_not_found: "This file could not be found. Your session might expire."
session_expired: "Your session expired. Please try again."
no_data_to_parse: "There's nothing to be parsed."
no_column_name: "Name column is required!"
duplicated_values: "Two or more columns have the same mapping."
edit_record: "Edit"
delete_record: "Delete"
save_record: "Save"
@ -922,6 +922,8 @@ en:
alert_line_1: "you will lose information in this column for %{nr} records;"
alert_line_2: "the column will be deleted for all team members."
delete: "Delete column"
modal_parse:
title: 'Import records'
modal_import:
title: 'Import records'
notice: 'You may upload .csv file (comma separated) or tab separated file (.txt or .tdv) or Excel file (.xls, .xlsx). First row should include header names, followed by rows with sample data.'
@ -944,7 +946,8 @@ en:
unassigned_records_flash: "Successfully unassigned record(s) <strong>%{records}</strong> from task"
no_records_assigned_flash: "No records were assigned to task"
no_records_unassigned_flash: "No records were unassigned from task"
default_column: 'Name'
samples:
columns: "Columns"
columns_visibility: "Toggle visibility"