Deserialize exercises files in ProFormA-XML

- includes determining main file property via comment and template
  attributes in XML

(closes leoselig/codeocean#3)
This commit is contained in:
leo.selig
2016-02-18 16:03:25 +01:00
parent 773088798c
commit 7cc612ddaf
2 changed files with 20 additions and 1 deletions

View File

@ -77,6 +77,7 @@ class ExercisesController < ApplicationController
if saved
render :text => 'SUCCESS', :status => 200
else
logger.info(exercise.errors.full_messages)
render :text => 'Invalid exercise', :status => 400
end
rescue => error

View File

@ -110,9 +110,27 @@ class Exercise < ActiveRecord::Base
# how to extract the proforma functionality into a different module in rails?
xml = Nokogiri::XML(xml_string)
xml.collect_namespaces
description = xml.xpath('/root/p:task/p:description/text()')[0].content
self.attributes = {
title: xml.xpath('/root/p:task/p:meta-data/p:title/text()')[0].content,
description: xml.xpath('/root/p:task/p:description/text()')[0].content
description: description,
instructions: description
}
xml.xpath('/root/p:task/p:files/p:file').all? { |file|
file_name_split = file.xpath('@filename').first.value.split('.')
file_class = file.xpath('@class').first.value
comment = file.xpath('@comment').first.value
is_main_file = (file_class == 'template') && (comment == 'main')
files.build({
name: file_name_split.first,
content: file.xpath('text()').first.content,
read_only: false,
hidden: file_class == 'internal',
role: is_main_file ? 'main_file' : 'regular_file',
file_type: FileType.where(
file_extension: ".#{file_name_split.second}"
).take
})
}
self.execution_environment_id = 1
end