mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-10 06:37:32 +08:00
Merge pull request #940 from okriuchykhin/ok_SCI_1946
Fix samples import [SCI-1946]
This commit is contained in:
commit
8ccb361525
1 changed files with 25 additions and 12 deletions
|
@ -7,17 +7,17 @@ class TeamsController < ApplicationController
|
||||||
def parse_sheet
|
def parse_sheet
|
||||||
session[:return_to] ||= request.referer
|
session[:return_to] ||= request.referer
|
||||||
|
|
||||||
unless params[:file]
|
unless import_params[:file]
|
||||||
return parse_sheet_error(t('teams.parse_sheet.errors.no_file_selected'))
|
return parse_sheet_error(t('teams.parse_sheet.errors.no_file_selected'))
|
||||||
end
|
end
|
||||||
if params[:file].size > Constants::FILE_MAX_SIZE_MB.megabytes
|
if import_params[:file].size > Constants::FILE_MAX_SIZE_MB.megabytes
|
||||||
error = t('general.file.size_exceeded',
|
error = t('general.file.size_exceeded',
|
||||||
file_size: Constants::FILE_MAX_SIZE_MB)
|
file_size: Constants::FILE_MAX_SIZE_MB)
|
||||||
return parse_sheet_error(error)
|
return parse_sheet_error(error)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
begin
|
||||||
sheet = SpreadsheetParser.open_spreadsheet(params[:file])
|
sheet = SpreadsheetParser.open_spreadsheet(import_params[:file])
|
||||||
@header, @columns = SpreadsheetParser.first_two_rows(sheet)
|
@header, @columns = SpreadsheetParser.first_two_rows(sheet)
|
||||||
|
|
||||||
if @header.empty? || @columns.empty?
|
if @header.empty? || @columns.empty?
|
||||||
|
@ -34,7 +34,7 @@ class TeamsController < ApplicationController
|
||||||
# Save file for next step (importing)
|
# Save file for next step (importing)
|
||||||
@temp_file = TempFile.new(
|
@temp_file = TempFile.new(
|
||||||
session_id: session.id,
|
session_id: session.id,
|
||||||
file: params[:file]
|
file: import_params[:file]
|
||||||
)
|
)
|
||||||
|
|
||||||
if @temp_file.save
|
if @temp_file.save
|
||||||
|
@ -65,23 +65,25 @@ class TeamsController < ApplicationController
|
||||||
session[:return_to] ||= request.referer
|
session[:return_to] ||= request.referer
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
if params[:file_id]
|
if import_params[:file_id]
|
||||||
@temp_file = TempFile.find_by_id(params[:file_id])
|
@temp_file = TempFile.find_by_id(import_params[:file_id])
|
||||||
|
|
||||||
if @temp_file
|
if @temp_file
|
||||||
# Check if session_id is equal to prevent file stealing
|
# Check if session_id is equal to prevent file stealing
|
||||||
if @temp_file.session_id == session.id
|
if @temp_file.session_id == session.id
|
||||||
# Check if mappings exists or else we don't have anything to parse
|
# Check if mappings exists or else we don't have anything to parse
|
||||||
if params[:mappings]
|
if import_params[:mappings]
|
||||||
@sheet = SpreadsheetParser.open_spreadsheet(@temp_file.file)
|
@sheet = SpreadsheetParser.open_spreadsheet(@temp_file.file)
|
||||||
|
|
||||||
# Check for duplicated values
|
# Check for duplicated values
|
||||||
h1 = params[:mappings].clone.delete_if { |k, v| v.empty? }
|
h1 = import_params[:mappings].clone.delete_if { |_, v| v.empty? }
|
||||||
if h1.length == h1.invert.length
|
if h1.length == h1.invert.length
|
||||||
|
|
||||||
# Check if there exist mapping for sample name (it's mandatory)
|
# Check if there exist mapping for sample name (it's mandatory)
|
||||||
if params[:mappings].has_value?("-1")
|
if import_params[:mappings].value?('-1')
|
||||||
result = @team.import_samples(@sheet, params[:mappings], current_user)
|
result = @team.import_samples(@sheet,
|
||||||
|
import_params[:mappings],
|
||||||
|
current_user)
|
||||||
nr_of_added = result[:nr_of_added]
|
nr_of_added = result[:nr_of_added]
|
||||||
total_nr = result[:total_nr]
|
total_nr = result[:total_nr]
|
||||||
|
|
||||||
|
@ -212,7 +214,7 @@ class TeamsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def export_samples
|
def export_samples
|
||||||
if params[:sample_ids] && params[:header_ids]
|
if export_params[:sample_ids] && export_params[:header_ids]
|
||||||
generate_samples_zip
|
generate_samples_zip
|
||||||
else
|
else
|
||||||
flash[:alert] = t('zip_export.export_error')
|
flash[:alert] = t('zip_export.export_error')
|
||||||
|
@ -248,6 +250,14 @@ class TeamsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def import_params
|
||||||
|
params.permit(:id, :file, :file_id, mappings: {}).to_h
|
||||||
|
end
|
||||||
|
|
||||||
|
def export_params
|
||||||
|
params.permit(sample_ids: [], header_ids: []).to_h
|
||||||
|
end
|
||||||
|
|
||||||
def check_create_sample_permissions
|
def check_create_sample_permissions
|
||||||
unless can_manage_samples?(@team)
|
unless can_manage_samples?(@team)
|
||||||
render_403
|
render_403
|
||||||
|
@ -264,7 +274,10 @@ class TeamsController < ApplicationController
|
||||||
zip = ZipExport.create(user: current_user)
|
zip = ZipExport.create(user: current_user)
|
||||||
zip.generate_exportable_zip(
|
zip.generate_exportable_zip(
|
||||||
current_user,
|
current_user,
|
||||||
@team.to_csv(Sample.where(id: params[:sample_ids]), params[:header_ids]),
|
@team.to_csv(
|
||||||
|
Sample.where(id: export_params[:sample_ids]),
|
||||||
|
export_params[:header_ids]
|
||||||
|
),
|
||||||
:samples
|
:samples
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue