added alias and implication importer
This commit is contained in:
@@ -0,0 +1,18 @@
|
|||||||
|
module Admin
|
||||||
|
class AliasAndImplicationImportsController < ApplicationController
|
||||||
|
before_filter :admin_only
|
||||||
|
|
||||||
|
def new
|
||||||
|
end
|
||||||
|
|
||||||
|
def create
|
||||||
|
@importer = AliasAndImplicationImporter.new(params[:batch][:text], params[:batch][:forum_id])
|
||||||
|
@importer.process!
|
||||||
|
flash[:notice] = "Import queued"
|
||||||
|
redirect_to new_admin_alias_and_implication_import_path
|
||||||
|
rescue => x
|
||||||
|
flash[:notice] = x.to_s
|
||||||
|
redirect_to new_admin_alias_and_implication_import_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
65
app/logical/alias_and_implication_importer.rb
Normal file
65
app/logical/alias_and_implication_importer.rb
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
class AliasAndImplicationImporter
|
||||||
|
attr_accessor :text, :commands, :forum_id
|
||||||
|
|
||||||
|
def initialize(text, forum_id)
|
||||||
|
@forum_id = forum_id
|
||||||
|
@text = text
|
||||||
|
end
|
||||||
|
|
||||||
|
def process!
|
||||||
|
tokens = tokenize(text)
|
||||||
|
parse(tokens)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def tokenize(text)
|
||||||
|
text.gsub!(/^\s+/, "")
|
||||||
|
text.gsub!(/\s+$/, "")
|
||||||
|
text.gsub!(/ {2,}/, " ")
|
||||||
|
text.split(/\r\n|\r|\n/).map do |line|
|
||||||
|
if line =~ /create alias (\S+) -> (\S+)/i
|
||||||
|
[:create_alias, $1, $2]
|
||||||
|
elsif line =~ /create implication (\S+) -> (\S+)/i
|
||||||
|
[:create_implication, $1, $2]
|
||||||
|
elsif line =~ /remove alias (\S+) -> (\S+)/i
|
||||||
|
[:remove_alias, $1, $2]
|
||||||
|
elsif line =~ /remove implication (\S+) -> (\S+)/i
|
||||||
|
[:remove_implication, $1, $2]
|
||||||
|
elsif line.empty?
|
||||||
|
# do nothing
|
||||||
|
else
|
||||||
|
raise "Unparseable line: #{line}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse(tokens)
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
tokens.map do |token|
|
||||||
|
case token[0]
|
||||||
|
when :create_alias
|
||||||
|
tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
|
||||||
|
tag_alias.delay.process!
|
||||||
|
|
||||||
|
when :create_implication
|
||||||
|
tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2])
|
||||||
|
tag_implication.delay.process!
|
||||||
|
|
||||||
|
when :remove_alias
|
||||||
|
tag_alias = TagAlias.where("antecedent_name = ?", token[1]).first
|
||||||
|
raise "Alias for #{token[1]} not found" if tag_alias.nil?
|
||||||
|
tag_alias.destroy
|
||||||
|
|
||||||
|
when :remove_implication
|
||||||
|
tag_implication = TagImplication.where("antecedent_name = ? and consequent_name = ?", token[1], token[2]).first
|
||||||
|
raise "Implication for #{token[1]} not found" if tag_implication.nil?
|
||||||
|
tag_implication.destroy
|
||||||
|
|
||||||
|
else
|
||||||
|
raise "Unknown token: #{token[0]}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
36
app/views/admin/alias_and_implication_imports/new.html.erb
Normal file
36
app/views/admin/alias_and_implication_imports/new.html.erb
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
<div id="c-admin-alias-and-implication-imports">
|
||||||
|
<div id="a-new">
|
||||||
|
<h1>Alias & Implication Import</h1>
|
||||||
|
|
||||||
|
<%= form_tag(admin_alias_and_implication_import_path, :class => "simple_form") do %>
|
||||||
|
<pre>
|
||||||
|
Use the following format:
|
||||||
|
|
||||||
|
remove alias aaa -> bbb
|
||||||
|
remove implication aaa -> bbb
|
||||||
|
create alias aaa -> bbb
|
||||||
|
create implication aaa -> bbb
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<div class="input">
|
||||||
|
<label for="batch_text">
|
||||||
|
Script
|
||||||
|
</label>
|
||||||
|
<%= text_area "batch", "text", :size => "60x20" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="input">
|
||||||
|
<label for="batch_forum_id">
|
||||||
|
Forum ID
|
||||||
|
</label>
|
||||||
|
<%= text_field "batch", "forum_id" %>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<%= submit_tag %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<% content_for(:page_title) do %>
|
||||||
|
Alias & Implication Import - <%= Danbooru.config.app_name %>
|
||||||
|
<% end %>
|
||||||
@@ -83,6 +83,7 @@
|
|||||||
<li><%= link_to("Janitor Trials", janitor_trials_path) %></li>
|
<li><%= link_to("Janitor Trials", janitor_trials_path) %></li>
|
||||||
<li><%= link_to("IP Bans", ip_bans_path) %></li>
|
<li><%= link_to("IP Bans", ip_bans_path) %></li>
|
||||||
<li><%= link_to("News Updates", news_updates_path) %></li>
|
<li><%= link_to("News Updates", news_updates_path) %></li>
|
||||||
|
<li><%= link_to("Alias & Implication Import", new_admin_alias_and_implication_import_path) %></li>
|
||||||
</ul>
|
</ul>
|
||||||
<% end %>
|
<% end %>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
Danbooru::Application.routes.draw do
|
Danbooru::Application.routes.draw do
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
resources :users, :only => [:edit, :update]
|
resources :users, :only => [:edit, :update]
|
||||||
|
resource :alias_and_implication_import, :only => [:new, :create]
|
||||||
end
|
end
|
||||||
namespace :moderator do
|
namespace :moderator do
|
||||||
resource :dashboard, :only => [:show]
|
resource :dashboard, :only => [:show]
|
||||||
|
|||||||
42
test/unit/alias_and_implication_importer_test.rb
Normal file
42
test/unit/alias_and_implication_importer_test.rb
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class AliasAndImplicationImporterTest < ActiveSupport::TestCase
|
||||||
|
context "The alias and implication importer" do
|
||||||
|
setup do
|
||||||
|
@user = Factory.create(:user)
|
||||||
|
CurrentUser.user = @user
|
||||||
|
CurrentUser.ip_addr = "127.0.0.1"
|
||||||
|
end
|
||||||
|
|
||||||
|
teardown do
|
||||||
|
CurrentUser.user = nil
|
||||||
|
CurrentUser.ip_addr = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context "given a valid list" do
|
||||||
|
setup do
|
||||||
|
@list = "create alias abc -> def\ncreate implication aaa -> bbb\n"
|
||||||
|
@importer = AliasAndImplicationImporter.new(@list, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "process it" do
|
||||||
|
assert_difference("Delayed::Job.count", 2) do
|
||||||
|
@importer.process!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "given a list with a logic error" do
|
||||||
|
setup do
|
||||||
|
@list = "remove alias zzz -> yyy\n"
|
||||||
|
@importer = AliasAndImplicationImporter.new(@list, nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
should "throw an exception" do
|
||||||
|
assert_raises(RuntimeError) do
|
||||||
|
@importer.process!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user