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

@@ -16,42 +16,26
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
@@ -65,4 +49,17
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 +
@@ -4,6 +4,12
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
@@ -28,6 +34,9
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
@@ -62,4 +71,25
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
@@ -2,6 +2,9
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']] +
You need to be logged in to leave comments. Login now