You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imports_controller.rb 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'csv'
  19. class ImportsController < ApplicationController
  20. before_action :find_import, :only => [:show, :settings, :mapping, :run]
  21. before_action :authorize_import
  22. layout :import_layout
  23. helper :issues
  24. helper :queries
  25. def new
  26. @import = import_type.new
  27. end
  28. def create
  29. @import = import_type.new
  30. @import.user = User.current
  31. @import.file = params[:file]
  32. @import.set_default_settings(:project_id => params[:project_id])
  33. if @import.save
  34. redirect_to import_settings_path(@import)
  35. else
  36. render :action => 'new'
  37. end
  38. end
  39. def show
  40. end
  41. def settings
  42. if request.post? && @import.parse_file
  43. if @import.total_items == 0
  44. flash.now[:error] = l(:error_no_data_in_file)
  45. else
  46. redirect_to import_mapping_path(@import)
  47. end
  48. end
  49. rescue CSV::MalformedCSVError, EncodingError => e
  50. if e.is_a?(CSV::MalformedCSVError) && e.message !~ /Invalid byte sequence/
  51. flash.now[:error] = l(:error_invalid_csv_file_or_settings, e.message)
  52. else
  53. flash.now[:error] = l(:error_invalid_file_encoding, :encoding => ERB::Util.h(@import.settings['encoding']))
  54. end
  55. rescue SystemCallError => e
  56. flash.now[:error] = l(:error_can_not_read_import_file)
  57. end
  58. def mapping
  59. @custom_fields = @import.mappable_custom_fields
  60. if request.get?
  61. auto_map_fields
  62. elsif request.post?
  63. respond_to do |format|
  64. format.html do
  65. if params[:previous]
  66. redirect_to import_settings_path(@import)
  67. else
  68. redirect_to import_run_path(@import)
  69. end
  70. end
  71. format.js # updates mapping form on project or tracker change
  72. end
  73. end
  74. end
  75. def run
  76. if request.post?
  77. @current = @import.run(
  78. :max_items => max_items_per_request,
  79. :max_time => 10.seconds
  80. )
  81. respond_to do |format|
  82. format.html do
  83. if @import.finished?
  84. redirect_to import_path(@import)
  85. else
  86. redirect_to import_run_path(@import)
  87. end
  88. end
  89. format.js
  90. end
  91. end
  92. end
  93. def current_menu(project)
  94. if import_layout == 'admin'
  95. nil
  96. else
  97. :application_menu
  98. end
  99. end
  100. private
  101. def find_import
  102. @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first
  103. if @import.nil?
  104. render_404
  105. return
  106. elsif @import.finished? && action_name != 'show'
  107. redirect_to import_path(@import)
  108. return
  109. end
  110. update_from_params if request.post?
  111. end
  112. def update_from_params
  113. if params[:import_settings].present?
  114. @import.settings ||= {}
  115. @import.settings.merge!(params[:import_settings].to_unsafe_hash)
  116. @import.save!
  117. end
  118. end
  119. def max_items_per_request
  120. 5
  121. end
  122. def import_layout
  123. import_type && import_type.layout || 'base'
  124. end
  125. def menu_items
  126. menu_item = import_type ? import_type.menu_item : nil
  127. {self.controller_name.to_sym => {:actions => {}, :default => menu_item}}
  128. end
  129. def authorize_import
  130. return render_404 unless import_type
  131. return render_403 unless import_type.authorized?(User.current)
  132. end
  133. def import_type
  134. return @import_type if defined? @import_type
  135. @import_type =
  136. if @import
  137. @import.class
  138. else
  139. type =
  140. begin
  141. Object.const_get(params[:type])
  142. rescue
  143. nil
  144. end
  145. type && type < Import ? type : nil
  146. end
  147. end
  148. def auto_map_fields
  149. # Try to auto map fields only when settings['enconding'] is present
  150. # otherwhise, the import fails for non UTF-8 files because the headers
  151. # cannot be retrieved (Invalid byte sequence in UTF-8)
  152. return if @import.settings['encoding'].blank?
  153. mappings = @import.settings['mapping'] ||= {}
  154. headers = @import.headers.map{|header| header&.downcase}
  155. # Core fields
  156. import_type::AUTO_MAPPABLE_FIELDS.each do |field_nm, label_nm|
  157. next if mappings.include?(field_nm)
  158. index = headers.index(field_nm) || headers.index(l(label_nm).downcase)
  159. if index
  160. mappings[field_nm] = index
  161. end
  162. end
  163. # Custom fields
  164. @custom_fields.each do |field|
  165. field_nm = "cf_#{field.id}"
  166. next if mappings.include?(field_nm)
  167. index = headers.index(field_nm) || headers.index(field.name.downcase)
  168. if index
  169. mappings[field_nm] = index
  170. end
  171. end
  172. mappings
  173. end
  174. end