add used storage quota include to courses api

So that an api consumer doesn't have to make a call for each
individual course to find courses that are close to their
quota usage.

Test Plan:
 * do an include[]=storage_quota_used_mb for the courses api
 * it should return the property 'storage_quota_used_mb'

closes PLAT-812

Change-Id: Ia617ec825b685cbd682baffedee5afa7b19c5280
Reviewed-on: https://gerrit.instructure.com/46247
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
Product-Review: Jeremy Stanley <jeremy@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
QA-Review: August Thornton <august@instructure.com>
This commit is contained in:
Bracken Mosbacker 2014-12-23 09:48:52 -07:00
parent 368f60053c
commit 5d67b73629
6 changed files with 31 additions and 2 deletions

View File

@ -245,6 +245,9 @@ class AccountsController < ApplicationController
# @argument search_term [String]
# The partial course name, code, or full ID to match and return in the results list. Must be at least 3 characters.
#
# @argument include[] [String, "needs_grading_count"|"syllabus_body"|"total_scores"|"term"|"course_progress"|"sections"|"storage_quota_used_mb"]
# - All explanations can be seen in the {api:CoursesController#index Course API index documentation}
#
# @returns [Course]
def courses_api
return unless authorized_action(@account, @current_user, :read)
@ -302,10 +305,14 @@ class AccountsController < ApplicationController
end
end
includes = Set.new(Array(params[:include]))
# We only want to return the permissions for single courses and not lists of courses.
includes.delete 'permissions'
@courses = Api.paginate(@courses, self, api_v1_account_courses_url)
ActiveRecord::Associations::Preloader.new(@courses, [:account, :root_account])
render :json => @courses.map { |c| course_json(c, @current_user, session, [], nil) }
render :json => @courses.map { |c| course_json(c, @current_user, session, includes, nil) }
end
# Delegated to by the update action (when the request is an api_request?)

View File

@ -207,6 +207,10 @@ require 'set'
# "example": 5,
# "type": "integer"
# },
# "storage_quota_used_mb": {
# "example": 5,
# "type": "float"
# },
# "hide_final_grades": {
# "example": false,
# "type": "boolean"
@ -291,7 +295,7 @@ class CoursesController < ApplicationController
# 'StudentEnrollment', 'TeacherEnrollment', 'TaEnrollment', 'ObserverEnrollment',
# or 'DesignerEnrollment'.
#
# @argument include[] [String, "needs_grading_count"|"syllabus_body"|"total_scores"|"term"|"course_progress"|"sections"]
# @argument include[] [String, "needs_grading_count"|"syllabus_body"|"total_scores"|"term"|"course_progress"|"sections"|"storage_quota_used_mb"]
# - "needs_grading_count": Optional information to include with each Course.
# When needs_grading_count is given, and the current user has grading
# rights, the total number of submissions needing grading for all
@ -329,6 +333,7 @@ class CoursesController < ApplicationController
# Returns an array of hashes containing the section ID (id), section name
# (name), start and end dates (start_at, end_at), as well as the enrollment
# type (enrollment_role, e.g. 'StudentEnrollment').
# - "storage_quota_used_mb": The amount of storage space used by the files in this course
#
# @argument state[] [String, "unpublished"|"available"|"completed"|"deleted"]
# If set, only return courses that are in the given state(s).

View File

@ -970,6 +970,10 @@ class Course < ActiveRecord::Base
self.storage_quota = val.try(:to_i).try(:megabytes)
end
def storage_quota_used_mb
Attachment.get_quota(self)[:quota_used].to_f / 1.megabyte
end
def storage_quota
return read_attribute(:storage_quota) ||
(self.account.default_storage_quota rescue nil) ||

View File

@ -29,6 +29,7 @@ module Api::V1
def methods_to_send
methods = ['end_at', 'public_syllabus', 'storage_quota_mb']
methods << 'hide_final_grades' if @includes.include?(:hide_final_grades)
methods << 'storage_quota_used_mb' if @includes.include?(:storage_quota_used_mb)
methods
end

View File

@ -434,6 +434,13 @@ describe "Accounts API", type: :request do
end
end
it "should honor the includes[]" do
@c1 = course_model(:name => 'c1', :account => @a1, :root_account => @a1)
json = api_call(:get, "/api/v1/accounts/#{@a1.id}/courses?include[]=storage_quota_used_mb",
{ :controller => 'accounts', :action => 'courses_api', :account_id => @a1.to_param, :format => 'json', :include => ['storage_quota_used_mb'] }, {})
expect(json[0].has_key?("storage_quota_used_mb")).to be_truthy
end
describe "courses filtered by state[]" do
before :once do
@me = @user

View File

@ -58,6 +58,11 @@ describe Api::V1::Course do
expect(@test_api.course_json(@course1, @me, {}, ['needs_grading_count'], [teacher_enrollment]).has_key?("needs_grading_count")).to be_truthy
end
it 'should return storage_quota_used_mb if requested' do
pp @test_api.course_json(@course1, @me, {}, ['storage_quota_used_mb'], [teacher_enrollment])
expect(@test_api.course_json(@course1, @me, {}, ['storage_quota_used_mb'], [teacher_enrollment]).has_key?("storage_quota_used_mb")).to be_truthy
end
it 'should not honor needs_grading_count for designers' do
@designer_enrollment = @course1.enroll_designer(@me)
@designer_enrollment.accept!