diff options
| author | Zog | 2018-03-08 15:07:41 +0100 | 
|---|---|---|
| committer | Zog | 2018-03-12 12:00:15 +0100 | 
| commit | 132db2e97fd6e28105e472ee562062b7ebc07b39 (patch) | |
| tree | 6a1e332e98abd011d0913044e75727ea11a4c3c6 | |
| parent | a4a075f5eec935dd6a0dbf0a861b6ada13b1dcbc (diff) | |
| download | chouette-core-132db2e97fd6e28105e472ee562062b7ebc07b39.tar.bz2 | |
Refs #6133; Sample SimpeImporter Implementation
Still needs to be made asynchronous
| -rw-r--r-- | app/helpers/exports_helper.rb | 22 | ||||
| -rw-r--r-- | app/models/export/base.rb | 14 | ||||
| -rw-r--r-- | app/models/export/referential_companies.rb | 65 | ||||
| -rw-r--r-- | app/models/simple_interface.rb | 2 | ||||
| -rw-r--r-- | app/uploaders/import_uploader.rb | 2 | ||||
| -rw-r--r-- | app/views/exports/_form.html.slim | 2 | ||||
| -rw-r--r-- | app/views/exports/show.html.slim | 4 | ||||
| -rw-r--r-- | app/views/shared/iev_interfaces/_messages.html.slim | 13 | ||||
| -rw-r--r-- | config/initializers/exporters.rb | 6 | ||||
| -rw-r--r-- | config/locales/export_messages.en.yml | 3 | ||||
| -rw-r--r-- | config/locales/export_messages.fr.yml | 3 | ||||
| -rw-r--r-- | config/locales/exports.fr.yml | 1 | 
12 files changed, 124 insertions, 13 deletions
| diff --git a/app/helpers/exports_helper.rb b/app/helpers/exports_helper.rb index 8ac494cfc..3a32e4761 100644 --- a/app/helpers/exports_helper.rb +++ b/app/helpers/exports_helper.rb @@ -1,6 +1,20 @@  # -*- coding: utf-8 -*-  module ExportsHelper -   +  def export_option_input form, export, attr, option_def +    opts = { required: option_def[:required], input_html: {value: @export.try(attr) || option_def[:default_value]}, as: option_def[:type]} +    opts[:collection] = option_def[:collection] if option_def.has_key?(:collection) +    opts[:collection] = @export.instance_exec(&option_def[:collection]) if option_def[:collection].is_a?(Proc) +    form.input attr, opts +  end + +  def export_message_content message +    if message.message_key == "full_text" +      message.message_attributes["text"] +    else +      t([message.class.name.underscore.gsub('/', '_').pluralize, message.message_key].join('.'), message.message_attributes.symbolize_keys) +    end +  end +    def fields_for_export_task_format(form)      begin        render :partial => export_partial_name(form), :locals => { :form => form } @@ -8,7 +22,7 @@ module ExportsHelper        ""      end    end -   +    def export_partial_name(form)      "fields_#{form.object.format.underscore}_export"    end @@ -22,7 +36,7 @@ module ExportsHelper        end.join.html_safe      end    end -   +    def compliance_icon( export_task)      return nil unless export_task.compliance_check_task      export_task.compliance_check_task.tap do |cct| @@ -33,5 +47,5 @@ module ExportsHelper        end      end    end -   +  end diff --git a/app/models/export/base.rb b/app/models/export/base.rb index 350edd215..ae34be0d0 100644 --- a/app/models/export/base.rb +++ b/app/models/export/base.rb @@ -21,7 +21,7 @@ class Export::Base < ActiveRecord::Base        Dir.chdir path do          Dir['**/*.rb'].each do |src|            next if src =~ /^base/ -          klass_name = "Export::#{src[0..-4].classify}" +          klass_name = "Export::#{src[0..-4].camelize}"            Rails.logger.info "Loading #{klass_name}"            begin              klass_name.constantize @@ -44,7 +44,7 @@ class Export::Base < ActiveRecord::Base    end    def self.options -    @options +    @options ||= {}    end    include IevInterfaces::Task @@ -61,6 +61,16 @@ class Export::Base < ActiveRecord::Base      true    end +  def display_option_value option_name, context +    option = self.class.options[option_name.to_sym] +    val = self.options[option_name.to_s] +    if option[:display] +      context.instance_exec(val, &option[:display]) +    else +      val +    end +  end +    private    def initialize_fields diff --git a/app/models/export/referential_companies.rb b/app/models/export/referential_companies.rb new file mode 100644 index 000000000..7a796fb48 --- /dev/null +++ b/app/models/export/referential_companies.rb @@ -0,0 +1,65 @@ +class Export::ReferentialCompanies < Export::Base +  option :referential_id, +    type: :select, +    collection: ->(){workbench.referentials.all}, +    required: true, +    display: ->(val){r = Referential.find(val); link_to(r.name, [r])} + +  after_commit :call_exporter, on: :create + +  def referential +    Referential.find referential_id +  end + +  def call_exporter +    tmp = Tempfile.new ["referential_companies", ".csv"] +    exporter =  SimpleExporter.create configuration_name: :referential_companies, filepath: tmp.path +    referential.switch +    exporter.configure do |config| +      config.collection = referential.companies.order(:name) +    end +    exporter.export +    if exporter.status == :error +      self.status = :failed +    else +      self.file = tmp +      if exporter.status == :success +        self.status = :successful +      else +        self.status = :warning +      end +    end +    convert_exporter_journal_to_messages exporter +    self.save! +  end + +  def convert_exporter_journal_to_messages exporter +    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/models/simple_interface.rb b/app/models/simple_interface.rb index 489419482..c766d483a 100644 --- a/app/models/simple_interface.rb +++ b/app/models/simple_interface.rb @@ -16,7 +16,7 @@ class SimpleInterface < ActiveRecord::Base      def find_configuration name        @importers ||= {}        configuration = @importers[name.to_sym] -      raise "Importer not found: #{name}" unless configuration +      raise "#{self.name} not found: #{name}" unless configuration        configuration      end    end diff --git a/app/uploaders/import_uploader.rb b/app/uploaders/import_uploader.rb index 60e17ca0f..6ef52a984 100644 --- a/app/uploaders/import_uploader.rb +++ b/app/uploaders/import_uploader.rb @@ -37,7 +37,7 @@ class ImportUploader < CarrierWave::Uploader::Base    # Add a white list of extensions which are allowed to be uploaded.    # For images you might use something like this:    def extension_whitelist -    %w(zip) +    %w(zip csv json)    end    # Override the filename of the uploaded files: diff --git a/app/views/exports/_form.html.slim b/app/views/exports/_form.html.slim index 68228d539..b8fe0f632 100644 --- a/app/views/exports/_form.html.slim +++ b/app/views/exports/_form.html.slim @@ -9,7 +9,7 @@        - Export::Base.user_visible_descendants.each do |child|          .slave data-master="[name='export[type]']" data-value=child.name            - child.options.each do |attr, option_def| -            = form.input attr, required: option_def[:required], input_html: {value: @export.try(attr) || option_def[:default_value]}, as: option_def[:type] +            = export_option_input form, export, attr, option_def    = form.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'wb_export_form' diff --git a/app/views/exports/show.html.slim b/app/views/exports/show.html.slim index 1b193f795..0455fe375 100644 --- a/app/views/exports/show.html.slim +++ b/app/views/exports/show.html.slim @@ -7,7 +7,9 @@      .row        .col-lg-6.col-md-6.col-sm-12.col-xs-12          - metadatas = { I18n.t("activerecord.attributes.export.type") => @export.object.class.human_name } -        - metadatas = metadatas.update Hash[*@export.options.map{|k, v| [t("activerecord.attributes.export.#{@export.object.class.name.demodulize.tableize.singularize}.#{k}"), v]}.flatten] +        - metadatas = metadatas.update({I18n.t("activerecord.attributes.export.status") => @export.status}) +        - metadatas = metadatas.update Hash[*@export.options.map{|k, v| [t("activerecord.attributes.export.#{@export.object.class.name.demodulize.tableize.singularize}.#{k}"), @export.display_option_value(k, self)]}.flatten] +        - metadatas = metadatas.update({I18n.t("activerecord.attributes.export.file") => link_to(t("actions.download"), @export.file.url)})          = definition_list t('metadatas'), metadatas      .row diff --git a/app/views/shared/iev_interfaces/_messages.html.slim b/app/views/shared/iev_interfaces/_messages.html.slim index bb6eea9f9..d65f006e6 100644 --- a/app/views/shared/iev_interfaces/_messages.html.slim +++ b/app/views/shared/iev_interfaces/_messages.html.slim @@ -2,6 +2,13 @@    ul.list-unstyled.import_message-list      - messages.each do | message |        li -        div(class="#{bootstrap_class_for_message_criticity message.criticity}") -          = t( [message.class.name.underscore.gsub('/', '_').pluralize, -            message.message_key].join('.'), message.message_attributes.symbolize_keys) +        .row class=bootstrap_class_for_message_criticity(message.criticity) +          - if message.message_attributes["line"] +            .col-md-1= "L. #{message.message_attributes["line"]}" +            .col-md-5= export_message_content message +          - else +            .col-md-6= export_message_content message +          .col-md-6 +            - if message.criticity != "info" +              pre +                = JSON.pretty_generate message.resource_attributes diff --git a/config/initializers/exporters.rb b/config/initializers/exporters.rb new file mode 100644 index 000000000..dfd82a54c --- /dev/null +++ b/config/initializers/exporters.rb @@ -0,0 +1,6 @@ +SimpleExporter.define :referential_companies do |config| +  config.separator = ";" +  config.encoding = 'ISO-8859-1' +  config.add_column :name +  config.add_column :registration_number +end diff --git a/config/locales/export_messages.en.yml b/config/locales/export_messages.en.yml new file mode 100644 index 000000000..f7951a103 --- /dev/null +++ b/config/locales/export_messages.en.yml @@ -0,0 +1,3 @@ +en: +  export_messages: +    success: Success diff --git a/config/locales/export_messages.fr.yml b/config/locales/export_messages.fr.yml new file mode 100644 index 000000000..5c2191f35 --- /dev/null +++ b/config/locales/export_messages.fr.yml @@ -0,0 +1,3 @@ +fr: +  export_messages: +    success: Succès diff --git a/config/locales/exports.fr.yml b/config/locales/exports.fr.yml index e2e9f1bbb..e88339c38 100644 --- a/config/locales/exports.fr.yml +++ b/config/locales/exports.fr.yml @@ -82,6 +82,7 @@ fr:          ignore_last_word: "ignorer le dernier mot"          ignore_end_chars: "ignorer les n derniers caractères"          type: "Type d'export" +        file: "Résultat"        export:          <<: *attrs          base: | 
