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
@@ -115,97 +115,97 | |||
|
115 | 115 | problem.save |
|
116 | 116 | end |
|
117 | 117 | redirect_to :action => 'list' |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | 120 | def turn_all_on |
|
121 | 121 | Problem.find(:all, |
|
122 | 122 | :conditions => "available = 0").each do |problem| |
|
123 | 123 | problem.available = true |
|
124 | 124 | problem.save |
|
125 | 125 | end |
|
126 | 126 | redirect_to :action => 'list' |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def stat |
|
130 | 130 | @problem = Problem.find(params[:id]) |
|
131 | 131 | if !@problem.available |
|
132 | 132 | redirect_to :controller => 'main', :action => 'list' |
|
133 | 133 | else |
|
134 | 134 | @submissions = Submission.find_all_last_by_problem(params[:id]) |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | |
|
138 | 138 | def manage |
|
139 | 139 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
140 | 140 | end |
|
141 | 141 | |
|
142 | 142 | def do_manage |
|
143 | 143 | if params.has_key? 'change_date_added' |
|
144 | 144 | change_date_added |
|
145 | 145 | else params.has_key? 'add_to_contest' |
|
146 | 146 | add_to_contest |
|
147 | 147 | end |
|
148 | 148 | redirect_to :action => 'manage' |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | def import |
|
152 | 152 | @allow_test_pair_import = allow_test_pair_import? |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | def do_import |
|
156 | 156 | old_problem = Problem.find_by_name(params[:name]) |
|
157 | 157 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
158 | 158 | params.delete :import_to_db |
|
159 | 159 | end |
|
160 | 160 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
161 | 161 | old_problem) |
|
162 | 162 | |
|
163 |
- if @problem.errors. |
|
|
163 | + if !@problem.errors.empty? | |
|
164 | 164 | render :action => 'import' and return |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | if old_problem!=nil |
|
168 | 168 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
169 | 169 | end |
|
170 | 170 | @log = import_log |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def remove_contest |
|
174 | 174 | problem = Problem.find(params[:id]) |
|
175 | 175 | contest = Contest.find(params[:contest_id]) |
|
176 | 176 | if problem!=nil and contest!=nil |
|
177 | 177 | problem.contests.delete(contest) |
|
178 | 178 | end |
|
179 | 179 | redirect_to :action => 'manage' |
|
180 | 180 | end |
|
181 | 181 | |
|
182 | 182 | ################################## |
|
183 | 183 | protected |
|
184 | 184 | |
|
185 | 185 | def allow_test_pair_import? |
|
186 | 186 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
187 | 187 | return ALLOW_TEST_PAIR_IMPORT |
|
188 | 188 | else |
|
189 | 189 | return false |
|
190 | 190 | end |
|
191 | 191 | end |
|
192 | 192 | |
|
193 | 193 | def change_date_added |
|
194 | 194 | problems = get_problems_from_params |
|
195 | 195 | year = params[:date_added][:year].to_i |
|
196 | 196 | month = params[:date_added][:month].to_i |
|
197 | 197 | day = params[:date_added][:day].to_i |
|
198 | 198 | date = Date.new(year,month,day) |
|
199 | 199 | problems.each do |p| |
|
200 | 200 | p.date_added = date |
|
201 | 201 | p.save |
|
202 | 202 | end |
|
203 | 203 | end |
|
204 | 204 | |
|
205 | 205 | def add_to_contest |
|
206 | 206 | problems = get_problems_from_params |
|
207 | 207 | contest = Contest.find(params[:contest][:id]) |
|
208 | 208 | if contest!=nil and contest.enabled |
|
209 | 209 | problems.each do |p| |
|
210 | 210 | p.contests << contest |
|
211 | 211 | end |
@@ -1,114 +1,114 | |||
|
1 | 1 | class Problem < ActiveRecord::Base |
|
2 | 2 | |
|
3 | 3 | belongs_to :description |
|
4 | 4 | has_and_belongs_to_many :contests, :uniq => true |
|
5 | 5 | has_many :test_pairs, :dependent => :delete_all |
|
6 | 6 | |
|
7 | 7 | validates_presence_of :name |
|
8 | 8 | validates_format_of :name, :with => /^\w+$/ |
|
9 | 9 | validates_presence_of :full_name |
|
10 | 10 | |
|
11 | 11 | scope :available, :conditions => {:available => true} |
|
12 | 12 | |
|
13 | 13 | DEFAULT_TIME_LIMIT = 1 |
|
14 | 14 | DEFAULT_MEMORY_LIMIT = 32 |
|
15 | 15 | |
|
16 | 16 | def self.find_available_problems |
|
17 | 17 | Problem.available.all(:order => "date_added DESC") |
|
18 | 18 | end |
|
19 | 19 | |
|
20 | 20 | def self.create_from_import_form_params(params, old_problem=nil) |
|
21 | 21 | org_problem = old_problem || Problem.new |
|
22 | 22 | import_params, problem = Problem.extract_params_and_check(params, |
|
23 | 23 | org_problem) |
|
24 | 24 | |
|
25 |
- if problem.errors. |
|
|
25 | + if !problem.errors.empty? | |
|
26 | 26 | return problem, 'Error importing' |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | problem.full_score = 100 |
|
30 | 30 | problem.date_added = Time.new |
|
31 | 31 | problem.test_allowed = true |
|
32 | 32 | problem.output_only = false |
|
33 | 33 | problem.available = false |
|
34 | 34 | |
|
35 | 35 | if not problem.save |
|
36 | 36 | return problem, 'Error importing' |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | import_to_db = params.has_key? :import_to_db |
|
40 | 40 | |
|
41 | 41 | importer = TestdataImporter.new(problem) |
|
42 | 42 | |
|
43 | 43 | if not importer.import_from_file(import_params[:file], |
|
44 | 44 | import_params[:time_limit], |
|
45 | 45 | import_params[:memory_limit], |
|
46 | 46 | import_to_db) |
|
47 | 47 | problem.errors.add_to_base('Import error.') |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | return problem, importer.log_msg |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def self.download_file_basedir |
|
54 | 54 | return "#{Rails.root}/data/tasks" |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | protected |
|
58 | 58 | |
|
59 | 59 | def self.to_i_or_default(st, default) |
|
60 | 60 | if st!='' |
|
61 | 61 | result = st.to_i |
|
62 | 62 | end |
|
63 | 63 | result ||= default |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def self.to_f_or_default(st, default) |
|
67 | 67 | if st!='' |
|
68 | 68 | result = st.to_f |
|
69 | 69 | end |
|
70 | 70 | result ||= default |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def self.extract_params_and_check(params, problem) |
|
74 | 74 | time_limit = Problem.to_f_or_default(params[:time_limit], |
|
75 | 75 | DEFAULT_TIME_LIMIT) |
|
76 | 76 | memory_limit = Problem.to_i_or_default(params[:memory_limit], |
|
77 | 77 | DEFAULT_MEMORY_LIMIT) |
|
78 | 78 | |
|
79 | 79 | if time_limit<=0 or time_limit >60 |
|
80 | 80 | problem.errors.add_to_base('Time limit out of range.') |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | if memory_limit==0 and params[:memory_limit]!='0' |
|
84 | 84 | problem.errors.add_to_base('Memory limit format errors.') |
|
85 | 85 | elsif memory_limit<=0 or memory_limit >512 |
|
86 | 86 | problem.errors.add_to_base('Memory limit out of range.') |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | if params[:file]==nil or params[:file]=='' |
|
90 | 90 | problem.errors.add_to_base('No testdata file.') |
|
91 | 91 | end |
|
92 | 92 | |
|
93 | 93 | file = params[:file] |
|
94 | 94 | |
|
95 |
- if problem.errors. |
|
|
95 | + if !problem.errors.empty? | |
|
96 | 96 | return nil, problem |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | problem.name = params[:name] |
|
100 | 100 | if params[:full_name]!='' |
|
101 | 101 | problem.full_name = params[:full_name] |
|
102 | 102 | else |
|
103 | 103 | problem.full_name = params[:name] |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | return [{ |
|
107 | 107 | :time_limit => time_limit, |
|
108 | 108 | :memory_limit => memory_limit, |
|
109 | 109 | :file => file |
|
110 | 110 | }, |
|
111 | 111 | problem] |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | end |
You need to be logged in to leave comments.
Login now