Description:
uses empty? instead of length to check model validation errors when importing problems
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r338:0d4a8ccf5805 - - 2 files changed: 3 inserted, 3 deleted
@@ -139,49 +139,49 | |||||
|
139 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
139 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
140 | end |
|
140 | end |
|
141 |
|
141 | ||
|
142 | def do_manage |
|
142 | def do_manage |
|
143 | if params.has_key? 'change_date_added' |
|
143 | if params.has_key? 'change_date_added' |
|
144 | change_date_added |
|
144 | change_date_added |
|
145 | else params.has_key? 'add_to_contest' |
|
145 | else params.has_key? 'add_to_contest' |
|
146 | add_to_contest |
|
146 | add_to_contest |
|
147 | end |
|
147 | end |
|
148 | redirect_to :action => 'manage' |
|
148 | redirect_to :action => 'manage' |
|
149 | end |
|
149 | end |
|
150 |
|
150 | ||
|
151 | def import |
|
151 | def import |
|
152 | @allow_test_pair_import = allow_test_pair_import? |
|
152 | @allow_test_pair_import = allow_test_pair_import? |
|
153 | end |
|
153 | end |
|
154 |
|
154 | ||
|
155 | def do_import |
|
155 | def do_import |
|
156 | old_problem = Problem.find_by_name(params[:name]) |
|
156 | old_problem = Problem.find_by_name(params[:name]) |
|
157 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
157 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
158 | params.delete :import_to_db |
|
158 | params.delete :import_to_db |
|
159 | end |
|
159 | end |
|
160 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
160 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
161 | old_problem) |
|
161 | old_problem) |
|
162 |
|
162 | ||
|
163 |
- if @problem.errors. |
|
163 | + if !@problem.errors.empty? |
|
164 | render :action => 'import' and return |
|
164 | render :action => 'import' and return |
|
165 | end |
|
165 | end |
|
166 |
|
166 | ||
|
167 | if old_problem!=nil |
|
167 | if old_problem!=nil |
|
168 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
168 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
169 | end |
|
169 | end |
|
170 | @log = import_log |
|
170 | @log = import_log |
|
171 | end |
|
171 | end |
|
172 |
|
172 | ||
|
173 | def remove_contest |
|
173 | def remove_contest |
|
174 | problem = Problem.find(params[:id]) |
|
174 | problem = Problem.find(params[:id]) |
|
175 | contest = Contest.find(params[:contest_id]) |
|
175 | contest = Contest.find(params[:contest_id]) |
|
176 | if problem!=nil and contest!=nil |
|
176 | if problem!=nil and contest!=nil |
|
177 | problem.contests.delete(contest) |
|
177 | problem.contests.delete(contest) |
|
178 | end |
|
178 | end |
|
179 | redirect_to :action => 'manage' |
|
179 | redirect_to :action => 'manage' |
|
180 | end |
|
180 | end |
|
181 |
|
181 | ||
|
182 | ################################## |
|
182 | ################################## |
|
183 | protected |
|
183 | protected |
|
184 |
|
184 | ||
|
185 | def allow_test_pair_import? |
|
185 | def allow_test_pair_import? |
|
186 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
186 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
187 | return ALLOW_TEST_PAIR_IMPORT |
|
187 | return ALLOW_TEST_PAIR_IMPORT |
@@ -1,49 +1,49 | |||||
|
1 | class Problem < ActiveRecord::Base |
|
1 | class Problem < ActiveRecord::Base |
|
2 |
|
2 | ||
|
3 | belongs_to :description |
|
3 | belongs_to :description |
|
4 | has_and_belongs_to_many :contests, :uniq => true |
|
4 | has_and_belongs_to_many :contests, :uniq => true |
|
5 | has_many :test_pairs, :dependent => :delete_all |
|
5 | has_many :test_pairs, :dependent => :delete_all |
|
6 |
|
6 | ||
|
7 | validates_presence_of :name |
|
7 | validates_presence_of :name |
|
8 | validates_format_of :name, :with => /^\w+$/ |
|
8 | validates_format_of :name, :with => /^\w+$/ |
|
9 | validates_presence_of :full_name |
|
9 | validates_presence_of :full_name |
|
10 |
|
10 | ||
|
11 | scope :available, :conditions => {:available => true} |
|
11 | scope :available, :conditions => {:available => true} |
|
12 |
|
12 | ||
|
13 | DEFAULT_TIME_LIMIT = 1 |
|
13 | DEFAULT_TIME_LIMIT = 1 |
|
14 | DEFAULT_MEMORY_LIMIT = 32 |
|
14 | DEFAULT_MEMORY_LIMIT = 32 |
|
15 |
|
15 | ||
|
16 | def self.find_available_problems |
|
16 | def self.find_available_problems |
|
17 | Problem.available.all(:order => "date_added DESC") |
|
17 | Problem.available.all(:order => "date_added DESC") |
|
18 | end |
|
18 | end |
|
19 |
|
19 | ||
|
20 | def self.create_from_import_form_params(params, old_problem=nil) |
|
20 | def self.create_from_import_form_params(params, old_problem=nil) |
|
21 | org_problem = old_problem || Problem.new |
|
21 | org_problem = old_problem || Problem.new |
|
22 | import_params, problem = Problem.extract_params_and_check(params, |
|
22 | import_params, problem = Problem.extract_params_and_check(params, |
|
23 | org_problem) |
|
23 | org_problem) |
|
24 |
|
24 | ||
|
25 |
- if problem.errors. |
|
25 | + if !problem.errors.empty? |
|
26 | return problem, 'Error importing' |
|
26 | return problem, 'Error importing' |
|
27 | end |
|
27 | end |
|
28 |
|
28 | ||
|
29 | problem.full_score = 100 |
|
29 | problem.full_score = 100 |
|
30 | problem.date_added = Time.new |
|
30 | problem.date_added = Time.new |
|
31 | problem.test_allowed = true |
|
31 | problem.test_allowed = true |
|
32 | problem.output_only = false |
|
32 | problem.output_only = false |
|
33 | problem.available = false |
|
33 | problem.available = false |
|
34 |
|
34 | ||
|
35 | if not problem.save |
|
35 | if not problem.save |
|
36 | return problem, 'Error importing' |
|
36 | return problem, 'Error importing' |
|
37 | end |
|
37 | end |
|
38 |
|
38 | ||
|
39 | import_to_db = params.has_key? :import_to_db |
|
39 | import_to_db = params.has_key? :import_to_db |
|
40 |
|
40 | ||
|
41 | importer = TestdataImporter.new(problem) |
|
41 | importer = TestdataImporter.new(problem) |
|
42 |
|
42 | ||
|
43 | if not importer.import_from_file(import_params[:file], |
|
43 | if not importer.import_from_file(import_params[:file], |
|
44 | import_params[:time_limit], |
|
44 | import_params[:time_limit], |
|
45 | import_params[:memory_limit], |
|
45 | import_params[:memory_limit], |
|
46 | import_to_db) |
|
46 | import_to_db) |
|
47 | problem.errors.add_to_base('Import error.') |
|
47 | problem.errors.add_to_base('Import error.') |
|
48 | end |
|
48 | end |
|
49 |
|
49 | ||
@@ -71,44 +71,44 | |||||
|
71 | end |
|
71 | end |
|
72 |
|
72 | ||
|
73 | def self.extract_params_and_check(params, problem) |
|
73 | def self.extract_params_and_check(params, problem) |
|
74 | time_limit = Problem.to_f_or_default(params[:time_limit], |
|
74 | time_limit = Problem.to_f_or_default(params[:time_limit], |
|
75 | DEFAULT_TIME_LIMIT) |
|
75 | DEFAULT_TIME_LIMIT) |
|
76 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
76 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
77 | DEFAULT_MEMORY_LIMIT) |
|
77 | DEFAULT_MEMORY_LIMIT) |
|
78 |
|
78 | ||
|
79 | if time_limit<=0 or time_limit >60 |
|
79 | if time_limit<=0 or time_limit >60 |
|
80 | problem.errors.add_to_base('Time limit out of range.') |
|
80 | problem.errors.add_to_base('Time limit out of range.') |
|
81 | end |
|
81 | end |
|
82 |
|
82 | ||
|
83 | if memory_limit==0 and params[:memory_limit]!='0' |
|
83 | if memory_limit==0 and params[:memory_limit]!='0' |
|
84 | problem.errors.add_to_base('Memory limit format errors.') |
|
84 | problem.errors.add_to_base('Memory limit format errors.') |
|
85 | elsif memory_limit<=0 or memory_limit >512 |
|
85 | elsif memory_limit<=0 or memory_limit >512 |
|
86 | problem.errors.add_to_base('Memory limit out of range.') |
|
86 | problem.errors.add_to_base('Memory limit out of range.') |
|
87 | end |
|
87 | end |
|
88 |
|
88 | ||
|
89 | if params[:file]==nil or params[:file]=='' |
|
89 | if params[:file]==nil or params[:file]=='' |
|
90 | problem.errors.add_to_base('No testdata file.') |
|
90 | problem.errors.add_to_base('No testdata file.') |
|
91 | end |
|
91 | end |
|
92 |
|
92 | ||
|
93 | file = params[:file] |
|
93 | file = params[:file] |
|
94 |
|
94 | ||
|
95 |
- if problem.errors. |
|
95 | + if !problem.errors.empty? |
|
96 | return nil, problem |
|
96 | return nil, problem |
|
97 | end |
|
97 | end |
|
98 |
|
98 | ||
|
99 | problem.name = params[:name] |
|
99 | problem.name = params[:name] |
|
100 | if params[:full_name]!='' |
|
100 | if params[:full_name]!='' |
|
101 | problem.full_name = params[:full_name] |
|
101 | problem.full_name = params[:full_name] |
|
102 | else |
|
102 | else |
|
103 | problem.full_name = params[:name] |
|
103 | problem.full_name = params[:name] |
|
104 | end |
|
104 | end |
|
105 |
|
105 | ||
|
106 | return [{ |
|
106 | return [{ |
|
107 | :time_limit => time_limit, |
|
107 | :time_limit => time_limit, |
|
108 | :memory_limit => memory_limit, |
|
108 | :memory_limit => memory_limit, |
|
109 | :file => file |
|
109 | :file => file |
|
110 | }, |
|
110 | }, |
|
111 | problem] |
|
111 | problem] |
|
112 | end |
|
112 | end |
|
113 |
|
113 | ||
|
114 | end |
|
114 | end |
You need to be logged in to leave comments.
Login now