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

@@ -1,68 +1,65
1 1 class MainController < ApplicationController
2 2
3 3 before_filter :authenticate, :except => [:index, :login]
4 4
5 5 verify :method => :post, :only => [:submit],
6 6 :redirect_to => { :action => :index }
7 7
8 8
9 9 def index
10 10 redirect_to :action => 'login'
11 11 end
12 12
13 13 def login
14 14 reset_session
15 15 render :action => 'login', :layout => 'empty'
16 16 end
17 17
18 18 def list
19 - @problems = Problem.find_available_problems
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
19 + prepare_list_information
26 20 end
27 21
28 22 def submit
29 - submission = Submission.new(params[:submission])
30 - submission.user_id = session[:user_id]
31 - submission.language_id = 0
32 - source = params['file'].read
33 - if source.length > 100_000
34 - flash[:notice] = 'Error: file too long'
35 - elsif (lang = Submission.find_language_in_source(source))==nil
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
23 + @submission = Submission.new(params[:submission])
24 + @submission.user_id = session[:user_id]
25 + @submission.language_id = 0
26 + @submission.source = params['file'].read if params['file']!=''
27 + @submission.submitted_at = Time.new
28 + if @submission.valid?
29 + if @submission.save == false
49 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 33 flash[:notice] = 'Error adding your submission to task queue'
52 34 end
53 35 end
54 - redirect_to :action => 'list'
36 +
37 + prepare_list_information
38 + render :action => 'list'
55 39 end
56 40
57 41 def get_source
58 42 submission = Submission.find(params[:id])
59 43 if submission.user_id == session[:user_id]
60 44 fname = submission.problem.name + '.' + submission.language.ext
61 45 send_data(submission.source,
62 46 {:filename => fname,
63 47 :type => 'text/plain'})
64 48 else
65 49 flash[:notice] = 'Error viewing source'
66 50 end
67 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 64 end
65 +
@@ -1,65 +1,95
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 + 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 13 def self.find_by_user_and_problem(user_id, problem_id)
8 14 subcount = count(:conditions => "user_id = #{user_id} AND problem_id = #{problem_id}")
9 15 if subcount != 0
10 16 last_sub = find(:first,
11 17 :conditions => {:user_id => user_id,
12 18 :problem_id => problem_id},
13 19 :order => 'submitted_at DESC')
14 20 else
15 21 last_sub = nil
16 22 end
17 23 return subcount, last_sub
18 24 end
19 25
20 26 def self.find_last_by_problem(problem_id)
21 27 # need to put in SQL command, maybe there's a better way
22 28 Submission.find_by_sql("SELECT * FROM submissions " +
23 29 "WHERE id = " +
24 30 "(SELECT MAX(id) FROM submissions AS subs " +
25 31 "WHERE subs.user_id = submissions.user_id AND " +
26 32 "problem_id = " + problem_id.to_s + " " +
27 33 "GROUP BY user_id)")
28 34 end
29 35
30 36 def self.find_option_in_source(option, source)
37 + if source==nil
38 + return nil
39 + end
31 40 i = 0
32 41 source.each_line do |s|
33 42 if s =~ option
34 43 words = s.split
35 44 return words[1]
36 45 end
37 46 i = i + 1
38 47 if i==10
39 48 return nil
40 49 end
41 50 end
42 51 return nil
43 52 end
44 53
45 54 def self.find_language_in_source(source)
46 55 langopt = find_option_in_source(/^LANG:/,source)
47 56 if language = Language.find_by_name(langopt)
48 57 return language
49 58 elsif language = Language.find_by_pretty_name(langopt)
50 59 return language
51 60 else
52 61 return nil
53 62 end
54 63 end
55 64
56 65 def self.find_problem_in_source(source)
57 66 prob_opt = find_option_in_source(/^TASK:/,source)
58 67 if problem = Problem.find_by_name(prob_opt)
59 68 return problem
60 69 else
61 70 return nil
62 71 end
63 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 95 end
@@ -1,28 +1,31
1 1 <div class="title">Hello <%=h @user.full_name %></div>
2 2 Current time is <%= format_short_time(Time.new) %>.
3 3
4 4 <div class="submitbox">
5 +
6 + <%= error_messages_for 'submission' %>
7 +
5 8 <% form_tag({:action => 'submit'}, :multipart => true) do %>
6 9 Problem: <%= select 'submission', 'problem_id',
7 10 [['Specified in header','-1']] +
8 11 @problems.collect {|p| [p.full_name, p.id]},
9 12 :selected => '-1' %>
10 13 File: <%= file_field_tag 'file' %>
11 14 <%= submit_tag 'Submit' %>
12 15 <% end %>
13 16 </div>
14 17
15 18 <hr/>
16 19
17 20 <p style="color: red"><%= flash[:notice] %></p>
18 21
19 22 <div class="problist">
20 23 <% i = 0 %>
21 24 <% @problems.each do |p| %>
22 25 <div class="problist-each">
23 26 <div class="probname">
24 27 <%= "#{i+1}: #{p.full_name} (#{p.name})" %>
25 28 <%= link_to '[task description]', p.url, :popup => true if (p.url!=nil) and (p.url!='') %>
26 29 </div>
27 30 <div class="subinfo">
28 31 <%= format_submission(@prob_submissions[i][1],
You need to be logged in to leave comments. Login now