Merge pull request #771 from openHPI/add_handling_missing_file_type_on_import

Add handling for missing file type on import
This commit is contained in:
Sebastian Serth
2020-11-02 19:44:51 +01:00
committed by GitHub
4 changed files with 28 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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