Show More
Commit Description:
Merge pull request #15 from nattee/master...
Commit Description:
Merge pull request #15 from nattee/master finalizing merge of nattee's master
References:
File last commit:
Show/Diff file:
Action:
app/models/test_request.rb | 157 lines | 4.4 KiB | text/x-ruby | RubyLexer |
jittat
added name_of to TestRequest for dealing with null problem...
r39 #
# A TestRequest is a composition of submission with user's testdata.
#
# Note about TestRequest#problem: Usually, A TestRequest has to be
# associated with a problem, so that execution environment can be
# determined. However, to be more flexible, we have to ensure that
# it works as well with problem=nil. In this case, we shall provide
# a "default" execution environment for it. This can be done
jittat
added problem_name to TestRequest and moved name_of to protected...
r40 # seamlessly by using TestRequest#problem_name or
# TestRequest#name_of(problem) when retrieving the name of the
# problem: #name_of would return problem.name when problem!=nil and
# it would return "default" when problem=nil, #problem_name just
# call #name_of.
jittat
added name_of to TestRequest for dealing with null problem...
r39 #
jittat
test interface upload...
r36 require 'fileutils'
class TestRequest < Task
set_table_name "test_requests"
belongs_to :user
belongs_to :problem
belongs_to :submission
jittat
[web] add validations to test_request and submission...
r87 validates_presence_of :submission
validate :must_have_valid_problem
jittat
updated test_request.rb to work with grader...
r43 def problem_name
TestRequest.name_of(self.problem)
end
def language
self.submission.language
end
jittat
test interface upload...
r36 def self.get_inqueue_and_change_status(status)
# since there will be only one grader grading TestRequest
# we do not need locking (hopefully)
jittat
moved test interface functionality to test_controller...
r44 test_request = TestRequest.find(:first,
:order => "created_at",
:conditions => {:status=> Task::STATUS_INQUEUE})
if test_request!=nil
test_request.status = status
test_request.save!
jittat
test interface upload...
r36 end
jittat
moved test interface functionality to test_controller...
r44 test_request
jittat
test interface upload...
r36 end
# interfacing with form
def self.new_from_form_params(user,params)
test_request = TestRequest.new
test_request.user = user
jittat
[web] add validations to test_request and submission...
r87 begin
problem = Problem.find(params[:problem_id])
rescue ActiveRecord::RecordNotFound
problem = nil
end
jittat
test interface upload...
r36 test_request.problem = problem
jittat
[web] add validations to test_request and submission...
r87 if problem!=nil
test_request.submission =
Submission.find_by_user_problem_number(user.id,
problem.id,
params[:submission_number])
else
test_request.submission = nil
end
# checks if the user submits any input file
if params[:input_file]==nil or params[:input_file]==""
update errors.add_to_base("x") to Rails 3 errors.add(:base,"x")
r347 test_request.errors.add(:base,"No input submitted.")
jittat
[web] add validations to test_request and submission...
r87 test_request.input_file_name = nil
else
test_request.input_file_name = save_input_file(params[:input_file], user, problem)
if test_request.input_file_name == nil
update errors.add_to_base("x") to Rails 3 errors.add(:base,"x")
r347 test_request.errors.adds(:base,"No input submitted.")
jittat
[web] add validations to test_request and submission...
r87 end
jittat
[web] test_request accepting additional file...
r112 if params[:additional_file]!=nil and params[:additional_file]!=""
save_additional_file(params[:additional_file],
"#{test_request.input_file_name}.files")
end
jittat
[web] fixed test_request error when user does not put in any file...
r86 end
jittat
[web] fix time.new, time.now to use gmtime...
r121 test_request.submitted_at = Time.new.gmtime
jittat
test interface upload...
r36 test_request.status_inqueue
test_request
end
jittat
added problem_name to TestRequest and moved name_of to protected...
r40 protected
jittat
added name_of to TestRequest for dealing with null problem...
r39 def self.name_of(problem)
if problem!=nil
problem.name
else
"default"
end
end
jittat
updated test_request.rb to work with grader...
r43 def self.random_input_file_name(user,problem)
jittat
added name_of to TestRequest for dealing with null problem...
r39 problem_name = TestRequest.name_of(problem)
jittat
test interface upload...
r36 begin
jittat
changed test request uploaded dir to data/test_request/input...
r38 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
jittat
test interface upload...
r36 end while File.exists?(tmpname)
tmpname
end
def self.save_input_file(tempfile, user, problem)
jittat
updated test_request.rb to work with grader...
r43 new_file_name = random_input_file_name(user,problem)
jittat
test interface upload...
r36 dirname = File.dirname(new_file_name)
FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
jittat
[web] fixed test_request error when user does not put in any file...
r86
# when the user did not submit any file
return nil if tempfile==""
jittat
test interface upload...
r36 if tempfile.instance_of?(Tempfile)
tempfile.close
FileUtils.move(tempfile.path,new_file_name)
jittat
[web] fixed test_request error when user does not put in any file...
r86 else
jittat
test interface upload...
r36 File.open(new_file_name, "wb") do |f|
f.write(tempfile.read)
end
end
new_file_name
end
jittat
[web] add validations to test_request and submission...
r87
jittat
[web] test_request accepting additional file...
r112 def self.save_additional_file(tempfile,dir)
new_file_name = "#{dir}/#{tempfile.original_filename}"
dirname = File.dirname(new_file_name)
FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
# when the user did not submit any file
return nil if tempfile==""
if tempfile.instance_of?(Tempfile)
tempfile.close
FileUtils.move(tempfile.path,new_file_name)
else
File.open(new_file_name, "wb") do |f|
f.write(tempfile.read)
end
end
new_file_name
end
jittat
[web] add validations to test_request and submission...
r87 #
# validations
#
def must_have_valid_problem
if problem==nil
errors.add('problem',"must be specified.")
elsif (!problem.available) and (self.new_record?)
errors.add('problem',"must be valid.")
end
end
jittat
test interface upload...
r36 end