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

r462:c960ef68f0b1 - - 2 files changed: 3 inserted, 1 deleted

@@ -1,162 +1,163
1 class MainController < ApplicationController
1 class MainController < ApplicationController
2
2
3 before_filter :authenticate, :except => [:index, :login]
3 before_filter :authenticate, :except => [:index, :login]
4 before_filter :check_viewability, :except => [:index, :login]
4 before_filter :check_viewability, :except => [:index, :login]
5
5
6 append_before_filter :confirm_and_update_start_time,
6 append_before_filter :confirm_and_update_start_time,
7 :except => [:index,
7 :except => [:index,
8 :login,
8 :login,
9 :confirm_contest_start]
9 :confirm_contest_start]
10
10
11 # to prevent log in box to be shown when user logged out of the
11 # to prevent log in box to be shown when user logged out of the
12 # system only in some tab
12 # system only in some tab
13 prepend_before_filter :reject_announcement_refresh_when_logged_out,
13 prepend_before_filter :reject_announcement_refresh_when_logged_out,
14 :only => [:announcements]
14 :only => [:announcements]
15
15
16 # COMMENTED OUT: filter in each action instead
16 # COMMENTED OUT: filter in each action instead
17 # before_filter :verify_time_limit, :only => [:submit]
17 # before_filter :verify_time_limit, :only => [:submit]
18
18
19 verify :method => :post, :only => [:submit],
19 verify :method => :post, :only => [:submit],
20 :redirect_to => { :action => :index }
20 :redirect_to => { :action => :index }
21
21
22 # COMMENT OUT: only need when having high load
22 # COMMENT OUT: only need when having high load
23 # caches_action :index, :login
23 # caches_action :index, :login
24
24
25 # NOTE: This method is not actually needed, 'config/routes.rb' has
25 # NOTE: This method is not actually needed, 'config/routes.rb' has
26 # assigned action login as a default action.
26 # assigned action login as a default action.
27 def index
27 def index
28 redirect_to :action => 'login'
28 redirect_to :action => 'login'
29 end
29 end
30
30
31 def login
31 def login
32 saved_notice = flash[:notice]
32 saved_notice = flash[:notice]
33 reset_session
33 reset_session
34 flash.now[:notice] = saved_notice
34 flash.now[:notice] = saved_notice
35
35
36 # EXPERIMENT:
36 # EXPERIMENT:
37 # Hide login if in single user mode and the url does not
37 # Hide login if in single user mode and the url does not
38 # explicitly specify /login
38 # explicitly specify /login
39 #
39 #
40 # logger.info "PATH: #{request.path}"
40 # logger.info "PATH: #{request.path}"
41 # if GraderConfiguration['system.single_user_mode'] and
41 # if GraderConfiguration['system.single_user_mode'] and
42 # request.path!='/main/login'
42 # request.path!='/main/login'
43 # @hidelogin = true
43 # @hidelogin = true
44 # end
44 # end
45
45
46 @announcements = Announcement.find_for_frontpage
46 @announcements = Announcement.find_for_frontpage
47 render :action => 'login', :layout => 'empty'
47 render :action => 'login', :layout => 'empty'
48 end
48 end
49
49
50 def list
50 def list
51 prepare_list_information
51 prepare_list_information
52 end
52 end
53
53
54 def help
54 def help
55 @user = User.find(session[:user_id])
55 @user = User.find(session[:user_id])
56 end
56 end
57
57
58 def submit
58 def submit
59 user = User.find(session[:user_id])
59 user = User.find(session[:user_id])
60
60
61 @submission = Submission.new
61 @submission = Submission.new
62 @submission.problem_id = params[:submission][:problem_id]
62 @submission.problem_id = params[:submission][:problem_id]
63 @submission.user = user
63 @submission.user = user
64 @submission.language_id = 0
64 @submission.language_id = 0
65 if (params['file']) and (params['file']!='')
65 if (params['file']) and (params['file']!='')
66 - @submission.source = params['file'].read
66 + @submission.source = File.open(params['file'].path,'r:UTF-8',&:read)
67 + @submission.source.encode!('UTF-8','UTF-8',invalid: :replace, replace: '')
67 @submission.source_filename = params['file'].original_filename
68 @submission.source_filename = params['file'].original_filename
68 end
69 end
69 @submission.submitted_at = Time.new.gmtime
70 @submission.submitted_at = Time.new.gmtime
70 @submission.ip_address = request.remote_ip
71 @submission.ip_address = request.remote_ip
71
72
72 if GraderConfiguration.time_limit_mode? and user.contest_finished?
73 if GraderConfiguration.time_limit_mode? and user.contest_finished?
73 @submission.errors.add_to_base "The contest is over."
74 @submission.errors.add_to_base "The contest is over."
74 prepare_list_information
75 prepare_list_information
75 render :action => 'list' and return
76 render :action => 'list' and return
76 end
77 end
77
78
78 if @submission.valid?
79 if @submission.valid?
79 if @submission.save == false
80 if @submission.save == false
80 flash[:notice] = 'Error saving your submission'
81 flash[:notice] = 'Error saving your submission'
81 elsif Task.create(:submission_id => @submission.id,
82 elsif Task.create(:submission_id => @submission.id,
82 :status => Task::STATUS_INQUEUE) == false
83 :status => Task::STATUS_INQUEUE) == false
83 flash[:notice] = 'Error adding your submission to task queue'
84 flash[:notice] = 'Error adding your submission to task queue'
84 end
85 end
85 else
86 else
86 prepare_list_information
87 prepare_list_information
87 render :action => 'list' and return
88 render :action => 'list' and return
88 end
89 end
89 redirect_to :action => 'list'
90 redirect_to :action => 'list'
90 end
91 end
91
92
92 def source
93 def source
93 submission = Submission.find(params[:id])
94 submission = Submission.find(params[:id])
94 if ((submission.user_id == session[:user_id]) and
95 if ((submission.user_id == session[:user_id]) and
95 (submission.problem != nil) and
96 (submission.problem != nil) and
96 (submission.problem.available))
97 (submission.problem.available))
97 send_data(submission.source,
98 send_data(submission.source,
98 {:filename => submission.download_filename,
99 {:filename => submission.download_filename,
99 :type => 'text/plain'})
100 :type => 'text/plain'})
100 else
101 else
101 flash[:notice] = 'Error viewing source'
102 flash[:notice] = 'Error viewing source'
102 redirect_to :action => 'list'
103 redirect_to :action => 'list'
103 end
104 end
104 end
105 end
105
106
106 def compiler_msg
107 def compiler_msg
107 @submission = Submission.find(params[:id])
108 @submission = Submission.find(params[:id])
108 if @submission.user_id == session[:user_id]
109 if @submission.user_id == session[:user_id]
109 render :action => 'compiler_msg', :layout => 'empty'
110 render :action => 'compiler_msg', :layout => 'empty'
110 else
111 else
111 flash[:notice] = 'Error viewing source'
112 flash[:notice] = 'Error viewing source'
112 redirect_to :action => 'list'
113 redirect_to :action => 'list'
113 end
114 end
114 end
115 end
115
116
116 def submission
117 def submission
117 @user = User.find(session[:user_id])
118 @user = User.find(session[:user_id])
118 @problems = @user.available_problems
119 @problems = @user.available_problems
119 if params[:id]==nil
120 if params[:id]==nil
120 @problem = nil
121 @problem = nil
121 @submissions = nil
122 @submissions = nil
122 else
123 else
123 @problem = Problem.find_by_name(params[:id])
124 @problem = Problem.find_by_name(params[:id])
124 if not @problem.available
125 if not @problem.available
125 redirect_to :action => 'list'
126 redirect_to :action => 'list'
126 flash[:notice] = 'Error: submissions for that problem are not viewable.'
127 flash[:notice] = 'Error: submissions for that problem are not viewable.'
127 return
128 return
128 end
129 end
129 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
130 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
130 end
131 end
131 end
132 end
132
133
133 def result
134 def result
134 if !GraderConfiguration.show_grading_result
135 if !GraderConfiguration.show_grading_result
135 redirect_to :action => 'list' and return
136 redirect_to :action => 'list' and return
136 end
137 end
137 @user = User.find(session[:user_id])
138 @user = User.find(session[:user_id])
138 @submission = Submission.find(params[:id])
139 @submission = Submission.find(params[:id])
139 if @submission.user!=@user
140 if @submission.user!=@user
140 flash[:notice] = 'You are not allowed to view result of other users.'
141 flash[:notice] = 'You are not allowed to view result of other users.'
141 redirect_to :action => 'list' and return
142 redirect_to :action => 'list' and return
142 end
143 end
143 prepare_grading_result(@submission)
144 prepare_grading_result(@submission)
144 end
145 end
145
146
146 def load_output
147 def load_output
147 if !GraderConfiguration.show_grading_result or params[:num]==nil
148 if !GraderConfiguration.show_grading_result or params[:num]==nil
148 redirect_to :action => 'list' and return
149 redirect_to :action => 'list' and return
149 end
150 end
150 @user = User.find(session[:user_id])
151 @user = User.find(session[:user_id])
151 @submission = Submission.find(params[:id])
152 @submission = Submission.find(params[:id])
152 if @submission.user!=@user
153 if @submission.user!=@user
153 flash[:notice] = 'You are not allowed to view result of other users.'
154 flash[:notice] = 'You are not allowed to view result of other users.'
154 redirect_to :action => 'list' and return
155 redirect_to :action => 'list' and return
155 end
156 end
156 case_num = params[:num].to_i
157 case_num = params[:num].to_i
157 out_filename = output_filename(@user.login,
158 out_filename = output_filename(@user.login,
158 @submission.problem.name,
159 @submission.problem.name,
159 @submission.id,
160 @submission.id,
160 case_num)
161 case_num)
161 if !FileTest.exists?(out_filename)
162 if !FileTest.exists?(out_filename)
162 flash[:notice] = 'Output not found.'
163 flash[:notice] = 'Output not found.'
@@ -31,164 +31,165
31 end
31 end
32
32
33 def chg_passwd
33 def chg_passwd
34 user = User.find(session[:user_id])
34 user = User.find(session[:user_id])
35 user.password = params[:passwd]
35 user.password = params[:passwd]
36 user.password_confirmation = params[:passwd_verify]
36 user.password_confirmation = params[:passwd_verify]
37 if user.save
37 if user.save
38 flash[:notice] = 'password changed'
38 flash[:notice] = 'password changed'
39 else
39 else
40 flash[:notice] = 'Error: password changing failed'
40 flash[:notice] = 'Error: password changing failed'
41 end
41 end
42 redirect_to :action => 'index'
42 redirect_to :action => 'index'
43 end
43 end
44
44
45 def new
45 def new
46 @user = User.new
46 @user = User.new
47 render :action => 'new', :layout => 'empty'
47 render :action => 'new', :layout => 'empty'
48 end
48 end
49
49
50 def register
50 def register
51 if(params[:cancel])
51 if(params[:cancel])
52 redirect_to :controller => 'main', :action => 'login'
52 redirect_to :controller => 'main', :action => 'login'
53 return
53 return
54 end
54 end
55 @user = User.new(params[:user])
55 @user = User.new(params[:user])
56 @user.password_confirmation = @user.password = User.random_password
56 @user.password_confirmation = @user.password = User.random_password
57 @user.activated = false
57 @user.activated = false
58 if (@user.valid?) and (@user.save)
58 if (@user.valid?) and (@user.save)
59 if send_confirmation_email(@user)
59 if send_confirmation_email(@user)
60 render :action => 'new_splash', :layout => 'empty'
60 render :action => 'new_splash', :layout => 'empty'
61 else
61 else
62 @admin_email = GraderConfiguration['system.admin_email']
62 @admin_email = GraderConfiguration['system.admin_email']
63 render :action => 'email_error', :layout => 'empty'
63 render :action => 'email_error', :layout => 'empty'
64 end
64 end
65 else
65 else
66 @user.errors.add_to_base("Email cannot be blank") if @user.email==''
66 @user.errors.add_to_base("Email cannot be blank") if @user.email==''
67 render :action => 'new', :layout => 'empty'
67 render :action => 'new', :layout => 'empty'
68 end
68 end
69 end
69 end
70
70
71 def confirm
71 def confirm
72 login = params[:login]
72 login = params[:login]
73 key = params[:activation]
73 key = params[:activation]
74 @user = User.find_by_login(login)
74 @user = User.find_by_login(login)
75 if (@user) and (@user.verify_activation_key(key))
75 if (@user) and (@user.verify_activation_key(key))
76 if @user.valid? # check uniquenss of email
76 if @user.valid? # check uniquenss of email
77 @user.activated = true
77 @user.activated = true
78 @user.save
78 @user.save
79 @result = :successful
79 @result = :successful
80 else
80 else
81 @result = :email_used
81 @result = :email_used
82 end
82 end
83 else
83 else
84 @result = :failed
84 @result = :failed
85 end
85 end
86 render :action => 'confirm', :layout => 'empty'
86 render :action => 'confirm', :layout => 'empty'
87 end
87 end
88
88
89 def forget
89 def forget
90 render :action => 'forget', :layout => 'empty'
90 render :action => 'forget', :layout => 'empty'
91 end
91 end
92
92
93 def retrieve_password
93 def retrieve_password
94 email = params[:email]
94 email = params[:email]
95 user = User.find_by_email(email)
95 user = User.find_by_email(email)
96 if user
96 if user
97 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
97 last_updated_time = user.updated_at || user.created_at || (Time.now.gmtime - 1.hour)
98 if last_updated_time > Time.now.gmtime - 5.minutes
98 if last_updated_time > Time.now.gmtime - 5.minutes
99 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
99 flash[:notice] = 'The account has recently created or new password has recently been requested. Please wait for 5 minutes'
100 else
100 else
101 user.password = user.password_confirmation = User.random_password
101 user.password = user.password_confirmation = User.random_password
102 user.save
102 user.save
103 send_new_password_email(user)
103 send_new_password_email(user)
104 flash[:notice] = 'New password has been mailed to you.'
104 flash[:notice] = 'New password has been mailed to you.'
105 end
105 end
106 else
106 else
107 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
107 flash[:notice] = I18n.t 'registration.password_retrieval.no_email'
108 end
108 end
109 redirect_to :action => 'forget'
109 redirect_to :action => 'forget'
110 end
110 end
111
111
112 def profile
112 def profile
113 @user = User.find(params[:id])
113 @user = User.find(params[:id])
114 @submission = Submission.includes(:problem).where(user_id: params[:id])
114 @submission = Submission.includes(:problem).where(user_id: params[:id])
115
115
116 range = 120
116 range = 120
117 @histogram = { data: Array.new(range,0), summary: {} }
117 @histogram = { data: Array.new(range,0), summary: {} }
118 @summary = {count: 0, solve: 0, attempt: 0}
118 @summary = {count: 0, solve: 0, attempt: 0}
119 problem = Hash.new(0)
119 problem = Hash.new(0)
120
120
121 @submission.find_each do |sub|
121 @submission.find_each do |sub|
122 #histogram
122 #histogram
123 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
123 d = (DateTime.now.in_time_zone - sub.submitted_at) / 24 / 60 / 60
124 @histogram[:data][d.to_i] += 1 if d < range
124 @histogram[:data][d.to_i] += 1 if d < range
125
125
126 @summary[:count] += 1
126 @summary[:count] += 1
127 + next unless sub.problem
127 problem[sub.problem] = [problem[sub.problem], (sub.points >= sub.problem.full_score) ? 1 : 0].max
128 problem[sub.problem] = [problem[sub.problem], (sub.points >= sub.problem.full_score) ? 1 : 0].max
128 end
129 end
129
130
130 @histogram[:summary][:max] = [@histogram[:data].max,1].max
131 @histogram[:summary][:max] = [@histogram[:data].max,1].max
131 @summary[:attempt] = problem.count
132 @summary[:attempt] = problem.count
132 problem.each_value { |v| @summary[:solve] += 1 if v == 1 }
133 problem.each_value { |v| @summary[:solve] += 1 if v == 1 }
133 end
134 end
134
135
135 protected
136 protected
136
137
137 def verify_online_registration
138 def verify_online_registration
138 if !GraderConfiguration['system.online_registration']
139 if !GraderConfiguration['system.online_registration']
139 redirect_to :controller => 'main', :action => 'login'
140 redirect_to :controller => 'main', :action => 'login'
140 end
141 end
141 end
142 end
142
143
143 def send_confirmation_email(user)
144 def send_confirmation_email(user)
144 contest_name = GraderConfiguration['contest.name']
145 contest_name = GraderConfiguration['contest.name']
145 activation_url = url_for(:action => 'confirm',
146 activation_url = url_for(:action => 'confirm',
146 :login => user.login,
147 :login => user.login,
147 :activation => user.activation_key)
148 :activation => user.activation_key)
148 home_url = url_for(:controller => 'main', :action => 'index')
149 home_url = url_for(:controller => 'main', :action => 'index')
149 mail_subject = "[#{contest_name}] Confirmation"
150 mail_subject = "[#{contest_name}] Confirmation"
150 mail_body = t('registration.email_body', {
151 mail_body = t('registration.email_body', {
151 :full_name => user.full_name,
152 :full_name => user.full_name,
152 :contest_name => contest_name,
153 :contest_name => contest_name,
153 :login => user.login,
154 :login => user.login,
154 :password => user.password,
155 :password => user.password,
155 :activation_url => activation_url,
156 :activation_url => activation_url,
156 :admin_email => admin_email
157 :admin_email => admin_email
157 })
158 })
158
159
159 logger.info mail_body
160 logger.info mail_body
160
161
161 send_mail(user.email, mail_subject, mail_body)
162 send_mail(user.email, mail_subject, mail_body)
162 end
163 end
163
164
164 def send_new_password_email(user)
165 def send_new_password_email(user)
165 contest_name = GraderConfiguration['contest.name']
166 contest_name = GraderConfiguration['contest.name']
166 mail_subject = "[#{contest_name}] Password recovery"
167 mail_subject = "[#{contest_name}] Password recovery"
167 mail_body = t('registration.password_retrieval.email_body', {
168 mail_body = t('registration.password_retrieval.email_body', {
168 :full_name => user.full_name,
169 :full_name => user.full_name,
169 :contest_name => contest_name,
170 :contest_name => contest_name,
170 :login => user.login,
171 :login => user.login,
171 :password => user.password,
172 :password => user.password,
172 :admin_email => admin_email
173 :admin_email => admin_email
173 })
174 })
174
175
175 logger.info mail_body
176 logger.info mail_body
176
177
177 send_mail(user.email, mail_subject, mail_body)
178 send_mail(user.email, mail_subject, mail_body)
178 end
179 end
179
180
180 # allow viewing of regular user profile only when options allow so
181 # allow viewing of regular user profile only when options allow so
181 # only admins can view admins profile
182 # only admins can view admins profile
182 def profile_authorization
183 def profile_authorization
183 #if view admins' profile, allow only admin
184 #if view admins' profile, allow only admin
184 return false unless(params[:id])
185 return false unless(params[:id])
185 user = User.find(params[:id])
186 user = User.find(params[:id])
186 return false unless user
187 return false unless user
187 return admin_authorization if user.admin?
188 return admin_authorization if user.admin?
188 return true if GraderConfiguration["right.user_view_submission"]
189 return true if GraderConfiguration["right.user_view_submission"]
189
190
190 #finally, we allow only admin
191 #finally, we allow only admin
191 admin_authorization
192 admin_authorization
192 end
193 end
193
194
194 end
195 end
You need to be logged in to leave comments. Login now