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,63 +1,62
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 @problems = Problem.find(:all, :order => 'date_added DESC')
10 @problems = Problem.find(:all, :order => 'date_added DESC')
11 end
11 end
12
12
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
13 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
14 - verify :method => :post, :only => [ :destroy,
14 + verify :method => :post, :only => [ :create, :quick_create,
15 - :create, :quick_create,
16 :do_manage,
15 :do_manage,
17 :do_import,
16 :do_import,
18 ],
17 ],
19 :redirect_to => { :action => :index }
18 :redirect_to => { :action => :index }
20
19
21 def show
20 def show
22 @problem = Problem.find(params[:id])
21 @problem = Problem.find(params[:id])
23 end
22 end
24
23
25 def new
24 def new
26 @problem = Problem.new
25 @problem = Problem.new
27 @description = nil
26 @description = nil
28 end
27 end
29
28
30 def create
29 def create
31 @problem = Problem.new(params[:problem])
30 @problem = Problem.new(params[:problem])
32 @description = Description.new(params[:description])
31 @description = Description.new(params[:description])
33 if @description.body!=''
32 if @description.body!=''
34 if !@description.save
33 if !@description.save
35 render :action => new and return
34 render :action => new and return
36 end
35 end
37 else
36 else
38 @description = nil
37 @description = nil
39 end
38 end
40 @problem.description = @description
39 @problem.description = @description
41 if @problem.save
40 if @problem.save
42 flash[:notice] = 'Problem was successfully created.'
41 flash[:notice] = 'Problem was successfully created.'
43 redirect_to action: :index
42 redirect_to action: :index
44 else
43 else
45 render :action => 'new'
44 render :action => 'new'
46 end
45 end
47 end
46 end
48
47
49 def quick_create
48 def quick_create
50 @problem = Problem.new(params[:problem])
49 @problem = Problem.new(params[:problem])
51 @problem.full_name = @problem.name if @problem.full_name == ''
50 @problem.full_name = @problem.name if @problem.full_name == ''
52 @problem.full_score = 100
51 @problem.full_score = 100
53 @problem.available = false
52 @problem.available = false
54 @problem.test_allowed = true
53 @problem.test_allowed = true
55 @problem.output_only = false
54 @problem.output_only = false
56 @problem.date_added = Time.new
55 @problem.date_added = Time.new
57 if @problem.save
56 if @problem.save
58 flash[:notice] = 'Problem was successfully created.'
57 flash[:notice] = 'Problem was successfully created.'
59 redirect_to action: :index
58 redirect_to action: :index
60 else
59 else
61 flash[:notice] = 'Error saving problem'
60 flash[:notice] = 'Error saving problem'
62 redirect_to action: :index
61 redirect_to action: :index
63 end
62 end
@@ -70,97 +69,97
70
69
71 def update
70 def update
72 @problem = Problem.find(params[:id])
71 @problem = Problem.find(params[:id])
73 @description = @problem.description
72 @description = @problem.description
74 if @description.nil? and params[:description][:body]!=''
73 if @description.nil? and params[:description][:body]!=''
75 @description = Description.new(params[:description])
74 @description = Description.new(params[:description])
76 if !@description.save
75 if !@description.save
77 flash[:notice] = 'Error saving description'
76 flash[:notice] = 'Error saving description'
78 render :action => 'edit' and return
77 render :action => 'edit' and return
79 end
78 end
80 @problem.description = @description
79 @problem.description = @description
81 elsif @description
80 elsif @description
82 if !@description.update_attributes(params[:description])
81 if !@description.update_attributes(params[:description])
83 flash[:notice] = 'Error saving description'
82 flash[:notice] = 'Error saving description'
84 render :action => 'edit' and return
83 render :action => 'edit' and return
85 end
84 end
86 end
85 end
87 if params[:file] and params[:file].content_type != 'application/pdf'
86 if params[:file] and params[:file].content_type != 'application/pdf'
88 flash[:notice] = 'Error: Uploaded file is not PDF'
87 flash[:notice] = 'Error: Uploaded file is not PDF'
89 render :action => 'edit' and return
88 render :action => 'edit' and return
90 end
89 end
91 if @problem.update_attributes(params[:problem])
90 if @problem.update_attributes(params[:problem])
92 flash[:notice] = 'Problem was successfully updated.'
91 flash[:notice] = 'Problem was successfully updated.'
93 unless params[:file] == nil or params[:file] == ''
92 unless params[:file] == nil or params[:file] == ''
94 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
93 flash[:notice] = 'Problem was successfully updated and a new PDF file is uploaded.'
95 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
94 out_dirname = "#{Problem.download_file_basedir}/#{@problem.id}"
96 if not FileTest.exists? out_dirname
95 if not FileTest.exists? out_dirname
97 Dir.mkdir out_dirname
96 Dir.mkdir out_dirname
98 end
97 end
99
98
100 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
99 out_filename = "#{out_dirname}/#{@problem.name}.pdf"
101 if FileTest.exists? out_filename
100 if FileTest.exists? out_filename
102 File.delete out_filename
101 File.delete out_filename
103 end
102 end
104
103
105 File.open(out_filename,"wb") do |file|
104 File.open(out_filename,"wb") do |file|
106 file.write(params[:file].read)
105 file.write(params[:file].read)
107 end
106 end
108 @problem.description_filename = "#{@problem.name}.pdf"
107 @problem.description_filename = "#{@problem.name}.pdf"
109 @problem.save
108 @problem.save
110 end
109 end
111 redirect_to :action => 'show', :id => @problem
110 redirect_to :action => 'show', :id => @problem
112 else
111 else
113 render :action => 'edit'
112 render :action => 'edit'
114 end
113 end
115 end
114 end
116
115
117 def destroy
116 def destroy
118 - Problem.find(params[:id]).destroy
117 + p = Problem.find(params[:id]).destroy
119 redirect_to action: :index
118 redirect_to action: :index
120 end
119 end
121
120
122 def toggle
121 def toggle
123 @problem = Problem.find(params[:id])
122 @problem = Problem.find(params[:id])
124 @problem.update_attributes(available: !(@problem.available) )
123 @problem.update_attributes(available: !(@problem.available) )
125 respond_to do |format|
124 respond_to do |format|
126 format.js { }
125 format.js { }
127 end
126 end
128 end
127 end
129
128
130 def toggle_test
129 def toggle_test
131 @problem = Problem.find(params[:id])
130 @problem = Problem.find(params[:id])
132 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
131 @problem.update_attributes(test_allowed: !(@problem.test_allowed?) )
133 respond_to do |format|
132 respond_to do |format|
134 format.js { }
133 format.js { }
135 end
134 end
136 end
135 end
137
136
138 def turn_all_off
137 def turn_all_off
139 Problem.find(:all,
138 Problem.find(:all,
140 :conditions => "available = 1").each do |problem|
139 :conditions => "available = 1").each do |problem|
141 problem.available = false
140 problem.available = false
142 problem.save
141 problem.save
143 end
142 end
144 redirect_to action: :index
143 redirect_to action: :index
145 end
144 end
146
145
147 def turn_all_on
146 def turn_all_on
148 Problem.find(:all,
147 Problem.find(:all,
149 :conditions => "available = 0").each do |problem|
148 :conditions => "available = 0").each do |problem|
150 problem.available = true
149 problem.available = true
151 problem.save
150 problem.save
152 end
151 end
153 redirect_to action: :index
152 redirect_to action: :index
154 end
153 end
155
154
156 def stat
155 def stat
157 @problem = Problem.find(params[:id])
156 @problem = Problem.find(params[:id])
158 unless @problem.available or session[:admin]
157 unless @problem.available or session[:admin]
159 redirect_to :controller => 'main', :action => 'list'
158 redirect_to :controller => 'main', :action => 'list'
160 return
159 return
161 end
160 end
162 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
161 @submissions = Submission.includes(:user).where(problem_id: params[:id]).order(:user_id,:id)
163
162
164 #stat summary
163 #stat summary
165 range =65
164 range =65
166 @histogram = { data: Array.new(range,0), summary: {} }
165 @histogram = { data: Array.new(range,0), summary: {} }
@@ -1,58 +1,58
1 require 'csv'
1 require 'csv'
2
2
3 class UserAdminController < ApplicationController
3 class UserAdminController < ApplicationController
4
4
5 include MailHelperMethods
5 include MailHelperMethods
6
6
7 before_filter :admin_authorization
7 before_filter :admin_authorization
8
8
9 # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
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 :create, :create_from_list,
11 :create, :create_from_list,
12 :update,
12 :update,
13 :manage_contest,
13 :manage_contest,
14 :bulk_mail
14 :bulk_mail
15 ],
15 ],
16 :redirect_to => { :action => :list }
16 :redirect_to => { :action => :list }
17
17
18 def index
18 def index
19 @user_count = User.count
19 @user_count = User.count
20 if params[:page] == 'all'
20 if params[:page] == 'all'
21 @users = User.all
21 @users = User.all
22 @paginated = false
22 @paginated = false
23 else
23 else
24 @users = User.paginate :page => params[:page]
24 @users = User.paginate :page => params[:page]
25 @paginated = true
25 @paginated = true
26 end
26 end
27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
27 @hidden_columns = ['hashed_password', 'salt', 'created_at', 'updated_at']
28 @contests = Contest.enabled
28 @contests = Contest.enabled
29 end
29 end
30
30
31 def active
31 def active
32 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
32 sessions = ActiveRecord::SessionStore::Session.find(:all, :conditions => ["updated_at >= ?", 60.minutes.ago])
33 @users = []
33 @users = []
34 sessions.each do |session|
34 sessions.each do |session|
35 if session.data[:user_id]
35 if session.data[:user_id]
36 @users << User.find(session.data[:user_id])
36 @users << User.find(session.data[:user_id])
37 end
37 end
38 end
38 end
39 end
39 end
40
40
41 def show
41 def show
42 @user = User.find(params[:id])
42 @user = User.find(params[:id])
43 end
43 end
44
44
45 def new
45 def new
46 @user = User.new
46 @user = User.new
47 end
47 end
48
48
49 def create
49 def create
50 @user = User.new(params[:user])
50 @user = User.new(params[:user])
51 @user.activated = true
51 @user.activated = true
52 if @user.save
52 if @user.save
53 flash[:notice] = 'User was successfully created.'
53 flash[:notice] = 'User was successfully created.'
54 redirect_to :action => 'index'
54 redirect_to :action => 'index'
55 else
55 else
56 render :action => 'new'
56 render :action => 'new'
57 end
57 end
58 end
58 end
You need to be logged in to leave comments. Login now