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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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. rescue CSV::MalformedCSVError => e
  44. flash.now[:error] = l(:error_invalid_csv_file_or_settings)
  45. rescue ArgumentError, Encoding::InvalidByteSequenceError => e
  46. flash.now[:error] = l(:error_invalid_file_encoding, :encoding => ERB::Util.h(@import.settings['encoding']))
  47. rescue SystemCallError => e
  48. flash.now[:error] = l(:error_can_not_read_import_file)
  49. end
  50. def mapping
  51. @custom_fields = @import.mappable_custom_fields
  52. if request.post?
  53. respond_to do |format|
  54. format.html {
  55. if params[:previous]
  56. redirect_to import_settings_path(@import)
  57. else
  58. redirect_to import_run_path(@import)
  59. end
  60. }
  61. format.js # updates mapping form on project or tracker change
  62. end
  63. end
  64. end
  65. def run
  66. if request.post?
  67. @current = @import.run(
  68. :max_items => max_items_per_request,
  69. :max_time => 10.seconds
  70. )
  71. respond_to do |format|
  72. format.html {
  73. if @import.finished?
  74. redirect_to import_path(@import)
  75. else
  76. redirect_to import_run_path(@import)
  77. end
  78. }
  79. format.js
  80. end
  81. end
  82. end
  83. private
  84. def find_import
  85. @import = Import.where(:user_id => User.current.id, :filename => params[:id]).first
  86. if @import.nil?
  87. render_404
  88. return
  89. elsif @import.finished? && action_name != 'show'
  90. redirect_to import_path(@import)
  91. return
  92. end
  93. update_from_params if request.post?
  94. end
  95. def update_from_params
  96. if params[:import_settings].is_a?(Hash)
  97. @import.settings ||= {}
  98. @import.settings.merge!(params[:import_settings])
  99. @import.save!
  100. end
  101. end
  102. def max_items_per_request
  103. 5
  104. end
  105. end