mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-19 05:09:38 +02:00
Escape quotations in CSV imports properly (#1929)
* Parse quotes in imports * Update invalid CSV for test
This commit is contained in:
parent
7c66f16750
commit
4d0df9b950
3 changed files with 15 additions and 12 deletions
|
@ -29,10 +29,8 @@ class Import::UploadsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def csv_valid?(str)
|
def csv_valid?(str)
|
||||||
require "csv"
|
|
||||||
|
|
||||||
begin
|
begin
|
||||||
csv = CSV.parse(str || "", headers: true, col_sep: upload_params[:col_sep])
|
csv = Import.parse_csv_str(str, col_sep: upload_params[:col_sep])
|
||||||
return false if csv.headers.empty?
|
return false if csv.headers.empty?
|
||||||
return false if csv.count == 0
|
return false if csv.count == 0
|
||||||
true
|
true
|
||||||
|
|
|
@ -34,6 +34,18 @@ class Import < ApplicationRecord
|
||||||
has_many :accounts, dependent: :destroy
|
has_many :accounts, dependent: :destroy
|
||||||
has_many :entries, dependent: :destroy, class_name: "Account::Entry"
|
has_many :entries, dependent: :destroy, class_name: "Account::Entry"
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def parse_csv_str(csv_str, col_sep: ",")
|
||||||
|
CSV.parse(
|
||||||
|
(csv_str || "").strip,
|
||||||
|
headers: true,
|
||||||
|
col_sep: col_sep,
|
||||||
|
converters: [ ->(str) { str&.strip } ],
|
||||||
|
liberal_parsing: true
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def publish_later
|
def publish_later
|
||||||
raise "Import is not publishable" unless publishable?
|
raise "Import is not publishable" unless publishable?
|
||||||
|
|
||||||
|
@ -178,12 +190,7 @@ class Import < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def parsed_csv
|
def parsed_csv
|
||||||
@parsed_csv ||= CSV.parse(
|
@parsed_csv ||= self.class.parse_csv_str(raw_file_str, col_sep: col_sep)
|
||||||
(raw_file_str || "").strip,
|
|
||||||
headers: true,
|
|
||||||
col_sep: col_sep,
|
|
||||||
converters: [ ->(str) { str&.strip } ]
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def sanitize_number(value)
|
def sanitize_number(value)
|
||||||
|
|
4
test/fixtures/files/imports/invalid.csv
vendored
4
test/fixtures/files/imports/invalid.csv
vendored
|
@ -1,3 +1 @@
|
||||||
name,age
|
name,description,amount,currency
|
||||||
"John Doe,23
|
|
||||||
"Jane Doe",25
|
|
Can't render this file because it contains an unexpected character in line 3 and column 1.
|
Loading…
Add table
Add a link
Reference in a new issue