Description:
fix destroy for user and problem
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r605:1317cbdbc3df - - 2 files changed: 3 inserted, 4 deleted

@@ -1,282 +1,281
1 1 class ProblemsController < ApplicationController
2 2
3 3 before_filter :authenticate, :authorization
4 4
5 5 in_place_edit_for :problem, :name
6 6 in_place_edit_for :problem, :full_name
7 7 in_place_edit_for :problem, :full_score
8 8
9 9 def index
10 10 @problems = Problem.find(:all, :order => 'date_added DESC')
11 11 end
12 12
13 13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
14 - verify :method => :post, :only => [ :destroy,
15 - :create, :quick_create,
14 + verify :method => :post, :only => [ :create, :quick_create,
16 15 :do_manage,
17 16 :do_import,
18 17 ],
19 18 :redirect_to => { :action => :index }
20 19
21 20 def show
22 21 @problem = Problem.find(params[:id])
23 22 end
24 23
25 24 def new
26 25 @problem = Problem.new
27 26 @description = nil
28 27 end
29 28
30 29 def create
31 30 @problem = Problem.new(params[:problem])
32 31 @description = Description.new(params[:description])
33 32 if @description.body!=''
34 33 if !@description.save
35 34 render :action => new and return
36 35 end
37 36 else
38 37 @description = nil
39 38 end
40 39 @problem.description = @description
41 40 if @problem.save
42 41 flash[:notice] = 'Problem was successfully created.'
43 42 redirect_to action: :index
44 43 else
45 44 render :action => 'new'
46 45 end
47 46 end
48 47
49 48 def quick_create
50 49 @problem = Problem.new(params[:problem])
51 50 @problem.full_name = @problem.name if @problem.full_name == ''
52 51 @problem.full_score = 100
53 52 @problem.available = false
54 53 @problem.test_allowed = true
55 54 @problem.output_only = false
56 55 @problem.date_added = Time.new
57 56 if @problem.save
58 57 flash[:notice] = 'Problem was successfully created.'
59 58 redirect_to action: :index
60 59 else
61 60 flash[:notice] = 'Error saving problem'
62 61 redirect_to action: :index
63 62 end
64 63 end
65 64
66 65 def edit
67 66 @problem = Problem.find(params[:id])
68 67 @description = @problem.description
69 68 end
70 69
71 70 def update
72 71 @problem = Problem.find(params[:id])
73 72 @description = @problem.description
74 73 if @description.nil? and params[:description][:body]!=''
75 74 @description = Description.new(params[:description])
76 75 if !@description.save
77 76 flash[:notice] = 'Error saving description'
78 77 render :action => 'edit' and return
79 78 end
80 79 @problem.description = @description
81 80 elsif @description
82 81 if !@description.update_attributes(params[:description])
83 82 flash[:notice] = 'Error saving description'
84 83 render :action => 'edit' and return
85 84 end
86 85 end
87 86 if params[:file] and params[:file].content_type != 'application/pdf'
88 87 flash[:notice] = 'Error: Uploaded file is not PDF'
89 88 render :action => 'edit' and return
90 89 end
91 90 if @problem.update_attributes(params[:problem])
92 91 flash[:notice] = 'Problem was successfully updated.'
93 92 unless params[:file] == nil or params[:file] == ''
94 93 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
95 94 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
96 95 if not FileTest.exists? out_dirname
97 96 Dir.mkdir out_dirname
98 97 end
99 98
100 99 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
101 100 if FileTest.exists? out_filename
102 101 File.delete out_filename
103 102 end
104 103
105 104 File.open(out_filename,"wb") do |file|
106 105 file.write(params[:file].read)
107 106 end
108 107 @problem.description_filename = "#{@problem.name}.pdf"
109 108 @problem.save
110 109 end
111 110 redirect_to :action => 'show', :id => @problem
112 111 else
113 112 render :action => 'edit'
114 113 end
115 114 end
116 115
117 116 def destroy
118 - Problem.find(params[:id]).destroy
117 + p = Problem.find(params[:id]).destroy
119 118 redirect_to action: :index
120 119 end
121 120
122 121 def toggle
123 122 @problem = Problem.find(params[:id])
124 123 @problem.update_attributes(available: !(@problem.available) )
125 124 respond_to do |format|
126 125 format.js { }
127 126 end
128 127 end
129 128
130 129 def toggle_test
131 130 @problem = Problem.find(params[:id])
132 131 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
133 132 respond_to do |format|
134 133 format.js { }
135 134 end
136 135 end
137 136
138 137 def turn_all_off
139 138 Problem.find(:all,
140 139 :conditions => "available = 1").each do |problem|
141 140 problem.available = false
142 141 problem.save
143 142 end
144 143 redirect_to action: :index
145 144 end
146 145
147 146 def turn_all_on
148 147 Problem.find(:all,
149 148 :conditions => "available = 0").each do |problem|
150 149 problem.available = true
151 150 problem.save
152 151 end
153 152 redirect_to action: :index
154 153 end
155 154
156 155 def stat
157 156 @problem = Problem.find(params[:id])
158 157 unless @problem.available or session[:admin]
159 158 redirect_to :controller => 'main', :action => 'list'
160 159 return
161 160 end
162 161 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
163 162
164 163 #stat summary
165 164 range =65
166 165 @histogram = { data: Array.new(range,0), summary: {} }
167 166 user = Hash.new(0)
168 167 @submissions.find_each do |sub|
169 168 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
170 169 @histogram[:data][d.to_i] += 1 if d < range
171 170 user[sub.user_id] = [user[sub.user_id], ((sub.try(:points) || 0) >= @problem.full_score) ? 1 : 0].max
172 171 end
173 172 @histogram[:summary][:max] = [@histogram[:data].max,1].max
174 173
175 174 @summary = { attempt: user.count, solve: 0 }
176 175 user.each_value { |v| @summary[:solve] += 1 if v == 1 }
177 176 end
178 177
179 178 def manage
180 179 @problems = Problem.find(:all, :order => 'date_added DESC')
181 180 end
182 181
183 182 def do_manage
184 183 if params.has_key? 'change_date_added'
185 184 change_date_added
186 185 elsif params.has_key? 'add_to_contest'
187 186 add_to_contest
188 187 elsif params.has_key? 'enable_problem'
189 188 set_available(true)
190 189 elsif params.has_key? 'disable_problem'
191 190 set_available(false)
192 191 end
193 192 redirect_to :action => 'manage'
194 193 end
195 194
196 195 def import
197 196 @allow_test_pair_import = allow_test_pair_import?
198 197 end
199 198
200 199 def do_import
201 200 old_problem = Problem.find_by_name(params[:name])
202 201 if !allow_test_pair_import? and params.has_key? :import_to_db
203 202 params.delete :import_to_db
204 203 end
205 204 @problem, import_log = Problem.create_from_import_form_params(params,
206 205 old_problem)
207 206
208 207 if !@problem.errors.empty?
209 208 render :action => 'import' and return
210 209 end
211 210
212 211 if old_problem!=nil
213 212 flash[:notice] = "The test data has been replaced for problem #{@problem.name}"
214 213 end
215 214 @log = import_log
216 215 end
217 216
218 217 def remove_contest
219 218 problem = Problem.find(params[:id])
220 219 contest = Contest.find(params[:contest_id])
221 220 if problem!=nil and contest!=nil
222 221 problem.contests.delete(contest)
223 222 end
224 223 redirect_to :action => 'manage'
225 224 end
226 225
227 226 ##################################
228 227 protected
229 228
230 229 def allow_test_pair_import?
231 230 if defined? ALLOW_TEST_PAIR_IMPORT
232 231 return ALLOW_TEST_PAIR_IMPORT
233 232 else
234 233 return false
235 234 end
236 235 end
237 236
238 237 def change_date_added
239 238 problems = get_problems_from_params
240 239 year = params[:date_added][:year].to_i
241 240 month = params[:date_added][:month].to_i
242 241 day = params[:date_added][:day].to_i
243 242 date = Date.new(year,month,day)
244 243 problems.each do |p|
245 244 p.date_added = date
246 245 p.save
247 246 end
248 247 end
249 248
250 249 def add_to_contest
251 250 problems = get_problems_from_params
252 251 contest = Contest.find(params[:contest][:id])
253 252 if contest!=nil and contest.enabled
254 253 problems.each do |p|
255 254 p.contests << contest
256 255 end
257 256 end
258 257 end
259 258
260 259 def set_available(avail)
261 260 problems = get_problems_from_params
262 261 problems.each do |p|
263 262 p.available = avail
264 263 p.save
265 264 end
266 265 end
267 266
268 267 def get_problems_from_params
269 268 problems = []
270 269 params.keys.each do |k|
271 270 if k.index('prob-')==0
272 271 name, id, order = k.split('-')
273 272 problems << Problem.find(id)
274 273 end
275 274 end
276 275 problems
277 276 end
278 277
279 278 def get_problems_stat
280 279 end
281 280
282 281 end
@@ -1,202 +1,202
1 1 require 'csv'
2 2
3 3 class UserAdminController < ApplicationController
4 4
5 5 include MailHelperMethods
6 6
7 7 before_filter :admin_authorization
8 8
9 9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
10 - verify :method => :post, :only => [ :destroy,
10 + verify :method => :post, :only => [
11 11 :create, :create_from_list,
12 12 :update,
13 13 :manage_contest,
14 14 :bulk_mail
15 15 ],
16 16 :redirect_to => { :action => :list }
17 17
18 18 def index
19 19 @user_count = User.count
20 20 if params[:page] == 'all'
21 21 @users = User.all
22 22 @paginated = false
23 23 else
24 24 @users = User.paginate :page => params[:page]
25 25 @paginated = true
26 26 end
27 27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
28 28 @contests = Contest.enabled
29 29 end
30 30
31 31 def active
32 32 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
33 33 @users = []
34 34 sessions.each do |session|
35 35 if session.data[:user_id]
36 36 @users << User.find(session.data[:user_id])
37 37 end
38 38 end
39 39 end
40 40
41 41 def show
42 42 @user = User.find(params[:id])
43 43 end
44 44
45 45 def new
46 46 @user = User.new
47 47 end
48 48
49 49 def create
50 50 @user = User.new(params[:user])
51 51 @user.activated = true
52 52 if @user.save
53 53 flash[:notice] = 'User was successfully created.'
54 54 redirect_to :action => 'index'
55 55 else
56 56 render :action => 'new'
57 57 end
58 58 end
59 59
60 60 def clear_last_ip
61 61 @user = User.find(params[:id])
62 62 @user.last_ip = nil
63 63 @user.save
64 64 redirect_to action: 'index', page: params[:page]
65 65 end
66 66
67 67 def create_from_list
68 68 lines = params[:user_list]
69 69
70 70 note = []
71 71
72 72 lines.split("\n").each do |line|
73 73 items = line.chomp.split(',')
74 74 if items.length>=2
75 75 login = items[0]
76 76 full_name = items[1]
77 77
78 78 added_random_password = false
79 79 if items.length>=3
80 80 password = items[2].chomp(" ")
81 81 user_alias = (items.length>=4) ? items[3] : login
82 82 else
83 83 password = random_password
84 84 user_alias = (items.length>=4) ? items[3] : login
85 85 added_random_password = true
86 86 end
87 87
88 88 user = User.find_by_login(login)
89 89 if (user)
90 90 user.full_name = full_name
91 91 user.password = password
92 92 else
93 93 user = User.new({:login => login,
94 94 :full_name => full_name,
95 95 :password => password,
96 96 :password_confirmation => password,
97 97 :alias => user_alias})
98 98 end
99 99 user.activated = true
100 100 user.save
101 101
102 102 if added_random_password
103 103 note << "'#{login}' (+)"
104 104 else
105 105 note << login
106 106 end
107 107 end
108 108 end
109 109 flash[:notice] = 'User(s) ' + note.join(', ') +
110 110 ' were successfully created. ' +
111 111 '( (+) - created with random passwords.)'
112 112 redirect_to :action => 'index'
113 113 end
114 114
115 115 def edit
116 116 @user = User.find(params[:id])
117 117 end
118 118
119 119 def update
120 120 @user = User.find(params[:id])
121 121 if @user.update_attributes(params[:user])
122 122 flash[:notice] = 'User was successfully updated.'
123 123 redirect_to :action => 'show', :id => @user
124 124 else
125 125 render :action => 'edit'
126 126 end
127 127 end
128 128
129 129 def destroy
130 130 User.find(params[:id]).destroy
131 131 redirect_to :action => 'index'
132 132 end
133 133
134 134 def user_stat
135 135 if params[:commit] == 'download csv'
136 136 @problems = Problem.all
137 137 else
138 138 @problems = Problem.find_available_problems
139 139 end
140 140 @users = User.includes(:contests, :contest_stat).where(enabled: true) #find(:all, :include => [:contests, :contest_stat]).where(enabled: true)
141 141 @scorearray = Array.new
142 142 @users.each do |u|
143 143 ustat = Array.new
144 144 ustat[0] = u
145 145 @problems.each do |p|
146 146 sub = Submission.find_last_by_user_and_problem(u.id,p.id)
147 147 if (sub!=nil) and (sub.points!=nil) and p and p.full_score
148 148 ustat << [(sub.points.to_f*100/p.full_score).round, (sub.points>=p.full_score)]
149 149 else
150 150 ustat << [0,false]
151 151 end
152 152 end
153 153 @scorearray << ustat
154 154 end
155 155 if params[:commit] == 'download csv' then
156 156 csv = gen_csv_from_scorearray(@scorearray,@problems)
157 157 send_data csv, filename: 'last_score.csv'
158 158 else
159 159 render template: 'user_admin/user_stat'
160 160 end
161 161 end
162 162
163 163 def user_stat_max
164 164 if params[:commit] == 'download csv'
165 165 @problems = Problem.all
166 166 else
167 167 @problems = Problem.find_available_problems
168 168 end
169 169 @users = User.find(:all, :include => [:contests, :contest_stat])
170 170 @scorearray = Array.new
171 171 #set up range from param
172 172 since_id = params.fetch(:since_id, 0).to_i
173 173 until_id = params.fetch(:until_id, 0).to_i
174 174 @users.each do |u|
175 175 ustat = Array.new
176 176 ustat[0] = u
177 177 @problems.each do |p|
178 178 max_points = 0
179 179 Submission.find_in_range_by_user_and_problem(u.id,p.id,since_id,until_id).each do |sub|
180 180 max_points = sub.points if sub and sub.points and (sub.points > max_points)
181 181 end
182 182 ustat << [(max_points.to_f*100/p.full_score).round, (max_points>=p.full_score)]
183 183 end
184 184 @scorearray << ustat
185 185 end
186 186
187 187 if params[:commit] == 'download csv' then
188 188 csv = gen_csv_from_scorearray(@scorearray,@problems)
189 189 send_data csv, filename: 'max_score.csv'
190 190 else
191 191 render template: 'user_admin/user_stat'
192 192 end
193 193 end
194 194
195 195 def import
196 196 if params[:file]==''
197 197 flash[:notice] = 'Error importing no file'
198 198 redirect_to :action => 'index' and return
199 199 end
200 200 import_from_file(params[:file])
201 201 end
202 202
You need to be logged in to leave comments. Login now