diff options
| author | Xinhui | 2017-02-22 16:04:53 +0100 | 
|---|---|---|
| committer | Xinhui | 2017-02-22 16:04:53 +0100 | 
| commit | 77d4aca6f3328e7918077e7f6c32462cf95caa84 (patch) | |
| tree | 1ea07d2e34fc3f07a177c12c243765294f7b1926 /app | |
| parent | b88cd552930099714000393584355c314f045cb6 (diff) | |
| parent | 15e221c2fe06ebd6ca18d7a392ab05f95b59be74 (diff) | |
| download | chouette-core-77d4aca6f3328e7918077e7f6c32462cf95caa84.tar.bz2 | |
Merge branch 'refactoring_import'
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/imports_controller.rb | 109 | ||||
| -rw-r--r-- | app/helpers/breadcrumb_helper.rb | 10 | ||||
| -rw-r--r-- | app/models/import.rb | 90 | ||||
| -rw-r--r-- | app/models/import_message.rb | 7 | ||||
| -rw-r--r-- | app/models/import_resource.rb | 23 | ||||
| -rw-r--r-- | app/models/referential.rb | 1 | ||||
| -rw-r--r-- | app/models/workbench.rb | 2 | ||||
| -rw-r--r-- | app/uploaders/import_uploader.rb | 49 | ||||
| -rw-r--r-- | app/views/imports/_form.html.slim | 5 | ||||
| -rw-r--r-- | app/views/imports/_import.html.slim | 24 | ||||
| -rw-r--r-- | app/views/imports/_imports.html.slim | 9 | ||||
| -rw-r--r-- | app/views/imports/index.html.slim | 15 | ||||
| -rw-r--r-- | app/views/imports/index.js.slim | 1 | ||||
| -rw-r--r-- | app/views/imports/new.html.slim | 4 | ||||
| -rw-r--r-- | app/views/imports/show.html.slim | 21 | ||||
| -rw-r--r-- | app/views/shared/_header.html.slim | 3 | 
16 files changed, 122 insertions, 251 deletions
| diff --git a/app/controllers/imports_controller.rb b/app/controllers/imports_controller.rb index 757e7f09e..3eab7e66b 100644 --- a/app/controllers/imports_controller.rb +++ b/app/controllers/imports_controller.rb @@ -1,110 +1,29 @@ -# coding: utf-8 -require 'will_paginate/array' -require 'open-uri' - -class ImportsController < ChouetteController +class ImportsController < BreadcrumbController    defaults :resource_class => Import - -  respond_to :html, :only => [:show, :index, :destroy, :imported_file, :rule_parameter_set, :compliance_check] -  respond_to :js, :only => [:index, :compliance_check] -  belongs_to :referential - -  def index -    begin -      index! do -        build_breadcrumb :index -      end -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) -    end -  end +  respond_to :html +  belongs_to :workbench    def show -    begin -      show! do -        build_breadcrumb :show -      end -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) -    end -  end - -  def destroy -    begin -      destroy! -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) -    end -  end - -  def imported_file -    # WARNING : files under 10kb in size get treated as StringIO by OpenUri -    # http://stackoverflow.com/questions/10496874/why-does-openuri-treat-files-under-10kb-in-size-as-stringio -    OpenURI::Buffer.send :remove_const, 'StringMax' if OpenURI::Buffer.const_defined?('StringMax') -    OpenURI::Buffer.const_set 'StringMax', 0 -    begin -      send_file open(resource.file_path), { :type => "application/#{resource.filename_extension}", :disposition => "attachment", :filename => resource.filename } -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) -    end -  end - -  def rule_parameter_set -    begin -      @rule_parameter_set = resource.rule_parameter_set -      build_breadcrumb :rule_parameter_set -      render "rule_parameter_sets/show" -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) +    show! do +      build_breadcrumb :show      end    end -  def export -    respond_to do |format| -      format.zip { send_file ComplianceCheckExport.new(resource, @referential.id, request).export, :type => :zip } +  def index +    index! do +      build_breadcrumb :index      end    end -  def compliance_check -    begin -      @compliance_check = resource -      build_breadcrumb :compliance_check -      render "compliance_checks/report" -    rescue Ievkit::Error, Faraday::Error => error -      logger.error("Iev failure : #{error.message}") -      flash[:error] = t(error.locale_for_error) -      redirect_to referential_path(@referential) +  def new +    new! do +      build_breadcrumb :new      end    end -  protected -  alias_method :import, :resource +  private -  def import_service -    ImportService.new(@referential) +  def import_params +    params.require(:import).permit(:name, :file, :referential_id)    end - -  def resource -    @import ||= import_service.find( params[:id] ) -    @line_items = @import.report.line_items -    if @line_items.size > 500 -      @line_items = @line_items.paginate(page: params[:page], per_page: 20) -    end -    @import -  end - -  def collection -    @imports ||= import_service.all.sort_by{ |import| import.created_at }.reverse.paginate(:page => params[:page]) -  end -  end diff --git a/app/helpers/breadcrumb_helper.rb b/app/helpers/breadcrumb_helper.rb index a382f1926..3a742d0dc 100644 --- a/app/helpers/breadcrumb_helper.rb +++ b/app/helpers/breadcrumb_helper.rb @@ -216,14 +216,10 @@ module BreadcrumbHelper    end    def import_breadcrumb (action) -    referential_breadcrumb -    add_breadcrumb Referential.human_attribute_name("imports"), referential_imports_path(@referential) unless action == :index - -    add_breadcrumb @import.name, referential_import_path(@referential, @import.id) if @import - -    #add_breadcrumb @rule_parameter_set.import.name, compliance_check_referential_import_path(@referential, @rule_parameter_set.import.id) if action == :rule_parameter_set +    add_breadcrumb I18n.t("breadcrumbs.referentials"), referentials_path +    add_breadcrumb breadcrumb_label(@workbench), workbench_path(@workbench), :title => breadcrumb_tooltip(@workbench) +    add_breadcrumb I18n.t("breadcrumbs.imports"), workbench_imports_path(@workbench) -    #add_breadcrumb "Tests de conformité", compliance_check_referential_import_path(@referential, @compliance_check.id) if @compliance_check    end    def export_breadcrumb (action) diff --git a/app/models/import.rb b/app/models/import.rb index 078f1698e..8ffac5c5e 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -1,87 +1,7 @@ -require 'open-uri' +class Import < ActiveRecord::Base +  mount_uploader :file, ImportUploader +  belongs_to :workbench +  belongs_to :referential -class Import -  include JobConcern - -  def initialize( response ) -    @datas = response -  end - -  def report? -    links["action_report"].present? -  end - -  def report -    Rails.cache.fetch("#{cache_key}/action_report", expires_in: cache_expiration) do -      report_path = links["action_report"] -      if report_path -        response = Ievkit.get(report_path) -        ImportReport.new(response) -      else -        nil -      end -    end -  end - -  def rule_parameter_set? -    links["validation_params"].present? -  end - -  def rule_parameter_set -    Rails.cache.fetch("#{cache_key}/validation_params", expires_in: cache_expiration) do -      rule_parameter_set_path = links["validation_params"] -      if rule_parameter_set_path -        response = Ievkit.get(rule_parameter_set_path) -        rule_parameter_set = RuleParameterSet.new(:name => "", :import => self).tap { |rps| rps.parameters = response.validation } -      else -        nil -      end -    end -  end - -  def compliance_check? -    links["validation_report"].present? -  end - -  def compliance_check_validation_report -    puts "compliance_check_validation_report" -    Rails.cache.fetch("#{cache_key}/validation_report", expires_in: cache_expiration) do -      compliance_check_path = links["validation_report"] -      if compliance_check_path -        response = Ievkit.get(compliance_check_path) -        ComplianceCheckResult.new(response) -      else -        nil -      end -    end -  end - -  def destroy -    delete_path =  links["delete"] -    cancel_path = links["cancel"] - -    if delete_path -      Ievkit.delete(delete_path) -    elsif cancel_path -      Ievkit.delete(cancel_path) -    else -      nil -    end -  end - -  def file_path? -    links["data"].present? -  end - -  def file_path -    links["data"] -  end - -  def filename -    File.basename(file_path) if file_path -  end - -  def filename_extension -    File.extname(filename).gsub(".", "") if filename -  end +  validates :file, presence: true  end diff --git a/app/models/import_message.rb b/app/models/import_message.rb new file mode 100644 index 000000000..5d0f5c862 --- /dev/null +++ b/app/models/import_message.rb @@ -0,0 +1,7 @@ +class ImportMessage < ActiveRecord::Base +  belongs_to :import +  belongs_to :resource, class_name: ImportResource +  enum criticity: [:info, :warning, :error] + +  validates :criticity, presence: true +end diff --git a/app/models/import_resource.rb b/app/models/import_resource.rb new file mode 100644 index 000000000..f140e1b36 --- /dev/null +++ b/app/models/import_resource.rb @@ -0,0 +1,23 @@ +class ImportResource < ActiveRecord::Base +  include AASM +  belongs_to :import + +  aasm column: :status do +    state :new, :initial => true +    state :pending +    state :successful +    state :failed + +    event :run do +      transitions :from => [:new, :failed], :to => :pending +    end + +    event :successful do +      transitions :from => [:pending, :failed], :to => :successful +    end + +    event :failed do +      transitions :from => :pending, :to => :failed +    end +  end +end diff --git a/app/models/referential.rb b/app/models/referential.rb index 34da65755..dbc7b76e7 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -34,7 +34,6 @@ class Referential < ActiveRecord::Base    has_many :companies, through: :line_referential    has_many :group_of_lines, through: :line_referential    has_many :networks, through: :line_referential -    has_many :metadatas, class_name: "ReferentialMetadata", inverse_of: :referential, dependent: :destroy    accepts_nested_attributes_for :metadatas diff --git a/app/models/workbench.rb b/app/models/workbench.rb index 21c586873..008f73d46 100644 --- a/app/models/workbench.rb +++ b/app/models/workbench.rb @@ -8,7 +8,7 @@ class Workbench < ActiveRecord::Base    has_many :companies, through: :line_referential    has_many :group_of_lines, through: :line_referential    has_many :stop_areas, through: :stop_area_referential - +  has_many :imports    validates :name, presence: true    validates :organisation, presence: true diff --git a/app/uploaders/import_uploader.rb b/app/uploaders/import_uploader.rb new file mode 100644 index 000000000..2740393ca --- /dev/null +++ b/app/uploaders/import_uploader.rb @@ -0,0 +1,49 @@ +class ImportUploader < CarrierWave::Uploader::Base + +  # Include RMagick or MiniMagick support: +  # include CarrierWave::RMagick +  # include CarrierWave::MiniMagick + +  # Choose what kind of storage to use for this uploader: +  storage :file +  # storage :fog + +  # Override the directory where uploaded files will be stored. +  # This is a sensible default for uploaders that are meant to be mounted: +  def store_dir +    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" +  end + +  # Provide a default URL as a default if there hasn't been a file uploaded: +  # def default_url +  #   # For Rails 3.1+ asset pipeline compatibility: +  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) +  # +  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_') +  # end + +  # Process files as they are uploaded: +  # process scale: [200, 300] +  # +  # def scale(width, height) +  #   # do something +  # end + +  # Create different versions of your uploaded files: +  # version :thumb do +  #   process resize_to_fit: [50, 50] +  # end + +  # Add a white list of extensions which are allowed to be uploaded. +  # For images you might use something like this: +  # def extension_whitelist +  #   %w(jpg jpeg gif png) +  # end + +  # Override the filename of the uploaded files: +  # Avoid using model.id or version_name here, see uploader/store.rb for details. +  # def filename +  #   "something.jpg" if original_filename +  # end + +end diff --git a/app/views/imports/_form.html.slim b/app/views/imports/_form.html.slim new file mode 100644 index 000000000..693ce6fc4 --- /dev/null +++ b/app/views/imports/_form.html.slim @@ -0,0 +1,5 @@ += simple_form_for [workbench, import] do |f| +  = f.input :name +  = f.input :file +  = f.association :referential +  = f.button :submit diff --git a/app/views/imports/_import.html.slim b/app/views/imports/_import.html.slim deleted file mode 100644 index 7d4753358..000000000 --- a/app/views/imports/_import.html.slim +++ /dev/null @@ -1,24 +0,0 @@ -#index_item.panel.panel-default.import -  .panel-heading -    .panel-title.clearfix -      span.pull-right -        = link_to referential_import_path(@referential, import.id), method: :delete, :data => {:confirm =>  t('imports.actions.destroy_confirm')}, class: 'btn btn-danger btn-sm' do -          span.fa.fa-trash-o - -      h5 -        = link_to( referential_import_path(@referential, import.id), class: 'preview', :title => "#{ImportTask.model_name.human.capitalize} #{import.name}") do -          = job_status_title(import) - -  .panel-body -    p -      = link_to font_awesome_classic_tag("fa-file-#{import.filename_extension}-o") + t("imports.show.imported_file"), imported_file_referential_import_path(@referential, import.id) if import.file_path? - -    p -      = link_to font_awesome_classic_tag("fa-external-link") + t("imports.show.compliance_check"), compliance_check_referential_import_path(@referential, import.id) if import.compliance_check? -  -  .panel-footer -    = import_attributes_tag(import) - -    .history -      = l(import.created_at, :format => "%d/%m/%Y %H:%M") if import.created_at -      = " | #{import.user_name}"
\ No newline at end of file diff --git a/app/views/imports/_imports.html.slim b/app/views/imports/_imports.html.slim deleted file mode 100644 index 3605fd10b..000000000 --- a/app/views/imports/_imports.html.slim +++ /dev/null @@ -1,9 +0,0 @@ -.page_info -  span.search = t("will_paginate.page_entries_info.search") -  = page_entries_info @imports - -.imports.paginated_content -  = paginated_content @imports, "imports/import" - -.pagination -  = will_paginate @imports, :container => false, renderer: RemoteBootstrapPaginationLinkRenderer
\ No newline at end of file diff --git a/app/views/imports/index.html.slim b/app/views/imports/index.html.slim index b1922d005..fd87de90f 100644 --- a/app/views/imports/index.html.slim +++ b/app/views/imports/index.html.slim @@ -1,13 +1,12 @@  = title_tag t('.title') +- @imports.each do |import| +  .import +    li = link_to import.name, workbench_import_path(@workbench, import) +    li = import.referential.name +    li = link_to import.file.file.filename, import.file.url, target: :_blank +    hr  .warning = t('.warning') - -#imports -  = render 'imports' -  - content_for :sidebar do    ul.actions -    li -      = link_to t('imports.actions.new'), new_referential_import_task_path(@referential), class: 'add' -    li -      = link_to t('rule_parameter_sets.actions.index'), organisation_rule_parameter_sets_path, class: 'link'
\ No newline at end of file +    li = link_to t('imports.actions.new'), new_workbench_import_path(workbench_id: @workbench), class: 'add' diff --git a/app/views/imports/index.js.slim b/app/views/imports/index.js.slim deleted file mode 100644 index 2fac7d26c..000000000 --- a/app/views/imports/index.js.slim +++ /dev/null @@ -1 +0,0 @@ -| $('#imports').html("#{escape_javascript(render("imports"))}");
\ No newline at end of file diff --git a/app/views/imports/new.html.slim b/app/views/imports/new.html.slim new file mode 100644 index 000000000..55b655a85 --- /dev/null +++ b/app/views/imports/new.html.slim @@ -0,0 +1,4 @@ += title_tag t('.title') +.row +  .col-lg-8.col-lg-offset-2.col-md-8.col-md-offset-2.col-sm-8.col-sm-offset-2 +    = render 'form', import: @import, workbench: @workbench diff --git a/app/views/imports/show.html.slim b/app/views/imports/show.html.slim index 5992af311..b40e11ea4 100644 --- a/app/views/imports/show.html.slim +++ b/app/views/imports/show.html.slim @@ -2,26 +2,13 @@    .col-md-8      = title_tag job_status_title(@import) -  .col-md-4 -    = import_attributes_tag(@import) - -- if @import.report.failure_code? -  .alert.alert-danger -    = t("iev.failure.#{@import.report.failure_code}") -   -.progress_bars -  = progress_bar_tag(@import) -  .import_show    .links -    = link_to font_awesome_classic_tag("fa-file-#{@import.filename_extension}-o") + t("imports.show.imported_file"), imported_file_referential_import_path(@referential, @import.id) if @import.file_path? -    = link_to font_awesome_classic_tag("fa-external-link") + t("imports.show.compliance_check"), compliance_check_referential_import_path(@referential, @import.id) if @import.compliance_check? - -  = render(partial: 'shared/ie_report', locals: {job: @import, line_items: @line_items}) +    = link_to font_awesome_classic_tag("fa-file-#{@import.file.file.extension}-o") + t("imports.show.imported_file"), @import.file.url  - content_for :sidebar do    ul.actions      li -      = link_to t('imports.actions.destroy'), referential_import_path(@referential, @import.id), method: :delete, data: {confirm: t('imports.actions.destroy_confirm')}, class: 'remove' -   -  = history_tag(@import)
\ No newline at end of file +      = link_to t('imports.actions.destroy'), workbench_import_path(@workbench, @import.id), method: :delete, data: {confirm: t('imports.actions.destroy_confirm')}, class: 'remove' + +  = history_tag(@import) diff --git a/app/views/shared/_header.html.slim b/app/views/shared/_header.html.slim index 73e705e35..33aee09a4 100644 --- a/app/views/shared/_header.html.slim +++ b/app/views/shared/_header.html.slim @@ -90,9 +90,6 @@ nav.navbar.navbar-default.navbar-fixed-top role="navigation"                  = link_to referential_timebands_path(@referential) do                    span.badge.pull-right = @referential.timebands.size                    = Referential.human_attribute_name("timebands") - -          li -            = link_to Referential.human_attribute_name("imports"), referential_imports_path(@referential)            li              = link_to Referential.human_attribute_name("exports"), referential_exports_path(@referential)            li | 
