2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2018-04-26 08:55:53 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
require 'csv'
|
|
|
|
|
|
|
|
module CanCsvImport
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
# methods defined here are going to extend the class, not the instance of it
|
|
|
|
class_methods do
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
result = Model.csv_import(
|
|
|
|
string: csv_string,
|
|
|
|
parse_params: {
|
|
|
|
col_sep: ',',
|
|
|
|
},
|
|
|
|
try: true,
|
2018-06-12 20:58:59 +00:00
|
|
|
delete: false,
|
2018-02-20 04:29:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = Model.csv_import(
|
|
|
|
file: '/file/location/of/file.csv',
|
|
|
|
parse_params: {
|
|
|
|
col_sep: ',',
|
|
|
|
},
|
|
|
|
try: true,
|
2018-06-12 20:58:59 +00:00
|
|
|
delete: false,
|
2018-02-20 04:29:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = TextModule.csv_import(
|
|
|
|
file: '/Users/me/Downloads/Textbausteine_final.csv',
|
|
|
|
parse_params: {
|
|
|
|
col_sep: ',',
|
|
|
|
},
|
|
|
|
try: false,
|
2018-06-12 20:58:59 +00:00
|
|
|
delete: false,
|
2018-02-20 04:29:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
records: [record1, ...]
|
|
|
|
try: true, # true|false
|
|
|
|
success: true, # true|false
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def csv_import(data)
|
2019-10-25 06:42:20 +00:00
|
|
|
try = data[:try].to_s == 'true'
|
|
|
|
delete = data[:delete].to_s == 'true'
|
|
|
|
|
|
|
|
begin
|
|
|
|
data[:string] = File.read(data[:file]) if data[:file].present?
|
|
|
|
rescue Errno::ENOENT
|
|
|
|
raise Exceptions::UnprocessableEntity, "No such file '#{data[:file]}'"
|
|
|
|
rescue => e
|
|
|
|
raise Exceptions::UnprocessableEntity, "Unable to read file '#{data[:file]}': #{e.inspect}"
|
2018-06-12 20:58:59 +00:00
|
|
|
end
|
|
|
|
|
2021-07-06 07:52:22 +00:00
|
|
|
header, *rows = ::CSV.parse(data[:string], **data[:parse_params])
|
2020-09-30 09:07:01 +00:00
|
|
|
|
|
|
|
header&.each do |column|
|
|
|
|
column.try(:strip!)
|
|
|
|
column.try(:downcase!)
|
|
|
|
end
|
2019-10-25 06:42:20 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
raise "Delete is not possible for #{self}." if delete && !csv_delete_possible
|
|
|
|
raise "Unable to parse empty file/string for #{self}." if data[:string].blank?
|
|
|
|
raise "Unable to parse file/string without header for #{self}." if header.blank?
|
|
|
|
raise "No records found in file/string for #{self}." if rows.first.blank?
|
|
|
|
raise "No lookup column like #{lookup_keys.map(&:to_s).join(',')} for #{self} found." if (header & lookup_keys.map(&:to_s)).none?
|
|
|
|
rescue => e
|
|
|
|
return {
|
2018-12-19 17:31:51 +00:00
|
|
|
try: try,
|
2018-06-12 20:58:59 +00:00
|
|
|
result: 'failed',
|
2019-10-25 06:42:20 +00:00
|
|
|
errors: [e.message],
|
2018-06-12 20:58:59 +00:00
|
|
|
}
|
2018-11-06 05:42:52 +00:00
|
|
|
end
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
# get payload based on csv
|
|
|
|
payload = []
|
|
|
|
rows.each do |row|
|
2019-10-25 06:42:20 +00:00
|
|
|
if row.first(2).any?(&:present?)
|
|
|
|
payload.push(
|
|
|
|
header.zip(row).to_h
|
|
|
|
.compact.transform_values(&:strip)
|
|
|
|
.except(nil).transform_keys(&:to_sym)
|
|
|
|
.except(*csv_attributes_ignored)
|
|
|
|
.merge(data[:fixed_params] || {})
|
|
|
|
)
|
|
|
|
else
|
|
|
|
header.zip(row).to_h
|
|
|
|
.compact.transform_values(&:strip)
|
|
|
|
.except(nil).transform_keys(&:to_sym)
|
|
|
|
.each { |col, val| payload.last[col] = [*payload.last[col], val] }
|
2018-02-20 04:29:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
stats = {
|
|
|
|
created: 0,
|
|
|
|
updated: 0,
|
2019-10-25 06:42:20 +00:00
|
|
|
deleted: (count if delete),
|
|
|
|
}.compact
|
2018-06-12 20:58:59 +00:00
|
|
|
|
|
|
|
# delete
|
2019-10-25 06:42:20 +00:00
|
|
|
destroy_all if delete && !try
|
2018-06-12 20:58:59 +00:00
|
|
|
|
|
|
|
# create or update records
|
|
|
|
records = []
|
2019-10-25 06:42:20 +00:00
|
|
|
errors = []
|
|
|
|
|
|
|
|
transaction do
|
|
|
|
payload.each.with_index do |attributes, i|
|
|
|
|
record = (lookup_keys & attributes.keys).lazy.map do |lookup_key|
|
|
|
|
params = attributes.slice(lookup_key)
|
|
|
|
params.transform_values!(&:downcase) if lookup_key.in?(%i[email login])
|
2021-07-06 07:52:22 +00:00
|
|
|
lookup(**params)
|
2019-10-25 06:42:20 +00:00
|
|
|
end.detect(&:present?)
|
|
|
|
|
|
|
|
if record&.in?(records)
|
|
|
|
errors.push "Line #{i.next}: duplicate record found."
|
2019-10-21 10:44:52 +00:00
|
|
|
next
|
|
|
|
end
|
2018-02-20 04:29:30 +00:00
|
|
|
|
2019-10-25 06:42:20 +00:00
|
|
|
if !record && attributes[:id].present?
|
|
|
|
errors.push "Line #{i.next}: unknown #{self} with id '#{attributes[:id]}'."
|
2019-10-21 10:44:52 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2019-10-25 06:42:20 +00:00
|
|
|
if record&.id&.in?(csv_object_ids_ignored)
|
|
|
|
errors.push "Line #{i.next}: unable to update #{self} with id '#{attributes[:id]}'."
|
2019-10-21 10:44:52 +00:00
|
|
|
next
|
|
|
|
end
|
2018-02-20 04:29:30 +00:00
|
|
|
|
2019-10-21 10:44:52 +00:00
|
|
|
begin
|
|
|
|
clean_params = association_name_to_id_convert(attributes)
|
|
|
|
rescue => e
|
2019-10-25 06:42:20 +00:00
|
|
|
errors.push "Line #{i.next}: #{e.message}"
|
2019-10-21 10:44:52 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# create object
|
2019-10-25 06:42:20 +00:00
|
|
|
Transaction.execute(disable_notification: true, reset_user_id: true, bulk: true) do
|
|
|
|
UserInfo.current_user_id = clean_params[:updated_by_id] || clean_params[:created_by_id]
|
2018-02-20 04:29:30 +00:00
|
|
|
|
2019-10-25 06:42:20 +00:00
|
|
|
if !record || delete == true
|
|
|
|
stats[:created] += 1
|
|
|
|
begin
|
|
|
|
csv_verify_attributes(clean_params)
|
2018-11-06 05:42:52 +00:00
|
|
|
|
2019-10-25 06:42:20 +00:00
|
|
|
record = new(param_cleanup(clean_params).reverse_merge(created_by_id: 1, updated_by_id: 1))
|
|
|
|
record.associations_from_param(attributes)
|
2018-11-06 05:42:52 +00:00
|
|
|
record.save!
|
2019-10-25 06:42:20 +00:00
|
|
|
rescue => e
|
|
|
|
errors.push "Line #{i.next}: Unable to create record - #{e.message}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
else
|
|
|
|
stats[:updated] += 1
|
|
|
|
|
|
|
|
begin
|
|
|
|
csv_verify_attributes(clean_params)
|
|
|
|
clean_params = param_cleanup(clean_params).reverse_merge(updated_by_id: 1)
|
|
|
|
|
|
|
|
record.with_lock do
|
|
|
|
record.associations_from_param(attributes)
|
|
|
|
record.assign_attributes(clean_params)
|
|
|
|
record.save! if record.changed?
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
errors.push "Line #{i.next}: Unable to update record - #{e.message}"
|
|
|
|
next
|
2018-02-20 04:29:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-21 10:44:52 +00:00
|
|
|
records.push record if record
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
raise ActiveRecord::Rollback if try || errors.any?
|
2018-02-20 04:29:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
stats: stats,
|
2018-02-20 04:29:30 +00:00
|
|
|
records: records,
|
2018-12-19 17:31:51 +00:00
|
|
|
errors: errors,
|
|
|
|
try: try,
|
2019-10-21 10:44:52 +00:00
|
|
|
result: errors.empty? ? 'success' : 'failed',
|
2018-02-20 04:29:30 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
verify if attributes are valid, will raise an ArgumentError with "unknown attribute '#{key}' for #{new.class}."
|
|
|
|
|
|
|
|
Model.csv_verify_attributes({'attribute': 'some value'})
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def csv_verify_attributes(clean_params)
|
|
|
|
all_clean_attributes = {}
|
|
|
|
new.attributes.each_key do |attribute|
|
|
|
|
all_clean_attributes[attribute.to_sym] = true
|
|
|
|
end
|
|
|
|
reflect_on_all_associations.map do |assoc|
|
|
|
|
all_clean_attributes[assoc.name.to_sym] = true
|
|
|
|
ref = if assoc.name.to_s.end_with?('_id')
|
|
|
|
"#{assoc.name}_id"
|
|
|
|
else
|
|
|
|
"#{assoc.name.to_s.chop}_ids"
|
|
|
|
end
|
|
|
|
all_clean_attributes[ref.to_sym] = true
|
|
|
|
end
|
|
|
|
clean_params.each_key do |key|
|
|
|
|
next if all_clean_attributes.key?(key.to_sym)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
raise ArgumentError, "unknown attribute '#{key}' for #{new.class}."
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
csv_string = Model.csv_example(
|
|
|
|
col_sep: ',',
|
|
|
|
)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
csv_string
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def csv_example(params = {})
|
|
|
|
header = []
|
|
|
|
records = where.not(id: csv_object_ids_ignored).offset(1).limit(23).to_a
|
|
|
|
if records.count < 20
|
|
|
|
record_ids = records.pluck(:id).concat(csv_object_ids_ignored)
|
|
|
|
local_records = where.not(id: record_ids).limit(20 - records.count)
|
2020-09-30 09:07:01 +00:00
|
|
|
records.concat(local_records)
|
2018-02-20 04:29:30 +00:00
|
|
|
end
|
|
|
|
records_attributes_with_association_names = []
|
|
|
|
records.each do |record|
|
|
|
|
record_attributes_with_association_names = record.attributes_with_association_names
|
|
|
|
records_attributes_with_association_names.push record_attributes_with_association_names
|
|
|
|
record_attributes_with_association_names.each do |key, value|
|
2020-10-22 13:57:01 +00:00
|
|
|
next if value.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
|
|
|
next if value.instance_of?(Hash)
|
2019-10-25 06:42:20 +00:00
|
|
|
next if csv_attributes_ignored&.include?(key.to_sym)
|
2020-05-25 07:05:17 +00:00
|
|
|
next if key.end_with?('_id')
|
|
|
|
next if key.end_with?('_ids')
|
2018-02-20 04:29:30 +00:00
|
|
|
next if key == 'created_by'
|
|
|
|
next if key == 'updated_by'
|
|
|
|
next if key == 'created_at'
|
|
|
|
next if key == 'updated_at'
|
|
|
|
next if header.include?(key)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
header.push key
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rows = []
|
|
|
|
records_attributes_with_association_names.each do |record|
|
|
|
|
row = []
|
|
|
|
rows_to_add = []
|
|
|
|
position = -1
|
|
|
|
header.each do |key|
|
|
|
|
position += 1
|
2020-10-22 13:57:01 +00:00
|
|
|
if record[key].instance_of?(ActiveSupport::TimeWithZone)
|
2018-02-20 04:29:30 +00:00
|
|
|
row.push record[key].iso8601
|
|
|
|
next
|
|
|
|
end
|
2020-10-22 13:57:01 +00:00
|
|
|
if record[key].instance_of?(Array)
|
2018-02-20 04:29:30 +00:00
|
|
|
entry_count = -2
|
|
|
|
record[key].each do |entry|
|
|
|
|
entry_count += 1
|
|
|
|
next if entry_count == -1
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
if !rows_to_add[entry_count]
|
|
|
|
rows_to_add[entry_count] = Array.new(header.count + 1) { '' }
|
|
|
|
end
|
|
|
|
rows_to_add[entry_count][position] = entry
|
|
|
|
end
|
|
|
|
record[key] = record[key][0]
|
|
|
|
end
|
|
|
|
row.push record[key]
|
|
|
|
end
|
|
|
|
rows.push row
|
2018-05-04 14:05:10 +00:00
|
|
|
next if rows_to_add.count.zero?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
rows_to_add.each do |item|
|
|
|
|
rows.push item
|
|
|
|
end
|
|
|
|
rows_to_add = []
|
|
|
|
end
|
2021-07-06 07:52:22 +00:00
|
|
|
::CSV.generate(**params) do |csv|
|
2018-02-20 04:29:30 +00:00
|
|
|
csv << header
|
|
|
|
rows.each do |row|
|
|
|
|
csv << row
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
serve method to ignore model based on id
|
2018-02-20 04:29:30 +00:00
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
include CanCsvImport
|
|
|
|
csv_object_ids_ignored(1, 2, 3)
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def csv_object_ids_ignored(*object_ids)
|
2019-10-25 06:42:20 +00:00
|
|
|
return @csv_object_ids_ignored || [] if object_ids.empty?
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
@csv_object_ids_ignored = object_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
serve method to ignore model attributes
|
2018-02-20 04:29:30 +00:00
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
include CanCsvImport
|
|
|
|
csv_attributes_ignored :password,
|
|
|
|
:image_source,
|
|
|
|
:login_failed,
|
|
|
|
:source,
|
|
|
|
:image_source,
|
|
|
|
:image,
|
|
|
|
:authorizations,
|
|
|
|
:organizations
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def csv_attributes_ignored(*attributes)
|
2019-10-25 06:42:20 +00:00
|
|
|
return @csv_attributes_ignored || [] if attributes.empty?
|
|
|
|
|
2018-02-20 04:29:30 +00:00
|
|
|
@csv_attributes_ignored = attributes
|
|
|
|
end
|
|
|
|
|
2018-06-12 20:58:59 +00:00
|
|
|
=begin
|
|
|
|
|
2019-07-31 08:23:48 +00:00
|
|
|
serve method to define if delete option is possible or not
|
2018-06-12 20:58:59 +00:00
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
include CanCsvImport
|
|
|
|
csv_delete_possible true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2019-10-25 06:42:20 +00:00
|
|
|
def csv_delete_possible(*value)
|
|
|
|
return @csv_delete_possible if value.empty?
|
|
|
|
|
|
|
|
@csv_delete_possible = value.first
|
2018-06-12 20:58:59 +00:00
|
|
|
end
|
2018-02-20 04:29:30 +00:00
|
|
|
end
|
|
|
|
end
|