aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorMarc Florisson2012-07-06 16:46:04 +0200
committerMarc Florisson2012-07-06 16:46:04 +0200
commitc0a4aeefa149f558daecbdbfeaf683a642771329 (patch)
treee454e189d9af3601421256f4665b87a9db9fe4ce /app/models
parenta94b6b696990fd038cc425ed93f6971c1093e8cf (diff)
parent9848694c64bf7e7183d23b8adce92a80fa0155b4 (diff)
downloadchouette-core-c0a4aeefa149f558daecbdbfeaf683a642771329.tar.bz2
Merge branch 'master' of chouette.dryade.priv:/srv/git/chouette2
Diffstat (limited to 'app/models')
-rw-r--r--app/models/export.rb5
-rw-r--r--app/models/file_validation.rb211
-rw-r--r--app/models/file_validation_log_message.rb68
-rw-r--r--app/models/help_page.rb23
-rw-r--r--app/models/import.rb8
-rw-r--r--app/models/referential.rb2
-rw-r--r--app/models/test_sheet_page.rb64
7 files changed, 370 insertions, 11 deletions
diff --git a/app/models/export.rb b/app/models/export.rb
index 401541a13..a83d89958 100644
--- a/app/models/export.rb
+++ b/app/models/export.rb
@@ -25,10 +25,6 @@ class Export < ActiveRecord::Base
exporter ||= ::Chouette::Exporter.new(referential.slug)
end
- def options
- read_attribute(:options) || write_attribute(:options, {})
- end
-
@@root = "#{Rails.root}/tmp/exports"
cattr_accessor :root
@@ -63,6 +59,7 @@ class Export < ActiveRecord::Base
before_validation :define_default_attributes, :on => :create
def define_default_attributes
self.status ||= "pending"
+ self.options ||= {}
end
after_create :delayed_export
diff --git a/app/models/file_validation.rb b/app/models/file_validation.rb
new file mode 100644
index 000000000..26f2b2e90
--- /dev/null
+++ b/app/models/file_validation.rb
@@ -0,0 +1,211 @@
+class FileValidation < ActiveRecord::Base
+ validates_presence_of :resources
+
+ validates_inclusion_of :status, :in => %w{ pending completed failed }
+
+ attr_accessor :resources,:uncheck_count,:ok_count,:warning_count,:error_count,:fatal_count,:log_message_tree
+ attr_accessor :validator
+
+ has_many :log_messages, :class_name => "FileValidationLogMessage", :order => :position, :dependent => :destroy
+
+ serialize :options
+
+ def self.option(name)
+ name = name.to_s
+
+ define_method(name) do
+ self.options[name]
+ end
+
+ define_method("#{name}=") do |prefix|
+ self.options[name] = prefix
+ end
+ end
+
+ def options
+ read_attribute(:options) || write_attribute(:options, {})
+ end
+
+ option :test3_1_minimal_distance
+ option :test3_2_minimal_distance
+ option :test3_2_polygon_points
+ option :test3_7_minimal_distance
+ option :test3_7_maximal_distance
+ option :test3_8a_minimal_speed
+ option :test3_8a_maximal_speed
+ option :test3_8b_minimal_speed
+ option :test3_8b_maximal_speed
+ option :test3_8c_minimal_speed
+ option :test3_8c_maximal_speed
+ option :test3_8d_minimal_speed
+ option :test3_8d_maximal_speed
+ option :test3_9_minimal_speed
+ option :test3_9_maximal_speed
+ option :test3_10_minimal_distance
+ option :test3_15_minimal_time
+ option :test3_16_1_maximal_time
+ option :test3_16_3a_maximal_time
+ option :test3_16_3b_maximal_time
+ option :test3_21a_minimal_speed
+ option :test3_21a_maximal_speed
+ option :test3_21b_minimal_speed
+ option :test3_21b_maximal_speed
+ option :test3_21c_minimal_speed
+ option :test3_21c_maximal_speed
+ option :test3_21d_minimal_speed
+ option :test3_21d_maximal_speed
+ option :projection_reference
+
+ def validator
+ @validator ||= ::Chouette::FileValidator.new("public")
+ end
+
+ def with_original_filename
+ Dir.mktmpdir do |tmp_dir|
+ tmp_link = File.join(tmp_dir, resources.original_filename)
+ FileUtils.ln_s resources.path, tmp_link
+ yield tmp_link
+ end
+ end
+
+ before_validation :define_default_attributes, :on => :create
+ def define_default_attributes
+ self.status ||= "pending"
+ end
+
+ after_validation :extract_file_type, :on => :create
+ def extract_file_type
+ if !resources.original_filename.nil?
+ self.file_type = resources.original_filename.rpartition(".").last
+ self.file_name = resources.original_filename
+ end
+ end
+
+ after_create :delayed_validate
+ def delayed_validate
+ save_resources
+ delay.validate
+ end
+
+ @@root = "#{Rails.root}/tmp/validations"
+ cattr_accessor :root
+
+ def save_resources
+ FileUtils.mkdir_p root
+ FileUtils.cp resources.path, saved_resources
+ end
+
+ after_destroy :destroy_resources
+ def destroy_resources
+ FileUtils.rm saved_resources if File.exists? saved_resources
+ end
+
+ def saved_resources
+ "#{root}/#{id}.#{file_type}"
+ end
+
+ def name
+ "#{FileValidation.model_name.humanize} #{id}"
+ end
+
+ def validation_options
+ { :validation_id => self.id ,
+ :file_format => self.file_type ,
+ :test3_1_minimal_distance => self.test3_1_minimal_distance ,
+ :test3_2_minimal_distance => self.test3_2_minimal_distance ,
+ :test3_2_polygon_points => self.test3_2_polygon_points ,
+ :test3_7_minimal_distance => self.test3_7_minimal_distance ,
+ :test3_7_maximal_distance => self.test3_7_maximal_distance ,
+ :test3_8a_minimal_speed => self.test3_8a_minimal_speed ,
+ :test3_8a_maximal_speed => self.test3_8a_maximal_speed ,
+ :test3_8b_minimal_speed => self.test3_8b_minimal_speed ,
+ :test3_8b_maximal_speed => self.test3_8b_maximal_speed ,
+ :test3_8c_minimal_speed => self.test3_8c_minimal_speed ,
+ :test3_8c_maximal_speed => self.test3_8c_maximal_speed ,
+ :test3_8d_minimal_speed => self.test3_8d_minimal_speed ,
+ :test3_8d_maximal_speed => self.test3_8d_maximal_speed ,
+ :test3_9_minimal_speed => self.test3_9_minimal_speed ,
+ :test3_9_maximal_speed => self.test3_9_maximal_speed ,
+ :test3_10_minimal_distance => self.test3_10_minimal_distance ,
+ :test3_15_minimal_time => self.test3_15_minimal_time ,
+ :test3_16_1_maximal_time => self.test3_16_1_maximal_time ,
+ :test3_16_3a_maximal_time => self.test3_16_3a_maximal_time ,
+ :test3_16_3b_maximal_time => self.test3_16_3b_maximal_time ,
+ :test3_21a_minimal_speed => self.test3_21a_minimal_speed ,
+ :test3_21a_maximal_speed => self.test3_21a_maximal_speed ,
+ :test3_21b_minimal_speed => self.test3_21b_minimal_speed ,
+ :test3_21b_maximal_speed => self.test3_21b_maximal_speed ,
+ :test3_21c_minimal_speed => self.test3_21c_minimal_speed ,
+ :test3_21c_maximal_speed => self.test3_21c_maximal_speed ,
+ :test3_21d_minimal_speed => self.test3_21d_minimal_speed ,
+ :test3_21d_maximal_speed => self.test3_21d_maximal_speed ,
+ :projection_reference => self.projection_reference
+ }
+ end
+
+ def validate
+ begin
+ # log_messages.create :key => :started
+ if resources
+ with_original_filename do |file|
+ # chouette-command checks the file extension (and requires .zip) :(
+ validator.validate file, validation_options
+ end
+ else
+ validator.validate saved_resources, validation_options
+ end
+ update_attribute :status, "completed"
+ rescue => e
+ Rails.logger.error "Validation #{id} failed : #{e}, #{e.backtrace}"
+ update_attribute :status, "failed"
+ end
+ # log_messages.create :key => status
+ end
+
+ after_find :compute_tests
+ def compute_tests
+ if status == 'completed'
+ self.uncheck_count = 0
+ self.ok_count = 0
+ self.warning_count = 0
+ self.error_count = 0
+ self.fatal_count = 0
+ self.log_message_tree = Array.new
+ father1=nil
+ father2=nil
+ father3=nil
+ log_messages.each do |message|
+ if message.level == 1
+ self.log_message_tree << message
+ father1=message
+ elsif message.level == 2
+ father1.add_child message
+ father2=message
+ elsif message.level == 3
+ father2.add_child message
+ father3=message
+ if message.severity == 'uncheck'
+ self.uncheck_count += 1
+ end
+ if message.severity == 'ok'
+ self.ok_count += 1
+ end
+ if message.severity == 'warning'
+ self.warning_count += 1
+ end
+ if message.severity == 'error'
+ self.error_count += 1
+ end
+ if message.severity == 'fatal'
+ self.fatal_count += 1
+ end
+ elsif message.level == 4
+ father3.add_child message
+ end
+ end
+ end
+
+ end
+
+
+end
diff --git a/app/models/file_validation_log_message.rb b/app/models/file_validation_log_message.rb
new file mode 100644
index 000000000..4159bb7d0
--- /dev/null
+++ b/app/models/file_validation_log_message.rb
@@ -0,0 +1,68 @@
+class FileValidationLogMessage < ActiveRecord::Base
+ belongs_to :file_validation
+ acts_as_list :scope => :file_validation
+
+ attr_accessor :children
+
+ 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 level
+ last_key=key.rpartition("|").last
+ if last_key == 'TooMuchDetails'
+ 4
+ else
+ last_key.count("_") + 1
+ end
+ end
+
+ def full_message
+ last_key=key.rpartition("|").last
+ I18n.translate last_key, arguments.symbolize_keys.merge(:scope => "file_validation_log_messages.messages").merge(:default => :undefined).merge(:key => last_key)
+ end
+
+ def label
+ last_key=key.rpartition("|").last
+ label = ""
+ last_key.split("_").each do |tag|
+ if (tag.start_with?("Test"))
+ label = tag.delete("Test")
+ else
+ label += "."+tag.delete("Sheet").delete("Step")
+ end
+ end
+ label
+ end
+
+ def add_child(child)
+ if self.children.nil?
+ self.children = Array.new
+ end
+ self.children << child
+ end
+
+end
diff --git a/app/models/help_page.rb b/app/models/help_page.rb
index 1207c5198..28d9e22b2 100644
--- a/app/models/help_page.rb
+++ b/app/models/help_page.rb
@@ -23,6 +23,29 @@ class HelpPage
self.content = $POSTMATCH
self.data.merge! YAML.load($1)
end
+
+ # workaround for special chars
+ self.content = self.content.gsub('é','&eacute;')
+ self.content = self.content.gsub('è','&egrave;')
+ self.content = self.content.gsub('à','&agrave;')
+ self.content = self.content.gsub('ù','&ugrave;')
+ self.content = self.content.gsub('É','&Eacute;')
+ self.content = self.content.gsub('È','&Egrave;')
+ self.content = self.content.gsub('Ê','&Ecirc;')
+ self.content = self.content.gsub('À','&Agrave;')
+ self.content = self.content.gsub('Ù','&Ugrave;')
+ self.content = self.content.gsub('â','&acirc;')
+ self.content = self.content.gsub('ê','&ecirc;')
+ self.content = self.content.gsub('ô','&ocirc;')
+ self.content = self.content.gsub('î','&icirc;')
+ self.content = self.content.gsub('û','&ucirc;')
+ self.content = self.content.gsub('ë','&eumlc;')
+ self.content = self.content.gsub('ï','&iuml;')
+ self.content = self.content.gsub('ç','&ccedil;')
+ self.content = self.content.gsub('oe','&oelig;')
+ self.content = self.content.gsub('<<','&laquo;')
+ self.content = self.content.gsub('>>','&raquo;')
+ self.content = self.content.gsub('°','&ordm;')
end
def method_missing(method, *arguments)
diff --git a/app/models/import.rb b/app/models/import.rb
index 662fe4a84..db59b41ac 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -17,18 +17,14 @@ class Import < ActiveRecord::Base
name = name.to_s
define_method(name) do
- self.options[name]
+ self.options and self.options[name]
end
define_method("#{name}=") do |prefix|
- self.options[name] = prefix
+ (self.options ||= {})[name] = prefix
end
end
- def options
- read_attribute(:options) || write_attribute(:options, {})
- end
-
def self.types
# if Rails.env.development? and subclasses.blank?
# Dir[File.expand_path("../*_import.rb", __FILE__)].each do |f|
diff --git a/app/models/referential.rb b/app/models/referential.rb
index 639e4de12..35d4e9141 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -68,7 +68,7 @@ end
Rails.application.config.after_initialize do
Chouette::ActiveRecord
- puts "patch Chouette::ActiveRecord (#{__FILE__})"
+
class Chouette::ActiveRecord
def referential
diff --git a/app/models/test_sheet_page.rb b/app/models/test_sheet_page.rb
new file mode 100644
index 000000000..da808f411
--- /dev/null
+++ b/app/models/test_sheet_page.rb
@@ -0,0 +1,64 @@
+class TestSheetPage
+
+ attr_accessor :slug, :content, :data
+
+ def initialize(slug)
+ @slug = slug
+ @data = {}.with_indifferent_access
+ end
+
+ def filename
+ "#{Rails.root}/app/views/test_sheet/#{slug}.html"
+ end
+
+ def exists?
+ File.exists? filename
+ end
+
+ def load
+ self.content = File.read(filename)
+ if self.slug == 'toc'
+ # workaround for special chars
+ self.content = self.content.gsub('é','&eacute;')
+ self.content = self.content.gsub('è','&egrave;')
+ self.content = self.content.gsub('à','&agrave;')
+ self.content = self.content.gsub('ù','&ugrave;')
+ self.content = self.content.gsub('É','&Eacute;')
+ self.content = self.content.gsub('È','&Egrave;')
+ self.content = self.content.gsub('Ê','&Ecirc;')
+ self.content = self.content.gsub('À','&Agrave;')
+ self.content = self.content.gsub('Ù','&Ugrave;')
+ self.content = self.content.gsub('â','&acirc;')
+ self.content = self.content.gsub('ê','&ecirc;')
+ self.content = self.content.gsub('ô','&ocirc;')
+ self.content = self.content.gsub('î','&icirc;')
+ self.content = self.content.gsub('û','&ucirc;')
+ self.content = self.content.gsub('ë','&eumlc;')
+ self.content = self.content.gsub('ï','&iuml;')
+ self.content = self.content.gsub('ç','&ccedil;')
+ self.content = self.content.gsub('oe','&oelig;')
+ self.content = self.content.gsub('<<','&laquo;')
+ self.content = self.content.gsub('>>','&raquo;')
+ self.content = self.content.gsub('°','&ordm;')
+ end
+ end
+
+ def method_missing(method, *arguments)
+ if arguments.empty? and data.has_key?(method)
+ data[method]
+ else
+ super
+ end
+ end
+
+ def self.find(slug)
+ new(slug).tap do |page|
+ if page.exists?
+ page.load
+ else
+ raise ActiveRecord::RecordNotFound
+ end
+ end
+ end
+
+end