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

@@ -13,48 +13,32
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
@@ -62,7 +46,20
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]
68 end
61 end
62 + end
63 +
64 + end
65 +
@@ -1,12 +1,18
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},
@@ -25,12 +31,15
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
@@ -59,7 +68,28
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
65 end
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 +
95 + end
@@ -1,10 +1,13
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' %>
You need to be logged in to leave comments. Login now