Description:
add option to update PDF file of a problem in /problem/edit/:id
(grafted from 15b2554bf365f8639b5e94db4f1d9a277007808a)
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r364:9bd14bb6e95d - - 3 files changed: 25 inserted, 1 deleted
@@ -1,189 +1,211 | |||||
|
1 | class ProblemsController < ApplicationController |
|
1 | class ProblemsController < ApplicationController |
|
2 |
|
2 | ||
|
3 | before_filter :authenticate, :authorization |
|
3 | before_filter :authenticate, :authorization |
|
4 |
|
4 | ||
|
5 | in_place_edit_for :problem, :name |
|
5 | in_place_edit_for :problem, :name |
|
6 | in_place_edit_for :problem, :full_name |
|
6 | in_place_edit_for :problem, :full_name |
|
7 | in_place_edit_for :problem, :full_score |
|
7 | in_place_edit_for :problem, :full_score |
|
8 |
|
8 | ||
|
9 | def index |
|
9 | def index |
|
10 | list |
|
10 | list |
|
11 | render :action => 'list' |
|
11 | render :action => 'list' |
|
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 => [ :destroy, |
|
15 | verify :method => :post, :only => [ :destroy, |
|
16 | :create, :quick_create, |
|
16 | :create, :quick_create, |
|
17 | :do_manage, |
|
17 | :do_manage, |
|
18 | :do_import, |
|
18 | :do_import, |
|
19 | :update ], |
|
19 | :update ], |
|
20 | :redirect_to => { :action => :list } |
|
20 | :redirect_to => { :action => :list } |
|
21 |
|
21 | ||
|
22 | def list |
|
22 | def list |
|
23 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
23 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
24 | end |
|
24 | end |
|
25 |
|
25 | ||
|
26 | def show |
|
26 | def show |
|
27 | @problem = Problem.find(params[:id]) |
|
27 | @problem = Problem.find(params[:id]) |
|
28 | end |
|
28 | end |
|
29 |
|
29 | ||
|
30 | def new |
|
30 | def new |
|
31 | @problem = Problem.new |
|
31 | @problem = Problem.new |
|
32 | @description = nil |
|
32 | @description = nil |
|
33 | end |
|
33 | end |
|
34 |
|
34 | ||
|
35 | def create |
|
35 | def create |
|
36 | @problem = Problem.new(params[:problem]) |
|
36 | @problem = Problem.new(params[:problem]) |
|
37 | @description = Description.new(params[:description]) |
|
37 | @description = Description.new(params[:description]) |
|
38 | if @description.body!='' |
|
38 | if @description.body!='' |
|
39 | if !@description.save |
|
39 | if !@description.save |
|
40 | render :action => new and return |
|
40 | render :action => new and return |
|
41 | end |
|
41 | end |
|
42 | else |
|
42 | else |
|
43 | @description = nil |
|
43 | @description = nil |
|
44 | end |
|
44 | end |
|
45 | @problem.description = @description |
|
45 | @problem.description = @description |
|
46 | if @problem.save |
|
46 | if @problem.save |
|
47 | flash[:notice] = 'Problem was successfully created.' |
|
47 | flash[:notice] = 'Problem was successfully created.' |
|
48 | redirect_to :action => 'list' |
|
48 | redirect_to :action => 'list' |
|
49 | else |
|
49 | else |
|
50 | render :action => 'new' |
|
50 | render :action => 'new' |
|
51 | end |
|
51 | end |
|
52 | end |
|
52 | end |
|
53 |
|
53 | ||
|
54 | def quick_create |
|
54 | def quick_create |
|
55 | @problem = Problem.new(params[:problem]) |
|
55 | @problem = Problem.new(params[:problem]) |
|
56 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
56 | @problem.full_name = @problem.name if @problem.full_name == '' |
|
57 | @problem.full_score = 100 |
|
57 | @problem.full_score = 100 |
|
58 | @problem.available = false |
|
58 | @problem.available = false |
|
59 | @problem.test_allowed = true |
|
59 | @problem.test_allowed = true |
|
60 | @problem.output_only = false |
|
60 | @problem.output_only = false |
|
61 | @problem.date_added = Time.new |
|
61 | @problem.date_added = Time.new |
|
62 | if @problem.save |
|
62 | if @problem.save |
|
63 | flash[:notice] = 'Problem was successfully created.' |
|
63 | flash[:notice] = 'Problem was successfully created.' |
|
64 | redirect_to :action => 'list' |
|
64 | redirect_to :action => 'list' |
|
65 | else |
|
65 | else |
|
66 | flash[:notice] = 'Error saving problem' |
|
66 | flash[:notice] = 'Error saving problem' |
|
67 | redirect_to :action => 'list' |
|
67 | redirect_to :action => 'list' |
|
68 | end |
|
68 | end |
|
69 | end |
|
69 | end |
|
70 |
|
70 | ||
|
71 | def edit |
|
71 | def edit |
|
72 | @problem = Problem.find(params[:id]) |
|
72 | @problem = Problem.find(params[:id]) |
|
73 | @description = @problem.description |
|
73 | @description = @problem.description |
|
74 | end |
|
74 | end |
|
75 |
|
75 | ||
|
76 | def update |
|
76 | def update |
|
77 | @problem = Problem.find(params[:id]) |
|
77 | @problem = Problem.find(params[:id]) |
|
78 | @description = @problem.description |
|
78 | @description = @problem.description |
|
79 | if @description == nil and params[:description][:body]!='' |
|
79 | if @description == nil and params[:description][:body]!='' |
|
80 | @description = Description.new(params[:description]) |
|
80 | @description = Description.new(params[:description]) |
|
81 | if !@description.save |
|
81 | if !@description.save |
|
82 | flash[:notice] = 'Error saving description' |
|
82 | flash[:notice] = 'Error saving description' |
|
83 | render :action => 'edit' and return |
|
83 | render :action => 'edit' and return |
|
84 | end |
|
84 | end |
|
85 | @problem.description = @description |
|
85 | @problem.description = @description |
|
86 | elsif @description!=nil |
|
86 | elsif @description!=nil |
|
87 | if !@description.update_attributes(params[:description]) |
|
87 | if !@description.update_attributes(params[:description]) |
|
88 | flash[:notice] = 'Error saving description' |
|
88 | flash[:notice] = 'Error saving description' |
|
89 | render :action => 'edit' and return |
|
89 | render :action => 'edit' and return |
|
90 | end |
|
90 | end |
|
91 | end |
|
91 | end |
|
|
92 | + if params[:file] and params[:file].content_type != 'application/pdf' | ||
|
|
93 | + flash[:notice] = 'Error: Uploaded file is not PDF' | ||
|
|
94 | + render :action => 'edit' and return | ||
|
|
95 | + end | ||
|
92 | if @problem.update_attributes(params[:problem]) |
|
96 | if @problem.update_attributes(params[:problem]) |
|
93 | flash[:notice] = 'Problem was successfully updated.' |
|
97 | flash[:notice] = 'Problem was successfully updated.' |
|
|
98 | + unless params[:file] == nil or params[:file] == '' | ||
|
|
99 | + flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.' | ||
|
|
100 | + out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}" | ||
|
|
101 | + if not FileTest.exists? out_dirname | ||
|
|
102 | + Dir.mkdir out_dirname | ||
|
|
103 | + end | ||
|
|
104 | + | ||
|
|
105 | + out_filename = "#{out_dirname}/#{@problem.name}.pdf" | ||
|
|
106 | + if FileTest.exists? out_filename | ||
|
|
107 | + File.delete out_filename | ||
|
|
108 | + end | ||
|
|
109 | + | ||
|
|
110 | + File.open(out_filename,"wb") do |file| | ||
|
|
111 | + file.write(params[:file].read) | ||
|
|
112 | + end | ||
|
|
113 | + @problem.description_filename = "#{@problem.name}.pdf" | ||
|
|
114 | + @problem.save | ||
|
|
115 | + end | ||
|
94 | redirect_to :action => 'show', :id => @problem |
|
116 | redirect_to :action => 'show', :id => @problem |
|
95 | else |
|
117 | else |
|
96 | render :action => 'edit' |
|
118 | render :action => 'edit' |
|
97 | end |
|
119 | end |
|
98 | end |
|
120 | end |
|
99 |
|
121 | ||
|
100 | def destroy |
|
122 | def destroy |
|
101 | Problem.find(params[:id]).destroy |
|
123 | Problem.find(params[:id]).destroy |
|
102 | redirect_to :action => 'list' |
|
124 | redirect_to :action => 'list' |
|
103 | end |
|
125 | end |
|
104 |
|
126 | ||
|
105 | def toggle |
|
127 | def toggle |
|
106 | @problem = Problem.find(params[:id]) |
|
128 | @problem = Problem.find(params[:id]) |
|
107 | @problem.available = !(@problem.available) |
|
129 | @problem.available = !(@problem.available) |
|
108 | @problem.save |
|
130 | @problem.save |
|
109 | end |
|
131 | end |
|
110 |
|
132 | ||
|
111 | def turn_all_off |
|
133 | def turn_all_off |
|
112 | Problem.find(:all, |
|
134 | Problem.find(:all, |
|
113 | :conditions => "available = 1").each do |problem| |
|
135 | :conditions => "available = 1").each do |problem| |
|
114 | problem.available = false |
|
136 | problem.available = false |
|
115 | problem.save |
|
137 | problem.save |
|
116 | end |
|
138 | end |
|
117 | redirect_to :action => 'list' |
|
139 | redirect_to :action => 'list' |
|
118 | end |
|
140 | end |
|
119 |
|
141 | ||
|
120 | def turn_all_on |
|
142 | def turn_all_on |
|
121 | Problem.find(:all, |
|
143 | Problem.find(:all, |
|
122 | :conditions => "available = 0").each do |problem| |
|
144 | :conditions => "available = 0").each do |problem| |
|
123 | problem.available = true |
|
145 | problem.available = true |
|
124 | problem.save |
|
146 | problem.save |
|
125 | end |
|
147 | end |
|
126 | redirect_to :action => 'list' |
|
148 | redirect_to :action => 'list' |
|
127 | end |
|
149 | end |
|
128 |
|
150 | ||
|
129 | def stat |
|
151 | def stat |
|
130 | @problem = Problem.find(params[:id]) |
|
152 | @problem = Problem.find(params[:id]) |
|
131 | if !@problem.available |
|
153 | if !@problem.available |
|
132 | redirect_to :controller => 'main', :action => 'list' |
|
154 | redirect_to :controller => 'main', :action => 'list' |
|
133 | else |
|
155 | else |
|
134 | @submissions = Submission.find_all_last_by_problem(params[:id]) |
|
156 | @submissions = Submission.find_all_last_by_problem(params[:id]) |
|
135 | end |
|
157 | end |
|
136 | end |
|
158 | end |
|
137 |
|
159 | ||
|
138 | def manage |
|
160 | def manage |
|
139 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
161 | @problems = Problem.find(:all, :order => 'date_added DESC') |
|
140 | end |
|
162 | end |
|
141 |
|
163 | ||
|
142 | def do_manage |
|
164 | def do_manage |
|
143 | if params.has_key? 'change_date_added' |
|
165 | if params.has_key? 'change_date_added' |
|
144 | change_date_added |
|
166 | change_date_added |
|
145 | else params.has_key? 'add_to_contest' |
|
167 | else params.has_key? 'add_to_contest' |
|
146 | add_to_contest |
|
168 | add_to_contest |
|
147 | end |
|
169 | end |
|
148 | redirect_to :action => 'manage' |
|
170 | redirect_to :action => 'manage' |
|
149 | end |
|
171 | end |
|
150 |
|
172 | ||
|
151 | def import |
|
173 | def import |
|
152 | @allow_test_pair_import = allow_test_pair_import? |
|
174 | @allow_test_pair_import = allow_test_pair_import? |
|
153 | end |
|
175 | end |
|
154 |
|
176 | ||
|
155 | def do_import |
|
177 | def do_import |
|
156 | old_problem = Problem.find_by_name(params[:name]) |
|
178 | old_problem = Problem.find_by_name(params[:name]) |
|
157 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
179 | if !allow_test_pair_import? and params.has_key? :import_to_db |
|
158 | params.delete :import_to_db |
|
180 | params.delete :import_to_db |
|
159 | end |
|
181 | end |
|
160 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
182 | @problem, import_log = Problem.create_from_import_form_params(params, |
|
161 | old_problem) |
|
183 | old_problem) |
|
162 |
|
184 | ||
|
163 | if !@problem.errors.empty? |
|
185 | if !@problem.errors.empty? |
|
164 | render :action => 'import' and return |
|
186 | render :action => 'import' and return |
|
165 | end |
|
187 | end |
|
166 |
|
188 | ||
|
167 | if old_problem!=nil |
|
189 | if old_problem!=nil |
|
168 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
190 | flash[:notice] = "The test data has been replaced for problem #{@problem.name}" |
|
169 | end |
|
191 | end |
|
170 | @log = import_log |
|
192 | @log = import_log |
|
171 | end |
|
193 | end |
|
172 |
|
194 | ||
|
173 | def remove_contest |
|
195 | def remove_contest |
|
174 | problem = Problem.find(params[:id]) |
|
196 | problem = Problem.find(params[:id]) |
|
175 | contest = Contest.find(params[:contest_id]) |
|
197 | contest = Contest.find(params[:contest_id]) |
|
176 | if problem!=nil and contest!=nil |
|
198 | if problem!=nil and contest!=nil |
|
177 | problem.contests.delete(contest) |
|
199 | problem.contests.delete(contest) |
|
178 | end |
|
200 | end |
|
179 | redirect_to :action => 'manage' |
|
201 | redirect_to :action => 'manage' |
|
180 | end |
|
202 | end |
|
181 |
|
203 | ||
|
182 | ################################## |
|
204 | ################################## |
|
183 | protected |
|
205 | protected |
|
184 |
|
206 | ||
|
185 | def allow_test_pair_import? |
|
207 | def allow_test_pair_import? |
|
186 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
208 | if defined? ALLOW_TEST_PAIR_IMPORT |
|
187 | return ALLOW_TEST_PAIR_IMPORT |
|
209 | return ALLOW_TEST_PAIR_IMPORT |
|
188 | else |
|
210 | else |
|
189 | return false |
|
211 | return false |
@@ -1,52 +1,54 | |||||
|
1 | <%= error_messages_for 'problem' %> |
|
1 | <%= error_messages_for 'problem' %> |
|
2 |
|
2 | ||
|
3 | <!--[form:problem]--> |
|
3 | <!--[form:problem]--> |
|
4 | <p><label for="problem_name">Name</label><br/> |
|
4 | <p><label for="problem_name">Name</label><br/> |
|
5 | <%= text_field 'problem', 'name' %></p> |
|
5 | <%= text_field 'problem', 'name' %></p> |
|
6 |
|
6 | ||
|
7 | <p><label for="problem_full_name">Full name</label><br/> |
|
7 | <p><label for="problem_full_name">Full name</label><br/> |
|
8 | <%= text_field 'problem', 'full_name' %></p> |
|
8 | <%= text_field 'problem', 'full_name' %></p> |
|
9 |
|
9 | ||
|
10 | <p><label for="problem_full_score">Full score</label><br/> |
|
10 | <p><label for="problem_full_score">Full score</label><br/> |
|
11 | <%= text_field 'problem', 'full_score' %></p> |
|
11 | <%= text_field 'problem', 'full_score' %></p> |
|
12 |
|
12 | ||
|
13 | <p><label for="problem_date_added">Date added</label><br/> |
|
13 | <p><label for="problem_date_added">Date added</label><br/> |
|
14 | <%= date_select 'problem', 'date_added' %></p> |
|
14 | <%= date_select 'problem', 'date_added' %></p> |
|
15 |
|
15 | ||
|
16 | <% |
|
16 | <% |
|
17 | # TODO: these should be put in model Problem, but I can't think of |
|
17 | # TODO: these should be put in model Problem, but I can't think of |
|
18 | # nice default values for them. These values look fine only |
|
18 | # nice default values for them. These values look fine only |
|
19 | # in this case (of lazily adding new problems). |
|
19 | # in this case (of lazily adding new problems). |
|
20 | @problem.available = true if @problem!=nil and @problem.available==nil |
|
20 | @problem.available = true if @problem!=nil and @problem.available==nil |
|
21 | @problem.test_allowed = true if @problem!=nil and @problem.test_allowed==nil |
|
21 | @problem.test_allowed = true if @problem!=nil and @problem.test_allowed==nil |
|
22 | @problem.output_only = false if @problem!=nil and @problem.output_only==nil |
|
22 | @problem.output_only = false if @problem!=nil and @problem.output_only==nil |
|
23 | %> |
|
23 | %> |
|
24 |
|
24 | ||
|
25 | <p> |
|
25 | <p> |
|
26 | <label for="problem_available">Available?</label> |
|
26 | <label for="problem_available">Available?</label> |
|
27 | <%= check_box :problem, :available %> |
|
27 | <%= check_box :problem, :available %> |
|
28 |
|
28 | ||
|
29 | <label for="problem_test_allowed">Test allowed?</label> |
|
29 | <label for="problem_test_allowed">Test allowed?</label> |
|
30 | <%= check_box :problem, :test_allowed %> |
|
30 | <%= check_box :problem, :test_allowed %> |
|
31 |
|
31 | ||
|
32 | <label for="problem_output_only">Output only?</label> |
|
32 | <label for="problem_output_only">Output only?</label> |
|
33 | <%= check_box :problem, :output_only %> |
|
33 | <%= check_box :problem, :output_only %> |
|
34 | </p> |
|
34 | </p> |
|
35 |
|
35 | ||
|
36 | <%= error_messages_for 'description' %> |
|
36 | <%= error_messages_for 'description' %> |
|
37 |
|
37 | ||
|
38 | <p><label for="description_body">Description</label><br/> |
|
38 | <p><label for="description_body">Description</label><br/> |
|
39 | <%= text_area :description, :body, :rows => 10, :cols => 80 %></p> |
|
39 | <%= text_area :description, :body, :rows => 10, :cols => 80 %></p> |
|
40 |
|
40 | ||
|
41 | <p><label for="description_markdowned">Markdowned?</label> |
|
41 | <p><label for="description_markdowned">Markdowned?</label> |
|
42 | <%= select "description", |
|
42 | <%= select "description", |
|
43 | "markdowned", |
|
43 | "markdowned", |
|
44 | [['True',true],['False',false]], |
|
44 | [['True',true],['False',false]], |
|
45 | {:selected => (@description) ? @description.markdowned : false } |
|
45 | {:selected => (@description) ? @description.markdowned : false } |
|
46 | %></p> |
|
46 | %></p> |
|
47 |
|
47 | ||
|
48 | <p><label for="problem_url">URL</label><br/> |
|
48 | <p><label for="problem_url">URL</label><br/> |
|
49 | <%= text_field 'problem', 'url' %></p> |
|
49 | <%= text_field 'problem', 'url' %></p> |
|
50 |
|
50 | ||
|
|
51 | + <p>Task PDF <%= file_field_tag 'file' %></p> | ||
|
|
52 | + | ||
|
51 |
|
53 | ||
|
52 | <!--[eoform:problem]--> |
|
54 | <!--[eoform:problem]--> |
@@ -1,9 +1,9 | |||||
|
1 | <h1>Editing problem</h1> |
|
1 | <h1>Editing problem</h1> |
|
2 |
|
2 | ||
|
3 |
- <%= form_tag |
|
3 | + <%= form_tag({action: 'update', id: @problem},multipart: true) do %> |
|
4 | <%= render :partial => 'form' %> |
|
4 | <%= render :partial => 'form' %> |
|
5 | <%= submit_tag 'Edit' %> |
|
5 | <%= submit_tag 'Edit' %> |
|
6 | <% end %> |
|
6 | <% end %> |
|
7 |
|
7 | ||
|
8 | <%= link_to 'Show', :action => 'show', :id => @problem %> | |
|
8 | <%= link_to 'Show', :action => 'show', :id => @problem %> | |
|
9 | <%= link_to 'Back', :action => 'list' %> |
|
9 | <%= link_to 'Back', :action => 'list' %> |
You need to be logged in to leave comments.
Login now