1
0
Fork 0
discourse-custom-wizard-unl.../controllers/custom_wizard/admin/manager.rb

125 Zeilen
2,7 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
class CustomWizard::AdminManagerController < CustomWizard::AdminController
skip_before_action :check_xhr, only: [:export]
before_action :get_wizard_ids, except: [:import]
def export
templates = []
2021-03-11 07:30:15 +01:00
@wizard_ids.each do |wizard_id|
if template = CustomWizard::Template.find(wizard_id)
templates.push(template)
end
end
2021-03-11 07:30:15 +01:00
if templates.empty?
return render_error(I18n.t('wizard.export.error.invalid_wizards'))
end
2021-03-11 07:30:15 +01:00
basename = SiteSetting.title.parameterize || 'discourse'
2021-03-11 07:30:15 +01:00
time = Time.now.to_i
filename = "#{basename}-wizards-#{time}.json"
send_data templates.to_json,
type: "application/json",
disposition: 'attachment',
filename: filename
end
def import
file = File.read(params['file'].tempfile)
2021-03-11 07:30:15 +01:00
if file.nil?
return render_error(I18n.t('wizard.export.error.no_file'))
end
file_size = file.size
max_file_size = 512 * 1024
2021-03-11 07:30:15 +01:00
if max_file_size < file_size
return render_error(I18n.t('wizard.import.error.file_large'))
end
2021-03-11 07:30:15 +01:00
begin
template_json = JSON.parse file
rescue JSON::ParserError
return render_error(I18n.t('wizard.import.error.invalid_json'))
end
2021-03-11 07:30:15 +01:00
imported = []
failures = []
2021-03-11 07:30:15 +01:00
template_json.each do |json|
template = CustomWizard::Template.new(json)
template.save(skip_jobs: true, create: true)
2021-03-11 07:30:15 +01:00
if template.errors.any?
failures.push(
id: json['id'],
messages: template.errors.full_messages.join(', ')
)
else
imported.push(
id: json['id'],
name: json['name']
)
end
end
render json: success_json.merge(
imported: imported,
failures: failures
)
end
2021-03-11 07:30:15 +01:00
def destroy
destroyed = []
failures = []
2021-03-11 07:30:15 +01:00
@wizard_ids.each do |wizard_id|
template = CustomWizard::Template.find(wizard_id)
2021-03-11 07:30:15 +01:00
if template && CustomWizard::Template.remove(wizard_id)
destroyed.push(
id: wizard_id,
name: template['name']
)
else
failures.push(
id: wizard_id,
messages: I18n.t("wizard.destroy.error.#{template ? 'default' : 'no_template'}")
)
end
end
2021-03-11 07:30:15 +01:00
render json: success_json.merge(
destroyed: destroyed,
failures: failures
)
end
2021-03-11 07:30:15 +01:00
private
2021-03-11 07:30:15 +01:00
def get_wizard_ids
if params['wizard_ids'].blank?
return render_error(I18n.t('wizard.export.error.select_one'))
end
2021-03-11 07:30:15 +01:00
wizard_ids = []
2021-03-11 07:30:15 +01:00
params['wizard_ids'].each do |wizard_id|
begin
wizard_ids.push(wizard_id.underscore)
rescue
#
end
end
2021-03-11 07:30:15 +01:00
if wizard_ids.empty?
return render_error(I18n.t('wizard.export.error.invalid_wizards'))
end
2021-03-11 07:30:15 +01:00
@wizard_ids = wizard_ids
end
end