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,12 +1,25
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
@@ -30,27 +43,35
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
You need to be logged in to leave comments. Login now