diff options
| -rw-r--r-- | app/models/export.rb | 53 | ||||
| -rw-r--r-- | app/models/export_log_message.rb | 42 | ||||
| -rw-r--r-- | app/models/export_report.rb | 8 | ||||
| -rw-r--r-- | app/models/export_service.rb | 23 | ||||
| -rw-r--r-- | app/models/export_task.rb | 119 | ||||
| -rw-r--r-- | app/models/import.rb | 2 | ||||
| -rw-r--r-- | app/models/import_message_export.rb | 48 | ||||
| -rw-r--r-- | app/models/import_report.rb | 8 | ||||
| -rw-r--r-- | app/models/import_service.rb | 23 | ||||
| -rw-r--r-- | app/models/import_task.rb | 141 | ||||
| -rw-r--r-- | db/migrate/20180306135204_clean_former_exports.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 22 | ||||
| -rw-r--r-- | spec/models/export_spec.rb | 66 | ||||
| -rw-r--r-- | spec/models/export_task_spec.rb | 8 |
14 files changed, 12 insertions, 556 deletions
diff --git a/app/models/export.rb b/app/models/export.rb deleted file mode 100644 index 8c38d6684..000000000 --- a/app/models/export.rb +++ /dev/null @@ -1,53 +0,0 @@ -class Export - 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) - ExportReport.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 - -end diff --git a/app/models/export_log_message.rb b/app/models/export_log_message.rb deleted file mode 100644 index 4bb9d3cc7..000000000 --- a/app/models/export_log_message.rb +++ /dev/null @@ -1,42 +0,0 @@ -class ExportLogMessage < ActiveRecord::Base - belongs_to :export - - acts_as_list :scope => :export - - validates_presence_of :key - validates_inclusion_of :severity, :in => %w{info warning error ok uncheck fatal} - - def arguments=(arguments) - write_attribute :arguments, (arguments.to_json if arguments.present?) - end - - def arguments - @decoded_arguments ||= - begin - if (stored_arguments = raw_attributes).present? - ActiveSupport::JSON.decode stored_arguments - else - {} - end - end - end - - def raw_attributes - read_attribute(:arguments) - end - - before_validation :define_default_attributes, :on => :create - def define_default_attributes - self.severity ||= "info" - end - - def full_message - last_key=key.rpartition("|").last - begin - I18n.translate last_key, arguments.symbolize_keys.merge(:scope => "export_log_messages.messages").merge(:default => :undefined).merge(:key => last_key) - rescue => e - Rails.logger.error "missing arguments for message "+last_key - I18n.translate "WRONG_DATA",{"0"=>last_key}.symbolize_keys.merge(:scope => "export_log_messages.messages").merge(:default => :undefined).merge(:key => "WRONG_DATA") - end - end -end diff --git a/app/models/export_report.rb b/app/models/export_report.rb deleted file mode 100644 index 3c0788106..000000000 --- a/app/models/export_report.rb +++ /dev/null @@ -1,8 +0,0 @@ -class ExportReport - #include ReportConcern - - def initialize( response ) - @datas = response.action_report - end - -end diff --git a/app/models/export_service.rb b/app/models/export_service.rb deleted file mode 100644 index 2dbe0d7b3..000000000 --- a/app/models/export_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -class ExportService - - attr_reader :referential - - def initialize(referential) - @referential = referential - end - - # Find an export whith his id - def find(id) - Export.new( Ievkit.scheduled_job(referential.slug, id, { :action => "exporter" }) ) - end - - # Find all exports - def all - [].tap do |jobs| - Ievkit.jobs(referential.slug, { :action => "exporter" }).each do |job| - jobs << Export.new( job ) - end - end - end - -end diff --git a/app/models/export_task.rb b/app/models/export_task.rb deleted file mode 100644 index f02cb914e..000000000 --- a/app/models/export_task.rb +++ /dev/null @@ -1,119 +0,0 @@ -class ExportTask - extend Enumerize - extend ActiveModel::Naming - extend ActiveModel::Translation - extend ActiveModel::Callbacks - include ActiveModel::Validations - include ActiveModel::Conversion - - attr_accessor :start_date, :end_date - - define_model_callbacks :initialize, only: :after - - enumerize :data_format, in: %w( neptune netex gtfs hub kml ) - attr_accessor :referential_id, :user_id, :user_name, :references_type, :data_format, :name, :projection_type, :reference_ids - - validates_presence_of :referential_id - validates_presence_of :user_id - validates_presence_of :user_name - validates_presence_of :name - validates_presence_of :data_format - - validate :period_validation - - after_initialize :init_period - - def initialize( params = {} ) - run_callbacks :initialize do - params.each {|k,v| send("#{k}=",v)} - end - end - - def period_validation - st_date = start_date.is_a?(String) ? Date.parse(start_date) : start_date - ed_date = end_date.is_a?(String) ? Date.parse(end_date) : end_date - - unless Chouette::TimeTable.start_validity_period.nil? || st_date.nil? - tt_st_date = Chouette::TimeTable.start_validity_period - errors.add(:start_date, ExportTask.human_attribute_name("start_date_greater_than" , {:tt_st_date => tt_st_date})) unless tt_st_date <= st_date - end - unless st_date.nil? || ed_date.nil? - errors.add(:end_date, ExportTask.human_attribute_name("end_date_greater_than_start_date")) unless st_date <= ed_date - end - unless ed_date.nil? || Chouette::TimeTable.end_validity_period.nil? - tt_ed_date = Chouette::TimeTable.end_validity_period - errors.add(:end_date, ExportTask.human_attribute_name("end_date_less_than", {:tt_ed_date => tt_ed_date})) unless ed_date <= tt_ed_date - end - end - - def init_period - unless Chouette::TimeTable.start_validity_period.nil? - if start_date.nil? - self.start_date = Chouette::TimeTable.start_validity_period - end - if end_date.nil? - self.end_date = Chouette::TimeTable.end_validity_period - end - end - end - - def referential - Referential.find(referential_id) - end - - def organisation - referential.organisation - end - - def save - if self.valid? - # Call Iev Server - begin - Ievkit.create_job( referential.slug, "exporter", data_format, { - :file1 => params_io, - } ) - rescue Exception => exception - raise exception - end - true - else - false - end - end - - def self.data_formats - self.data_format.values - end - - def self.references_types - self.references_type.values - end - - def params - {}.tap do |h| - h["parameters"] = action_params - end - end - - def action_params - {} - end - - def params_io - file = StringIO.new( params.to_json ) - Faraday::UploadIO.new(file, "application/json", "parameters.json") - end - - def self.optional_attributes(references_type) - [] - end - - def optional_attributes - self.class.optional_attributes(references_type.to_s) - end - - def optional_attribute?(attribute) - optional_attributes.include? attribute.to_sym - end - -end diff --git a/app/models/import.rb b/app/models/import.rb index 29aadcd56..adad39b59 100644 --- a/app/models/import.rb +++ b/app/models/import.rb @@ -11,7 +11,7 @@ class Import < ActiveRecord::Base scope :where_started_at_in, ->(period_range) do where('started_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end) - end + end scope :blocked, -> { where('created_at < ? AND status = ?', 4.hours.ago, 'running') } diff --git a/app/models/import_message_export.rb b/app/models/import_message_export.rb deleted file mode 100644 index 991eb0f61..000000000 --- a/app/models/import_message_export.rb +++ /dev/null @@ -1,48 +0,0 @@ -# -*- coding: utf-8 -*- -require "csv" -require "zip" - -class ImportMessageExport - include ActiveModel::Validations - include ActiveModel::Conversion - extend ActiveModel::Naming - - attr_accessor :import_messages - - def initialize(attributes = {}) - attributes.each { |name, value| send("#{name}=", value) } - end - - def persisted? - false - end - - def label(name) - I18n.t "vehicle_journey_exports.label.#{name}" - end - - def column_names - ["criticity", "message key", "message", "file name", "line", "column"] - end - - def to_csv(options = {}) - csv_string = CSV.generate(options) do |csv| - csv << column_names - import_messages.each do |import_message| - csv << [import_message.criticity, import_message.message_key, I18n.t("import_messages.#{import_message.message_key}", import_message.message_attributes.deep_symbolize_keys), *import_message.resource_attributes.values_at("filename", "line_number", "column_number") ] - end - end - # We add a BOM to indicate we use UTF-8 - "\uFEFF" + csv_string - end - - def to_zip(temp_file,options = {}) - ::Zip::OutputStream.open(temp_file) { |zos| } - ::Zip::File.open(temp_file.path, ::Zip::File::CREATE) do |zipfile| - zipfile.get_output_stream(label("vj_filename")+route.id.to_s+".csv") { |f| f.puts to_csv(options) } - zipfile.get_output_stream(label("tt_filename")+".csv") { |f| f.puts time_tables_to_csv(options) } - zipfile.get_output_stream(label("ftn_filename")+".csv") { |f| f.puts footnotes_to_csv(options) } - end - end - -end diff --git a/app/models/import_report.rb b/app/models/import_report.rb deleted file mode 100644 index ba13f0118..000000000 --- a/app/models/import_report.rb +++ /dev/null @@ -1,8 +0,0 @@ -class ImportReport - #include ReportConcern - - def initialize( response ) - @datas = response.action_report - end - -end diff --git a/app/models/import_service.rb b/app/models/import_service.rb deleted file mode 100644 index 2e3c1012b..000000000 --- a/app/models/import_service.rb +++ /dev/null @@ -1,23 +0,0 @@ -class ImportService - - attr_reader :referential - - def initialize( referential ) - @referential = referential - end - - # Find an import whith his id - def find(id) - Import.new( Ievkit.scheduled_job(referential.slug, id, { :action => "importer" }) ) - end - - # Find all imports - def all - [].tap do |jobs| - Ievkit.jobs(referential.slug, { :action => "importer" }).each do |job| - jobs << Import.new( job ) - end - end - end - -end diff --git a/app/models/import_task.rb b/app/models/import_task.rb deleted file mode 100644 index 7dfa2c644..000000000 --- a/app/models/import_task.rb +++ /dev/null @@ -1,141 +0,0 @@ -require "zip" - -class ImportTask - extend Enumerize - extend ActiveModel::Naming - extend ActiveModel::Translation - include ActiveModel::Validations - include ActiveModel::Conversion - - # TODO : Move in configuration - @@root = "#{Rails.root}/tmp/imports" - cattr_accessor :root - - enumerize :data_format, in: %w( neptune netex gtfs ) - attr_accessor :referential_id, :user_id, :user_name, :data_format, :resources, :name, :no_save - - validates_presence_of :referential_id - validates_presence_of :resources - validates_presence_of :user_id - validates_presence_of :user_name - validates_presence_of :name - - validate :validate_file_size, :validate_file_content - - def initialize( params = {} ) - params.each {|k,v| send("#{k}=",v)} - end - - def referential - Referential.find(referential_id) - end - - def organisation - referential.organisation - end - - def save - if valid? - # Save resources - save_resources - - # Call Iev Server - begin - Ievkit.create_job(referential.slug, "importer", data_format, { - :file1 => params_io, - :file2 => transport_data_io - } - - ) - - # Delete resources - delete_resources - rescue Exception => exception - # If iev server has an error must delete resources before - delete_resources - - raise exception - end - true - else - false - end - end - - def params - {}.tap do |h| - h["parameters"] = {} - end - end - - def self.data_formats - self.data_format.values - end - - 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 delete_resources - FileUtils.rm saved_resources_path if File.exists? saved_resources_path - end - - def original_filename - resources.original_filename - end - - def file_extname - File.extname(original_filename) if original_filename - end - - def saved_resources_path - @saved_resources_path ||= "#{root}/#{Time.now.to_i}#{file_extname}" - end - - @@maximum_file_size = 80.megabytes - cattr_accessor :maximum_file_size - - def validate_file_size - return unless resources.present? and resources.path.present? and File.exists? resources.path - - if File.size(resources.path) > maximum_file_size - message = I18n.t("activemodel.errors.models.import_task.attributes.resources.maximum_file_size", file_size: ActionController::Base.helpers.number_to_human_size(File.size(resources.path)), maximum_file_size: ActionController::Base.helpers.number_to_human_size(maximum_file_size)) - errors.add(:resources, message) - end - end - - @@valid_mime_types = { - neptune: %w{application/zip application/xml}, - netex: %w{application/zip}, - gtfs: %w{application/zip text/plain} - } - cattr_accessor :valid_mime_types - - def validate_file_content - return unless resources.present? and resources.path.present? and File.exists? resources.path - - mime_type = (File.open(resources.path) { |f| MimeMagic.by_magic f }).try :type - expected_mime_types = valid_mime_types[data_format.to_sym] - - unless expected_mime_types.include? mime_type - message = I18n.t("activemodel.errors.models.import_task.attributes.resources.invalid_mime_type", mime_type: mime_type) - errors.add(:resources, message) - end - end - -end diff --git a/db/migrate/20180306135204_clean_former_exports.rb b/db/migrate/20180306135204_clean_former_exports.rb new file mode 100644 index 000000000..46a595c12 --- /dev/null +++ b/db/migrate/20180306135204_clean_former_exports.rb @@ -0,0 +1,5 @@ +class CleanFormerExports < ActiveRecord::Migration + def change + drop_table :exports + end +end diff --git a/db/schema.rb b/db/schema.rb index e29d076e0..276d7f79a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,13 +11,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180301142531) do +ActiveRecord::Schema.define(version: 20180306135204) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - enable_extension "postgis" enable_extension "hstore" + enable_extension "postgis" enable_extension "unaccent" + enable_extension "objectid" create_table "access_links", id: :bigserial, force: :cascade do |t| t.integer "access_point_id", limit: 8 @@ -90,9 +91,9 @@ ActiveRecord::Schema.define(version: 20180301142531) do t.integer "organisation_id", limit: 8 t.datetime "created_at" t.datetime "updated_at" - t.integer "workgroup_id", limit: 8 t.integer "int_day_types" t.date "excluded_dates", array: true + t.integer "workgroup_id", limit: 8 end add_index "calendars", ["organisation_id"], name: "index_calendars_on_organisation_id", using: :btree @@ -119,6 +120,7 @@ ActiveRecord::Schema.define(version: 20180301142531) do t.datetime "updated_at" t.date "end_date" t.string "date_type" + t.string "mode" end add_index "clean_ups", ["referential_id"], name: "index_clean_ups_on_referential_id", using: :btree @@ -299,19 +301,6 @@ ActiveRecord::Schema.define(version: 20180301142531) do add_index "custom_fields", ["resource_type"], name: "index_custom_fields_on_resource_type", using: :btree - create_table "exports", id: :bigserial, force: :cascade do |t| - t.integer "referential_id", limit: 8 - t.string "status" - t.string "type" - t.string "options" - t.datetime "created_at" - t.datetime "updated_at" - t.string "references_type" - t.string "reference_ids" - end - - add_index "exports", ["referential_id"], name: "index_exports_on_referential_id", using: :btree - create_table "facilities", id: :bigserial, force: :cascade do |t| t.integer "stop_area_id", limit: 8 t.integer "line_id", limit: 8 @@ -766,6 +755,7 @@ ActiveRecord::Schema.define(version: 20180301142531) do t.datetime "created_at" t.datetime "updated_at" t.string "objectid_format" + t.string "registration_number_format" end create_table "stop_areas", id: :bigserial, force: :cascade do |t| diff --git a/spec/models/export_spec.rb b/spec/models/export_spec.rb deleted file mode 100644 index 13953078a..000000000 --- a/spec/models/export_spec.rb +++ /dev/null @@ -1,66 +0,0 @@ -# require 'spec_helper' - -# describe Export, :type => :model do - -# subject { create :export } - -# RSpec::Matchers.define :be_log_message do |expected| -# match do |actual| -# actual and expected.all? { |k,v| actual[k.to_s] == v } -# end -# end - -# describe "#export" do - -# before(:each) do -# allow(subject).to receive_messages :exporter => double(:export => true) -# end - -# it "should create a ExportLogmessage :started when started" do -# subject.export -# expect(subject.log_messages.first).to be_log_message(:key => "started") -# end - -# it "should create a ExportLogmessage :completed when completed" do -# subject.export -# expect(subject.log_messages.last).to be_log_message(:key => "completed") -# end - -# it "should create a ExportLogmessage :failed when failed" do -# pending -# # subject.loader.stub(:export).and_raise("export failed") -# subject.export -# expect(subject.log_messages.last).to be_log_message(:key => "failed") -# end - -# end - -# describe "#options" do - -# it "should be empty by default" do -# expect(subject.options).to be_empty -# end - -# end - -# describe ".types" do - -# it "should return available Export implementations" do -# expect(Export.types).to match_array(%w{NeptuneExport CsvExport GtfsExport NetexExport KmlExport HubExport}) -# end - -# end - -# describe ".new" do - -# it "should use type attribute to create a subclass" do -# expect(Export.new(:type => "NeptuneExport")).to be_an_instance_of(NeptuneExport) -# end - -# end - -# it_behaves_like TypeIdsModelable do -# let(:type_ids_model) { subject} -# end - -# end diff --git a/spec/models/export_task_spec.rb b/spec/models/export_task_spec.rb deleted file mode 100644 index 1a52a6175..000000000 --- a/spec/models/export_task_spec.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'spec_helper' - -describe ExportTask, :type => :model do - - it { should_not validate_presence_of(:start_date) } - it { should_not validate_presence_of(:end_date) } - -end |
