Description:
- fix pdf loading fail
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r634:56cf4357ce2a - - 4 files changed: 11 inserted, 4 deleted

@@ -43,97 +43,97
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(params[:problem])
50 @problem = Problem.new(params[:problem])
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
81 elsif @description
81 elsif @description
82 if !@description.update_attributes(params[:description])
82 if !@description.update_attributes(params[:description])
83 flash[:notice] = 'Error saving description'
83 flash[:notice] = 'Error saving description'
84 render :action => 'edit' and return
84 render :action => 'edit' and return
85 end
85 end
86 end
86 end
87 if params[:file] and params[:file].content_type != 'application/pdf'
87 if params[:file] and params[:file].content_type != 'application/pdf'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
89 render :action => 'edit' and return
89 render :action => 'edit' and return
90 end
90 end
91 - if @problem.update_attributes(params[:problem])
91 + if @problem.update_attributes(problem_params)
92 flash[:notice] = 'Problem was successfully updated.'
92 flash[:notice] = 'Problem was successfully updated.'
93 unless params[:file] == nil or params[:file] == ''
93 unless params[:file] == nil or params[:file] == ''
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
96 if not FileTest.exists? out_dirname
96 if not FileTest.exists? out_dirname
97 Dir.mkdir out_dirname
97 Dir.mkdir out_dirname
98 end
98 end
99
99
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
101 if FileTest.exists? out_filename
101 if FileTest.exists? out_filename
102 File.delete out_filename
102 File.delete out_filename
103 end
103 end
104
104
105 File.open(out_filename,"wb") do |file|
105 File.open(out_filename,"wb") do |file|
106 file.write(params[:file].read)
106 file.write(params[:file].read)
107 end
107 end
108 @problem.description_filename = "#{@problem.name}.pdf"
108 @problem.description_filename = "#{@problem.name}.pdf"
109 @problem.save
109 @problem.save
110 end
110 end
111 redirect_to :action => 'show', :id => @problem
111 redirect_to :action => 'show', :id => @problem
112 else
112 else
113 render :action => 'edit'
113 render :action => 'edit'
114 end
114 end
115 end
115 end
116
116
117 def destroy
117 def destroy
118 p = Problem.find(params[:id]).destroy
118 p = Problem.find(params[:id]).destroy
119 redirect_to action: :index
119 redirect_to action: :index
120 end
120 end
121
121
122 def toggle
122 def toggle
123 @problem = Problem.find(params[:id])
123 @problem = Problem.find(params[:id])
124 @problem.update_attributes(available: !(@problem.available) )
124 @problem.update_attributes(available: !(@problem.available) )
125 respond_to do |format|
125 respond_to do |format|
126 format.js { }
126 format.js { }
127 end
127 end
128 end
128 end
129
129
130 def toggle_test
130 def toggle_test
131 @problem = Problem.find(params[:id])
131 @problem = Problem.find(params[:id])
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
133 respond_to do |format|
133 respond_to do |format|
134 format.js { }
134 format.js { }
135 end
135 end
136 end
136 end
137
137
138 def toggle_view_testcase
138 def toggle_view_testcase
139 @problem = Problem.find(params[:id])
139 @problem = Problem.find(params[:id])
@@ -240,49 +240,55
240 return false
240 return false
241 end
241 end
242 end
242 end
243
243
244 def change_date_added
244 def change_date_added
245 problems = get_problems_from_params
245 problems = get_problems_from_params
246 year = params[:date_added][:year].to_i
246 year = params[:date_added][:year].to_i
247 month = params[:date_added][:month].to_i
247 month = params[:date_added][:month].to_i
248 day = params[:date_added][:day].to_i
248 day = params[:date_added][:day].to_i
249 date = Date.new(year,month,day)
249 date = Date.new(year,month,day)
250 problems.each do |p|
250 problems.each do |p|
251 p.date_added = date
251 p.date_added = date
252 p.save
252 p.save
253 end
253 end
254 end
254 end
255
255
256 def add_to_contest
256 def add_to_contest
257 problems = get_problems_from_params
257 problems = get_problems_from_params
258 contest = Contest.find(params[:contest][:id])
258 contest = Contest.find(params[:contest][:id])
259 if contest!=nil and contest.enabled
259 if contest!=nil and contest.enabled
260 problems.each do |p|
260 problems.each do |p|
261 p.contests << contest
261 p.contests << contest
262 end
262 end
263 end
263 end
264 end
264 end
265
265
266 def set_available(avail)
266 def set_available(avail)
267 problems = get_problems_from_params
267 problems = get_problems_from_params
268 problems.each do |p|
268 problems.each do |p|
269 p.available = avail
269 p.available = avail
270 p.save
270 p.save
271 end
271 end
272 end
272 end
273
273
274 def get_problems_from_params
274 def get_problems_from_params
275 problems = []
275 problems = []
276 params.keys.each do |k|
276 params.keys.each do |k|
277 if k.index('prob-')==0
277 if k.index('prob-')==0
278 name, id, order = k.split('-')
278 name, id, order = k.split('-')
279 problems << Problem.find(id)
279 problems << Problem.find(id)
280 end
280 end
281 end
281 end
282 problems
282 problems
283 end
283 end
284
284
285 def get_problems_stat
285 def get_problems_stat
286 end
286 end
287
287
288 + private
289 +
290 + def problem_params
291 + params.require(:problem).permit(:name, :full_name, :full_score, :date_added, :available, :test_allowed,:output_only, :url, :description)
288 end
292 end
293 +
294 + end
@@ -73,97 +73,97
73 items = line.chomp.split(',')
73 items = line.chomp.split(',')
74 if items.length>=2
74 if items.length>=2
75 login = items[0]
75 login = items[0]
76 full_name = items[1]
76 full_name = items[1]
77 remark =''
77 remark =''
78 user_alias = ''
78 user_alias = ''
79
79
80 added_random_password = false
80 added_random_password = false
81 if items.length >= 3 and items[2].chomp(" ").length > 0;
81 if items.length >= 3 and items[2].chomp(" ").length > 0;
82 password = items[2].chomp(" ")
82 password = items[2].chomp(" ")
83 else
83 else
84 password = random_password
84 password = random_password
85 add_random_password=true;
85 add_random_password=true;
86 end
86 end
87
87
88 if items.length>= 4 and items[3].chomp(" ").length > 0;
88 if items.length>= 4 and items[3].chomp(" ").length > 0;
89 user_alias = items[3].chomp(" ")
89 user_alias = items[3].chomp(" ")
90 else
90 else
91 user_alias = login
91 user_alias = login
92 end
92 end
93
93
94 if items.length>=5
94 if items.length>=5
95 remark = items[4].strip;
95 remark = items[4].strip;
96 end
96 end
97
97
98 user = User.find_by_login(login)
98 user = User.find_by_login(login)
99 if (user)
99 if (user)
100 user.full_name = full_name
100 user.full_name = full_name
101 user.password = password
101 user.password = password
102 user.remark = remark
102 user.remark = remark
103 else
103 else
104 user = User.new({:login => login,
104 user = User.new({:login => login,
105 :full_name => full_name,
105 :full_name => full_name,
106 :password => password,
106 :password => password,
107 :password_confirmation => password,
107 :password_confirmation => password,
108 :alias => user_alias,
108 :alias => user_alias,
109 :remark => remark})
109 :remark => remark})
110 end
110 end
111 user.activated = true
111 user.activated = true
112 user.save
112 user.save
113
113
114 if added_random_password
114 if added_random_password
115 note << "'#{login}' (+)"
115 note << "'#{login}' (+)"
116 else
116 else
117 note << login
117 note << login
118 end
118 end
119 end
119 end
120 end
120 end
121 - flash[:notice] = 'User(s) ' + note.join(', ') +
121 + flash[:success] = 'User(s) ' + note.join(', ') +
122 ' were successfully created. ' +
122 ' were successfully created. ' +
123 '( (+) - created with random passwords.)'
123 '( (+) - created with random passwords.)'
124 redirect_to :action => 'index'
124 redirect_to :action => 'index'
125 end
125 end
126
126
127 def edit
127 def edit
128 @user = User.find(params[:id])
128 @user = User.find(params[:id])
129 end
129 end
130
130
131 def update
131 def update
132 @user = User.find(params[:id])
132 @user = User.find(params[:id])
133 if @user.update_attributes(user_params)
133 if @user.update_attributes(user_params)
134 flash[:notice] = 'User was successfully updated.'
134 flash[:notice] = 'User was successfully updated.'
135 redirect_to :action => 'show', :id => @user
135 redirect_to :action => 'show', :id => @user
136 else
136 else
137 render :action => 'edit'
137 render :action => 'edit'
138 end
138 end
139 end
139 end
140
140
141 def destroy
141 def destroy
142 User.find(params[:id]).destroy
142 User.find(params[:id]).destroy
143 redirect_to :action => 'index'
143 redirect_to :action => 'index'
144 end
144 end
145
145
146 def user_stat
146 def user_stat
147 if params[:commit] == 'download csv'
147 if params[:commit] == 'download csv'
148 @problems = Problem.all
148 @problems = Problem.all
149 else
149 else
150 @problems = Problem.available_problems
150 @problems = Problem.available_problems
151 end
151 end
152 @users = User.includes(:contests, :contest_stat).where(enabled: true)
152 @users = User.includes(:contests, :contest_stat).where(enabled: true)
153 @scorearray = Array.new
153 @scorearray = Array.new
154 @users.each do |u|
154 @users.each do |u|
155 ustat = Array.new
155 ustat = Array.new
156 ustat[0] = u
156 ustat[0] = u
157 @problems.each do |p|
157 @problems.each do |p|
158 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
158 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
159 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
159 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
160 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
160 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
161 else
161 else
162 ustat << [0,false]
162 ustat << [0,false]
163 end
163 end
164 end
164 end
165 @scorearray << ustat
165 @scorearray << ustat
166 end
166 end
167 if params[:commit] == 'download csv' then
167 if params[:commit] == 'download csv' then
168 csv = gen_csv_from_scorearray(@scorearray,@problems)
168 csv = gen_csv_from_scorearray(@scorearray,@problems)
169 send_data csv, filename: 'last_score.csv'
169 send_data csv, filename: 'last_score.csv'
@@ -1,53 +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' %> Do not directly edit the problem name, unless you know what you are doing. If you want to change the name, use the name change button in the problem management menu instead.
6 + </p>
6
7
7 <p><label for="problem_full_name">Full name</label><br/>
8 <p><label for="problem_full_name">Full name</label><br/>
8 <%= text_field 'problem', 'full_name' %></p>
9 <%= text_field 'problem', 'full_name' %></p>
9
10
10 <p><label for="problem_full_score">Full score</label><br/>
11 <p><label for="problem_full_score">Full score</label><br/>
11 <%= text_field 'problem', 'full_score' %></p>
12 <%= text_field 'problem', 'full_score' %></p>
12
13
13 <p><label for="problem_date_added">Date added</label><br/>
14 <p><label for="problem_date_added">Date added</label><br/>
14 <%= date_select 'problem', 'date_added' %></p>
15 <%= date_select 'problem', 'date_added' %></p>
15
16
16 <%
17 <%
17 # TODO: these should be put in model Problem, but I can't think of
18 # 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
19 # nice default values for them. These values look fine only
19 # in this case (of lazily adding new problems).
20 # in this case (of lazily adding new problems).
20 @problem.available = true if @problem!=nil and @problem.available==nil
21 @problem.available = true if @problem!=nil and @problem.available==nil
21 @problem.test_allowed = true if @problem!=nil and @problem.test_allowed==nil
22 @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
23 @problem.output_only = false if @problem!=nil and @problem.output_only==nil
23 %>
24 %>
24
25
25 <p>
26 <p>
26 <label for="problem_available">Available?</label>
27 <label for="problem_available">Available?</label>
27 <%= check_box :problem, :available %>
28 <%= check_box :problem, :available %>
28
29
29 <label for="problem_test_allowed">Test allowed?</label>
30 <label for="problem_test_allowed">Test allowed?</label>
30 <%= check_box :problem, :test_allowed %>
31 <%= check_box :problem, :test_allowed %>
31
32
32 <label for="problem_output_only">Output only?</label>
33 <label for="problem_output_only">Output only?</label>
33 <%= check_box :problem, :output_only %>
34 <%= check_box :problem, :output_only %>
34 </p>
35 </p>
35
36
36 <%= error_messages_for 'description' %>
37 <%= error_messages_for 'description' %>
37
38
38 <p><label for="description_body">Description</label><br/>
39 <p><label for="description_body">Description</label><br/>
39 <%= text_area :description, :body, :rows => 10, :cols => 80 %></p>
40 <%= text_area :description, :body, :rows => 10, :cols => 80 %></p>
40
41
41 <p><label for="description_markdowned">Markdowned?</label>
42 <p><label for="description_markdowned">Markdowned?</label>
42 <%= select "description",
43 <%= select "description",
43 "markdowned",
44 "markdowned",
44 [['True',true],['False',false]],
45 [['True',true],['False',false]],
45 {:selected => (@description) ? @description.markdowned : false }
46 {:selected => (@description) ? @description.markdowned : false }
46 %></p>
47 %></p>
47
48
48 <p><label for="problem_url">URL</label><br/>
49 <p><label for="problem_url">URL</label><br/>
49 <%= text_field 'problem', 'url' %></p>
50 <%= text_field 'problem', 'url' %></p>
50
51
51 <p>Task PDF <%= file_field_tag 'file' %></p>
52 <p>Task PDF <%= file_field_tag 'file' %></p>
52
53
53
54
@@ -1,24 +1,24
1 <% for column in Problem.content_columns %>
1 <% for column in Problem.content_columns %>
2 <p>
2 <p>
3 <b><%= column.human_name %>:</b>
3 <b><%= column.human_name %>:</b>
4 <%=h @problem.send(column.name) %>
4 <%=h @problem.send(column.name) %>
5 </p>
5 </p>
6 <% end %>
6 <% end %>
7
7
8 <p>
8 <p>
9 <b>Description:</b><br/>
9 <b>Description:</b><br/>
10 <% if @problem.description!=nil %>
10 <% if @problem.description!=nil %>
11 <% if @problem.description.markdowned %>
11 <% if @problem.description.markdowned %>
12 <%= markdown(@problem.description.body) %>
12 <%= markdown(@problem.description.body) %>
13 <% else %>
13 <% else %>
14 <pre>
14 <pre>
15 <%= @problem.description.body %>
15 <%= @problem.description.body %>
16 </pre>
16 </pre>
17 <% end %>
17 <% end %>
18 <% else %>
18 <% else %>
19 (not available)
19 (not available)
20 <% end %>
20 <% end %>
21 </p>
21 </p>
22
22
23 <%= link_to 'Edit', :action => 'edit', :id => @problem %> |
23 <%= link_to 'Edit', :action => 'edit', :id => @problem %> |
24 - <%= link_to 'Back', :action => 'list' %>
24 + <%= link_to 'Back', problems_path %>
You need to be logged in to leave comments. Login now