Description:
added problem_name to TestRequest and moved name_of to protected git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@88 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

r40:b5c26b86e7d2 - - 1 file changed: 11 inserted, 4 deleted

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