Description:
added timestamp to source download git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@365 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r168:1a0b47f4c2a4 - - 2 files changed: 10 inserted, 6 deleted

@@ -1,188 +1,183
1 class MainController < ApplicationController
1 class MainController < ApplicationController
2
2
3 SYSTEM_MODE_CONF_KEY = 'system.mode'
3 SYSTEM_MODE_CONF_KEY = 'system.mode'
4
4
5 before_filter :authenticate, :except => [:index, :login]
5 before_filter :authenticate, :except => [:index, :login]
6 before_filter :check_viewability, :except => [:index, :login]
6 before_filter :check_viewability, :except => [:index, :login]
7
7
8 # COMMENTED OUT: filter in each action instead
8 # COMMENTED OUT: filter in each action instead
9 # before_filter :verify_time_limit, :only => [:submit]
9 # before_filter :verify_time_limit, :only => [:submit]
10
10
11 verify :method => :post, :only => [:submit],
11 verify :method => :post, :only => [:submit],
12 :redirect_to => { :action => :index }
12 :redirect_to => { :action => :index }
13
13
14 # COMMENT OUT: only need when having high load
14 # COMMENT OUT: only need when having high load
15 # caches_action :index, :login
15 # caches_action :index, :login
16
16
17 # NOTE: This method is not actually needed, 'config/routes.rb' has
17 # NOTE: This method is not actually needed, 'config/routes.rb' has
18 # assigned action login as a default action.
18 # assigned action login as a default action.
19 def index
19 def index
20 redirect_to :action => 'login'
20 redirect_to :action => 'login'
21 end
21 end
22
22
23 def login
23 def login
24 saved_notice = flash[:notice]
24 saved_notice = flash[:notice]
25 reset_session
25 reset_session
26 flash[:notice] = saved_notice
26 flash[:notice] = saved_notice
27
27
28 # EXPERIMENT:
28 # EXPERIMENT:
29 # Hide login if in single user mode and the url does not
29 # Hide login if in single user mode and the url does not
30 # explicitly specify /login
30 # explicitly specify /login
31 #
31 #
32 # logger.info "PATH: #{request.path}"
32 # logger.info "PATH: #{request.path}"
33 # if Configuration['system.single_user_mode'] and
33 # if Configuration['system.single_user_mode'] and
34 # request.path!='/main/login'
34 # request.path!='/main/login'
35 # @hidelogin = true
35 # @hidelogin = true
36 # end
36 # end
37
37
38 @announcements = Announcement.find_for_frontpage
38 @announcements = Announcement.find_for_frontpage
39 render :action => 'login', :layout => 'empty'
39 render :action => 'login', :layout => 'empty'
40 end
40 end
41
41
42 def list
42 def list
43 prepare_list_information
43 prepare_list_information
44 end
44 end
45
45
46 def help
46 def help
47 @user = User.find(session[:user_id])
47 @user = User.find(session[:user_id])
48 end
48 end
49
49
50 def submit
50 def submit
51 user = User.find(session[:user_id])
51 user = User.find(session[:user_id])
52
52
53 @submission = Submission.new(params[:submission])
53 @submission = Submission.new(params[:submission])
54 @submission.user = user
54 @submission.user = user
55 @submission.language_id = 0
55 @submission.language_id = 0
56 if params['file']!=''
56 if params['file']!=''
57 @submission.source = params['file'].read
57 @submission.source = params['file'].read
58 @submission.source_filename = params['file'].original_filename
58 @submission.source_filename = params['file'].original_filename
59 end
59 end
60 @submission.submitted_at = Time.new.gmtime
60 @submission.submitted_at = Time.new.gmtime
61
61
62 if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and
62 if Configuration[SYSTEM_MODE_CONF_KEY]=='contest' and
63 user.site!=nil and user.site.finished?
63 user.site!=nil and user.site.finished?
64 @submission.errors.add_to_base "The contest is over."
64 @submission.errors.add_to_base "The contest is over."
65 prepare_list_information
65 prepare_list_information
66 render :action => 'list' and return
66 render :action => 'list' and return
67 end
67 end
68
68
69 if @submission.valid?
69 if @submission.valid?
70 if @submission.save == false
70 if @submission.save == false
71 flash[:notice] = 'Error saving your submission'
71 flash[:notice] = 'Error saving your submission'
72 elsif Task.create(:submission_id => @submission.id,
72 elsif Task.create(:submission_id => @submission.id,
73 :status => Task::STATUS_INQUEUE) == false
73 :status => Task::STATUS_INQUEUE) == false
74 flash[:notice] = 'Error adding your submission to task queue'
74 flash[:notice] = 'Error adding your submission to task queue'
75 end
75 end
76 else
76 else
77 prepare_list_information
77 prepare_list_information
78 render :action => 'list' and return
78 render :action => 'list' and return
79 end
79 end
80 redirect_to :action => 'list'
80 redirect_to :action => 'list'
81 end
81 end
82
82
83 def source
83 def source
84 submission = Submission.find(params[:id])
84 submission = Submission.find(params[:id])
85 if submission.user_id == session[:user_id]
85 if submission.user_id == session[:user_id]
86 - if submission.problem.output_only
87 - fname = submission.source_filename
88 - else
89 - fname = submission.problem.name + '.' + submission.language.ext
90 - end
91 send_data(submission.source,
86 send_data(submission.source,
92 - {:filename => fname,
87 + {:filename => submission.download_filename,
93 :type => 'text/plain'})
88 :type => 'text/plain'})
94 else
89 else
95 flash[:notice] = 'Error viewing source'
90 flash[:notice] = 'Error viewing source'
96 redirect_to :action => 'list'
91 redirect_to :action => 'list'
97 end
92 end
98 end
93 end
99
94
100 def compiler_msg
95 def compiler_msg
101 @submission = Submission.find(params[:id])
96 @submission = Submission.find(params[:id])
102 if @submission.user_id == session[:user_id]
97 if @submission.user_id == session[:user_id]
103 render :action => 'compiler_msg', :layout => 'empty'
98 render :action => 'compiler_msg', :layout => 'empty'
104 else
99 else
105 flash[:notice] = 'Error viewing source'
100 flash[:notice] = 'Error viewing source'
106 redirect_to :action => 'list'
101 redirect_to :action => 'list'
107 end
102 end
108 end
103 end
109
104
110 def submission
105 def submission
111 @user = User.find(session[:user_id])
106 @user = User.find(session[:user_id])
112 @problems = Problem.find_available_problems
107 @problems = Problem.find_available_problems
113 if params[:id]==nil
108 if params[:id]==nil
114 @problem = nil
109 @problem = nil
115 @submissions = nil
110 @submissions = nil
116 else
111 else
117 @problem = Problem.find_by_name(params[:id])
112 @problem = Problem.find_by_name(params[:id])
118 if not @problem.available
113 if not @problem.available
119 redirect_to :action => 'list'
114 redirect_to :action => 'list'
120 flash[:notice] = 'Error: submissions for that problem are not viewable.'
115 flash[:notice] = 'Error: submissions for that problem are not viewable.'
121 return
116 return
122 end
117 end
123 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
118 @submissions = Submission.find_all_by_user_problem(@user.id, @problem.id)
124 end
119 end
125 end
120 end
126
121
127 def result
122 def result
128 if !Configuration.show_grading_result
123 if !Configuration.show_grading_result
129 redirect_to :action => 'list' and return
124 redirect_to :action => 'list' and return
130 end
125 end
131 @user = User.find(session[:user_id])
126 @user = User.find(session[:user_id])
132 @submission = Submission.find(params[:id])
127 @submission = Submission.find(params[:id])
133 if @submission.user!=@user
128 if @submission.user!=@user
134 flash[:notice] = 'You are not allowed to view result of other users.'
129 flash[:notice] = 'You are not allowed to view result of other users.'
135 redirect_to :action => 'list' and return
130 redirect_to :action => 'list' and return
136 end
131 end
137 prepare_grading_result(@submission)
132 prepare_grading_result(@submission)
138 end
133 end
139
134
140 def load_output
135 def load_output
141 if !Configuration.show_grading_result or params[:num]==nil
136 if !Configuration.show_grading_result or params[:num]==nil
142 redirect_to :action => 'list' and return
137 redirect_to :action => 'list' and return
143 end
138 end
144 @user = User.find(session[:user_id])
139 @user = User.find(session[:user_id])
145 @submission = Submission.find(params[:id])
140 @submission = Submission.find(params[:id])
146 if @submission.user!=@user
141 if @submission.user!=@user
147 flash[:notice] = 'You are not allowed to view result of other users.'
142 flash[:notice] = 'You are not allowed to view result of other users.'
148 redirect_to :action => 'list' and return
143 redirect_to :action => 'list' and return
149 end
144 end
150 case_num = params[:num].to_i
145 case_num = params[:num].to_i
151 out_filename = output_filename(@user.login,
146 out_filename = output_filename(@user.login,
152 @submission.problem.name,
147 @submission.problem.name,
153 @submission.id,
148 @submission.id,
154 case_num)
149 case_num)
155 if !FileTest.exists?(out_filename)
150 if !FileTest.exists?(out_filename)
156 flash[:notice] = 'Output not found.'
151 flash[:notice] = 'Output not found.'
157 redirect_to :action => 'list' and return
152 redirect_to :action => 'list' and return
158 end
153 end
159
154
160 response.headers['Content-Type'] = "application/force-download"
155 response.headers['Content-Type'] = "application/force-download"
161 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
156 response.headers['Content-Disposition'] = "attachment; filename=\"output-#{case_num}.txt\""
162 response.headers["X-Sendfile"] = out_filename
157 response.headers["X-Sendfile"] = out_filename
163 response.headers['Content-length'] = File.size(out_filename)
158 response.headers['Content-length'] = File.size(out_filename)
164 render :nothing => true
159 render :nothing => true
165 end
160 end
166
161
167 def error
162 def error
168 @user = User.find(session[:user_id])
163 @user = User.find(session[:user_id])
169 end
164 end
170
165
171 protected
166 protected
172 def prepare_list_information
167 def prepare_list_information
173 @problems = Problem.find_available_problems
168 @problems = Problem.find_available_problems
174 @prob_submissions = Array.new
169 @prob_submissions = Array.new
175 @user = User.find(session[:user_id])
170 @user = User.find(session[:user_id])
176 @problems.each do |p|
171 @problems.each do |p|
177 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
172 sub = Submission.find_last_by_user_and_problem(@user.id,p.id)
178 if sub!=nil
173 if sub!=nil
179 @prob_submissions << { :count => sub.number, :submission => sub }
174 @prob_submissions << { :count => sub.number, :submission => sub }
180 else
175 else
181 @prob_submissions << { :count => 0, :submission => nil }
176 @prob_submissions << { :count => 0, :submission => nil }
182 end
177 end
183 end
178 end
184 if Configuration.show_tasks_to?(@user)
179 if Configuration.show_tasks_to?(@user)
185 @announcements = Announcement.find_published(true)
180 @announcements = Announcement.find_published(true)
186 else
181 else
187 @announcements = Announcement.find_published
182 @announcements = Announcement.find_published
188 end
183 end
@@ -1,157 +1,166
1 class Submission < ActiveRecord::Base
1 class Submission < ActiveRecord::Base
2
2
3 belongs_to :language
3 belongs_to :language
4 belongs_to :problem
4 belongs_to :problem
5 belongs_to :user
5 belongs_to :user
6
6
7 before_validation :assign_problem
7 before_validation :assign_problem
8 before_validation :assign_language
8 before_validation :assign_language
9
9
10 validates_presence_of :source
10 validates_presence_of :source
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 validate :must_have_valid_problem
13 validate :must_have_valid_problem
14 validate :must_specify_language
14 validate :must_specify_language
15
15
16 before_save :assign_latest_number_if_new_recond
16 before_save :assign_latest_number_if_new_recond
17
17
18 def self.find_last_by_user_and_problem(user_id, problem_id)
18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 last_sub = find(:first,
19 last_sub = find(:first,
20 :conditions => {:user_id => user_id,
20 :conditions => {:user_id => user_id,
21 :problem_id => problem_id},
21 :problem_id => problem_id},
22 :order => 'number DESC')
22 :order => 'number DESC')
23 return last_sub
23 return last_sub
24 end
24 end
25
25
26 def self.find_all_last_by_problem(problem_id)
26 def self.find_all_last_by_problem(problem_id)
27 # need to put in SQL command, maybe there's a better way
27 # need to put in SQL command, maybe there's a better way
28 Submission.find_by_sql("SELECT * FROM submissions " +
28 Submission.find_by_sql("SELECT * FROM submissions " +
29 "WHERE id = " +
29 "WHERE id = " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
31 "WHERE subs.user_id = submissions.user_id AND " +
31 "WHERE subs.user_id = submissions.user_id AND " +
32 "problem_id = " + problem_id.to_s + " " +
32 "problem_id = " + problem_id.to_s + " " +
33 "GROUP BY user_id) " +
33 "GROUP BY user_id) " +
34 "ORDER BY user_id")
34 "ORDER BY user_id")
35 end
35 end
36
36
37 def self.find_last_for_all_available_problems(user_id)
37 def self.find_last_for_all_available_problems(user_id)
38 submissions = Array.new
38 submissions = Array.new
39 problems = Problem.find_available_problems
39 problems = Problem.find_available_problems
40 problems.each do |problem|
40 problems.each do |problem|
41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
41 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
42 submissions << sub if sub!=nil
42 submissions << sub if sub!=nil
43 end
43 end
44 submissions
44 submissions
45 end
45 end
46
46
47 def self.find_by_user_problem_number(user_id, problem_id, number)
47 def self.find_by_user_problem_number(user_id, problem_id, number)
48 Submission.find(:first,
48 Submission.find(:first,
49 :conditions => {
49 :conditions => {
50 :user_id => user_id,
50 :user_id => user_id,
51 :problem_id => problem_id,
51 :problem_id => problem_id,
52 :number => number
52 :number => number
53 })
53 })
54 end
54 end
55
55
56 def self.find_all_by_user_problem(user_id, problem_id)
56 def self.find_all_by_user_problem(user_id, problem_id)
57 Submission.find(:all,
57 Submission.find(:all,
58 :conditions => {
58 :conditions => {
59 :user_id => user_id,
59 :user_id => user_id,
60 :problem_id => problem_id,
60 :problem_id => problem_id,
61 })
61 })
62 end
62 end
63
63
64 + def download_filename
65 + if self.problem.output_only
66 + return self.source_filename
67 + else
68 + timestamp = self.submitted_at.localtime.strftime("%H%M%S")
69 + return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
70 + end
71 + end
72 +
64 protected
73 protected
65
74
66 def self.find_option_in_source(option, source)
75 def self.find_option_in_source(option, source)
67 if source==nil
76 if source==nil
68 return nil
77 return nil
69 end
78 end
70 i = 0
79 i = 0
71 source.each_line do |s|
80 source.each_line do |s|
72 if s =~ option
81 if s =~ option
73 words = s.split
82 words = s.split
74 return words[1]
83 return words[1]
75 end
84 end
76 i = i + 1
85 i = i + 1
77 if i==10
86 if i==10
78 return nil
87 return nil
79 end
88 end
80 end
89 end
81 return nil
90 return nil
82 end
91 end
83
92
84 def self.find_language_in_source(source, source_filename="")
93 def self.find_language_in_source(source, source_filename="")
85 langopt = find_option_in_source(/^LANG:/,source)
94 langopt = find_option_in_source(/^LANG:/,source)
86 if langopt
95 if langopt
87 return (Language.find_by_name(langopt) ||
96 return (Language.find_by_name(langopt) ||
88 Language.find_by_pretty_name(langopt))
97 Language.find_by_pretty_name(langopt))
89 else
98 else
90 if source_filename
99 if source_filename
91 return Language.find_by_extension(source_filename.split('.').last)
100 return Language.find_by_extension(source_filename.split('.').last)
92 else
101 else
93 return nil
102 return nil
94 end
103 end
95 end
104 end
96 end
105 end
97
106
98 def self.find_problem_in_source(source, source_filename="")
107 def self.find_problem_in_source(source, source_filename="")
99 prob_opt = find_option_in_source(/^TASK:/,source)
108 prob_opt = find_option_in_source(/^TASK:/,source)
100 if problem = Problem.find_by_name(prob_opt)
109 if problem = Problem.find_by_name(prob_opt)
101 return problem
110 return problem
102 else
111 else
103 if source_filename
112 if source_filename
104 return Problem.find_by_name(source_filename.split('.').first)
113 return Problem.find_by_name(source_filename.split('.').first)
105 else
114 else
106 return nil
115 return nil
107 end
116 end
108 end
117 end
109 end
118 end
110
119
111 def assign_problem
120 def assign_problem
112 if self.problem_id!=-1
121 if self.problem_id!=-1
113 begin
122 begin
114 self.problem = Problem.find(self.problem_id)
123 self.problem = Problem.find(self.problem_id)
115 rescue ActiveRecord::RecordNotFound
124 rescue ActiveRecord::RecordNotFound
116 self.problem = nil
125 self.problem = nil
117 end
126 end
118 else
127 else
119 self.problem = Submission.find_problem_in_source(self.source,
128 self.problem = Submission.find_problem_in_source(self.source,
120 self.source_filename)
129 self.source_filename)
121 end
130 end
122 end
131 end
123
132
124 def assign_language
133 def assign_language
125 self.language = Submission.find_language_in_source(self.source,
134 self.language = Submission.find_language_in_source(self.source,
126 self.source_filename)
135 self.source_filename)
127 end
136 end
128
137
129 # validation codes
138 # validation codes
130 def must_specify_language
139 def must_specify_language
131 return if self.source==nil
140 return if self.source==nil
132
141
133 # for output_only tasks
142 # for output_only tasks
134 return if self.problem!=nil and self.problem.output_only
143 return if self.problem!=nil and self.problem.output_only
135
144
136 if self.language==nil
145 if self.language==nil
137 errors.add('source',"must specify programming language") unless self.language!=nil
146 errors.add('source',"must specify programming language") unless self.language!=nil
138 end
147 end
139 end
148 end
140
149
141 def must_have_valid_problem
150 def must_have_valid_problem
142 return if self.source==nil
151 return if self.source==nil
143 if self.problem==nil
152 if self.problem==nil
144 errors.add('problem',"must be specified.")
153 errors.add('problem',"must be specified.")
145 elsif (!self.problem.available) and (self.new_record?)
154 elsif (!self.problem.available) and (self.new_record?)
146 errors.add('problem',"must be valid.")
155 errors.add('problem',"must be valid.")
147 end
156 end
148 end
157 end
149
158
150 # callbacks
159 # callbacks
151 def assign_latest_number_if_new_recond
160 def assign_latest_number_if_new_recond
152 return if !self.new_record?
161 return if !self.new_record?
153 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
162 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
154 self.number = (latest==nil) ? 1 : latest.number + 1;
163 self.number = (latest==nil) ? 1 : latest.number + 1;
155 end
164 end
156
165
157 end
166 end
You need to be logged in to leave comments. Login now