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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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. redirect_to import_mapping_path(@import)
  44. end
  45. rescue CSV::MalformedCSVError, EncodingError => e
  46. if e.is_a?(CSV::MalformedCSVError) && e.message !~ /Invalid byte sequence/
  47. flash.now[:error] = l(:error_invalid_csv_file_or_settings, e.message)
  48. else
  49. flash.now[:error] = l(:error_invalid_file_encoding, :encoding => ERB::Util.h(@import.settings['encoding']))
  50. end
  51. rescue SystemCallError => e
  52. flash.now[:error] = l(:error_can_not_read_import_file)
  53. end
  54. def mapping
  55. @custom_fields = @import.mappable_custom_fields
  56. if request.get?
  57. auto_map_fields
  58. elsif request.post?
  59. respond_to do |format|
  60. format.html do
  61. if params[:previous]
  62. redirect_to import_settings_path(@import)
  63. else
  64. redirect_to import_run_path(@import)
  65. end
  66. end
  67. format.js # updates mapping form on project or tracker change
  68. end
  69. end
  70. end
  71. def run
  72. if request.post?
  73. @current = @import.run(
  74. :max_items => max_items_per_request,
  75. :max_time => 10.seconds
  76. )
  77. respond_to do |format|
  78. format.html do
  79. if @import.finished?
  80. redirect_to import_path(@import)
  81. else
  82. redirect_to import_run_path(@import)
  83. end
  84. end
  85. format.js
  86. end
  87. end
  88. end
  89. def current_menu(project)
  90. if import_layout == 'admin'
  91. nil
  92. else
  93. :application_menu
  94. end
  95. end
  96. private
  97. def find_import
  98. @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first
  99. if @import.nil?
  100. render_404
  101. return
  102. elsif @import.finished? && action_name != 'show'
  103. redirect_to import_path(@import)
  104. return
  105. end
  106. update_from_params if request.post?
  107. end
  108. def update_from_params
  109. if params[:import_settings].present?
  110. @import.settings ||= {}
  111. @import.settings.merge!(params[:import_settings].to_unsafe_hash)
  112. @import.save!
  113. end
  114. end
  115. def max_items_per_request
  116. 5
  117. end
  118. def import_layout
  119. import_type && import_type.layout || 'base'
  120. end
  121. def menu_items
  122. menu_item = import_type ? import_type.menu_item : nil
  123. {self.controller_name.to_sym => {:actions => {}, :default => menu_item}}
  124. end
  125. def authorize_import
  126. return render_404 unless import_type
  127. return render_403 unless import_type.authorized?(User.current)
  128. end
  129. def import_type
  130. return @import_type if defined? @import_type
  131. @import_type =
  132. if @import
  133. @import.class
  134. else
  135. type =
  136. begin
  137. Object.const_get(params[:type])
  138. rescue
  139. nil
  140. end
  141. type && type < Import ? type : nil
  142. end
  143. end
  144. def auto_map_fields
  145. # Try to auto map fields only when settings['enconding'] is present
  146. # otherwhise, the import fails for non UTF-8 files because the headers
  147. # cannot be retrieved (Invalid byte sequence in UTF-8)
  148. return if @import.settings['encoding'].blank?
  149. mappings = @import.settings['mapping'] ||= {}
  150. headers = @import.headers.map{|header| header&.downcase}
  151. # Core fields
  152. import_type::AUTO_MAPPABLE_FIELDS.each do |field_nm, label_nm|
  153. next if mappings.include?(field_nm)
  154. index = headers.index(field_nm) || headers.index(l(label_nm).downcase)
  155. if index
  156. mappings[field_nm] = index
  157. end
  158. end
  159. # Custom fields
  160. @custom_fields.each do |field|
  161. field_nm = "cf_#{field.id}"
  162. next if mappings.include?(field_nm)
  163. index = headers.index(field_nm) || headers.index(field.name.downcase)
  164. if index
  165. mappings[field_nm] = index
  166. end
  167. end
  168. mappings
  169. end
  170. end