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

@@ -57,117 +57,117
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