From 7cc612ddaf85946d9cf7c1c9826952abc7d234aa Mon Sep 17 00:00:00 2001 From: "leo.selig" Date: Thu, 18 Feb 2016 16:03:25 +0100 Subject: [PATCH] Deserialize exercises files in ProFormA-XML - includes determining main file property via comment and template attributes in XML (closes leoselig/codeocean#3) --- app/controllers/exercises_controller.rb | 1 + app/models/exercise.rb | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 07bd033d..69d1f46f 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -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 diff --git a/app/models/exercise.rb b/app/models/exercise.rb index 1b917ecb..9b29ca02 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -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