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