@@ -1,151 +1,151 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2015 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 | class IssueImport < Import |
|
19 | 19 | |
|
20 | 20 | # Returns the objects that were imported |
|
21 | 21 | def saved_objects |
|
22 | 22 | object_ids = saved_items.pluck(:obj_id) |
|
23 | 23 | objects = Issue.where(:id => object_ids).order(:id).preload(:tracker, :priority, :status) |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | # Returns a scope of projects that user is allowed to |
|
27 | 27 | # import issue to |
|
28 | 28 | def allowed_target_projects |
|
29 | 29 | Project.allowed_to(user, :import_issues) |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def project |
|
33 | 33 | project_id = mapping['project_id'].to_i |
|
34 | 34 | allowed_target_projects.find_by_id(project_id) || allowed_target_projects.first |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Returns a scope of trackers that user is allowed to |
|
38 | 38 | # import issue to |
|
39 | 39 | def allowed_target_trackers |
|
40 | 40 | project.trackers |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def tracker |
|
44 | 44 | tracker_id = mapping['tracker_id'].to_i |
|
45 | 45 | allowed_target_trackers.find_by_id(tracker_id) || allowed_target_trackers.first |
|
46 | 46 | end |
|
47 | 47 | |
|
48 | 48 | # Returns true if missing categories should be created during the import |
|
49 | 49 | def create_categories? |
|
50 | 50 | user.allowed_to?(:manage_categories, project) && |
|
51 | 51 | mapping['create_categories'] == '1' |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | # Returns true if missing versions should be created during the import |
|
55 | 55 | def create_versions? |
|
56 | 56 | user.allowed_to?(:manage_versions, project) && |
|
57 | 57 | mapping['create_versions'] == '1' |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | private |
|
61 | 61 | |
|
62 | 62 | def build_object(row) |
|
63 | 63 | issue = Issue.new |
|
64 | 64 | issue.author = user |
|
65 | 65 | issue.notify = false |
|
66 | 66 | |
|
67 | 67 | attributes = { |
|
68 | 68 | 'project_id' => mapping['project_id'], |
|
69 | 69 | 'tracker_id' => mapping['tracker_id'], |
|
70 | 70 | 'subject' => row_value(row, 'subject'), |
|
71 | 71 | 'description' => row_value(row, 'description') |
|
72 | 72 | } |
|
73 | 73 | issue.send :safe_attributes=, attributes, user |
|
74 | 74 | |
|
75 | 75 | attributes = {} |
|
76 | 76 | if priority_name = row_value(row, 'priority') |
|
77 | 77 | if priority_id = IssuePriority.active.named(priority_name).first.try(:id) |
|
78 | 78 | attributes['priority_id'] = priority_id |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | if issue.project && category_name = row_value(row, 'category') |
|
82 | 82 | if category = issue.project.issue_categories.named(category_name).first |
|
83 | 83 | attributes['category_id'] = category.id |
|
84 | 84 | elsif create_categories? |
|
85 | 85 | category = issue.project.issue_categories.build |
|
86 | 86 | category.name = category_name |
|
87 | 87 | if category.save |
|
88 | 88 | attributes['category_id'] = category.id |
|
89 | 89 | end |
|
90 | 90 | end |
|
91 | 91 | end |
|
92 | 92 | if assignee_name = row_value(row, 'assigned_to') |
|
93 | 93 | if assignee = issue.assignable_users.detect {|u| u.name.downcase == assignee_name.downcase} |
|
94 | 94 | attributes['assigned_to_id'] = assignee.id |
|
95 | 95 | end |
|
96 | 96 | end |
|
97 | 97 | if issue.project && version_name = row_value(row, 'fixed_version') |
|
98 |
if version = issue.project.versions. |
|
|
98 | if version = issue.project.versions.named(version_name).first | |
|
99 | 99 | attributes['fixed_version_id'] = version.id |
|
100 | 100 | elsif create_versions? |
|
101 | 101 | version = issue.project.versions.build |
|
102 | 102 | version.name = version_name |
|
103 | 103 | if version.save |
|
104 | 104 | attributes['fixed_version_id'] = version.id |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | end |
|
108 | 108 | if is_private = row_value(row, 'is_private') |
|
109 | 109 | if yes?(is_private) |
|
110 | 110 | attributes['is_private'] = '1' |
|
111 | 111 | end |
|
112 | 112 | end |
|
113 | 113 | if parent_issue_id = row_value(row, 'parent_issue_id') |
|
114 | 114 | if parent_issue_id =~ /\A(#)?(\d+)\z/ |
|
115 | 115 | parent_issue_id = $2 |
|
116 | 116 | if $1 |
|
117 | 117 | attributes['parent_issue_id'] = parent_issue_id |
|
118 | 118 | elsif issue_id = items.where(:position => parent_issue_id).first.try(:obj_id) |
|
119 | 119 | attributes['parent_issue_id'] = issue_id |
|
120 | 120 | end |
|
121 | 121 | else |
|
122 | 122 | attributes['parent_issue_id'] = parent_issue_id |
|
123 | 123 | end |
|
124 | 124 | end |
|
125 | 125 | if start_date = row_date(row, 'start_date') |
|
126 | 126 | attributes['start_date'] = start_date |
|
127 | 127 | end |
|
128 | 128 | if due_date = row_date(row, 'due_date') |
|
129 | 129 | attributes['due_date'] = due_date |
|
130 | 130 | end |
|
131 | 131 | if done_ratio = row_value(row, 'done_ratio') |
|
132 | 132 | attributes['done_ratio'] = done_ratio |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | attributes['custom_field_values'] = issue.custom_field_values.inject({}) do |h, v| |
|
136 | 136 | value = case v.custom_field.field_format |
|
137 | 137 | when 'date' |
|
138 | 138 | row_date(row, "cf_#{v.custom_field.id}") |
|
139 | 139 | else |
|
140 | 140 | row_value(row, "cf_#{v.custom_field.id}") |
|
141 | 141 | end |
|
142 | 142 | if value |
|
143 | 143 | h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, issue) |
|
144 | 144 | end |
|
145 | 145 | h |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | issue.send :safe_attributes=, attributes, user |
|
149 | 149 | issue |
|
150 | 150 | end |
|
151 | 151 | end |
General Comments 0
You need to be logged in to leave comments.
Login now