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 1 class Submission < ActiveRecord::Base
2 2
3 3 belongs_to :language
4 4 belongs_to :problem
5 5 belongs_to :user
6 6
7 7 before_validation :assign_problem
8 8 before_validation :assign_language
9 9
10 10 validates_presence_of :source
11 11 validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
12 12 validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
13 13 validate :must_have_valid_problem
14 14 validate :must_specify_language
15 15
16 16 before_save :assign_latest_number_if_new_recond
17 17
18 18 def self.find_last_by_user_and_problem(user_id, problem_id)
19 19 last_sub = find(:first,
20 20 :conditions => {:user_id => user_id,
21 21 :problem_id => problem_id},
22 22 :order => 'number DESC')
23 23 return last_sub
24 24 end
25 25
26 26 def self.find_all_last_by_problem(problem_id)
27 27 # need to put in SQL command, maybe there's a better way
28 28 Submission.includes(:user).find_by_sql("SELECT * FROM submissions " +
29 29 "WHERE id = " +
30 30 "(SELECT MAX(id) FROM submissions AS subs " +
31 31 "WHERE subs.user_id = submissions.user_id AND " +
32 32 "problem_id = " + problem_id.to_s + " " +
33 33 "GROUP BY user_id) " +
34 34 "ORDER BY user_id")
35 35 end
36 36
37 37 def self.find_in_range_by_user_and_problem(user_id, problem_id,since_id,until_id)
38 38 records = Submission.where(problem_id: problem_id,user_id: user_id)
39 39 records = records.where('id >= ?',since_id) if since_id > 0
40 40 records = records.where('id <= ?',until_id) if until_id > 0
41 41 records.all
42 42 end
43 43
44 44 def self.find_last_for_all_available_problems(user_id)
45 45 submissions = Array.new
46 46 problems = Problem.find_available_problems
47 47 problems.each do |problem|
48 48 sub = Submission.find_last_by_user_and_problem(user_id, problem.id)
49 49 submissions << sub if sub!=nil
50 50 end
51 51 submissions
52 52 end
53 53
54 54 def self.find_by_user_problem_number(user_id, problem_id, number)
55 55 Submission.find(:first,
56 56 :conditions => {
57 57 :user_id => user_id,
58 58 :problem_id => problem_id,
59 59 :number => number
60 60 })
61 61 end
62 62
63 63 def self.find_all_by_user_problem(user_id, problem_id)
64 64 Submission.find(:all,
65 65 :conditions => {
66 66 :user_id => user_id,
67 67 :problem_id => problem_id,
68 68 })
69 69 end
70 70
71 71 def download_filename
72 72 if self.problem.output_only
73 73 return self.source_filename
74 74 else
75 75 timestamp = self.submitted_at.localtime.strftime("%H%M%S")
76 76 return "#{self.problem.name}-#{timestamp}.#{self.language.ext}"
77 77 end
78 78 end
79 79
80 80 protected
81 81
82 82 def self.find_option_in_source(option, source)
83 83 if source==nil
84 84 return nil
85 85 end
86 86 i = 0
87 87 source.each_line do |s|
88 88 if s =~ option
89 89 words = s.split
90 90 return words[1]
91 91 end
92 92 i = i + 1
93 93 if i==10
94 94 return nil
95 95 end
96 96 end
97 97 return nil
98 98 end
99 99
100 100 def self.find_language_in_source(source, source_filename="")
101 101 langopt = find_option_in_source(/^LANG:/,source)
102 102 if langopt
103 103 return (Language.find_by_name(langopt) ||
104 104 Language.find_by_pretty_name(langopt))
105 105 else
106 106 if source_filename
107 107 return Language.find_by_extension(source_filename.split('.').last)
108 108 else
109 109 return nil
110 110 end
111 111 end
112 112 end
113 113
114 114 def self.find_problem_in_source(source, source_filename="")
115 115 prob_opt = find_option_in_source(/^TASK:/,source)
116 116 if problem = Problem.find_by_name(prob_opt)
117 117 return problem
118 118 else
119 119 if source_filename
120 120 return Problem.find_by_name(source_filename.split('.').first)
121 121 else
122 122 return nil
123 123 end
124 124 end
125 125 end
126 126
127 127 def assign_problem
128 128 if self.problem_id!=-1
129 129 begin
130 130 self.problem = Problem.find(self.problem_id)
131 131 rescue ActiveRecord::RecordNotFound
132 132 self.problem = nil
133 133 end
134 134 else
135 135 self.problem = Submission.find_problem_in_source(self.source,
136 136 self.source_filename)
137 137 end
138 138 end
139 139
140 140 def assign_language
141 141 self.language = Submission.find_language_in_source(self.source,
142 142 self.source_filename)
143 143 end
144 144
145 145 # validation codes
146 146 def must_specify_language
147 147 return if self.source==nil
148 148
149 149 # for output_only tasks
150 150 return if self.problem!=nil and self.problem.output_only
151 151
152 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 154 end
155 155 end
156 156
157 157 def must_have_valid_problem
158 158 return if self.source==nil
159 159 if self.problem==nil
160 160 errors.add('problem',"must be specified.")
161 161 elsif (!self.problem.available) and (self.new_record?)
162 162 errors.add('problem',"must be valid.")
163 163 end
164 164 end
165 165
166 166 # callbacks
167 167 def assign_latest_number_if_new_recond
168 168 return if !self.new_record?
169 169 latest = Submission.find_last_by_user_and_problem(self.user_id, self.problem_id)
170 170 self.number = (latest==nil) ? 1 : latest.number + 1;
171 171 end
172 172
173 173 end
You need to be logged in to leave comments. Login now