Description:
small refactoring in Problem.new_from_import_form_params
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@439 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r208:1b6d3b48289e - - 1 file changed: 42 inserted, 26 deleted
@@ -5,6 +5,9 | |||||
|
5 | validates_presence_of :name |
|
5 | validates_presence_of :name |
|
6 | validates_format_of :name, :with => /^\w+$/ |
|
6 | validates_format_of :name, :with => /^\w+$/ |
|
7 | validates_presence_of :full_name |
|
7 | validates_presence_of :full_name |
|
|
8 | + | ||
|
|
9 | + DEFAULT_TIME_LIMIT = 1 | ||
|
|
10 | + DEFAULT_MEMORY_LIMIT = 32 | ||
|
8 |
|
11 | ||
|
9 | def self.find_available_problems |
|
12 | def self.find_available_problems |
|
10 | find(:all, :conditions => {:available => true}, :order => "date_added DESC") |
|
13 | find(:all, :conditions => {:available => true}, :order => "date_added DESC") |
@@ -12,17 +15,44 | |||||
|
12 |
|
15 | ||
|
13 | def self.new_from_import_form_params(params) |
|
16 | def self.new_from_import_form_params(params) |
|
14 | problem = Problem.new |
|
17 | problem = Problem.new |
|
|
18 | + import_params = Problem.extract_params_and_check(params, problem) | ||
|
15 |
|
19 | ||
|
16 | - # form error checking |
|
20 | + if not problem.valid? |
|
|
21 | + return problem | ||
|
|
22 | + end | ||
|
17 |
|
23 | ||
|
18 | - time_limit_s = params[:time_limit] |
|
24 | + importer = TestdataImporter.new |
|
19 | - memory_limit_s = params[:memory_limit] |
|
25 | + |
|
|
26 | + if not importer.import_from_file(problem.name, | ||
|
|
27 | + import_params[:file], | ||
|
|
28 | + import_params[:time_limit], | ||
|
|
29 | + import_params[:memory_limit]) | ||
|
|
30 | + problem.errors.add_to_base('Import error.') | ||
|
|
31 | + end | ||
|
20 |
|
32 | ||
|
21 | - time_limit_s = '1' if time_limit_s=='' |
|
33 | + problem.full_score = 100 |
|
22 | - memory_limit_s = '32' if memory_limit_s=='' |
|
34 | + problem.date_added = Time.new |
|
|
35 | + problem.test_allowed = true | ||
|
|
36 | + problem.output_only = false | ||
|
|
37 | + problem.available = false | ||
|
|
38 | + return problem, importer.log_msg | ||
|
|
39 | + end | ||
|
|
40 | + | ||
|
|
41 | + protected | ||
|
23 |
|
42 | ||
|
24 | - time_limit = time_limit_s.to_i |
|
43 | + def self.to_i_or_default(st, default) |
|
25 | - memory_limit = memory_limit_s.to_i |
|
44 | + if st!='' |
|
|
45 | + st.to_i | ||
|
|
46 | + else | ||
|
|
47 | + default | ||
|
|
48 | + end | ||
|
|
49 | + end | ||
|
|
50 | + | ||
|
|
51 | + def self.extract_params_and_check(params, problem) | ||
|
|
52 | + time_limit = Problem.to_i_or_default(params[:time_limit], | ||
|
|
53 | + DEFAULT_TIME_LIMIT) | ||
|
|
54 | + memory_limit = Problem.to_i_or_default(params[:memory_limit], | ||
|
|
55 | + DEFAULT_MEMORY_LIMIT) | ||
|
26 |
|
56 | ||
|
27 | if time_limit==0 and time_limit_s!='0' |
|
57 | if time_limit==0 and time_limit_s!='0' |
|
28 | problem.errors.add_to_base('Time limit format errors.') |
|
58 | problem.errors.add_to_base('Time limit format errors.') |
@@ -53,25 +83,11 | |||||
|
53 | problem.full_name = params[:name] |
|
83 | problem.full_name = params[:name] |
|
54 | end |
|
84 | end |
|
55 |
|
85 | ||
|
56 | - if not problem.valid? |
|
86 | + return { |
|
57 | - return problem |
|
87 | + :time_limit => time_limit, |
|
58 | - end |
|
88 | + :memory_limit => memory_limit, |
|
59 | - |
|
89 | + :file => file |
|
60 | - importer = TestdataImporter.new |
|
90 | + } |
|
61 | - |
|
||
|
62 | - if not importer.import_from_file(problem.name, |
|
||
|
63 | - file, |
|
||
|
64 | - time_limit, |
|
||
|
65 | - memory_limit) |
|
||
|
66 | - problem.errors.add_to_base('Import error.') |
|
||
|
67 | - end |
|
||
|
68 | - |
|
||
|
69 | - problem.full_score = 100 |
|
||
|
70 | - problem.date_added = Time.new |
|
||
|
71 | - problem.test_allowed = true |
|
||
|
72 | - problem.output_only = false |
|
||
|
73 | - problem.available = false |
|
||
|
74 | - return problem, importer.log_msg |
|
||
|
75 | end |
|
91 | end |
|
76 |
|
92 | ||
|
77 | end |
|
93 | end |
You need to be logged in to leave comments.
Login now