Description:
fixed problem insertion attribute error
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r775:3d1a060b0378 - - 1 file changed: 5 inserted, 1 deleted
@@ -1,80 +1,80 | |||||
|
1 | class ProblemsController < ApplicationController |
|
1 | class ProblemsController < ApplicationController |
|
2 |
|
2 | ||
|
3 | before_action :authenticate, :authorization |
|
3 | before_action :authenticate, :authorization |
|
4 | before_action :testcase_authorization, only: [:show_testcase] |
|
4 | before_action :testcase_authorization, only: [:show_testcase] |
|
5 |
|
5 | ||
|
6 | in_place_edit_for :problem, :name |
|
6 | in_place_edit_for :problem, :name |
|
7 | in_place_edit_for :problem, :full_name |
|
7 | in_place_edit_for :problem, :full_name |
|
8 | in_place_edit_for :problem, :full_score |
|
8 | in_place_edit_for :problem, :full_score |
|
9 |
|
9 | ||
|
10 | def index |
|
10 | def index |
|
11 | @problems = Problem.order(date_added: :desc) |
|
11 | @problems = Problem.order(date_added: :desc) |
|
12 | end |
|
12 | end |
|
13 |
|
13 | ||
|
14 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
14 | # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html) |
|
15 | verify :method => :post, :only => [ :create, :quick_create, |
|
15 | verify :method => :post, :only => [ :create, :quick_create, |
|
16 | :do_manage, |
|
16 | :do_manage, |
|
17 | :do_import, |
|
17 | :do_import, |
|
18 | ], |
|
18 | ], |
|
19 | :redirect_to => { :action => :index } |
|
19 | :redirect_to => { :action => :index } |
|
20 |
|
20 | ||
|
21 | def show |
|
21 | def show |
|
22 | @problem = Problem.find(params[:id]) |
|
22 | @problem = Problem.find(params[:id]) |
|
23 | end |
|
23 | end |
|
24 |
|
24 | ||
|
25 | def new |
|
25 | def new |
|
26 | @problem = Problem.new |
|
26 | @problem = Problem.new |
|
27 | @description = nil |
|
27 | @description = nil |
|
28 | end |
|
28 | end |
|
29 |
|
29 | ||
|
30 | def create |
|
30 | def create |
|
31 | @problem = Problem.new(problem_params) |
|
31 | @problem = Problem.new(problem_params) |
|
32 |
- @description = Description.new( |
|
32 | + @description = Description.new(description_params) |
|
33 | if @description.body!='' |
|
33 | if @description.body!='' |
|
34 | if !@description.save |
|
34 | if !@description.save |
|
35 | render :action => new and return |
|
35 | render :action => new and return |
|
36 | end |
|
36 | end |
|
37 | else |
|
37 | else |
|
38 | @description = nil |
|
38 | @description = nil |
|
39 | end |
|
39 | end |
|
40 | @problem.description = @description |
|
40 | @problem.description = @description |
|
41 | if @problem.save |
|
41 | if @problem.save |
|
42 | flash[:notice] = 'Problem was successfully created.' |
|
42 | flash[:notice] = 'Problem was successfully created.' |
|
43 | redirect_to action: :index |
|
43 | redirect_to action: :index |
|
44 | else |
|
44 | else |
|
45 | render :action => 'new' |
|
45 | render :action => 'new' |
|
46 | end |
|
46 | end |
|
47 | end |
|
47 | end |
|
48 |
|
48 | ||
|
49 | def quick_create |
|
49 | def quick_create |
|
50 | @problem = Problem.new(problem_params) |
|
50 | @problem = Problem.new(problem_params) |
|
51 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
51 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
52 | @problem.full_score = 100 |
|
52 | @problem.full_score = 100 |
|
53 | @problem.available = false |
|
53 | @problem.available = false |
|
54 | @problem.test_allowed = true |
|
54 | @problem.test_allowed = true |
|
55 | @problem.output_only = false |
|
55 | @problem.output_only = false |
|
56 | @problem.date_added = Time.new |
|
56 | @problem.date_added = Time.new |
|
57 | if @problem.save |
|
57 | if @problem.save |
|
58 | flash[:notice] = 'Problem was successfully created.' |
|
58 | flash[:notice] = 'Problem was successfully created.' |
|
59 | redirect_to action: :index |
|
59 | redirect_to action: :index |
|
60 | else |
|
60 | else |
|
61 | flash[:notice] = 'Error saving problem' |
|
61 | flash[:notice] = 'Error saving problem' |
|
62 | redirect_to action: :index |
|
62 | redirect_to action: :index |
|
63 | end |
|
63 | end |
|
64 | end |
|
64 | end |
|
65 |
|
65 | ||
|
66 | def edit |
|
66 | def edit |
|
67 | @problem = Problem.find(params[:id]) |
|
67 | @problem = Problem.find(params[:id]) |
|
68 | @description = @problem.description |
|
68 | @description = @problem.description |
|
69 | end |
|
69 | end |
|
70 |
|
70 | ||
|
71 | def update |
|
71 | def update |
|
72 | @problem = Problem.find(params[:id]) |
|
72 | @problem = Problem.find(params[:id]) |
|
73 | @description = @problem.description |
|
73 | @description = @problem.description |
|
74 | if @description.nil? and params[:description][:body]!='' |
|
74 | if @description.nil? and params[:description][:body]!='' |
|
75 | @description = Description.new(params[:description]) |
|
75 | @description = Description.new(params[:description]) |
|
76 | if !@description.save |
|
76 | if !@description.save |
|
77 | flash[:notice] = 'Error saving description' |
|
77 | flash[:notice] = 'Error saving description' |
|
78 | render :action => 'edit' and return |
|
78 | render :action => 'edit' and return |
|
79 | end |
|
79 | end |
|
80 | @problem.description = @description |
|
80 | @problem.description = @description |
@@ -207,96 +207,100 | |||||
|
207 | failed << p.full_name |
|
207 | failed << p.full_name |
|
208 | end |
|
208 | end |
|
209 | end |
|
209 | end |
|
210 | flash[:success] = "The following problems are added to the group #{group.name}: " + ok.join(', ') if ok.count > 0 |
|
210 | flash[:success] = "The following problems are added to the group #{group.name}: " + ok.join(', ') if ok.count > 0 |
|
211 | flash[:alert] = "The following problems are already in the group #{group.name}: " + failed.join(', ') if failed.count > 0 |
|
211 | flash[:alert] = "The following problems are already in the group #{group.name}: " + failed.join(', ') if failed.count > 0 |
|
212 | elsif params.has_key? 'add_tags' |
|
212 | elsif params.has_key? 'add_tags' |
|
213 | get_problems_from_params.each do |p| |
|
213 | get_problems_from_params.each do |p| |
|
214 | p.tag_ids += params[:tag_ids] |
|
214 | p.tag_ids += params[:tag_ids] |
|
215 | end |
|
215 | end |
|
216 | end |
|
216 | end |
|
217 |
|
217 | ||
|
218 | redirect_to :action => 'manage' |
|
218 | redirect_to :action => 'manage' |
|
219 | end |
|
219 | end |
|
220 |
|
220 | ||
|
221 | def import |
|
221 | def import |
|
222 | @allow_test_pair_import = allow_test_pair_import? |
|
222 | @allow_test_pair_import = allow_test_pair_import? |
|
223 | end |
|
223 | end |
|
224 |
|
224 | ||
|
225 | def do_import |
|
225 | def do_import |
|
226 | old_problem = Problem.find_by_name(params[:name]) |
|
226 | old_problem = Problem.find_by_name(params[:name]) |
|
227 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
227 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
228 | params.delete :import_to_db |
|
228 | params.delete :import_to_db |
|
229 | end |
|
229 | end |
|
230 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
230 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
231 | old_problem) |
|
231 | old_problem) |
|
232 |
|
232 | ||
|
233 | if !@problem.errors.empty? |
|
233 | if !@problem.errors.empty? |
|
234 | render :action => 'import' and return |
|
234 | render :action => 'import' and return |
|
235 | end |
|
235 | end |
|
236 |
|
236 | ||
|
237 | if old_problem!=nil |
|
237 | if old_problem!=nil |
|
238 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
238 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
239 | end |
|
239 | end |
|
240 | @log = import_log |
|
240 | @log = import_log |
|
241 | end |
|
241 | end |
|
242 |
|
242 | ||
|
243 | def remove_contest |
|
243 | def remove_contest |
|
244 | problem = Problem.find(params[:id]) |
|
244 | problem = Problem.find(params[:id]) |
|
245 | contest = Contest.find(params[:contest_id]) |
|
245 | contest = Contest.find(params[:contest_id]) |
|
246 | if problem!=nil and contest!=nil |
|
246 | if problem!=nil and contest!=nil |
|
247 | problem.contests.delete(contest) |
|
247 | problem.contests.delete(contest) |
|
248 | end |
|
248 | end |
|
249 | redirect_to :action => 'manage' |
|
249 | redirect_to :action => 'manage' |
|
250 | end |
|
250 | end |
|
251 |
|
251 | ||
|
252 | ################################## |
|
252 | ################################## |
|
253 | protected |
|
253 | protected |
|
254 |
|
254 | ||
|
|
255 | + def description_params | ||
|
|
256 | + params.require(:description).permit(:body, :markdowned) | ||
|
|
257 | + end | ||
|
|
258 | + | ||
|
255 | def allow_test_pair_import? |
|
259 | def allow_test_pair_import? |
|
256 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
260 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
257 | return ALLOW_TEST_PAIR_IMPORT |
|
261 | return ALLOW_TEST_PAIR_IMPORT |
|
258 | else |
|
262 | else |
|
259 | return false |
|
263 | return false |
|
260 | end |
|
264 | end |
|
261 | end |
|
265 | end |
|
262 |
|
266 | ||
|
263 | def change_date_added |
|
267 | def change_date_added |
|
264 | problems = get_problems_from_params |
|
268 | problems = get_problems_from_params |
|
265 | date = Date.parse(params[:date_added]) |
|
269 | date = Date.parse(params[:date_added]) |
|
266 | problems.each do |p| |
|
270 | problems.each do |p| |
|
267 | p.date_added = date |
|
271 | p.date_added = date |
|
268 | p.save |
|
272 | p.save |
|
269 | end |
|
273 | end |
|
270 | end |
|
274 | end |
|
271 |
|
275 | ||
|
272 | def add_to_contest |
|
276 | def add_to_contest |
|
273 | problems = get_problems_from_params |
|
277 | problems = get_problems_from_params |
|
274 | contest = Contest.find(params[:contest][:id]) |
|
278 | contest = Contest.find(params[:contest][:id]) |
|
275 | if contest!=nil and contest.enabled |
|
279 | if contest!=nil and contest.enabled |
|
276 | problems.each do |p| |
|
280 | problems.each do |p| |
|
277 | p.contests << contest |
|
281 | p.contests << contest |
|
278 | end |
|
282 | end |
|
279 | end |
|
283 | end |
|
280 | end |
|
284 | end |
|
281 |
|
285 | ||
|
282 | def set_available(avail) |
|
286 | def set_available(avail) |
|
283 | problems = get_problems_from_params |
|
287 | problems = get_problems_from_params |
|
284 | problems.each do |p| |
|
288 | problems.each do |p| |
|
285 | p.available = avail |
|
289 | p.available = avail |
|
286 | p.save |
|
290 | p.save |
|
287 | end |
|
291 | end |
|
288 | end |
|
292 | end |
|
289 |
|
293 | ||
|
290 | def get_problems_from_params |
|
294 | def get_problems_from_params |
|
291 | problems = [] |
|
295 | problems = [] |
|
292 | params.keys.each do |k| |
|
296 | params.keys.each do |k| |
|
293 | if k.index('prob-')==0 |
|
297 | if k.index('prob-')==0 |
|
294 | name, id, order = k.split('-') |
|
298 | name, id, order = k.split('-') |
|
295 | problems << Problem.find(id) |
|
299 | problems << Problem.find(id) |
|
296 | end |
|
300 | end |
|
297 | end |
|
301 | end |
|
298 | problems |
|
302 | problems |
|
299 | end |
|
303 | end |
|
300 |
|
304 | ||
|
301 | def get_problems_stat |
|
305 | def get_problems_stat |
|
302 | end |
|
306 | end |
You need to be logged in to leave comments.
Login now