diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 9eca85cf..b8bc9f32 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -178,7 +178,6 @@ class ExercisesController < ApplicationController user = user_from_api_key return render json: {}, status: 401 if user.nil? - exercise = nil ActiveRecord::Base.transaction do exercise = ::ProformaService::Import.call(zip: tempfile, user: user) exercise.save! @@ -188,7 +187,8 @@ class ExercisesController < ApplicationController render json: {}, status: 401 rescue Proforma::ProformaError render json: t('exercises.import_codeharbor.import_errors.invalid'), status: 400 - rescue StandardError + rescue StandardError => e + Raven.capture_exception(e) render json: t('exercises.import_codeharbor.import_errors.internal_error'), status: 500 end diff --git a/app/services/proforma_service/convert_task_to_exercise.rb b/app/services/proforma_service/convert_task_to_exercise.rb index 7f49fa40..fe35eab2 100644 --- a/app/services/proforma_service/convert_task_to_exercise.rb +++ b/app/services/proforma_service/convert_task_to_exercise.rb @@ -47,9 +47,10 @@ module ProformaService end def codeocean_file_from_task_file(file) + extension = File.extname(file.filename) codeocean_file = CodeOcean::File.new( context: @exercise, - file_type: FileType.find_by(file_extension: File.extname(file.filename)), + file_type: file_type(extension), hidden: file.visible == 'no', name: File.basename(file.filename, '.*'), read_only: file.usage_by_lms != 'edit', @@ -63,5 +64,14 @@ module ProformaService end codeocean_file end + + def file_type(extension) + FileType.find_or_create_by(file_extension: extension) do |file_type| + file_type.name = extension[1..] + file_type.user = @user + file_type.indent_size = 4 + file_type.editor_mode = 'ace/mode/plain_text' + end + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 78464135..77d2b24f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -344,7 +344,7 @@ en: import_codeharbor: import_errors: invalid: Invalid exercise - internal_error: An internal error occurred on Codeharbor while importing the exercise. + internal_error: An internal error occurred on CodeOcean while importing the exercise. export_codeharbor: label: Export to Codeharbor dialogtitle: Export to Codeharbor diff --git a/spec/services/proforma_service/convert_task_to_exercise_spec.rb b/spec/services/proforma_service/convert_task_to_exercise_spec.rb index 7cd93af8..a9f4f0e1 100644 --- a/spec/services/proforma_service/convert_task_to_exercise_spec.rb +++ b/spec/services/proforma_service/convert_task_to_exercise_spec.rb @@ -69,7 +69,7 @@ describe ProformaService::ConvertTaskToExercise do Proforma::TaskFile.new( id: 'id', content: content, - filename: "#{path}filename.txt", + filename: filename, used_by_grader: 'used_by_grader', visible: 'yes', usage_by_lms: usage_by_lms, @@ -78,6 +78,7 @@ describe ProformaService::ConvertTaskToExercise do mimetype: mimetype ) end + let(:filename) { "#{path}filename.txt" } let(:usage_by_lms) { 'display' } let(:mimetype) { 'mimetype' } let(:binary) { false } @@ -148,6 +149,18 @@ describe ProformaService::ConvertTaskToExercise do expect(convert_to_exercise_service.files).to be_empty end end + + context 'when file has an unkown file_type' do + let(:filename) { 'unknown_file_type.asdf' } + + it 'creates a new Exercise on save' do + expect { convert_to_exercise_service.save! }.to change(Exercise, :count).by(1) + end + + it 'creates the missing FileType on save' do + expect { convert_to_exercise_service.save! }.to change(FileType, :count).by(1) + end + end end context 'when task has a model-solution' do