Browse Source

Auto-detection of field wrapper type when importing CSV file (#39511).

Patch by Go MAEDA.


git-svn-id: https://svn.redmine.org/redmine/trunk@22440 e93f8b46-1217-0410-a6f0-8f06a7374b81
pull/147/head
Go MAEDA 7 months ago
parent
commit
fa7c481028
2 changed files with 17 additions and 1 deletions
  1. 2
    1
      app/models/import.rb
  2. 15
    0
      test/unit/issue_import_test.rb

+ 2
- 1
app/models/import.rb View File



def set_default_settings(options={}) def set_default_settings(options={})
separator = lu(user, :general_csv_separator) separator = lu(user, :general_csv_separator)
wrapper = '"'
encoding = lu(user, :general_csv_encoding) encoding = lu(user, :general_csv_encoding)
if file_exists? if file_exists?
begin begin
content = File.read(filepath, 256) content = File.read(filepath, 256)


separator = [',', ';'].max_by {|sep| content.count(sep)} separator = [',', ';'].max_by {|sep| content.count(sep)}
wrapper = ['"', "'"].max_by {|quote_char| content.count(quote_char)}


guessed_encoding = Redmine::CodesetUtil.guess_encoding(content) guessed_encoding = Redmine::CodesetUtil.guess_encoding(content)
encoding = encoding =
rescue => e rescue => e
end end
end end
wrapper = '"'


date_format = lu(user, "date.formats.default", :default => "foo") date_format = lu(user, "date.formats.default", :default => "foo")
date_format = DATE_FORMATS.first unless DATE_FORMATS.include?(date_format) date_format = DATE_FORMATS.first unless DATE_FORMATS.include?(date_format)

+ 15
- 0
test/unit/issue_import_test.rb View File

assert_equal 'CP932', guessed_encoding assert_equal 'CP932', guessed_encoding
end end
end end

def test_set_default_settings_should_detect_field_wrapper
to_test = {
'import_issues.csv' => '"',
'import_issues_single_quotation.csv' => "'",
# Use '"' as a wrapper for CSV file with no wrappers
'import_dates.csv' => '"',
}

to_test.each do |file, expected|
import = generate_import(file)
import.set_default_settings
assert_equal expected, import.settings['wrapper']
end
end
end end

Loading…
Cancel
Save