diff options
| author | Luc Donnet | 2015-05-06 08:43:01 +0200 |
|---|---|---|
| committer | Luc Donnet | 2015-05-06 08:43:01 +0200 |
| commit | 3df25c76f7bc42ab8e8186d69e63c02d370027a8 (patch) | |
| tree | 0e3bdcea091a85570ba492b92d3be90a907905e8 | |
| parent | c76b635911df8d3a40189960896f493206046bce (diff) | |
| download | chouette-core-3df25c76f7bc42ab8e8186d69e63c02d370027a8.tar.bz2 | |
Add compliance check task call for iev
| -rw-r--r-- | app/assets/javascripts/application.js | 1 | ||||
| -rw-r--r-- | app/assets/javascripts/compliance_check_tasks/new.js.coffee (renamed from app/assets/javascripts/compliance_check_tasks.js.coffee) | 10 | ||||
| -rw-r--r-- | app/controllers/compliance_check_tasks_controller.rb | 43 | ||||
| -rw-r--r-- | app/models/compliance_check_task.rb | 173 | ||||
| -rw-r--r-- | app/views/compliance_check_tasks/new.html.erb | 20 | ||||
| -rw-r--r-- | config/routes.rb | 7 |
6 files changed, 139 insertions, 115 deletions
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index d138ec187..790306832 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -24,5 +24,6 @@ //= require_directory ./vehicle_journeys //= require_directory ./import_tasks //= require_directory ./compliance_check_tasks +//= require_directory ./export_tasks //= require_directory ./exports //= require_directory ./lines diff --git a/app/assets/javascripts/compliance_check_tasks.js.coffee b/app/assets/javascripts/compliance_check_tasks/new.js.coffee index feed5de2d..e4b4ba763 100644 --- a/app/assets/javascripts/compliance_check_tasks.js.coffee +++ b/app/assets/javascripts/compliance_check_tasks/new.js.coffee @@ -1,5 +1,5 @@ -jQuery -> - compliance_check_task_references_type_change = (event) -> +$(".compliance_check_tasks.new").ready -> + compliance_check_task_references_type_change = (event) -> references_type = $(event.target).val() toggle_input = (li) -> @@ -7,8 +7,8 @@ jQuery -> # Hide li block li.toggle(enabled) # Disable textarea to ignore it in POST data - li.find("textarea").attr("disabled", ! enabled) - - toggle_input($(li)) for li in $(event.target).parents('form').find("li.compliance_check_task_reference_ids") + li.find(".token-input").attr("disabled", ! enabled) + + toggle_input($(li)) for li in $(event.target).parents('form').find("li.reference_ids") $('select[name="compliance_check_task[references_type]"]').change(compliance_check_task_references_type_change) diff --git a/app/controllers/compliance_check_tasks_controller.rb b/app/controllers/compliance_check_tasks_controller.rb index f1ea267af..cc5ba8ee7 100644 --- a/app/controllers/compliance_check_tasks_controller.rb +++ b/app/controllers/compliance_check_tasks_controller.rb @@ -1,6 +1,49 @@ class ComplianceCheckTasksController < ChouetteController defaults :resource_class => ComplianceCheckTask + respond_to :html, :only => [:new, :create] + respond_to :js, :only => [:new, :create] + belongs_to :referential + def new + begin + new! + rescue Ievkit::Error => error + logger.error("Iev failure : #{error.message}") + flash[:error] = t('iev.failure') + redirect_to referential_path(@referential) + end + end + + def create + begin + create! do |success, failure| + success.html { redirect_to referential_compliance_checks_path(@referential) } + end + rescue Ievkit::Error => error + logger.error("Iev failure : #{error.message}") + flash[:error] = t('iev.failure') + redirect_to referential_path(@referential) + end + end + + def references + references_type = params[:filter].pluralize + references = @referential.send(references_type).where("name ilike ?", "%#{params[:q]}%").select("id, name") + puts references.inspect + respond_to do |format| + format.json do + render :json => references.collect { |child| { :id => child.id, :name => child.name } } + end + end + end + + protected + + def build_resource + @compliance_check_task ||= ComplianceCheckTask.new( params[:compliance_check_task] || {} ) + end + + end diff --git a/app/models/compliance_check_task.rb b/app/models/compliance_check_task.rb index 5d9ce6ca2..32cc08c19 100644 --- a/app/models/compliance_check_task.rb +++ b/app/models/compliance_check_task.rb @@ -2,134 +2,111 @@ class ComplianceCheckTask extend Enumerize extend ActiveModel::Naming extend ActiveModel::Translation - include ActiveModel::Model - attr_reader :datas + extend ActiveModel::Callbacks + include ActiveModel::Validations + include ActiveModel::Conversion - def initialize(response) - @datas = response - end + enumerize :references_type, in: %w( network line company group_of_line ) + attr_accessor :rule_parameter_set_id, :referential_id, :user_id, :user_name, :name, :references_type, :reference_ids - def compliance_check_result - report_path = datas.links.select{ |link| link["rel"] == "validation_report"}.first.href - if report_path - response = Ievkit.get(report_path) - ComplianceCheckResult.new(response) - else - raise Ievkit::IevError("Impossible to access report path link for validation") - end - end + validates_presence_of :referential_id + validates_presence_of :user_id + validates_presence_of :user_name + validates_presence_of :name - def import_task - if datas.action == "importer" - Import.new(Ievkit.scheduled_job(referential_name, id, { :action => "importer" }) ) - end + def initialize( params = {} ) + params.each {|k,v| send("#{k}=",v)} end - def export_task - if datas.action == "exporter" - Export.new(Ievkit.scheduled_job(referential_name, id, { :action => "exporter" }) ) - end + def referential + Referential.find(referential_id) end - def rule_parameter_set - rule_parameter_set = datas.links.select{ |link| link["rel"] == "validation_params"}.first.href - if rule_parameter_set - response = Ievkit.get(rule_parameter_set) - rule_parameter_set = RuleParameterSet.new.tap { |rps| rps.parameters = response.validation } - else - raise Ievkit::Error("Impossible to access rule parameter set link for validation") - end - end - - def compliance_check - compliance_check_path = datas.links.select{ |link| link["rel"] == "validation_report"}.first.href - if compliance_check_path - response = Ievkit.get(compliance_check_path) - ComplianceCheck.new(response) - else - raise Ievkit::Error("Impossible to access compliance check path link for validation") - end + def organisation + referential.organisation end - def delete - delete_path = datas.links.select{ |link| link["rel"] == "delete"}.first.href - if delete_path - Ievkit.delete(delete_path) - else - raise Ievkit::Error("Impossible to access delete path link for validation") - end + def rule_parameter_set + organisation.rule_parameter_sets.find(rule_parameter_set_id) if rule_parameter_set_id.present? end - def cancel - cancel_path = datas.links.select{ |link| link["rel"] == "cancel"}.first.href - if cancel_path - Ievkit.delete(cancel_path) - else - raise Ievkit::Error("Impossible to access cancel path link for validation") + def save + # Call Iev Server + begin + Ievkit.create_job( referential.slug, "validator", "", { + :file1 => params_io, + } ) + rescue Exception => exception + raise exception end end - - def id - datas.id + + def self.references_types + self.references_type.values end - def status - # pending processing completed failed - # CREATED, SCHEDULED, STARTED, TERMINATED, CANCELED, ABORTED, DELETED - if datas.status == "CREATED" - "pending" - elsif datas.status == "SCHEDULED" - "pending" - elsif datas.status == "STARTED" - "processing" - elsif datas.status == "TERMINATED" - "completed" - elsif datas.status == "CANCELED" - "failed" - elsif datas.status == "ABORTED" - "failed" - elsif datas.status == "DELETED" - "failed" + def params + {}.tap do |h| + h["parameters"] = validation_params ? action_params.merge(validation_params) : action_params end end - def format - datas.type + def action_params + { + "validate" => { + "name" => name, + "references_type" => references_type, + "reference_ids" => reference_ids, + "user_name" => user_name, + "organisation_name" => organisation.name, + "referential_name" => referential.name, + } + + } end - def referential_id - Referential.where(:slug => referential_name).id + def validation_params + { + "validation" => rule_parameter_set.parameters + } if rule_parameter_set.present? end - def referential_name - datas.referential + def self.data_formats + self.data_format.values end - - def name - datas.action_parameters.name - end - - def user_name - datas.action_parameters.user_name + + def params_io + file = StringIO.new( params.to_json ) + Faraday::UploadIO.new(file, "application/json", "parameters.json") + end + + def transport_data_io + file = File.new(saved_resources_path, "r") + if file_extname == ".zip" + Faraday::UploadIO.new(file, "application/zip", original_filename ) + elsif file_extname == ".xml" + Faraday::UploadIO.new(file, "application/xml", original_filename ) + end + end + + def save_resources + FileUtils.mkdir_p root + FileUtils.cp resources.path, saved_resources_path end - def no_save - datas.action_parameters.no_save + def delete_resources + FileUtils.rm saved_resources_path if File.exists? saved_resources_path end - def created_at? - datas.created? + def original_filename + resources.original_filename end - def created_at - Time.at(datas.created.to_i / 1000) if created_at? - end - - def updated_at? - datas.updated? + def file_extname + File.extname(resources.original_filename) end - def updated_at - Time.at(datas.updated.to_i / 1000) if updated_at? + def saved_resources_path + "#{root}/#{Time.now.to_i}#{file_extname}" end end diff --git a/app/views/compliance_check_tasks/new.html.erb b/app/views/compliance_check_tasks/new.html.erb index 1d15377e4..49e4fae19 100644 --- a/app/views/compliance_check_tasks/new.html.erb +++ b/app/views/compliance_check_tasks/new.html.erb @@ -1,15 +1,16 @@ <%= title_tag t(".title") %> -<%= semantic_form_for [@referential, @compliance_check_task] do |form| %> +<%= semantic_form_for [@referential, @compliance_check_task], :url => referential_compliance_check_tasks_path(@referential) do |form| %> <%= form.inputs do %> + <%= form.input :user_name, :as => :hidden, :input_html => { :value => current_user.name } %> <%= form.input :user_id, :as => :hidden, :input_html => { :value => current_user.id } %> + <%= form.input :referential_id, :as => :hidden, :input_html => { :value => @referential.id } %> + <%= form.input :name %> <%= form.input :rule_parameter_set_id, :as => :select, - :collection => @referential.rule_parameter_sets.map { |rps| [ rps.name, rps.id ] }, :include_blank => false %> - - <%= form.input :references_type, :as => :select, :collection => ComplianceCheckTask.references_types.map { |c| [ c.model_name.human.capitalize.pluralize, c.name ] }, :include_blank => t(".all") %> - - <% ComplianceCheckTask.references_types.each do |type| %> - <%= type_ids_model_references_input(form, @compliance_check_task, ComplianceCheckTask, type).input %> - <% end %> + :collection => @referential.organisation.rule_parameter_sets.map { |rps| [ rps.name, rps.id ] }, :include_blank => false %> + <%= form.input :references_type, :as => :select, :include_blank => t(".all") %> + <% @compliance_check_task.class.references_types.each do |type| %> + <%= form.input :reference_ids, :as => :reference_ids, :json => references_referential_compliance_check_tasks_path(@referential, :format => :json) + "?filter=#{type}", :hint_text => t('search_hint'), :no_result_text => t('no_result_text'),:searching_text => t('searching_term'), :id => "#{type}_reference_ids", :input_html => { :id => "#{type}_reference_ids" }, :wrapper_html => { :style => "display:none;", :id => "#{type}", :"data-type" => "#{type}" } %> + <% end %> <% end %> <%= form.actions do %> @@ -17,6 +18,3 @@ <%= form.action :cancel, :as => :link %> <% end %> <% end %> - -<%= javascript_include_tag new_referential_compliance_check_task_path(@referential, :format => :js) %> - diff --git a/config/routes.rb b/config/routes.rb index 879e9bc23..abf381bb8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -101,7 +101,12 @@ ChouetteIhm::Application.routes.draw do end end - resources :compliance_check_tasks, :only => [:new, :create] + resources :compliance_check_tasks, :only => [:new, :create] do + collection do + get 'references' + end + end + resources :compliance_checks, :only => [:index, :show, :destroy] do member do get 'export', defaults: { format: 'zip' } |
