Description:
[web] improving readability of test_interface, re: ticket #10
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@141 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r64:f6cdb5f921b2 - - 5 files changed: 6 inserted, 5 deleted
@@ -1,52 +1,53 | |||||
|
1 | class TestController < ApplicationController |
|
1 | class TestController < ApplicationController |
|
2 |
|
2 | ||
|
3 | before_filter :authenticate |
|
3 | before_filter :authenticate |
|
4 |
|
4 | ||
|
5 | verify :method => :post, :only => [:test_submit], |
|
5 | verify :method => :post, :only => [:test_submit], |
|
6 | :redirect_to => { :action => :index } |
|
6 | :redirect_to => { :action => :index } |
|
7 |
|
7 | ||
|
8 | def index |
|
8 | def index |
|
9 | @user = User.find(session[:user_id]) |
|
9 | @user = User.find(session[:user_id]) |
|
10 | prepare_index_information |
|
10 | prepare_index_information |
|
|
11 | + @test_requests = @user.test_requests | ||
|
11 | end |
|
12 | end |
|
12 |
|
13 | ||
|
13 | def submit |
|
14 | def submit |
|
14 | @user = User.find(session[:user_id]) |
|
15 | @user = User.find(session[:user_id]) |
|
15 | test_request = TestRequest.new_from_form_params(@user,params[:test_request]) |
|
16 | test_request = TestRequest.new_from_form_params(@user,params[:test_request]) |
|
16 | if test_request.save |
|
17 | if test_request.save |
|
17 | redirect_to :action => 'index' |
|
18 | redirect_to :action => 'index' |
|
18 | else |
|
19 | else |
|
19 | flash[:notice] = 'Error saving your test submission' |
|
20 | flash[:notice] = 'Error saving your test submission' |
|
20 | redirect_to :action => 'index' |
|
21 | redirect_to :action => 'index' |
|
21 | end |
|
22 | end |
|
22 | end |
|
23 | end |
|
23 |
|
24 | ||
|
24 | def read |
|
25 | def read |
|
25 | user = User.find(session[:user_id]) |
|
26 | user = User.find(session[:user_id]) |
|
26 | test_request = TestRequest.find(params[:id]) |
|
27 | test_request = TestRequest.find(params[:id]) |
|
27 | if test_request.user_id != user.id |
|
28 | if test_request.user_id != user.id |
|
28 | flash[:notice] = 'Invalid output' |
|
29 | flash[:notice] = 'Invalid output' |
|
29 | redirect_to :action => 'index' |
|
30 | redirect_to :action => 'index' |
|
30 | return |
|
31 | return |
|
31 | end |
|
32 | end |
|
32 | if test_request.output_file_name!=nil |
|
33 | if test_request.output_file_name!=nil |
|
33 | data = File.open(test_request.output_file_name).read(2048) |
|
34 | data = File.open(test_request.output_file_name).read(2048) |
|
34 | if data==nil |
|
35 | if data==nil |
|
35 | data="" |
|
36 | data="" |
|
36 | end |
|
37 | end |
|
37 | send_data(data, |
|
38 | send_data(data, |
|
38 | {:filename => 'output.txt', |
|
39 | {:filename => 'output.txt', |
|
39 | :type => 'text/plain'}) |
|
40 | :type => 'text/plain'}) |
|
40 | return |
|
41 | return |
|
41 | end |
|
42 | end |
|
42 | redirect_to :action => 'index' |
|
43 | redirect_to :action => 'index' |
|
43 | end |
|
44 | end |
|
44 |
|
45 | ||
|
45 | protected |
|
46 | protected |
|
46 |
|
47 | ||
|
47 | def prepare_index_information |
|
48 | def prepare_index_information |
|
48 | @submissions = Submission.find_last_for_all_available_problems(@user.id) |
|
49 | @submissions = Submission.find_last_for_all_available_problems(@user.id) |
|
49 | @problems = @submissions.collect { |submission| submission.problem } |
|
50 | @problems = @submissions.collect { |submission| submission.problem } |
|
50 | end |
|
51 | end |
|
51 |
|
52 | ||
|
52 | end |
|
53 | end |
@@ -1,76 +1,76 | |||||
|
1 | require 'digest/sha1' |
|
1 | require 'digest/sha1' |
|
2 |
|
2 | ||
|
3 | class User < ActiveRecord::Base |
|
3 | class User < ActiveRecord::Base |
|
4 |
|
4 | ||
|
5 | has_and_belongs_to_many :roles |
|
5 | has_and_belongs_to_many :roles |
|
6 |
|
6 | ||
|
7 |
- has_many :test_requests, :order => " |
|
7 | + has_many :test_requests, :order => "submitted_at DESC" |
|
8 |
|
8 | ||
|
9 | validates_presence_of :login |
|
9 | validates_presence_of :login |
|
10 | validates_presence_of :full_name |
|
10 | validates_presence_of :full_name |
|
11 | validates_length_of :full_name, :minimum => 1 |
|
11 | validates_length_of :full_name, :minimum => 1 |
|
12 |
|
12 | ||
|
13 | validates_presence_of :password, :if => :password_required? |
|
13 | validates_presence_of :password, :if => :password_required? |
|
14 | validates_length_of :password, :within => 4..20, :if => :password_required? |
|
14 | validates_length_of :password, :within => 4..20, :if => :password_required? |
|
15 | validates_confirmation_of :password, :if => :password_required? |
|
15 | validates_confirmation_of :password, :if => :password_required? |
|
16 |
|
16 | ||
|
17 | attr_accessor :password |
|
17 | attr_accessor :password |
|
18 |
|
18 | ||
|
19 | before_save :encrypt_new_password |
|
19 | before_save :encrypt_new_password |
|
20 |
|
20 | ||
|
21 | def self.authenticate(login, password) |
|
21 | def self.authenticate(login, password) |
|
22 | user = find_by_login(login) |
|
22 | user = find_by_login(login) |
|
23 | return user if user && user.authenticated?(password) |
|
23 | return user if user && user.authenticated?(password) |
|
24 | end |
|
24 | end |
|
25 |
|
25 | ||
|
26 | def authenticated?(password) |
|
26 | def authenticated?(password) |
|
27 | hashed_password == User.encrypt(password,self.salt) |
|
27 | hashed_password == User.encrypt(password,self.salt) |
|
28 | end |
|
28 | end |
|
29 |
|
29 | ||
|
30 | def admin? |
|
30 | def admin? |
|
31 | self.roles.detect {|r| r.name == 'admin' } |
|
31 | self.roles.detect {|r| r.name == 'admin' } |
|
32 | end |
|
32 | end |
|
33 |
|
33 | ||
|
34 | def email_for_editing |
|
34 | def email_for_editing |
|
35 | if self.email==nil |
|
35 | if self.email==nil |
|
36 | "(unknown)" |
|
36 | "(unknown)" |
|
37 | elsif self.email=='' |
|
37 | elsif self.email=='' |
|
38 | "(blank)" |
|
38 | "(blank)" |
|
39 | else |
|
39 | else |
|
40 | self.email |
|
40 | self.email |
|
41 | end |
|
41 | end |
|
42 | end |
|
42 | end |
|
43 |
|
43 | ||
|
44 | def email_for_editing=(e) |
|
44 | def email_for_editing=(e) |
|
45 | self.email=e |
|
45 | self.email=e |
|
46 | end |
|
46 | end |
|
47 |
|
47 | ||
|
48 | def alias_for_editing |
|
48 | def alias_for_editing |
|
49 | if self.alias==nil |
|
49 | if self.alias==nil |
|
50 | "(unknown)" |
|
50 | "(unknown)" |
|
51 | elsif self.alias=='' |
|
51 | elsif self.alias=='' |
|
52 | "(blank)" |
|
52 | "(blank)" |
|
53 | else |
|
53 | else |
|
54 | self.alias |
|
54 | self.alias |
|
55 | end |
|
55 | end |
|
56 | end |
|
56 | end |
|
57 |
|
57 | ||
|
58 | def alias_for_editing=(e) |
|
58 | def alias_for_editing=(e) |
|
59 | self.alias=e |
|
59 | self.alias=e |
|
60 | end |
|
60 | end |
|
61 |
|
61 | ||
|
62 | protected |
|
62 | protected |
|
63 | def encrypt_new_password |
|
63 | def encrypt_new_password |
|
64 | return if password.blank? |
|
64 | return if password.blank? |
|
65 | self.salt = (10+rand(90)).to_s |
|
65 | self.salt = (10+rand(90)).to_s |
|
66 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
66 | self.hashed_password = User.encrypt(self.password,self.salt) |
|
67 | end |
|
67 | end |
|
68 |
|
68 | ||
|
69 | def password_required? |
|
69 | def password_required? |
|
70 | self.hashed_password.blank? || !self.password.blank? |
|
70 | self.hashed_password.blank? || !self.password.blank? |
|
71 | end |
|
71 | end |
|
72 |
|
72 | ||
|
73 | def self.encrypt(string,salt) |
|
73 | def self.encrypt(string,salt) |
|
74 | Digest::SHA1.hexdigest(salt + string) |
|
74 | Digest::SHA1.hexdigest(salt + string) |
|
75 | end |
|
75 | end |
|
76 | end |
|
76 | end |
@@ -1,11 +1,11 | |||||
|
1 | %tr{:class => (test_request_counter%2==0) ? "info-even" : "info-odd"} |
|
1 | %tr{:class => (test_request_counter%2==0) ? "info-even" : "info-odd"} |
|
2 | %td= test_request_counter +1 |
|
2 | %td= test_request_counter +1 |
|
3 | %td= test_request.problem.full_name |
|
3 | %td= test_request.problem.full_name |
|
4 | %td= test_request.submission.number |
|
4 | %td= test_request.submission.number |
|
5 | %td= test_request.status_str |
|
5 | %td= test_request.status_str |
|
6 | - %td= test_request.running_stat or '' |
|
6 | + %td= simple_format((test_request.running_stat or '')) |
|
7 | %td |
|
7 | %td |
|
8 | - if test_request.output_file_name!=nil |
|
8 | - if test_request.output_file_name!=nil |
|
9 | = link_to '[output]', :action => 'read', :id => test_request.id |
|
9 | = link_to '[output]', :action => 'read', :id => test_request.id |
|
10 | %td= test_request.grader_comment or '' |
|
10 | %td= test_request.grader_comment or '' |
|
11 | - %td= test_request.compiler_message or '' |
|
11 | + %td= simple_format((test_request.compiler_message or '')) |
@@ -1,73 +1,73 | |||||
|
1 | <h2>Test Interface</h2> |
|
1 | <h2>Test Interface</h2> |
|
2 |
|
2 | ||
|
3 | <% if @problems.length==0 %> |
|
3 | <% if @problems.length==0 %> |
|
4 | There is no submission |
|
4 | There is no submission |
|
5 | <% else %> |
|
5 | <% else %> |
|
6 |
|
6 | ||
|
7 | <script type="text/javascript"> |
|
7 | <script type="text/javascript"> |
|
8 | var submissionCount = { |
|
8 | var submissionCount = { |
|
9 | <% @submissions.each do |submission| %> |
|
9 | <% @submissions.each do |submission| %> |
|
10 | <%= submission.problem_id %> : <%= submission.number %>, |
|
10 | <%= submission.problem_id %> : <%= submission.number %>, |
|
11 | <% end %> |
|
11 | <% end %> |
|
12 | }; |
|
12 | }; |
|
13 | function updateSubmissionList() { |
|
13 | function updateSubmissionList() { |
|
14 | currentProb = document.getElementById("test_request_problem_id").value; |
|
14 | currentProb = document.getElementById("test_request_problem_id").value; |
|
15 | count = submissionCount[currentProb]; |
|
15 | count = submissionCount[currentProb]; |
|
16 | submissionSelect = document.getElementById("test_request_submission_number"); |
|
16 | submissionSelect = document.getElementById("test_request_submission_number"); |
|
17 | submissionSelect.options.length = 0; |
|
17 | submissionSelect.options.length = 0; |
|
18 | for(i=0; i<count; i++) { |
|
18 | for(i=0; i<count; i++) { |
|
19 | submissionSelect.options[i] = new Option(""+(i+1),""+(i+1),false,false); |
|
19 | submissionSelect.options[i] = new Option(""+(i+1),""+(i+1),false,false); |
|
20 | } |
|
20 | } |
|
21 | } |
|
21 | } |
|
22 | </script> |
|
22 | </script> |
|
23 |
|
23 | ||
|
24 | <% form_for :test_request, nil, |
|
24 | <% form_for :test_request, nil, |
|
25 | :url => { :action => 'submit'}, |
|
25 | :url => { :action => 'submit'}, |
|
26 | :html => { :multipart => true } do |f| %> |
|
26 | :html => { :multipart => true } do |f| %> |
|
27 | <table> |
|
27 | <table> |
|
28 | <tr> |
|
28 | <tr> |
|
29 | <td>Task:</td> |
|
29 | <td>Task:</td> |
|
30 | <td> |
|
30 | <td> |
|
31 | <%= select(:test_request, |
|
31 | <%= select(:test_request, |
|
32 | :problem_id, |
|
32 | :problem_id, |
|
33 | @problems.collect {|p| [p.name, p.id]}, {}, |
|
33 | @problems.collect {|p| [p.name, p.id]}, {}, |
|
34 | { :onclick => "updateSubmissionList();" }) %> |
|
34 | { :onclick => "updateSubmissionList();" }) %> |
|
35 | </td> |
|
35 | </td> |
|
36 | </tr> |
|
36 | </tr> |
|
37 | <tr> |
|
37 | <tr> |
|
38 | <td>Submission:</td> |
|
38 | <td>Submission:</td> |
|
39 | <td> |
|
39 | <td> |
|
40 | <%= select(:test_request, |
|
40 | <%= select(:test_request, |
|
41 | :submission_number, |
|
41 | :submission_number, |
|
42 | ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %> |
|
42 | ((1..@submissions[0].number).collect {|n| [n,n]}).reverse) %> |
|
43 | </td> |
|
43 | </td> |
|
44 | </tr> |
|
44 | </tr> |
|
45 | <tr> |
|
45 | <tr> |
|
46 | <td>Input data:</td> |
|
46 | <td>Input data:</td> |
|
47 | <td><%= f.file_field :input_file %></td> |
|
47 | <td><%= f.file_field :input_file %></td> |
|
48 | <tr> |
|
48 | <tr> |
|
49 | <td colspan="2"> |
|
49 | <td colspan="2"> |
|
50 | <%= submit_tag 'submit' %> |
|
50 | <%= submit_tag 'submit' %> |
|
51 | </td> |
|
51 | </td> |
|
52 | </tr> |
|
52 | </tr> |
|
53 | </table> |
|
53 | </table> |
|
54 | <% end %> |
|
54 | <% end %> |
|
55 |
|
55 | ||
|
56 | <h3>Previous requests</h3> |
|
56 | <h3>Previous requests</h3> |
|
57 |
|
57 | ||
|
58 | <table class="info"> |
|
58 | <table class="info"> |
|
59 | <tr class="info-head"> |
|
59 | <tr class="info-head"> |
|
60 | <th></td> |
|
60 | <th></td> |
|
61 | <th>problem</th> |
|
61 | <th>problem</th> |
|
62 | <th>#</th> |
|
62 | <th>#</th> |
|
63 | <th>status</th> |
|
63 | <th>status</th> |
|
64 | <th>running stat</th> |
|
64 | <th>running stat</th> |
|
65 | <th>output (first 2kb)</th> |
|
65 | <th>output (first 2kb)</th> |
|
66 | <th>grading comment</th> |
|
66 | <th>grading comment</th> |
|
67 | <th>compiler message</th> |
|
67 | <th>compiler message</th> |
|
68 | </tr> |
|
68 | </tr> |
|
69 |
- <%= render :partial => 'test_request', :collection => @ |
|
69 | + <%= render :partial => 'test_request', :collection => @test_requests %> |
|
70 | </table> |
|
70 | </table> |
|
71 |
|
71 | ||
|
72 | <% end %> |
|
72 | <% end %> |
|
73 |
|
73 |
@@ -1,37 +1,37 | |||||
|
1 |
|
1 | ||
|
2 | require File.dirname(__FILE__) + '/../spec_helper' |
|
2 | require File.dirname(__FILE__) + '/../spec_helper' |
|
3 |
|
3 | ||
|
4 | - describe Submission do |
|
4 | + describe Submission, "when verifying user submission" do |
|
5 |
|
5 | ||
|
6 | before(:each) do |
|
6 | before(:each) do |
|
7 | @submission = Submission.new |
|
7 | @submission = Submission.new |
|
8 | @submission.source = <<SOURCE |
|
8 | @submission.source = <<SOURCE |
|
9 | /* |
|
9 | /* |
|
10 | LANG: C++ |
|
10 | LANG: C++ |
|
11 | TASK: testproblem |
|
11 | TASK: testproblem |
|
12 | */ |
|
12 | */ |
|
13 | SOURCE |
|
13 | SOURCE |
|
14 | end |
|
14 | end |
|
15 |
|
15 | ||
|
16 | it "should find language in source" do |
|
16 | it "should find language in source" do |
|
17 | langcpp = stub(Language, :name => 'cpp', :ext => 'cpp') |
|
17 | langcpp = stub(Language, :name => 'cpp', :ext => 'cpp') |
|
18 | Language.should_receive(:find_by_name).with('C++').and_return(langcpp) |
|
18 | Language.should_receive(:find_by_name).with('C++').and_return(langcpp) |
|
19 | Submission.find_language_in_source(@submission.source).should == langcpp |
|
19 | Submission.find_language_in_source(@submission.source).should == langcpp |
|
20 | end |
|
20 | end |
|
21 |
|
21 | ||
|
22 | it "should find problem in source, when there is any" do |
|
22 | it "should find problem in source, when there is any" do |
|
23 | problem = stub(Problem, :name => 'testproblem') |
|
23 | problem = stub(Problem, :name => 'testproblem') |
|
24 | Problem.should_receive(:find_by_name).with('testproblem').and_return(problem) |
|
24 | Problem.should_receive(:find_by_name).with('testproblem').and_return(problem) |
|
25 | Submission.find_problem_in_source(@submission.source).should == problem |
|
25 | Submission.find_problem_in_source(@submission.source).should == problem |
|
26 | end |
|
26 | end |
|
27 |
|
27 | ||
|
28 | it "should return nil when it cannot find problem in source" do |
|
28 | it "should return nil when it cannot find problem in source" do |
|
29 | Submission.find_problem_in_source(<<SOURCE |
|
29 | Submission.find_problem_in_source(<<SOURCE |
|
30 | /* |
|
30 | /* |
|
31 | LANG: C |
|
31 | LANG: C |
|
32 | */ |
|
32 | */ |
|
33 | SOURCE |
|
33 | SOURCE |
|
34 | ).should == nil |
|
34 | ).should == nil |
|
35 | end |
|
35 | end |
|
36 |
|
36 | ||
|
37 | end |
|
37 | end |
You need to be logged in to leave comments.
Login now