##// END OF EJS Templates
Add column `estimated_hours` for CSV import (#21867)....
Jean-Philippe Lang -
r14749:81b681764ab7
parent child
Show More
@@ -1,151 +1,154
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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 class IssueImport < Import
18 class IssueImport < Import
19
19
20 # Returns the objects that were imported
20 # Returns the objects that were imported
21 def saved_objects
21 def saved_objects
22 object_ids = saved_items.pluck(:obj_id)
22 object_ids = saved_items.pluck(:obj_id)
23 objects = Issue.where(:id => object_ids).order(:id).preload(:tracker, :priority, :status)
23 objects = Issue.where(:id => object_ids).order(:id).preload(:tracker, :priority, :status)
24 end
24 end
25
25
26 # Returns a scope of projects that user is allowed to
26 # Returns a scope of projects that user is allowed to
27 # import issue to
27 # import issue to
28 def allowed_target_projects
28 def allowed_target_projects
29 Project.allowed_to(user, :import_issues)
29 Project.allowed_to(user, :import_issues)
30 end
30 end
31
31
32 def project
32 def project
33 project_id = mapping['project_id'].to_i
33 project_id = mapping['project_id'].to_i
34 allowed_target_projects.find_by_id(project_id) || allowed_target_projects.first
34 allowed_target_projects.find_by_id(project_id) || allowed_target_projects.first
35 end
35 end
36
36
37 # Returns a scope of trackers that user is allowed to
37 # Returns a scope of trackers that user is allowed to
38 # import issue to
38 # import issue to
39 def allowed_target_trackers
39 def allowed_target_trackers
40 project.trackers
40 project.trackers
41 end
41 end
42
42
43 def tracker
43 def tracker
44 tracker_id = mapping['tracker_id'].to_i
44 tracker_id = mapping['tracker_id'].to_i
45 allowed_target_trackers.find_by_id(tracker_id) || allowed_target_trackers.first
45 allowed_target_trackers.find_by_id(tracker_id) || allowed_target_trackers.first
46 end
46 end
47
47
48 # Returns true if missing categories should be created during the import
48 # Returns true if missing categories should be created during the import
49 def create_categories?
49 def create_categories?
50 user.allowed_to?(:manage_categories, project) &&
50 user.allowed_to?(:manage_categories, project) &&
51 mapping['create_categories'] == '1'
51 mapping['create_categories'] == '1'
52 end
52 end
53
53
54 # Returns true if missing versions should be created during the import
54 # Returns true if missing versions should be created during the import
55 def create_versions?
55 def create_versions?
56 user.allowed_to?(:manage_versions, project) &&
56 user.allowed_to?(:manage_versions, project) &&
57 mapping['create_versions'] == '1'
57 mapping['create_versions'] == '1'
58 end
58 end
59
59
60 private
60 private
61
61
62 def build_object(row)
62 def build_object(row)
63 issue = Issue.new
63 issue = Issue.new
64 issue.author = user
64 issue.author = user
65 issue.notify = false
65 issue.notify = false
66
66
67 attributes = {
67 attributes = {
68 'project_id' => mapping['project_id'],
68 'project_id' => mapping['project_id'],
69 'tracker_id' => mapping['tracker_id'],
69 'tracker_id' => mapping['tracker_id'],
70 'subject' => row_value(row, 'subject'),
70 'subject' => row_value(row, 'subject'),
71 'description' => row_value(row, 'description')
71 'description' => row_value(row, 'description')
72 }
72 }
73 issue.send :safe_attributes=, attributes, user
73 issue.send :safe_attributes=, attributes, user
74
74
75 attributes = {}
75 attributes = {}
76 if priority_name = row_value(row, 'priority')
76 if priority_name = row_value(row, 'priority')
77 if priority_id = IssuePriority.active.named(priority_name).first.try(:id)
77 if priority_id = IssuePriority.active.named(priority_name).first.try(:id)
78 attributes['priority_id'] = priority_id
78 attributes['priority_id'] = priority_id
79 end
79 end
80 end
80 end
81 if issue.project && category_name = row_value(row, 'category')
81 if issue.project && category_name = row_value(row, 'category')
82 if category = issue.project.issue_categories.named(category_name).first
82 if category = issue.project.issue_categories.named(category_name).first
83 attributes['category_id'] = category.id
83 attributes['category_id'] = category.id
84 elsif create_categories?
84 elsif create_categories?
85 category = issue.project.issue_categories.build
85 category = issue.project.issue_categories.build
86 category.name = category_name
86 category.name = category_name
87 if category.save
87 if category.save
88 attributes['category_id'] = category.id
88 attributes['category_id'] = category.id
89 end
89 end
90 end
90 end
91 end
91 end
92 if assignee_name = row_value(row, 'assigned_to')
92 if assignee_name = row_value(row, 'assigned_to')
93 if assignee = Principal.detect_by_keyword(issue.assignable_users, assignee_name)
93 if assignee = Principal.detect_by_keyword(issue.assignable_users, assignee_name)
94 attributes['assigned_to_id'] = assignee.id
94 attributes['assigned_to_id'] = assignee.id
95 end
95 end
96 end
96 end
97 if issue.project && version_name = row_value(row, 'fixed_version')
97 if issue.project && version_name = row_value(row, 'fixed_version')
98 if version = issue.project.versions.named(version_name).first
98 if version = issue.project.versions.named(version_name).first
99 attributes['fixed_version_id'] = version.id
99 attributes['fixed_version_id'] = version.id
100 elsif create_versions?
100 elsif create_versions?
101 version = issue.project.versions.build
101 version = issue.project.versions.build
102 version.name = version_name
102 version.name = version_name
103 if version.save
103 if version.save
104 attributes['fixed_version_id'] = version.id
104 attributes['fixed_version_id'] = version.id
105 end
105 end
106 end
106 end
107 end
107 end
108 if is_private = row_value(row, 'is_private')
108 if is_private = row_value(row, 'is_private')
109 if yes?(is_private)
109 if yes?(is_private)
110 attributes['is_private'] = '1'
110 attributes['is_private'] = '1'
111 end
111 end
112 end
112 end
113 if parent_issue_id = row_value(row, 'parent_issue_id')
113 if parent_issue_id = row_value(row, 'parent_issue_id')
114 if parent_issue_id =~ /\A(#)?(\d+)\z/
114 if parent_issue_id =~ /\A(#)?(\d+)\z/
115 parent_issue_id = $2
115 parent_issue_id = $2
116 if $1
116 if $1
117 attributes['parent_issue_id'] = parent_issue_id
117 attributes['parent_issue_id'] = parent_issue_id
118 elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
118 elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id)
119 attributes['parent_issue_id'] = issue_id
119 attributes['parent_issue_id'] = issue_id
120 end
120 end
121 else
121 else
122 attributes['parent_issue_id'] = parent_issue_id
122 attributes['parent_issue_id'] = parent_issue_id
123 end
123 end
124 end
124 end
125 if start_date = row_date(row, 'start_date')
125 if start_date = row_date(row, 'start_date')
126 attributes['start_date'] = start_date
126 attributes['start_date'] = start_date
127 end
127 end
128 if due_date = row_date(row, 'due_date')
128 if due_date = row_date(row, 'due_date')
129 attributes['due_date'] = due_date
129 attributes['due_date'] = due_date
130 end
130 end
131 if estimated_hours = row_value(row, 'estimated_hours')
132 attributes['estimated_hours'] = estimated_hours
133 end
131 if done_ratio = row_value(row, 'done_ratio')
134 if done_ratio = row_value(row, 'done_ratio')
132 attributes['done_ratio'] = done_ratio
135 attributes['done_ratio'] = done_ratio
133 end
136 end
134
137
135 attributes['custom_field_values'] = issue.custom_field_values.inject({}) do |h, v|
138 attributes['custom_field_values'] = issue.custom_field_values.inject({}) do |h, v|
136 value = case v.custom_field.field_format
139 value = case v.custom_field.field_format
137 when 'date'
140 when 'date'
138 row_date(row, "cf_#{v.custom_field.id}")
141 row_date(row, "cf_#{v.custom_field.id}")
139 else
142 else
140 row_value(row, "cf_#{v.custom_field.id}")
143 row_value(row, "cf_#{v.custom_field.id}")
141 end
144 end
142 if value
145 if value
143 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, issue)
146 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, issue)
144 end
147 end
145 h
148 h
146 end
149 end
147
150
148 issue.send :safe_attributes=, attributes, user
151 issue.send :safe_attributes=, attributes, user
149 issue
152 issue
150 end
153 end
151 end
154 end
@@ -1,82 +1,86
1 <div class="splitcontent">
1 <div class="splitcontent">
2 <div class="splitcontentleft">
2 <div class="splitcontentleft">
3 <p>
3 <p>
4 <label><%= l(:label_project) %></label>
4 <label><%= l(:label_project) %></label>
5 <%= select_tag 'import_settings[mapping][project_id]',
5 <%= select_tag 'import_settings[mapping][project_id]',
6 options_for_select(project_tree_options_for_select(@import.allowed_target_projects, :selected => @import.project)),
6 options_for_select(project_tree_options_for_select(@import.allowed_target_projects, :selected => @import.project)),
7 :id => 'issue_project_id' %>
7 :id => 'issue_project_id' %>
8 </p>
8 </p>
9 <p>
9 <p>
10 <label><%= l(:label_tracker) %></label>
10 <label><%= l(:label_tracker) %></label>
11 <%= select_tag 'import_settings[mapping][tracker_id]',
11 <%= select_tag 'import_settings[mapping][tracker_id]',
12 options_for_select(@import.allowed_target_trackers.sorted.map {|t| [t.name, t.id]}, @import.tracker.try(:id)),
12 options_for_select(@import.allowed_target_trackers.sorted.map {|t| [t.name, t.id]}, @import.tracker.try(:id)),
13 :id => 'issue_tracker_id' %>
13 :id => 'issue_tracker_id' %>
14 </p>
14 </p>
15 <p>
15 <p>
16 <label><%= l(:field_subject) %></label>
16 <label><%= l(:field_subject) %></label>
17 <%= mapping_select_tag @import, 'subject', :required => true %>
17 <%= mapping_select_tag @import, 'subject', :required => true %>
18 </p>
18 </p>
19 <p>
19 <p>
20 <label><%= l(:field_description) %></label>
20 <label><%= l(:field_description) %></label>
21 <%= mapping_select_tag @import, 'description' %>
21 <%= mapping_select_tag @import, 'description' %>
22 </p>
22 </p>
23 <p>
23 <p>
24 <label><%= l(:field_priority) %></label>
24 <label><%= l(:field_priority) %></label>
25 <%= mapping_select_tag @import, 'priority' %>
25 <%= mapping_select_tag @import, 'priority' %>
26 </p>
26 </p>
27 <p>
27 <p>
28 <label><%= l(:field_category) %></label>
28 <label><%= l(:field_category) %></label>
29 <%= mapping_select_tag @import, 'category' %>
29 <%= mapping_select_tag @import, 'category' %>
30 <% if User.current.allowed_to?(:manage_categories, @import.project) %>
30 <% if User.current.allowed_to?(:manage_categories, @import.project) %>
31 <label class="block">
31 <label class="block">
32 <%= check_box_tag 'import_settings[mapping][create_categories]', '1', @import.create_categories? %>
32 <%= check_box_tag 'import_settings[mapping][create_categories]', '1', @import.create_categories? %>
33 <%= l(:label_create_missing_values) %>
33 <%= l(:label_create_missing_values) %>
34 </label>
34 </label>
35 <% end %>
35 <% end %>
36 </p>
36 </p>
37 <p>
37 <p>
38 <label><%= l(:field_assigned_to) %></label>
38 <label><%= l(:field_assigned_to) %></label>
39 <%= mapping_select_tag @import, 'assigned_to' %>
39 <%= mapping_select_tag @import, 'assigned_to' %>
40 </p>
40 </p>
41 <p>
41 <p>
42 <label><%= l(:field_fixed_version) %></label>
42 <label><%= l(:field_fixed_version) %></label>
43 <%= mapping_select_tag @import, 'fixed_version' %>
43 <%= mapping_select_tag @import, 'fixed_version' %>
44 <% if User.current.allowed_to?(:manage_versions, @import.project) %>
44 <% if User.current.allowed_to?(:manage_versions, @import.project) %>
45 <label class="block">
45 <label class="block">
46 <%= check_box_tag 'import_settings[mapping][create_versions]', '1', @import.create_versions? %>
46 <%= check_box_tag 'import_settings[mapping][create_versions]', '1', @import.create_versions? %>
47 <%= l(:label_create_missing_values) %>
47 <%= l(:label_create_missing_values) %>
48 </label>
48 </label>
49 <% end %>
49 <% end %>
50 </p>
50 </p>
51 <% @custom_fields.each do |field| %>
51 <% @custom_fields.each do |field| %>
52 <p>
52 <p>
53 <label><%= field.name %></label>
53 <label><%= field.name %></label>
54 <%= mapping_select_tag @import, "cf_#{field.id}" %>
54 <%= mapping_select_tag @import, "cf_#{field.id}" %>
55 </p>
55 </p>
56 <% end %>
56 <% end %>
57 </div>
57 </div>
58
58
59 <div class="splitcontentright">
59 <div class="splitcontentright">
60 <p>
60 <p>
61 <label><%= l(:field_is_private) %></label>
61 <label><%= l(:field_is_private) %></label>
62 <%= mapping_select_tag @import, 'is_private' %>
62 <%= mapping_select_tag @import, 'is_private' %>
63 </p>
63 </p>
64 <p>
64 <p>
65 <label><%= l(:field_parent_issue) %></label>
65 <label><%= l(:field_parent_issue) %></label>
66 <%= mapping_select_tag @import, 'parent_issue_id' %>
66 <%= mapping_select_tag @import, 'parent_issue_id' %>
67 </p>
67 </p>
68 <p>
68 <p>
69 <label><%= l(:field_start_date) %></label>
69 <label><%= l(:field_start_date) %></label>
70 <%= mapping_select_tag @import, 'start_date' %>
70 <%= mapping_select_tag @import, 'start_date' %>
71 </p>
71 </p>
72 <p>
72 <p>
73 <label><%= l(:field_due_date) %></label>
73 <label><%= l(:field_due_date) %></label>
74 <%= mapping_select_tag @import, 'due_date' %>
74 <%= mapping_select_tag @import, 'due_date' %>
75 </p>
75 </p>
76 <p>
76 <p>
77 <label><%= l(:field_estimated_hours) %></label>
78 <%= mapping_select_tag @import, 'estimated_hours' %>
79 </p>
80 <p>
77 <label><%= l(:field_done_ratio) %></label>
81 <label><%= l(:field_done_ratio) %></label>
78 <%= mapping_select_tag @import, 'done_ratio' %>
82 <%= mapping_select_tag @import, 'done_ratio' %>
79 </p>
83 </p>
80 </div>
84 </div>
81 </div>
85 </div>
82
86
@@ -1,4 +1,4
1 priority;subject;description;start_date;due_date;parent;private;progress;custom;version;category;user
1 priority;subject;description;start_date;due_date;parent;private;progress;custom;version;category;user;estimated_hours
2 High;First;First description;2015-07-08;2015-08-25;;no;;PostgreSQL;;New category;dlopper
2 High;First;First description;2015-07-08;2015-08-25;;no;;PostgreSQL;;New category;dlopper;1
3 Normal;Child 1;Child description;;;1;yes;10;MySQL;2.0;New category;
3 Normal;Child 1;Child description;;;1;yes;10;MySQL;2.0;New category;;2
4 Normal;Child of existing issue;Child description;;;#2;no;20;;2.1;Printing;
4 Normal;Child of existing issue;Child description;;;#2;no;20;;2.1;Printing;;3
General Comments 0
You need to be logged in to leave comments. Login now