Commit 9effefce authored by PiTrem's avatar PiTrem
Browse files

export_samples_from_sel to acept uiState params

allow to export samples from ui state selection
(sample/reaction/wellplate)
not only from current element type
parent 22b3ab5b
...@@ -64,13 +64,28 @@ module Chemotion ...@@ -64,13 +64,28 @@ module Chemotion
end end
params do params do
requires :type, type: String requires :columns, type: Array[String]
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
requires :currentCollection, type: Integer
requires :removedColumns, type: Array[String]
requires :exportType, type: Integer requires :exportType, type: Integer
requires :uiState, type: Hash do
requires :sample, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :reaction, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :wellplate, type: Hash do
requires :checkedIds, type: Array
requires :uncheckedIds, type: Array
requires :checkedAll, type: Boolean
end
requires :currentCollection, type: Integer
requires :isSync, type: Boolean
end
requires :columns, type: Array
end end
post :export_samples_from_selections do post :export_samples_from_selections do
env['api.format'] = :binary env['api.format'] = :binary
...@@ -85,34 +100,32 @@ module Chemotion ...@@ -85,34 +100,32 @@ module Chemotion
fileType = '.sdf' fileType = '.sdf'
export = Reporter::SdfExport.new export = Reporter::SdfExport.new
end end
fileName = params[:type].capitalize + '_' + time_now + fileType fileName = 'sample_export_' + time_now + fileType
fileURI = URI.escape(fileName) fileURI = URI.escape(fileName)
header 'Content-Disposition', "attachment; filename=\"#{fileURI}\"" header 'Content-Disposition', "attachment; filename=\"#{fileURI}\""
# header 'Content-Disposition', "attachment; filename*=UTF-8''#{fileURI}" # header 'Content-Disposition', "attachment; filename*=UTF-8''#{fileURI}"
currColl = params[:uiState][:isSync] ? 0 : params[:uiState][:currentCollection]
type = params[:type] removed_field = params[:columns]
checkedIds = params[:checkedIds] [:sample, :reaction, :wellplate].each do |type|
uncheckedIds = params[:uncheckedIds] next unless ( p_t = params[:uiState][type])
checkedAll = params[:checkedAll] elements = selected_elements(
currentCollection = params[:currentCollection] type.to_s, p_t[:checkedAll], p_t[:checkedIds],
removed_field = params[:removedColumns] p_t[:uncheckedIds], currColl
)
elements = selected_elements( samples = case type.to_s
type, checkedAll, checkedIds, uncheckedIds, currentCollection when 'sample'
) elements.includes([:molecule,:molecule_name])
samples = if type == 'sample' when 'reaction'
elements.includes([:molecule,:molecule_name]) elements.map { |r|
elsif type == 'reaction' r.starting_materials + r.reactants + r.products
elements.map { |r| }.flatten
r.starting_materials + r.reactants + r.products when 'wellplate'
}.flatten elements.map { |wellplate|
elsif type == 'wellplate' wellplate.wells.map(&:sample).flatten
elements.map { |wellplate| }.flatten
wellplate.wells.map(&:sample).flatten end
}.flatten samples.each { |sample| export.add_sample(sample) }
end end
samples.each { |sample| export.add_sample(sample) }
export.generate_file(excluded_field, included_field, removed_field) export.generate_file(excluded_field, included_field, removed_field)
end end
......
...@@ -122,29 +122,33 @@ export default class ModalExport extends React.Component { ...@@ -122,29 +122,33 @@ export default class ModalExport extends React.Component {
} }
} }
const exportSelections = (uiState, userState, removedColumns, e) => { const exportSelections = (uiState, userState, columns, e) => {
const { currentCollection, sample, reaction, wellplate } = uiState;
const { currentType } = userState;
const { checkedIds, uncheckedIds, checkedAll } = sample
ReportsFetcher.createDownloadFile({ ReportsFetcher.createDownloadFile({
type: currentType,
exportType: e, exportType: e,
checkedIds: checkedIds.toArray(), uiState: filterUIState(uiState),
uncheckedIds: uncheckedIds.toArray(), columns: columns
checkedAll: checkedAll,
currentCollection: currentCollection.id,
removedColumns: removedColumns
}); });
} }
const filterUIState = (uiState) =>{
const { currentCollection, sample, reaction, wellplate, isSync } = uiState;
const selectedStringfy = (input, currentCollection, removedColumns, e) => { return {
const { checkedIds, uncheckedIds, checkedAll } = input; sample: {
return "&exportType=" + e + checkedIds: sample.checkedIds.toArray(),
"&checkedIds=" + checkedIds.toArray() + uncheckedIds: sample.uncheckedIds.toArray(),
"&uncheckedIds=" + uncheckedIds.toArray() + checkedAll: sample.checkedAll,
"&checkedAll=" + checkedAll + },
"&currentCollection=" + currentCollection.id + reaction: {
"&removedColumns=" + removedColumns checkedIds: reaction.checkedIds.toArray(),
uncheckedIds: reaction.uncheckedIds.toArray(),
checkedAll: reaction.checkedAll,
},
wellplate: {
checkedIds: wellplate.checkedIds.toArray(),
uncheckedIds: wellplate.uncheckedIds.toArray(),
checkedAll: wellplate.checkedAll,
},
currentCollection: currentCollection.id,
isSync: isSync,
}
} }
...@@ -3,15 +3,15 @@ class Reporter::ExcelExport ...@@ -3,15 +3,15 @@ class Reporter::ExcelExport
DEFAULT_ROW_HEIGHT = 20 DEFAULT_ROW_HEIGHT = 20
def initialize def initialize
@@sample_list = Array.new @sample_list = Array.new
end end
def add_sample(sample) def add_sample(sample)
@@sample_list << sample @sample_list << sample
end end
def generate_file(default_excluded_field, default_included_field, removed_field = []) def generate_file(default_excluded_field, default_included_field, removed_field = [])
return -1 if @@sample_list.empty? || @@sample_list.first == nil return -1 if @sample_list.empty? || @sample_list.first == nil
header = process_header(default_excluded_field, default_included_field, removed_field) header = process_header(default_excluded_field, default_included_field, removed_field)
return -1 if header.empty? return -1 if header.empty?
p = Axlsx::Package.new p = Axlsx::Package.new
...@@ -23,7 +23,7 @@ class Reporter::ExcelExport ...@@ -23,7 +23,7 @@ class Reporter::ExcelExport
files = [] # do not let Tempfile object to be garbage collected files = [] # do not let Tempfile object to be garbage collected
need_images = header.index("Image") need_images = header.index("Image")
@@sample_list.compact.each_with_index do |sample, row| @sample_list.compact.each_with_index do |sample, row|
data_hash = [] data_hash = []
start = 0 start = 0
...@@ -50,7 +50,7 @@ class Reporter::ExcelExport ...@@ -50,7 +50,7 @@ class Reporter::ExcelExport
end end
def process_header(default_excluded_field, default_included_field, removed_field) def process_header(default_excluded_field, default_included_field, removed_field)
header = @@sample_list.first.attribute_names header = @sample_list.first.attribute_names
# Exclude field # Exclude field
header.delete_if { |x| default_excluded_field.include?(x) } header.delete_if { |x| default_excluded_field.include?(x) }
header = header.reject { |x| x.empty? } header = header.reject { |x| x.empty? }
......
class Reporter::SdfExport class Reporter::SdfExport
def initialize def initialize
@@sample_list = Array.new @sample_list = Array.new
end end
def add_sample(sample) def add_sample(sample)
@@sample_list << sample @sample_list << sample
end end
def generate_file(default_excluded_field, default_included_field, removed_field = []) def generate_file(default_excluded_field, default_included_field, removed_field = [])
return -1 if @@sample_list.empty? || @@sample_list.first == nil return -1 if @sample_list.empty? || @sample_list.first == nil
mdf_string = "" mdf_string = ""
# We dont want to export Sample description # We dont want to export Sample description
default_excluded_field += ["description", "image", "molfile"] default_excluded_field += ["description", "image", "molfile"]
...@@ -16,13 +16,13 @@ class Reporter::SdfExport ...@@ -16,13 +16,13 @@ class Reporter::SdfExport
x.slice!("molecule.") x.slice!("molecule.")
x x
} }
included_field = @@sample_list.first.attribute_names included_field = @sample_list.first.attribute_names
# Exclude field # Exclude field
included_field.delete_if { |x| default_excluded_field.include?(x) } included_field.delete_if { |x| default_excluded_field.include?(x) }
included_field = included_field.reject { |x| x.empty? } included_field = included_field.reject { |x| x.empty? }
included_field = included_field.uniq - removed_field included_field = included_field.uniq - removed_field
@@sample_list.compact.each_with_index do |sample, index| @sample_list.compact.each_with_index do |sample, index|
sample_mdf = sample.molfile.split(/#|END/).first + "END\n" sample_mdf = sample.molfile.split(/#|END/).first + "END\n"
(default_included_field - removed_field).each do |field| (default_included_field - removed_field).each do |field|
......
...@@ -56,13 +56,27 @@ describe Chemotion::ReportAPI do ...@@ -56,13 +56,27 @@ describe Chemotion::ReportAPI do
before do before do
params = { params = {
type: 'sample',
exportType: 1, exportType: 1,
checkedIds: [sample_1.id], uiState: {
uncheckedIds: [], sample: {
checkedAll: false, checkedIds: [sample_1.id],
currentCollection: c.id, uncheckedIds: [],
removedColumns: %w( checkedAll: false,
},
reaction: {
checkedIds: [],
uncheckedIds: [],
checkedAll: false,
},
wellplate: {
checkedIds: [],
uncheckedIds: [],
checkedAll: false,
},
currentCollection: c.id,
isSync: false
},
columns: %w(
target_amount_value target_amount_unit target_amount_value target_amount_unit
created_at updated_at molfile created_at updated_at molfile
) )
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment