file import wip, "native" missing

This commit is contained in:
Karol
2019-08-22 18:37:47 +02:00
parent 2af93ea308
commit aafb3f21df
2 changed files with 51 additions and 59 deletions

View File

@ -123,10 +123,10 @@ class ExercisesController < ApplicationController
# saved = exercise.save
if exercise.save
# render text: 'SUCCESS', status: 200
render json: {status: 201}
render json: {}, status: 201
else
logger.info(exercise.errors.full_messages)
render json: {status: 400}
render json: {}, status: 400
end
# rescue => error
# if error.class == Hash

View File

@ -21,76 +21,68 @@ module ProformaService
title: @task.title,
description: @task.description,
instructions: @task.internal_description,
files: task_files
files: files
# tests: tests,
# execution_environment: execution_environment,
# state_list: @exercise.persisted? ? 'updated' : 'new'
)
end
def task_files
@task.all_files.map do |file|
CodeOcean::File.new(
context: @exercise,
file_type: FileType.find_by(file_extension: File.extname(file.filename)),
hidden: file.visible == 'no',
name: File.basename(file.filename, '.*'),
read_only: file.usage_by_lms != 'edit',
# native_file: somehting something,
role: file.internal_description.underscore.gsub(' ', '_'),
# feedback_message: #if file is testfilethingy take that message,
# weight: see above,
path: File.dirname(file.filename)
)
end
# @task_files ||= Hash[
# @task.all_files.reject { |file| file.id == 'ms-placeholder-file' }.map do |task_file|
# [task_file.id, exercise_file_from_task_file(task_file)]
# end
# ]
def files
test_files + task_files.values
end
def exercise_file_from_task_file(task_file)
ExerciseFile.new({
full_file_name: task_file.filename,
read_only: task_file.usage_by_lms.in?(%w[display download]),
hidden: task_file.visible == 'no',
role: task_file.internal_description
}.tap do |params|
if task_file.binary
params[:attachment] = file_base64(task_file)
params[:attachment_file_name] = task_file.filename
params[:attachment_content_type] = task_file.mimetype
else
params[:content] = task_file.content
def test_files
@task.tests.map do |test_object|
task_files.delete(test_object.files.first.id).tap do |file|
file.weight = 1.0
file.feedback_message = test_object.meta_data['feedback-message']
end
end)
end
end
def task_files
@task_files ||= Hash[
@task.all_files.reject { |file| file.id == 'ms-placeholder-file' }.map do |task_file|
[task_file.id, codeocean_file_from_task_file(task_file)]
end
]
end
def codeocean_file_from_task_file(file)
CodeOcean::File.new(
context: @exercise,
content: file.content,
file_type: FileType.find_by(file_extension: File.extname(file.filename)),
hidden: file.visible == 'no',
name: File.basename(file.filename, '.*'),
read_only: file.usage_by_lms != 'edit',
# native_file: somehting something, uploader something
role: file.internal_description.underscore.gsub(' ', '_'),
# feedback_message: file.purpose == 'test' ? file.test.feedback_message : nil,
# weight: file.test? ? 1.0 : nil,
path: File.dirname(file.filename)
)
# ExerciseFile.new({
# full_file_name: task_file.filename,
# read_only: task_file.usage_by_lms.in?(%w[display download]),
# hidden: task_file.visible == 'no',
# role: task_file.internal_description
# }.tap do |params|
# if task_file.binary
# params[:attachment] = file_base64(task_file)
# params[:attachment_file_name] = task_file.filename
# params[:attachment_content_type] = task_file.mimetype
# else
# params[:content] = task_file.content
# end
# end)
end
def file_base64(file)
"data:#{file.mimetype || 'image/jpeg'};base64,#{Base64.encode64(file.content)}"
end
def tests
@task.tests.map do |test_object|
Test.new(
feedback_message: test_object.meta_data['feedback-message'],
testing_framework: TestingFramework.where(
name: test_object.meta_data['testing-framework'],
version: test_object.meta_data['testing-framework-version']
).first_or_initialize,
exercise_file: test_file(test_object)
)
end
end
def test_file(test_object)
task_files.delete(test_object.files.first.id).tap { |file| file.purpose = 'test' }
end
def execution_environment
ExecutionEnvironment.last
# ExecutionEnvironment.where(language: @task.proglang[:name], version: @task.proglang[:version]).first_or_initialize
end
end
end