Skip to content

Instantly share code, notes, and snippets.

@benjaminvialle
Created November 11, 2012 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminvialle/4055194 to your computer and use it in GitHub Desktop.
Save benjaminvialle/4055194 to your computer and use it in GitHub Desktop.
Diff from RB from Jordan Saleh
diff --git a/app/controllers/api/main_api_controller.rb b/app/controllers/api/main_api_controller.rb
--- a/app/controllers/api/main_api_controller.rb
+++ b/app/controllers/api/main_api_controller.rb
@@ -65,12 +65,6 @@ module Api
return
end
end
- # Student's aren't allowed yet
- if @current_user.student?
- # API is available for TAs and Admins only
- render 'shared/http_status', :locals => { :code => "403", :message => HttpStatusHelper::ERROR_CODE["message"]["403"] }, :status => 403
- return
- end
end
#=== Description
@@ -95,4 +89,4 @@ module Api
end
end
-end # end Api module
\ No newline at end of file
+end # end Api module
diff --git a/app/controllers/api/test_results_controller.rb b/app/controllers/api/test_results_controller.rb
--- a/app/controllers/api/test_results_controller.rb
+++ b/app/controllers/api/test_results_controller.rb
@@ -28,13 +28,27 @@ module Api
render 'shared/http_status', :locals => { :code => "404", :message => "Submission was not found" }, :status => 404
return
end
+
+ # retrieve api key to get current user id
+ auth_token = parse_auth_token(request.headers["HTTP_AUTHORIZATION"])
+ # check that it properly got the api key
+ if auth_token.nil?
+ render 'shared/http_status', :locals => { :code => "403", :message => HttpStatusHelper::ERROR_CODE["message"]["403"] }, :status => 403
+ return
+ end
+ # Find the user from the api key
+ @current_user = User.find_by_api_key(auth_token)
+
# Request seems good. Check if filename already exists.
# If it does, update it instead of creating a new one.
- new_test_result = submission.test_results.find_by_filename(params[:filename])
+ new_test_result = submission.test_results.find_by_filename_and_user_id(params[:filename], @current_user.id)
+
if new_test_result.nil?
if TestResult.create(:filename => params[:filename],
:file_content => params[:file_content],
- :submission_id => submission.id)
+ :submission_id => submission.id,
+ :status => 'success',
+ :user_id => @current_user.id)
# All good, so return a success response
render 'shared/http_status', :locals => { :code => "200", :message => "Success" }, :status => 200
return
diff --git a/app/controllers/assignments_controller.rb b/app/controllers/assignments_controller.rb
--- a/app/controllers/assignments_controller.rb
+++ b/app/controllers/assignments_controller.rb
@@ -37,10 +37,8 @@ class AssignmentsController < ApplicationController
@assignment = Assignment.find(params[:aid])
@test_result = TestResult.find(params[:test_result_id])
- # Students can use this action only, when marks have been released
if current_user.student? &&
- (@test_result.submission.grouping.membership_status(current_user).nil? ||
- @test_result.submission.result.released_to_students == false)
+ @test_result.submission.grouping.membership_status(current_user).nil?
render :partial => 'shared/handle_error',
:locals => {:error => I18n.t('test_result.error.no_access', :test_result_id => @test_result.id)}
return
@@ -101,7 +99,7 @@ class AssignmentsController < ApplicationController
@revision_number = @revision.revision_number
# For running tests
- if params[:collect]
+ if params[:collect] && @grouping.submissions.empty?
@result = manually_collect_and_prepare_test(@grouping, @revision.revision_number)
else
@result = automatically_collect_and_prepare_test(@grouping, @revision.revision_number)
diff --git a/app/controllers/automated_tests_controller.rb b/app/controllers/automated_tests_controller.rb
--- a/app/controllers/automated_tests_controller.rb
+++ b/app/controllers/automated_tests_controller.rb
@@ -13,6 +13,7 @@ class AutomatedTestsController < ApplicationController
@group = @grouping.group
@test_result_files = @submission.test_results
if can_run_test?
+ @current_user.set_student_api_key
export_repository(@group, File.join(MarkusConfigurator.markus_config_automated_tests_repository, @group.repo_name))
copy_ant_files(@assignment, File.join(MarkusConfigurator.markus_config_automated_tests_repository, @group.repo_name))
export_configuration_files(@assignment, @group, File.join(MarkusConfigurator.markus_config_automated_tests_repository, @group.repo_name))
diff --git a/app/helpers/automated_tests_helper.rb b/app/helpers/automated_tests_helper.rb
--- a/app/helpers/automated_tests_helper.rb
+++ b/app/helpers/automated_tests_helper.rb
@@ -344,6 +344,7 @@ module AutomatedTestsHelper
:submission_id => result.submission.id,
:status => status,
:user_id => @current_user.id)
+ @current_user.set_student_api_key
end
# Send output to parser(s) if any
diff --git a/app/models/user.rb b/app/models/user.rb
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -224,16 +224,33 @@ class User < ActiveRecord::Base
# expire every X days/hours/weeks. When it does, a new
# token should be automatically generated.
def set_api_key
- if self.api_key.nil?
+ if self.student?
+ self.api_key = nil
+ return self.save
+ else
+ if self.api_key.nil?
+ key = generate_api_key
+ md5 = Digest::MD5.new
+ md5.update(key)
+ # base64 encode md5 hash
+ self.api_key = Base64.encode64(md5.to_s).strip
+ return self.save
+ else
+ return true
+ end
+ end
+ end
+
+ def set_student_api_key
+ if self.student?
key = generate_api_key
md5 = Digest::MD5.new
md5.update(key)
# base64 encode md5 hash
self.api_key = Base64.encode64(md5.to_s).strip
return self.save
- else
- return true
end
+ return true
end
# Resets the api key. Usually triggered, if the
diff --git a/app/views/assignments/student_interface.html.erb b/app/views/assignments/student_interface.html.erb
--- a/app/views/assignments/student_interface.html.erb
+++ b/app/views/assignments/student_interface.html.erb
@@ -333,7 +333,7 @@
<br />
<% else %>
<% if @result.nil? && @test_result_files.nil? %>
- <%= I18n.t("automated_tests.need_one_file") %>
+ <%= raw(I18n.t("automated_tests.need_one_file")) %>
<br />
<% else %>
<%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment