better validation for bulk update requests
This commit is contained in:
@@ -10,6 +10,7 @@ class BulkUpdateRequest < ActiveRecord::Base
|
||||
validates_inclusion_of :status, :in => %w(pending approved rejected)
|
||||
validate :script_formatted_correctly
|
||||
validate :forum_topic_id_not_invalid
|
||||
validate :validate_script
|
||||
attr_accessible :user_id, :forum_topic_id, :script, :title, :reason
|
||||
attr_accessible :status, :as => [:admin]
|
||||
before_validation :initialize_attributes, :on => :create
|
||||
@@ -130,7 +131,6 @@ class BulkUpdateRequest < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def update_forum_topic_for_approve
|
||||
if forum_topic
|
||||
forum_topic.posts.create(
|
||||
@@ -150,4 +150,15 @@ class BulkUpdateRequest < ActiveRecord::Base
|
||||
def normalize_text
|
||||
self.script = script.downcase
|
||||
end
|
||||
|
||||
def validate_script
|
||||
begin
|
||||
AliasAndImplicationImporter.new(script, forum_topic_id, "1").validate!
|
||||
rescue RuntimeError => e
|
||||
self.errors[:base] = e.message
|
||||
return false
|
||||
end
|
||||
|
||||
errors.empty?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,6 +8,8 @@ class TagAlias < ActiveRecord::Base
|
||||
validates_uniqueness_of :antecedent_name
|
||||
validate :absence_of_transitive_relation
|
||||
validate :antecedent_and_consequent_are_different
|
||||
# validate :consequent_has_wiki_page
|
||||
# validate :mininum_antecedent_count
|
||||
belongs_to :creator, :class_name => "User"
|
||||
belongs_to :forum_topic
|
||||
attr_accessible :antecedent_name, :consequent_name, :forum_topic_id, :status
|
||||
@@ -139,7 +141,7 @@ class TagAlias < ActiveRecord::Base
|
||||
# We don't want a -> b && b -> c chains if the b -> c alias was created first.
|
||||
# If the a -> b alias was created first, the new one will be allowed and the old one will be moved automatically instead.
|
||||
if self.class.active.exists?(["antecedent_name = ?", consequent_name])
|
||||
self.errors[:base] << "Tag alias can not create a transitive relation with another tag alias"
|
||||
self.errors[:base] << "A tag alias for #{consequent_name} already exists"
|
||||
false
|
||||
end
|
||||
end
|
||||
@@ -290,6 +292,23 @@ class TagAlias < ActiveRecord::Base
|
||||
destroy
|
||||
end
|
||||
|
||||
def consequent_has_wiki_page
|
||||
return if !Danbooru.config.strict_tag_requirements
|
||||
|
||||
unless WikiPage.titled(consequent_name).exists?
|
||||
self.errors[:base] = "The #{consequent_name} tag needs a corresponding wiki page"
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def mininum_antecedent_count
|
||||
return if !Danbooru.config.strict_tag_requirements
|
||||
|
||||
unless Post.fast_count(antecedent_name) >= 50
|
||||
self.errors[:base] = "The #{antecedent_name} tag must have at least 50 posts for an alias to be created"
|
||||
end
|
||||
end
|
||||
|
||||
def self.update_cached_post_counts_for_all
|
||||
TagAlias.without_timeout do
|
||||
execute_sql("UPDATE tag_aliases SET post_count = tags.post_count FROM tags WHERE tags.name = tag_aliases.consequent_name")
|
||||
|
||||
@@ -12,6 +12,7 @@ class TagImplication < ActiveRecord::Base
|
||||
validate :antecedent_is_not_aliased
|
||||
validate :consequent_is_not_aliased
|
||||
validate :antecedent_and_consequent_are_different
|
||||
# validate :wiki_pages_present
|
||||
attr_accessible :antecedent_name, :consequent_name, :descendant_names, :forum_topic_id, :status, :forum_topic
|
||||
|
||||
module DescendantMethods
|
||||
@@ -256,6 +257,20 @@ class TagImplication < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def wiki_pages_present
|
||||
return if !Danbooru.config.strict_tag_requirements
|
||||
|
||||
unless WikiPage.titled(consequent_name).exists?
|
||||
self.errors[:base] = "The #{consequent_name} tag needs a corresponding wiki page"
|
||||
return false
|
||||
end
|
||||
|
||||
unless WikiPage.titled(antecedent_name).exists?
|
||||
self.errors[:base] = "The #{antecedent_name} tag needs a corresponding wiki page"
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def reject!
|
||||
update_forum_topic_for_reject
|
||||
destroy
|
||||
|
||||
Reference in New Issue
Block a user