Skip to content
Browse files

Switched to database for intermediate store

  • Loading branch information...
1 parent 94f1bd5 commit ae86592f4f1abaa8e68254747f830e96c8103ea6 Leo Hourvitz committed
View
74 app/controllers/importer_controller.rb
@@ -14,27 +14,30 @@ def index
end
def match
- # params
- file = params[:file]
- splitter = params[:splitter]
- wrapper = params[:wrapper]
- encoding = params[:encoding]
+ # Delete existing iip to ensure there can't be two iips for a user
+ print "params are ", params
+ ImportInProgress.delete_all(["user_id = ?",User.current.id])
+ # save import-in-progress data
+ iip = ImportInProgress.find_or_create_by_user_id(User.current.id)
+ iip.quote_char = params[:wrapper]
+ iip.col_sep = params[:splitter]
+ iip.encoding = params[:encoding]
+ iip.created = Time.new
+ iip.csv_data = params[:file].read
+ iip.save
- # save import file
- @original_filename = file.original_filename
- # Abuse of session system, but...
- session[:importer_csvdata] = file.read
- session[:importer_splitter] = splitter
- session[:importer_wrapper] = wrapper
- session[:importer_encoding] = encoding
+ # Put the timestamp in the params to detect
+ # users with two imports in progress
+ @import_timestamp = iip.created.strftime("%Y-%m-%d %H:%M:%S")
+ @original_filename = params[:file].original_filename
# display sample
sample_count = 5
i = 0
@samples = []
- FasterCSV.new(session[:importer_csvdata], {:headers=>true,
- :encoding=>encoding, :quote_char=>wrapper, :col_sep=>splitter}).each do |row|
+ FasterCSV.new(iip.csv_data, {:headers=>true,
+ :encoding=>iip.encoding, :quote_char=>iip.quote_char, :col_sep=>iip.col_sep}).each do |row|
@samples[i] = row
i += 1
@@ -60,12 +63,27 @@ def match
end
def result
- csvdata = session[:importer_csvdata]
- splitter = session[:importer_splitter]
- wrapper = session[:importer_wrapper]
- encoding = session[:importer_encoding]
- # Give the poor session system some relief
- session[:importer_csvdata] = ""
+ @handle_count = 0
+ @update_count = 0
+ @skip_count = 0
+ @failed_count = 0
+ @failed_issues = Hash.new
+ @affect_projects_issues = Hash.new
+
+ # Retrieve saved import data
+ iip = ImportInProgress.find_by_user_id(User.current.id)
+ if iip == nil
+ flash[:error] = "No import is currently in progress"
+ return
+ end
+ print "iip.created is ", iip.created
+ print "params[:import_timestamp] is ", params[:import_timestamp]
+ if iip.created.strftime("%Y-%m-%d %H:%M:%S") != params[:import_timestamp]
+ flash[:error] = "You seem to have started another import " \
+ "since starting this one. " \
+ "This import cannot be completed"
+ return
+ end
default_tracker = params[:default_tracker]
update_issue = params[:update_issue]
@@ -81,17 +99,11 @@ def result
return
end
- @handle_count = 0
- @update_count = 0
- @skip_count = 0
- @failed_count = 0
- @failed_issues = Hash.new
- @affect_projects_issues = Hash.new
-
# attrs_map is fields_map's invert
attrs_map = fields_map.invert
- FasterCSV.new(csvdata, {:headers=>true, :encoding=>encoding, :quote_char=>wrapper, :col_sep=>splitter}).each do |row|
+ FasterCSV.new(iip.csv_data, {:headers=>true, :encoding=>iip.encoding,
+ :quote_char=>iip.quote_char, :col_sep=>iip.col_sep}).each do |row|
project = Project.find_by_name(row[attrs_map["project"]])
tracker = Tracker.find_by_name(row[attrs_map["tracker"]])
@@ -216,6 +228,12 @@ def result
@failed_issues = @failed_issues.sort
@headers = @failed_issues[0][1].headers
end
+
+ # Clean up after ourselves
+ iip.delete
+
+ # Garbage prevention: clean up iips older than 3 days
+ ImportInProgress.delete_all(["created < ?",Time.new - 3*24*60*60])
end
private
View
5 app/models/import_in_progress.rb
@@ -0,0 +1,5 @@
+class ImportInProgress < ActiveRecord::Base
+ unloadable
+ belongs_to :user
+ belongs_to :project
+end
View
1 app/views/importer/match.html.erb
@@ -12,6 +12,7 @@
-->
<% form_tag({:action => 'result'}, {:multipart => true}) do %>
<%= hidden_field_tag 'project_id', @project.id %>
+ <%= hidden_field_tag 'import_timestamp', @import_timestamp %>
<fieldset class="box"><legend><%= l(:label_match_select) %></legend>
<% @headers.each do |column| %>
<% matched = ''
View
6 config/locales/en.yml
@@ -1,11 +1,11 @@
en:
label_import: "Import"
label_issue_importer: "Issue Importer"
- label_upload_notice: "Select a CSV file to import. Import file must have a header row."
+ label_upload_notice: "Select a CSV file to import. Import file must have a header row. Maximum size 4MB."
label_upload_format: "File format settings"
label_upload_encoding: "Encoding:"
- label_upload_splitter: "Field seperate char:"
- label_upload_wrapper: "Field wrap char:"
+ label_upload_splitter: "Field separator character:"
+ label_upload_wrapper: "Field quoting character:"
label_load_rules: "Load saved rules"
label_toplines: "Refer to top lines of {{value}}:"
View
34 config/locales/ja.yml
@@ -0,0 +1,34 @@
+ja:
+ label_import: "インポート"
+ label_issue_importer: "チケットのインポート"
+ label_upload_notice: "CSVファイルを選択する。ヘーダー行が要る。4MBのサイズの限定がある。"
+ label_upload_format: "ファイルフォーマットのオプション"
+ label_upload_encoding: "エンコーディング:"
+ label_upload_splitter: "フィールド区切り文字:"
+ label_upload_wrapper: "引用文字:"
+
+ label_load_rules: "保存されたルールを読み込む"
+ label_toplines: "{{value}}の上の行を参考するÔºö"
+ label_match_columns: "マッチされたコラム:"
+ label_match_select: "マッチするフィールドを選択:"
+ label_import_rule: "インポートのルール"
+ label_default_tracker: "トラッカーのディフォルト:"
+ label_update_issue: "存在があるチケットを更新"
+ label_journal_field: "メモとして入るフィールドを選択:"
+ label_unique_field: "チケットをマッチするフィールドを選択:"
+ label_update_other_project: "他のプロジェクトのチケットでも更新"
+ label_ignore_non_exist: "チケットの存在がなくても無視"
+ label_rule_name: "ルール舞を入れる:"
+
+ label_import_result: "インポートの結果"
+ label_result_notice: "{{handle_count}}行整理された。{{success_count}}行がうまく行った。"
+ label_result_projects: "関われるプロジェクト:"
+ label_result_issues: "{{count}}行"
+ label_result_failed: "{{count}}行が失敗:"
+
+ option_ignore: "無視"
+
+ button_upload: "ファイルをアップ"
+ button_submit: "確認"
+ button_save_rules_and_submit: "マッチルールを保存して確認"
+
View
16 db/migrate/001_create_import_in_progresses.rb
@@ -0,0 +1,16 @@
+class CreateImportInProgresses < ActiveRecord::Migration
+ def self.up
+ create_table :import_in_progresses do |t|
+ t.column :user_id, :integer, :null => false
+ t.string :quote_char, :limit => 8
+ t.string :col_sep, :limit => 8
+ t.string :encoding, :limit => 64
+ t.column :created, :datetime
+ t.column :csv_data, :binary, :limit => 4096*1024
+ end
+ end
+
+ def self.down
+ drop_table :import_in_progresses
+ end
+end
View
2 init.rb
@@ -4,7 +4,7 @@
name 'Issue Importer'
author 'Martin Liu'
description 'Issue import plugin for Redmine.'
- version '0.4'
+ version '0.7'
project_module :importer do
permission :import, :importer => :index
View
11 test/fixtures/import_in_progresses.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+one:
+ id: 1
+ csv_data:
+ user:
+ created: 2010-10-09 20:40:03
+two:
+ id: 2
+ csv_data:
+ user:
+ created: 2010-10-09 20:40:03
View
10 test/unit/import_in_progress_test.rb
@@ -0,0 +1,10 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class ImportInProgressTest < ActiveSupport::TestCase
+ fixtures :import_in_progresses
+
+ # Replace this with your real tests.
+ def test_truth
+ assert true
+ end
+end

0 comments on commit ae86592

Please sign in to comment.
Something went wrong with that request. Please try again.