Description:
moved submission validation to model git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@56 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

r31:07d90e75a295 - - 3 files changed: 59 inserted, 29 deleted

@@ -7,62 +7,59
7
7
8
8
9 def index
9 def index
10 redirect_to :action => 'login'
10 redirect_to :action => 'login'
11 end
11 end
12
12
13 def login
13 def login
14 reset_session
14 reset_session
15 render :action => 'login', :layout => 'empty'
15 render :action => 'login', :layout => 'empty'
16 end
16 end
17
17
18 def list
18 def list
19 - @problems = Problem.find_available_problems
19 + prepare_list_information
20 - @prob_submissions = Array.new
21 - @user = User.find(session[:user_id])
22 - @problems.each do |p|
23 - c, sub = Submission.find_by_user_and_problem(@user.id,p.id)
24 - @prob_submissions << [c,sub]
25 - end
26 end
20 end
27
21
28 def submit
22 def submit
29 - submission = Submission.new(params[:submission])
23 + @submission = Submission.new(params[:submission])
30 - submission.user_id = session[:user_id]
24 + @submission.user_id = session[:user_id]
31 - submission.language_id = 0
25 + @submission.language_id = 0
32 - source = params['file'].read
26 + @submission.source = params['file'].read if params['file']!=''
33 - if source.length > 100_000
27 + @submission.submitted_at = Time.new
34 - flash[:notice] = 'Error: file too long'
28 + if @submission.valid?
35 - elsif (lang = Submission.find_language_in_source(source))==nil
29 + if @submission.save == false
36 - flash[:notice] = 'Error: cannot determine language used'
37 - elsif ((submission.problem_id==-1) and
38 - !(problem=Submission.find_problem_in_source(source)))
39 - flash[:notice] = 'Error: cannot determine problem submitted'
40 - elsif ((submission.problem_id==-1) and
41 - (problem.available == false))
42 - flash[:notice] = 'Error: problem is not available'
43 - else
44 - submission.problem_id = problem.id if submission.problem_id == -1
45 - submission.source = source
46 - submission.language_id = lang.id
47 - submission.submitted_at = Time.new
48 - if submission.save == false
49 flash[:notice] = 'Error saving your submission'
30 flash[:notice] = 'Error saving your submission'
50 - elsif Task.create(:submission_id => submission.id) == false
31 + elsif Task.create(:submission_id => @submission.id,
32 + :status => Task::STATUS_INQUEUE) == false
51 flash[:notice] = 'Error adding your submission to task queue'
33 flash[:notice] = 'Error adding your submission to task queue'
52 end
34 end
53 end
35 end
54 - redirect_to :action => 'list'
36 +
37 + prepare_list_information
38 + render :action => 'list'
55 end
39 end
56
40
57 def get_source
41 def get_source
58 submission = Submission.find(params[:id])
42 submission = Submission.find(params[:id])
59 if submission.user_id == session[:user_id]
43 if submission.user_id == session[:user_id]
60 fname = submission.problem.name + '.' + submission.language.ext
44 fname = submission.problem.name + '.' + submission.language.ext
61 send_data(submission.source,
45 send_data(submission.source,
62 {:filename => fname,
46 {:filename => fname,
63 :type => 'text/plain'})
47 :type => 'text/plain'})
64 else
48 else
65 flash[:notice] = 'Error viewing source'
49 flash[:notice] = 'Error viewing source'
66 end
50 end
67 end
51 end
52 +
53 + protected
54 + def prepare_list_information
55 + @problems = Problem.find_available_problems
56 + @prob_submissions = Array.new
57 + @user = User.find(session[:user_id])
58 + @problems.each do |p|
59 + c, sub = Submission.find_by_user_and_problem(@user.id,p.id)
60 + @prob_submissions << [c,sub]
61 + end
62 + end
63 +
68 end
64 end
65 +
@@ -1,42 +1,51
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 + validates_presence_of :source
8 + validates_length_of :source, :maximum => 100_000, :allow_blank => true, :message => 'too long'
9 + validates_length_of :source, :minimum => 1, :allow_blank => true, :message => 'too short'
10 + validate :must_specify_language
11 + validate :must_have_valid_problem
12 +
7 def self.find_by_user_and_problem(user_id, problem_id)
13 def self.find_by_user_and_problem(user_id, problem_id)
8 subcount = count(:conditions => "user_id = #{user_id} AND problem_id = #{problem_id}")
14 subcount = count(:conditions => "user_id = #{user_id} AND problem_id = #{problem_id}")
9 if subcount != 0
15 if subcount != 0
10 last_sub = find(:first,
16 last_sub = find(:first,
11 :conditions => {:user_id => user_id,
17 :conditions => {:user_id => user_id,
12 :problem_id => problem_id},
18 :problem_id => problem_id},
13 :order => 'submitted_at DESC')
19 :order => 'submitted_at DESC')
14 else
20 else
15 last_sub = nil
21 last_sub = nil
16 end
22 end
17 return subcount, last_sub
23 return subcount, last_sub
18 end
24 end
19
25
20 def self.find_last_by_problem(problem_id)
26 def self.find_last_by_problem(problem_id)
21 # 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
22 Submission.find_by_sql("SELECT * FROM submissions " +
28 Submission.find_by_sql("SELECT * FROM submissions " +
23 "WHERE id = " +
29 "WHERE id = " +
24 "(SELECT MAX(id) FROM submissions AS subs " +
30 "(SELECT MAX(id) FROM submissions AS subs " +
25 "WHERE subs.user_id = submissions.user_id AND " +
31 "WHERE subs.user_id = submissions.user_id AND " +
26 "problem_id = " + problem_id.to_s + " " +
32 "problem_id = " + problem_id.to_s + " " +
27 "GROUP BY user_id)")
33 "GROUP BY user_id)")
28 end
34 end
29
35
30 def self.find_option_in_source(option, source)
36 def self.find_option_in_source(option, source)
37 + if source==nil
38 + return nil
39 + end
31 i = 0
40 i = 0
32 source.each_line do |s|
41 source.each_line do |s|
33 if s =~ option
42 if s =~ option
34 words = s.split
43 words = s.split
35 return words[1]
44 return words[1]
36 end
45 end
37 i = i + 1
46 i = i + 1
38 if i==10
47 if i==10
39 return nil
48 return nil
40 end
49 end
41 end
50 end
42 return nil
51 return nil
@@ -53,13 +62,34
53 end
62 end
54 end
63 end
55
64
56 def self.find_problem_in_source(source)
65 def self.find_problem_in_source(source)
57 prob_opt = find_option_in_source(/^TASK:/,source)
66 prob_opt = find_option_in_source(/^TASK:/,source)
58 if problem = Problem.find_by_name(prob_opt)
67 if problem = Problem.find_by_name(prob_opt)
59 return problem
68 return problem
60 else
69 else
61 return nil
70 return nil
62 end
71 end
63 end
72 end
64
73
74 + # validation codes
75 + def must_specify_language
76 + return if self.source==nil
77 + self.language = Submission.find_language_in_source(self.source)
78 + errors.add_to_base("must specify programming language") unless self.language!=nil
79 + end
80 +
81 + def must_have_valid_problem
82 + return if self.source==nil
83 + if self.problem_id!=-1
84 + problem = Problem.find(self.problem_id)
85 + else
86 + problem = Submission.find_problem_in_source(self.source)
87 + end
88 + if problem==nil
89 + errors.add_to_base("must specify problem")
90 + elsif !problem.available
91 + errors.add_to_base("must specify valid problem")
92 + end
93 + end
94 +
65 end
95 end
@@ -1,16 +1,19
1 <div class="title">Hello <%=h @user.full_name %></div>
1 <div class="title">Hello <%=h @user.full_name %></div>
2 Current time is <%= format_short_time(Time.new) %>.
2 Current time is <%= format_short_time(Time.new) %>.
3
3
4 <div class="submitbox">
4 <div class="submitbox">
5 +
6 + <%= error_messages_for 'submission' %>
7 +
5 <% form_tag({:action => 'submit'}, :multipart => true) do %>
8 <% form_tag({:action => 'submit'}, :multipart => true) do %>
6 Problem: <%= select 'submission', 'problem_id',
9 Problem: <%= select 'submission', 'problem_id',
7 [['Specified in header','-1']] +
10 [['Specified in header','-1']] +
8 @problems.collect {|p| [p.full_name, p.id]},
11 @problems.collect {|p| [p.full_name, p.id]},
9 :selected => '-1' %>
12 :selected => '-1' %>
10 File: <%= file_field_tag 'file' %>
13 File: <%= file_field_tag 'file' %>
11 <%= submit_tag 'Submit' %>
14 <%= submit_tag 'Submit' %>
12 <% end %>
15 <% end %>
13 </div>
16 </div>
14
17
15 <hr/>
18 <hr/>
16
19
You need to be logged in to leave comments. Login now