##// END OF EJS Templates
Use same logic for finding user when importing a file (#950)....
Jean-Philippe Lang -
r14119:212f7ffe588e
parent child
Show More
@@ -1,151 +1,151
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 = issue.assignable_users.detect {|u| u.name.downcase == assignee_name.downcase}
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 done_ratio = row_value(row, 'done_ratio')
131 if done_ratio = row_value(row, 'done_ratio')
132 attributes['done_ratio'] = done_ratio
132 attributes['done_ratio'] = done_ratio
133 end
133 end
134
134
135 attributes['custom_field_values'] = issue.custom_field_values.inject({}) do |h, v|
135 attributes['custom_field_values'] = issue.custom_field_values.inject({}) do |h, v|
136 value = case v.custom_field.field_format
136 value = case v.custom_field.field_format
137 when 'date'
137 when 'date'
138 row_date(row, "cf_#{v.custom_field.id}")
138 row_date(row, "cf_#{v.custom_field.id}")
139 else
139 else
140 row_value(row, "cf_#{v.custom_field.id}")
140 row_value(row, "cf_#{v.custom_field.id}")
141 end
141 end
142 if value
142 if value
143 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, issue)
143 h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, issue)
144 end
144 end
145 h
145 h
146 end
146 end
147
147
148 issue.send :safe_attributes=, attributes, user
148 issue.send :safe_attributes=, attributes, user
149 issue
149 issue
150 end
150 end
151 end
151 end
@@ -1,4 +1,4
1 priority;subject;description;start_date;due_date;parent;private;progress;custom;version;category
1 priority;subject;description;start_date;due_date;parent;private;progress;custom;version;category;user
2 High;First;First description;2015-07-08;2015-08-25;;no;;PostgreSQL;;New category
2 High;First;First description;2015-07-08;2015-08-25;;no;;PostgreSQL;;New category;dlopper
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;
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;
@@ -1,113 +1,122
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 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class IssueImportTest < ActiveSupport::TestCase
20 class IssueImportTest < ActiveSupport::TestCase
21 fixtures :projects, :enabled_modules,
21 fixtures :projects, :enabled_modules,
22 :users, :email_addresses,
22 :users, :email_addresses,
23 :roles, :members, :member_roles,
23 :roles, :members, :member_roles,
24 :issues, :issue_statuses,
24 :issues, :issue_statuses,
25 :trackers, :projects_trackers,
25 :trackers, :projects_trackers,
26 :versions,
26 :versions,
27 :issue_categories,
27 :issue_categories,
28 :enumerations,
28 :enumerations,
29 :workflows,
29 :workflows,
30 :custom_fields,
30 :custom_fields,
31 :custom_values,
31 :custom_values,
32 :custom_fields_projects,
32 :custom_fields_projects,
33 :custom_fields_trackers
33 :custom_fields_trackers
34
34
35 def test_create_versions_should_create_missing_versions
35 def test_create_versions_should_create_missing_versions
36 import = generate_import_with_mapping
36 import = generate_import_with_mapping
37 import.mapping.merge!('fixed_version' => '9', 'create_versions' => '1')
37 import.mapping.merge!('fixed_version' => '9', 'create_versions' => '1')
38 import.save!
38 import.save!
39
39
40 version = new_record(Version) do
40 version = new_record(Version) do
41 assert_difference 'Issue.count', 3 do
41 assert_difference 'Issue.count', 3 do
42 import.run
42 import.run
43 end
43 end
44 end
44 end
45 assert_equal '2.1', version.name
45 assert_equal '2.1', version.name
46 end
46 end
47
47
48 def test_create_categories_should_create_missing_categories
48 def test_create_categories_should_create_missing_categories
49 import = generate_import_with_mapping
49 import = generate_import_with_mapping
50 import.mapping.merge!('category' => '10', 'create_categories' => '1')
50 import.mapping.merge!('category' => '10', 'create_categories' => '1')
51 import.save!
51 import.save!
52
52
53 category = new_record(IssueCategory) do
53 category = new_record(IssueCategory) do
54 assert_difference 'Issue.count', 3 do
54 assert_difference 'Issue.count', 3 do
55 import.run
55 import.run
56 end
56 end
57 end
57 end
58 assert_equal 'New category', category.name
58 assert_equal 'New category', category.name
59 end
59 end
60
60
61 def test_parent_should_be_set
61 def test_parent_should_be_set
62 import = generate_import_with_mapping
62 import = generate_import_with_mapping
63 import.mapping.merge!('parent_issue_id' => '5')
63 import.mapping.merge!('parent_issue_id' => '5')
64 import.save!
64 import.save!
65
65
66 issues = new_records(Issue, 3) { import.run }
66 issues = new_records(Issue, 3) { import.run }
67 assert_nil issues[0].parent
67 assert_nil issues[0].parent
68 assert_equal issues[0].id, issues[1].parent_id
68 assert_equal issues[0].id, issues[1].parent_id
69 assert_equal 2, issues[2].parent_id
69 assert_equal 2, issues[2].parent_id
70 end
70 end
71
71
72 def test_assignee_should_be_set
73 import = generate_import_with_mapping
74 import.mapping.merge!('assigned_to' => '11')
75 import.save!
76
77 issues = new_records(Issue, 3) { import.run }
78 assert_equal [User.find(3), nil, nil], issues.map(&:assigned_to)
79 end
80
72 def test_is_private_should_be_set_based_on_user_locale
81 def test_is_private_should_be_set_based_on_user_locale
73 import = generate_import_with_mapping
82 import = generate_import_with_mapping
74 import.mapping.merge!('is_private' => '6')
83 import.mapping.merge!('is_private' => '6')
75 import.save!
84 import.save!
76
85
77 issues = new_records(Issue, 3) { import.run }
86 issues = new_records(Issue, 3) { import.run }
78 assert_equal [false, true, false], issues.map(&:is_private)
87 assert_equal [false, true, false], issues.map(&:is_private)
79 end
88 end
80
89
81 def test_dates_should_be_parsed_using_date_format_setting
90 def test_dates_should_be_parsed_using_date_format_setting
82 field = IssueCustomField.generate!(:field_format => 'date', :is_for_all => true, :trackers => Tracker.all)
91 field = IssueCustomField.generate!(:field_format => 'date', :is_for_all => true, :trackers => Tracker.all)
83 import = generate_import_with_mapping('import_dates.csv')
92 import = generate_import_with_mapping('import_dates.csv')
84 import.settings.merge!('date_format' => Import::DATE_FORMATS[1])
93 import.settings.merge!('date_format' => Import::DATE_FORMATS[1])
85 import.mapping.merge!('subject' => '0', 'start_date' => '1', 'due_date' => '2', "cf_#{field.id}" => '3')
94 import.mapping.merge!('subject' => '0', 'start_date' => '1', 'due_date' => '2', "cf_#{field.id}" => '3')
86 import.save!
95 import.save!
87
96
88 issue = new_record(Issue) { import.run } # only 1 valid issue
97 issue = new_record(Issue) { import.run } # only 1 valid issue
89 assert_equal "Valid dates", issue.subject
98 assert_equal "Valid dates", issue.subject
90 assert_equal Date.parse('2015-07-10'), issue.start_date
99 assert_equal Date.parse('2015-07-10'), issue.start_date
91 assert_equal Date.parse('2015-08-12'), issue.due_date
100 assert_equal Date.parse('2015-08-12'), issue.due_date
92 assert_equal '2015-07-14', issue.custom_field_value(field)
101 assert_equal '2015-07-14', issue.custom_field_value(field)
93 end
102 end
94
103
95 def test_date_format_should_default_to_user_language
104 def test_date_format_should_default_to_user_language
96 user = User.generate!(:language => 'fr')
105 user = User.generate!(:language => 'fr')
97 import = Import.new
106 import = Import.new
98 import.user = user
107 import.user = user
99 assert_nil import.settings['date_format']
108 assert_nil import.settings['date_format']
100
109
101 import.set_default_settings
110 import.set_default_settings
102 assert_equal '%d/%m/%Y', import.settings['date_format']
111 assert_equal '%d/%m/%Y', import.settings['date_format']
103 end
112 end
104
113
105 def test_run_should_remove_the_file
114 def test_run_should_remove_the_file
106 import = generate_import_with_mapping
115 import = generate_import_with_mapping
107 file_path = import.filepath
116 file_path = import.filepath
108 assert File.exists?(file_path)
117 assert File.exists?(file_path)
109
118
110 import.run
119 import.run
111 assert !File.exists?(file_path)
120 assert !File.exists?(file_path)
112 end
121 end
113 end
122 end
General Comments 0
You need to be logged in to leave comments. Login now