mirror of
https://github.com/discourse/discourse.git
synced 2025-02-24 12:22:11 +08:00
DEV: we don't need a collection of entities (#22745)
This commit is contained in:
parent
5a99243629
commit
e3c6d3c1cb
@ -63,18 +63,15 @@ module Jobs
|
|||||||
@extra = HashWithIndifferentAccess.new(args[:args]) if args[:args]
|
@extra = HashWithIndifferentAccess.new(args[:args]) if args[:args]
|
||||||
@current_user = User.find_by(id: args[:user_id])
|
@current_user = User.find_by(id: args[:user_id])
|
||||||
|
|
||||||
entities = [{ name: @entity }]
|
entity = { name: @entity }
|
||||||
|
entity[:method] = :"#{entity[:name]}_export"
|
||||||
|
raise Discourse::InvalidParameters.new(:entity) unless respond_to?(entity[:method])
|
||||||
|
|
||||||
entities.each do |entity|
|
@timestamp ||= Time.now.strftime("%y%m%d-%H%M%S")
|
||||||
entity[:method] = :"#{entity[:name]}_export"
|
entity[:filename] = if entity[:name] == "report" && @extra[:name].present?
|
||||||
raise Discourse::InvalidParameters.new(:entity) unless respond_to?(entity[:method])
|
"#{@extra[:name].dasherize}-#{@timestamp}"
|
||||||
|
else
|
||||||
@timestamp ||= Time.now.strftime("%y%m%d-%H%M%S")
|
"#{entity[:name].dasherize}-#{@timestamp}"
|
||||||
entity[:filename] = if entity[:name] == "report" && @extra[:name].present?
|
|
||||||
"#{@extra[:name].dasherize}-#{@timestamp}"
|
|
||||||
else
|
|
||||||
"#{entity[:name].dasherize}-#{@timestamp}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
export_title =
|
export_title =
|
||||||
@ -84,11 +81,11 @@ module Jobs
|
|||||||
@entity.gsub("_", " ").titleize
|
@entity.gsub("_", " ").titleize
|
||||||
end
|
end
|
||||||
|
|
||||||
filename = entities[0][:filename] # use first entity as a name for this export
|
filename = entity[:filename]
|
||||||
user_export = UserExport.create(file_name: filename, user_id: @current_user.id)
|
user_export = UserExport.create(file_name: filename, user_id: @current_user.id)
|
||||||
filename = "#{filename}-#{user_export.id}"
|
filename = "#{filename}-#{user_export.id}"
|
||||||
|
|
||||||
zip_filename = write_to_csv_and_zip(filename, entities)
|
zip_filename = write_to_csv_and_zip(filename, entity)
|
||||||
|
|
||||||
# create upload
|
# create upload
|
||||||
upload = nil
|
upload = nil
|
||||||
@ -461,15 +458,13 @@ module Jobs
|
|||||||
post
|
post
|
||||||
end
|
end
|
||||||
|
|
||||||
def write_to_csv_and_zip(filename, entities)
|
def write_to_csv_and_zip(filename, entity)
|
||||||
dirname = "#{UserExport.base_directory}/#{filename}"
|
dirname = "#{UserExport.base_directory}/#{filename}"
|
||||||
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)
|
FileUtils.mkdir_p(dirname) unless Dir.exist?(dirname)
|
||||||
begin
|
begin
|
||||||
entities.each do |entity|
|
CSV.open("#{dirname}/#{entity[:filename]}.csv", "w") do |csv|
|
||||||
CSV.open("#{dirname}/#{entity[:filename]}.csv", "w") do |csv|
|
csv << get_header(entity[:name]) if entity[:name] != "report"
|
||||||
csv << get_header(entity[:name]) if entity[:name] != "report"
|
public_send(entity[:method]) { |d| csv << d }
|
||||||
public_send(entity[:method]) { |d| csv << d }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Compression::Zip.new.compress(UserExport.base_directory, filename)
|
Compression::Zip.new.compress(UserExport.base_directory, filename)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user