@@ -1,120 +1,121 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'csv' |
|
19 | 19 | |
|
20 | 20 | class ImportsController < ApplicationController |
|
21 | 21 | |
|
22 | 22 | before_filter :find_import, :only => [:show, :settings, :mapping, :run] |
|
23 | 23 | before_filter :authorize_global |
|
24 | 24 | |
|
25 | 25 | helper :issues |
|
26 | helper :queries | |
|
26 | 27 | |
|
27 | 28 | def new |
|
28 | 29 | end |
|
29 | 30 | |
|
30 | 31 | def create |
|
31 | 32 | @import = IssueImport.new |
|
32 | 33 | @import.user = User.current |
|
33 | 34 | @import.file = params[:file] |
|
34 | 35 | @import.set_default_settings |
|
35 | 36 | |
|
36 | 37 | if @import.save |
|
37 | 38 | redirect_to import_settings_path(@import) |
|
38 | 39 | else |
|
39 | 40 | render :action => 'new' |
|
40 | 41 | end |
|
41 | 42 | end |
|
42 | 43 | |
|
43 | 44 | def show |
|
44 | 45 | end |
|
45 | 46 | |
|
46 | 47 | def settings |
|
47 | 48 | if request.post? && @import.parse_file |
|
48 | 49 | redirect_to import_mapping_path(@import) |
|
49 | 50 | end |
|
50 | 51 | |
|
51 | 52 | rescue CSV::MalformedCSVError => e |
|
52 | 53 | flash.now[:error] = l(:error_invalid_csv_file_or_settings) |
|
53 | 54 | rescue ArgumentError, Encoding::InvalidByteSequenceError => e |
|
54 | 55 | flash.now[:error] = l(:error_invalid_file_encoding, :encoding => ERB::Util.h(@import.settings['encoding'])) |
|
55 | 56 | rescue SystemCallError => e |
|
56 | 57 | flash.now[:error] = l(:error_can_not_read_import_file) |
|
57 | 58 | end |
|
58 | 59 | |
|
59 | 60 | def mapping |
|
60 | 61 | @custom_fields = @import.mappable_custom_fields |
|
61 | 62 | |
|
62 | 63 | if request.post? |
|
63 | 64 | respond_to do |format| |
|
64 | 65 | format.html { |
|
65 | 66 | if params[:previous] |
|
66 | 67 | redirect_to import_settings_path(@import) |
|
67 | 68 | else |
|
68 | 69 | redirect_to import_run_path(@import) |
|
69 | 70 | end |
|
70 | 71 | } |
|
71 | 72 | format.js # updates mapping form on project or tracker change |
|
72 | 73 | end |
|
73 | 74 | end |
|
74 | 75 | end |
|
75 | 76 | |
|
76 | 77 | def run |
|
77 | 78 | if request.post? |
|
78 | 79 | @current = @import.run( |
|
79 | 80 | :max_items => max_items_per_request, |
|
80 | 81 | :max_time => 10.seconds |
|
81 | 82 | ) |
|
82 | 83 | respond_to do |format| |
|
83 | 84 | format.html { |
|
84 | 85 | if @import.finished? |
|
85 | 86 | redirect_to import_path(@import) |
|
86 | 87 | else |
|
87 | 88 | redirect_to import_run_path(@import) |
|
88 | 89 | end |
|
89 | 90 | } |
|
90 | 91 | format.js |
|
91 | 92 | end |
|
92 | 93 | end |
|
93 | 94 | end |
|
94 | 95 | |
|
95 | 96 | private |
|
96 | 97 | |
|
97 | 98 | def find_import |
|
98 | 99 | @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first |
|
99 | 100 | if @import.nil? |
|
100 | 101 | render_404 |
|
101 | 102 | return |
|
102 | 103 | elsif @import.finished? && action_name != 'show' |
|
103 | 104 | redirect_to import_path(@import) |
|
104 | 105 | return |
|
105 | 106 | end |
|
106 | 107 | update_from_params if request.post? |
|
107 | 108 | end |
|
108 | 109 | |
|
109 | 110 | def update_from_params |
|
110 | 111 | if params[:import_settings].is_a?(Hash) |
|
111 | 112 | @import.settings ||= {} |
|
112 | 113 | @import.settings.merge!(params[:import_settings]) |
|
113 | 114 | @import.save! |
|
114 | 115 | end |
|
115 | 116 | end |
|
116 | 117 | |
|
117 | 118 | def max_items_per_request |
|
118 | 119 | 5 |
|
119 | 120 | end |
|
120 | 121 | end |
General Comments 0
You need to be logged in to leave comments.
Login now