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