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 def import_records
import_records = repostiory_import_actions import_records = repostiory_import_actions
status = import_records.import! 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
end end

View file

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

View file

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

View file

@ -6,7 +6,7 @@
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <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> </div>
<%= bootstrap_form_tag(url: import_records_repository_path(@import_data.repository, format: :json), <%= bootstrap_form_tag(url: import_records_repository_path(@import_data.repository, format: :json),
html: {'data-type' => 'json', id: 'form-import'}, html: {'data-type' => 'json', id: 'form-import'},
@ -63,7 +63,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t('general.cancel')%></button> <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> </div>
<% end %> <% end %>
</div> </div>

View file

@ -892,11 +892,11 @@ en:
success_flash: "%{number_of_rows} new record(s) successfully imported." success_flash: "%{number_of_rows} new record(s) successfully imported."
error_flash: "Something went wrong: %{message}" error_flash: "Something went wrong: %{message}"
error_message: error_message:
temp_file_not_found: 'This file could not be found. Your session might expire.' temp_file_not_found: "This file could not be found. Your session might expire."
session_expired: 'Your session expired. Please try again.' session_expired: "Your session expired. Please try again."
no_data_to_parse: 'There's nothing to be parsed.' no_data_to_parse: "There's nothing to be parsed."
no_column_name: 'Name column is required!' no_column_name: "Name column is required!"
duplicated_values: 'Two or more columns have the same mapping.' duplicated_values: "Two or more columns have the same mapping."
edit_record: "Edit" edit_record: "Edit"
delete_record: "Delete" delete_record: "Delete"
save_record: "Save" save_record: "Save"
@ -922,6 +922,8 @@ en:
alert_line_1: "you will lose information in this column for %{nr} records;" 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." alert_line_2: "the column will be deleted for all team members."
delete: "Delete column" delete: "Delete column"
modal_parse:
title: 'Import records'
modal_import: modal_import:
title: 'Import records' 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.' 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" unassigned_records_flash: "Successfully unassigned record(s) <strong>%{records}</strong> from task"
no_records_assigned_flash: "No records were assigned to task" no_records_assigned_flash: "No records were assigned to task"
no_records_unassigned_flash: "No records were unassigned from task" no_records_unassigned_flash: "No records were unassigned from task"
default_column: 'Name'
samples: samples:
columns: "Columns" columns: "Columns"
columns_visibility: "Toggle visibility" columns_visibility: "Toggle visibility"