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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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
  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)
  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.post?
  57. respond_to do |format|
  58. format.html {
  59. if params[:previous]
  60. redirect_to import_settings_path(@import)
  61. else
  62. redirect_to import_run_path(@import)
  63. end
  64. }
  65. format.js # updates mapping form on project or tracker change
  66. end
  67. end
  68. end
  69. def run
  70. if request.post?
  71. @current = @import.run(
  72. :max_items => max_items_per_request,
  73. :max_time => 10.seconds
  74. )
  75. respond_to do |format|
  76. format.html {
  77. if @import.finished?
  78. redirect_to import_path(@import)
  79. else
  80. redirect_to import_run_path(@import)
  81. end
  82. }
  83. format.js
  84. end
  85. end
  86. end
  87. def current_menu(project)
  88. if import_layout == 'admin'
  89. nil
  90. else
  91. :application_menu
  92. end
  93. end
  94. private
  95. def find_import
  96. @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first
  97. if @import.nil?
  98. render_404
  99. return
  100. elsif @import.finished? && action_name != 'show'
  101. redirect_to import_path(@import)
  102. return
  103. end
  104. update_from_params if request.post?
  105. end
  106. def update_from_params
  107. if params[:import_settings].present?
  108. @import.settings ||= {}
  109. @import.settings.merge!(params[:import_settings].to_unsafe_hash)
  110. @import.save!
  111. end
  112. end
  113. def max_items_per_request
  114. 5
  115. end
  116. def import_layout
  117. import_type && import_type.layout || 'base'
  118. end
  119. def menu_items
  120. menu_item = import_type ? import_type.menu_item : nil
  121. { self.controller_name.to_sym => { :actions => {}, :default => menu_item } }
  122. end
  123. def authorize_import
  124. return render_404 unless import_type
  125. return render_403 unless import_type.authorized?(User.current)
  126. end
  127. def import_type
  128. return @import_type if defined? @import_type
  129. @import_type =
  130. if @import
  131. @import.class
  132. else
  133. type = Object.const_get(params[:type]) rescue nil
  134. type && type < Import ? type : nil
  135. end
  136. end
  137. end