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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require 'csv'
  18. class ImportsController < ApplicationController
  19. menu_item :issues
  20. before_action :find_import, :only => [:show, :settings, :mapping, :run]
  21. before_action :authorize_global
  22. helper :issues
  23. helper :queries
  24. def new
  25. end
  26. def create
  27. @import = IssueImport.new
  28. @import.user = User.current
  29. @import.file = params[:file]
  30. @import.set_default_settings
  31. if @import.save
  32. redirect_to import_settings_path(@import)
  33. else
  34. render :action => 'new'
  35. end
  36. end
  37. def show
  38. end
  39. def settings
  40. if request.post? && @import.parse_file
  41. redirect_to import_mapping_path(@import)
  42. end
  43. # TODO: Remove ArgumentError when support for Ruby 2.2 is dropped (#28689)
  44. rescue CSV::MalformedCSVError, ArgumentError, EncodingError => e
  45. if e.is_a?(CSV::MalformedCSVError) && e.message !~ /Invalid byte sequence/
  46. flash.now[:error] = l(:error_invalid_csv_file_or_settings)
  47. else
  48. flash.now[:error] = l(:error_invalid_file_encoding, :encoding => ERB::Util.h(@import.settings['encoding']))
  49. end
  50. rescue SystemCallError => e
  51. flash.now[:error] = l(:error_can_not_read_import_file)
  52. end
  53. def mapping
  54. @custom_fields = @import.mappable_custom_fields
  55. if request.post?
  56. respond_to do |format|
  57. format.html {
  58. if params[:previous]
  59. redirect_to import_settings_path(@import)
  60. else
  61. redirect_to import_run_path(@import)
  62. end
  63. }
  64. format.js # updates mapping form on project or tracker change
  65. end
  66. end
  67. end
  68. def run
  69. if request.post?
  70. @current = @import.run(
  71. :max_items => max_items_per_request,
  72. :max_time => 10.seconds
  73. )
  74. respond_to do |format|
  75. format.html {
  76. if @import.finished?
  77. redirect_to import_path(@import)
  78. else
  79. redirect_to import_run_path(@import)
  80. end
  81. }
  82. format.js
  83. end
  84. end
  85. end
  86. private
  87. def find_import
  88. @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first
  89. if @import.nil?
  90. render_404
  91. return
  92. elsif @import.finished? && action_name != 'show'
  93. redirect_to import_path(@import)
  94. return
  95. end
  96. update_from_params if request.post?
  97. end
  98. def update_from_params
  99. if params[:import_settings].present?
  100. @import.settings ||= {}
  101. @import.settings.merge!(params[:import_settings].to_unsafe_hash)
  102. @import.save!
  103. end
  104. end
  105. def max_items_per_request
  106. 5
  107. end
  108. end