Description:
added name_of to TestRequest for dealing with null problem git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@87 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r39:7df73373290b - - 1 file changed: 22 inserted, 1 deleted

@@ -1,65 +1,86
1 + #
2 + # A TestRequest is a composition of submission with user's testdata.
3 + #
4 + # Note about TestRequest#problem: Usually, A TestRequest has to be
5 + # associated with a problem, so that execution environment can be
6 + # determined. However, to be more flexible, we have to ensure that
7 + # it works as well with problem=nil. In this case, we shall provide
8 + # a "default" execution environment for it. This can be done
9 + # seamlessly by using TestRequest#name_of when retrieving the name
10 + # of the problem: name_of would return problem.name when
11 + # problem!=nil and it would return "default" when problem=nil.
12 + #
13 +
1 require 'fileutils'
14 require 'fileutils'
2
15
3 class TestRequest < Task
16 class TestRequest < Task
4
17
5 set_table_name "test_requests"
18 set_table_name "test_requests"
6
19
7 belongs_to :user
20 belongs_to :user
8 belongs_to :problem
21 belongs_to :problem
9 belongs_to :submission
22 belongs_to :submission
10
23
11 def self.get_inqueue_and_change_status(status)
24 def self.get_inqueue_and_change_status(status)
12 # since there will be only one grader grading TestRequest
25 # since there will be only one grader grading TestRequest
13 # we do not need locking (hopefully)
26 # we do not need locking (hopefully)
14
27
15 task = Task.find(:first,
28 task = Task.find(:first,
16 :order => "created_at",
29 :order => "created_at",
17 :conditions => {:status=> Task::STATUS_INQUEUE})
30 :conditions => {:status=> Task::STATUS_INQUEUE})
18 if task!=nil
31 if task!=nil
19 task.status = status
32 task.status = status
20 task.save!
33 task.save!
21 end
34 end
22
35
23 task
36 task
24 end
37 end
25
38
26 # interfacing with form
39 # interfacing with form
27 def self.new_from_form_params(user,params)
40 def self.new_from_form_params(user,params)
28 test_request = TestRequest.new
41 test_request = TestRequest.new
29 test_request.user = user
42 test_request.user = user
30 problem = Problem.find(params[:problem_id])
43 problem = Problem.find(params[:problem_id])
31 test_request.problem = problem
44 test_request.problem = problem
32 test_request.submission =
45 test_request.submission =
33 Submission.find_by_user_problem_number(user.id,
46 Submission.find_by_user_problem_number(user.id,
34 problem.id,
47 problem.id,
35 params[:submission_number])
48 params[:submission_number])
36 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
49 test_request.input_file_name = save_input_file(params[:input_file], user, problem)
37 test_request.submitted_at = Time.new
50 test_request.submitted_at = Time.new
38 test_request.status_inqueue
51 test_request.status_inqueue
39 test_request
52 test_request
40 end
53 end
41
54
55 + def self.name_of(problem)
56 + if problem!=nil
57 + problem.name
58 + else
59 + "default"
60 + end
61 + end
62 +
42 protected
63 protected
43 def self.input_file_name(user,problem)
64 def self.input_file_name(user,problem)
44 - problem_name = (problem!=nil) ? problem.name : ""
65 + problem_name = TestRequest.name_of(problem)
45 begin
66 begin
46 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
67 tmpname = TEST_REQUEST_INPUT_FILE_DIR + "/#{user.login}/#{problem_name}/#{rand(10000)}"
47 end while File.exists?(tmpname)
68 end while File.exists?(tmpname)
48 tmpname
69 tmpname
49 end
70 end
50
71
51 def self.save_input_file(tempfile, user, problem)
72 def self.save_input_file(tempfile, user, problem)
52 new_file_name = input_file_name(user,problem)
73 new_file_name = input_file_name(user,problem)
53 dirname = File.dirname(new_file_name)
74 dirname = File.dirname(new_file_name)
54 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
75 FileUtils.mkdir_p(File.dirname(new_file_name)) if !File.exists?(dirname)
55 if tempfile.instance_of?(Tempfile)
76 if tempfile.instance_of?(Tempfile)
56 tempfile.close
77 tempfile.close
57 FileUtils.move(tempfile.path,new_file_name)
78 FileUtils.move(tempfile.path,new_file_name)
58 else
79 else
59 File.open(new_file_name, "wb") do |f|
80 File.open(new_file_name, "wb") do |f|
60 f.write(tempfile.read)
81 f.write(tempfile.read)
61 end
82 end
62 end
83 end
63 new_file_name
84 new_file_name
64 end
85 end
65 end
86 end
You need to be logged in to leave comments. Login now