diff options
| author | Zog | 2018-03-26 10:35:19 +0200 |
|---|---|---|
| committer | Zog | 2018-03-26 10:47:25 +0200 |
| commit | 3d370c032c3fa06fd03643263e29b6eded5fe6c1 (patch) | |
| tree | 533ded9a19bd9c5fe703d5c20e195d419420abab | |
| parent | c2a288a9d9db99ce06eb963246fc5e760cfb831d (diff) | |
| download | chouette-core-3d370c032c3fa06fd03643263e29b6eded5fe6c1.tar.bz2 | |
Refs #6218; Link exports to referentials
| -rw-r--r-- | app/models/export/base.rb | 4 | ||||
| -rw-r--r-- | app/models/export/simple_exporter/base.rb | 94 | ||||
| -rw-r--r-- | app/views/exports/_form.html.slim | 1 | ||||
| -rw-r--r-- | app/workers/workgroup_export_worker.rb | 1 | ||||
| -rw-r--r-- | config/locales/exports.en.yml | 1 | ||||
| -rw-r--r-- | config/locales/exports.fr.yml | 1 |
6 files changed, 101 insertions, 1 deletions
diff --git a/app/models/export/base.rb b/app/models/export/base.rb index 6a1eb791d..6cf4c6b02 100644 --- a/app/models/export/base.rb +++ b/app/models/export/base.rb @@ -1,7 +1,9 @@ class Export::Base < ActiveRecord::Base self.table_name = "exports" - validates :type, presence: true + belongs_to :referential + + validates :type, :referential_id, presence: true def self.messages_class_name "Export::Message" diff --git a/app/models/export/simple_exporter/base.rb b/app/models/export/simple_exporter/base.rb new file mode 100644 index 000000000..4e6e8eba4 --- /dev/null +++ b/app/models/export/simple_exporter/base.rb @@ -0,0 +1,94 @@ +class Export::SimpleExporter::Base < Export::Base + after_commit :call_exporter_async, on: :create + + def self.user_visible? + false + end + + def self.inherited child + super child + child.options = @options + child.instance_eval do + def self.user_visible? + true + end + end + end + + def call_exporter_async + SimpleExportWorker.perform_async(id) + end + + def simple_exporter_configuration_name + + end + + def exporter + @exporter ||= begin + if options[:_exporter_id] + exporter = SimpleJsonExporter.find options[:_exporter_id] + else + exporter = SimpleJsonExporter.create configuration_name: simple_exporter_configuration_name + options[:_exporter_id] = exporter.id + end + exporter + end + end + + def configure_exporter config + end + + def call_exporter + tmp = Tempfile.new [simple_exporter_configuration_name.to_s, ".json"] + referential.switch + exporter.configure do |config| + configure_exporter config + end + exporter.filepath = tmp.path + exporter.export + set_status_from_exporter + convert_exporter_journal_to_messages + self.file = tmp + self.save! + end + + def set_status_from_exporter + if exporter.status.to_s == "error" + self.status = :failed + elsif exporter.status.to_s == "success" + self.status = :successful + else + self.status = :warning + end + end + + def convert_exporter_journal_to_messages + self.messages.destroy_all + exporter.journal.each do |journal_item| + journal_item.symbolize_keys! + vals = {} + + if journal_item[:kind].to_s == "warning" + vals[:criticity] = :warning + elsif journal_item[:kind].to_s == "error" + vals[:criticity] = :error + else + vals[:criticity] = :info + if journal_item[:event].to_s == "success" + vals[:message_key] = :success + end + end + vals[:resource_attributes] = journal_item[:row] + + if journal_item[:message].present? + vals[:message_key] = :full_text + vals[:message_attributes] = { + text: journal_item[:message] + } + end + vals[:message_attributes] ||= {} + vals[:message_attributes][:line] = journal_item[:line] + self.messages.build vals + end + end +end diff --git a/app/views/exports/_form.html.slim b/app/views/exports/_form.html.slim index 7817fdf1a..44b4e447c 100644 --- a/app/views/exports/_form.html.slim +++ b/app/views/exports/_form.html.slim @@ -5,6 +5,7 @@ = form.input :name .col-lg-12 = form.input :type, as: :select, collection: Export::Base.user_visible_descendants, label_method: :human_name + = form.input :referential_id, as: :select, collection: workbench.referentials, label_method: :name - Export::Base.user_visible_descendants.each do |child| .slave data-master="[name='export[type]']" data-value=child.name diff --git a/app/workers/workgroup_export_worker.rb b/app/workers/workgroup_export_worker.rb index 29493cea6..a354573e8 100644 --- a/app/workers/workgroup_export_worker.rb +++ b/app/workers/workgroup_export_worker.rb @@ -27,6 +27,7 @@ class WorkgroupExportWorker netex_export.workbench = workbench_export.workbench netex_export.creator = workbench_export.creator netex_export.export_type = :line + netex_export.referential = workbench_export.referential netex_export.duration = workbench_export.duration netex_export.line_code = line.objectid netex_export.parent = workbench_export diff --git a/config/locales/exports.en.yml b/config/locales/exports.en.yml index 628065ab0..de34e797c 100644 --- a/config/locales/exports.en.yml +++ b/config/locales/exports.en.yml @@ -88,6 +88,7 @@ en: file: "Output" files: "Outputs" parent: Parent + referential_id: Referential export: <<: *attrs base: diff --git a/config/locales/exports.fr.yml b/config/locales/exports.fr.yml index a223b85f8..62aae70cd 100644 --- a/config/locales/exports.fr.yml +++ b/config/locales/exports.fr.yml @@ -88,6 +88,7 @@ fr: file: "Résultat" files: "Résultats" parent: Parent + referential_id: Jeu de données export: <<: *attrs base: |
