Description:
Change to better error message when the submitted file has illegal extension Solve issue #4
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r548:ba97cbe38d86 - - 1 file changed: 1 inserted, 1 deleted

@@ -1,173 +1,173
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.includes(:user).find_by_sql("SELECT * FROM submissions " +
28 Submission.includes(:user).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_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
37 def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
38 records = Submission.where(problem_id: problem_id,user_id: user_id)
38 records = Submission.where(problem_id: problem_id,user_id: user_id)
39 records = records.where('id >= ?',since_id) if since_id > 0
39 records = records.where('id >= ?',since_id) if since_id > 0
40 records = records.where('id <= ?',until_id) if until_id > 0
40 records = records.where('id <= ?',until_id) if until_id > 0
41 records.all
41 records.all
42 end
42 end
43
43
44 def self.find_last_for_all_available_problems(user_id)
44 def self.find_last_for_all_available_problems(user_id)
45 submissions = Array.new
45 submissions = Array.new
46 problems = Problem.find_available_problems
46 problems = Problem.find_available_problems
47 problems.each do |problem|
47 problems.each do |problem|
48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
49 submissions << sub if sub!=nil
49 submissions << sub if sub!=nil
50 end
50 end
51 submissions
51 submissions
52 end
52 end
53
53
54 def self.find_by_user_problem_number(user_id, problem_id, number)
54 def self.find_by_user_problem_number(user_id, problem_id, number)
55 Submission.find(:first,
55 Submission.find(:first,
56 :conditions => {
56 :conditions => {
57 :user_id => user_id,
57 :user_id => user_id,
58 :problem_id => problem_id,
58 :problem_id => problem_id,
59 :number => number
59 :number => number
60 })
60 })
61 end
61 end
62
62
63 def self.find_all_by_user_problem(user_id, problem_id)
63 def self.find_all_by_user_problem(user_id, problem_id)
64 Submission.find(:all,
64 Submission.find(:all,
65 :conditions => {
65 :conditions => {
66 :user_id => user_id,
66 :user_id => user_id,
67 :problem_id => problem_id,
67 :problem_id => problem_id,
68 })
68 })
69 end
69 end
70
70
71 def download_filename
71 def download_filename
72 if self.problem.output_only
72 if self.problem.output_only
73 return self.source_filename
73 return self.source_filename
74 else
74 else
75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
77 end
77 end
78 end
78 end
79
79
80 protected
80 protected
81
81
82 def self.find_option_in_source(option, source)
82 def self.find_option_in_source(option, source)
83 if source==nil
83 if source==nil
84 return nil
84 return nil
85 end
85 end
86 i = 0
86 i = 0
87 source.each_line do |s|
87 source.each_line do |s|
88 if s =~ option
88 if s =~ option
89 words = s.split
89 words = s.split
90 return words[1]
90 return words[1]
91 end
91 end
92 i = i + 1
92 i = i + 1
93 if i==10
93 if i==10
94 return nil
94 return nil
95 end
95 end
96 end
96 end
97 return nil
97 return nil
98 end
98 end
99
99
100 def self.find_language_in_source(source, source_filename="")
100 def self.find_language_in_source(source, source_filename="")
101 langopt = find_option_in_source(/^LANG:/,source)
101 langopt = find_option_in_source(/^LANG:/,source)
102 if langopt
102 if langopt
103 return (Language.find_by_name(langopt) ||
103 return (Language.find_by_name(langopt) ||
104 Language.find_by_pretty_name(langopt))
104 Language.find_by_pretty_name(langopt))
105 else
105 else
106 if source_filename
106 if source_filename
107 return Language.find_by_extension(source_filename.split('.').last)
107 return Language.find_by_extension(source_filename.split('.').last)
108 else
108 else
109 return nil
109 return nil
110 end
110 end
111 end
111 end
112 end
112 end
113
113
114 def self.find_problem_in_source(source, source_filename="")
114 def self.find_problem_in_source(source, source_filename="")
115 prob_opt = find_option_in_source(/^TASK:/,source)
115 prob_opt = find_option_in_source(/^TASK:/,source)
116 if problem = Problem.find_by_name(prob_opt)
116 if problem = Problem.find_by_name(prob_opt)
117 return problem
117 return problem
118 else
118 else
119 if source_filename
119 if source_filename
120 return Problem.find_by_name(source_filename.split('.').first)
120 return Problem.find_by_name(source_filename.split('.').first)
121 else
121 else
122 return nil
122 return nil
123 end
123 end
124 end
124 end
125 end
125 end
126
126
127 def assign_problem
127 def assign_problem
128 if self.problem_id!=-1
128 if self.problem_id!=-1
129 begin
129 begin
130 self.problem = Problem.find(self.problem_id)
130 self.problem = Problem.find(self.problem_id)
131 rescue ActiveRecord::RecordNotFound
131 rescue ActiveRecord::RecordNotFound
132 self.problem = nil
132 self.problem = nil
133 end
133 end
134 else
134 else
135 self.problem = Submission.find_problem_in_source(self.source,
135 self.problem = Submission.find_problem_in_source(self.source,
136 self.source_filename)
136 self.source_filename)
137 end
137 end
138 end
138 end
139
139
140 def assign_language
140 def assign_language
141 self.language = Submission.find_language_in_source(self.source,
141 self.language = Submission.find_language_in_source(self.source,
142 self.source_filename)
142 self.source_filename)
143 end
143 end
144
144
145 # validation codes
145 # validation codes
146 def must_specify_language
146 def must_specify_language
147 return if self.source==nil
147 return if self.source==nil
148
148
149 # for output_only tasks
149 # for output_only tasks
150 return if self.problem!=nil and self.problem.output_only
150 return if self.problem!=nil and self.problem.output_only
151
151
152 if self.language==nil
152 if self.language==nil
153 - errors.add('source',"must specify programming language") unless self.language!=nil
153 + errors.add('source',"Cannot detect language. Did you submit a correct source file?") unless self.language!=nil
154 end
154 end
155 end
155 end
156
156
157 def must_have_valid_problem
157 def must_have_valid_problem
158 return if self.source==nil
158 return if self.source==nil
159 if self.problem==nil
159 if self.problem==nil
160 errors.add('problem',"must be specified.")
160 errors.add('problem',"must be specified.")
161 elsif (!self.problem.available) and (self.new_record?)
161 elsif (!self.problem.available) and (self.new_record?)
162 errors.add('problem',"must be valid.")
162 errors.add('problem',"must be valid.")
163 end
163 end
164 end
164 end
165
165
166 # callbacks
166 # callbacks
167 def assign_latest_number_if_new_recond
167 def assign_latest_number_if_new_recond
168 return if !self.new_record?
168 return if !self.new_record?
169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
170 self.number = (latest==nil) ? 1 : latest.number + 1;
170 self.number = (latest==nil) ? 1 : latest.number + 1;
171 end
171 end
172
172
173 end
173 end
You need to be logged in to leave comments. Login now