Skip to content

Commit e65ecd0

Browse files
committed
Setup job for web export
closes OFFW-36 test plan: - you should be able to start a web zip export through the console - this should cause module progression for all viewable items - ensure epub exports still export Change-Id: I901d491325fba3955af4544d1c1a2b87270cffff Reviewed-on: https://gerrit.instructure.com/96752 Reviewed-by: Cameron Sutter <csutter@instructure.com> Tested-by: Jenkins QA-Review: Nathan Rogowski <nathan@instructure.com> Product-Review: Jon Willesen <jonw+gerrit@instructure.com>
1 parent 670740a commit e65ecd0

9 files changed

Lines changed: 112 additions & 27 deletions

File tree

app/controllers/courses_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class CoursesController < ApplicationController
300300
include CustomSidebarLinksHelper
301301
include SyllabusHelper
302302

303-
before_filter :require_user, :only => [:index, :activity_stream, :activity_stream_summary, :effective_due_dates]
303+
before_filter :require_user, :only => [:index, :activity_stream, :activity_stream_summary, :effective_due_dates, :offline_web_exports]
304304
before_filter :require_user_or_observer, :only=>[:user_index]
305305
before_filter :require_context, :only => [:roster, :locks, :create_file, :ping, :effective_due_dates, :offline_web_exports]
306306
skip_after_filter :update_enrollment_last_activity_at, only: [:enrollment_invitation, :activity_stream_summary]
@@ -2734,8 +2734,10 @@ def can_change_group_weighting_scheme?
27342734

27352735
def offline_web_exports
27362736
return render status: 404, template: 'shared/errors/404_message' unless @context.allow_web_export_download?
2737-
mvmp = MustViewModuleProgressor.new(@current_user, @context)
2738-
mvmp.make_progress
2737+
if authorized_action(WebZipExport.new(course: @context), @current_user, :create)
2738+
@service = EpubExports::CreateService.new(@context, @current_user, :web_zip_export)
2739+
@service.save
2740+
end
27392741
@page_title = t('Course Content Downloads')
27402742
render :text => 'Downloads'.html_safe, :layout => true
27412743
end

app/controllers/epub_exports_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ def index
146146
def create
147147
if authorized_action(EpubExport.new(course: @context), @current_user, :create)
148148
@course = Course.find(params[:course_id])
149-
@service = EpubExports::CreateService.new(@course, @current_user)
149+
@service = EpubExports::CreateService.new(@course, @current_user, :epub_export)
150150
status = @service.save ? 201 : 422
151151
respond_to do |format|
152152
format.json do
153-
@course.latest_epub_export = @service.epub_export
153+
@course.latest_epub_export = @service.offline_export
154154
render({
155155
status: status, json: course_epub_export_json(@course)
156156
})

app/models/course.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,9 @@ def inherited_assessment_question_banks(include_self = false)
219219
has_many :role_overrides, :as => :context
220220
has_many :content_migrations, :as => :context
221221
has_many :content_exports, :as => :context
222-
has_many :epub_exports, -> { order("created_at DESC") }
222+
has_many :epub_exports, -> { where("type IS NULL").order("created_at DESC") }
223223
attr_accessor :latest_epub_export
224+
has_many :web_zip_exports, -> { where(type: "WebZipExport") }
224225
has_many :alerts, -> { preload(:criteria) }, as: :context
225226
has_many :appointment_group_contexts, :as => :context
226227
has_many :appointment_groups, :through => :appointment_group_contexts

app/models/epub_export.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class EpubExport < ActiveRecord::Base
1414
has_one :job_progress, as: :context, class_name: 'Progress'
1515
validates :course_id, :workflow_state, presence: true
1616

17+
attr_accessible :course, :user, :content_export
18+
1719
PERCENTAGE_COMPLETE = {
1820
created: 0,
1921
exported: 80,

app/models/epub_exports/create_service.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,31 @@
1717
#
1818
module EpubExports
1919
class CreateService
20-
def initialize(course, user)
20+
def initialize(course, user, export_type)
2121
@course = course
2222
@user = user
23+
@export_type = export_type
2324
end
24-
attr_reader :course, :user
25+
attr_reader :course, :user, :export_type
2526

26-
def epub_export
27-
unless @_epub_export
28-
@_epub_export = course.epub_exports.visible_to(user).running.first
29-
@_epub_export ||= course.epub_exports.build({
27+
def offline_export
28+
unless @_offline_export
29+
@_offline_export = course.send(export_type.to_s.pluralize).visible_to(user).running.first
30+
@_offline_export ||= course.send(export_type.to_s.pluralize).build({
3031
user: user
3132
})
3233
end
33-
@_epub_export
34+
@_offline_export
3435
end
3536

3637
def already_running?
37-
!epub_export.new_record?
38+
!offline_export.new_record?
3839
end
3940

4041
def save
41-
if !already_running? && epub_export.save
42+
if !already_running? && offline_export.save
4243
# Queuing jobs always returns nil, yay
43-
epub_export.export
44+
offline_export.export
4445
true
4546
else
4647
false

app/models/web_zip_export.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class WebZipExport < EpubExport
2+
3+
def export
4+
MustViewModuleProgressor.new(user, course).make_progress
5+
super
6+
end
7+
8+
def generate
9+
job_progress.update_attribute(:completion, PERCENTAGE_COMPLETE[:generating])
10+
update_attribute(:workflow_state, 'generating')
11+
convert_to_offline_web_zip
12+
end
13+
handle_asynchronously :generate, priority: Delayed::LOW_PRIORITY, max_attempts: 1
14+
15+
def convert_to_offline_web_zip
16+
begin
17+
set_locale
18+
# TODO: connect to cameron's patchset
19+
# file_path = super
20+
file_path = "/Users/mysti/canvas/chaos.mov.zip"
21+
I18n.locale = :en
22+
rescue => e
23+
mark_as_failed
24+
raise e
25+
end
26+
27+
create_attachment_from_path!(file_path)
28+
mark_as_generated
29+
cleanup_file_path!(file_path)
30+
end
31+
handle_asynchronously :convert_to_offline_web_zip, priority: Delayed::LOW_PRIORITY, max_attempts: 1
32+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddTypeToEpubExports < ActiveRecord::Migration
2+
tag :predeploy
3+
4+
def change
5+
add_column :epub_exports, :type, :string, :limit => 255
6+
end
7+
end

spec/models/epub_exports/create_service_spec.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,34 @@
2626

2727
describe "#save" do
2828
let_once(:create_service) do
29-
EpubExports::CreateService.new(@course, @student)
29+
EpubExports::CreateService.new(@course, @student, :epub_export)
3030
end
3131

3232
it "should send save & export to epub_export" do
33-
expect(create_service.epub_export.new_record?).to be_truthy, 'precondition'
34-
create_service.epub_export.expects(:export).once.returns(nil)
33+
expect(create_service.offline_export.new_record?).to be_truthy, 'precondition'
34+
create_service.offline_export.expects(:export).once.returns(nil)
3535
expect(create_service.save).to be_truthy
36-
expect(create_service.epub_export.new_record?).to be_falsey
36+
expect(create_service.offline_export.new_record?).to be_falsey
3737
end
3838
end
3939

40-
describe "#epub_export" do
40+
describe "#offline_export" do
4141
context "when user has an active epub_export" do
4242
before(:once) do
4343
@epub_export = @course.epub_exports.create(user: @student)
4444
@epub_export.export_without_send_later
45-
@service = EpubExports::CreateService.new(@course, @student)
45+
@service = EpubExports::CreateService.new(@course, @student, :epub_export)
4646
end
4747

4848
it "should return said epub_export" do
49-
expect(@service.epub_export).to eq @epub_export
49+
expect(@service.offline_export).to eq @epub_export
5050
end
5151
end
5252

5353
context "when user has no active epub_exports" do
5454
it "should return a new epub_export instance" do
55-
service = EpubExports::CreateService.new(@course, @student)
56-
expect(service.epub_export).to be_new_record
55+
service = EpubExports::CreateService.new(@course, @student, :epub_export)
56+
expect(service.offline_export).to be_new_record
5757
end
5858
end
5959
end
@@ -62,7 +62,7 @@
6262
context "when user has an active epub_export" do
6363
before(:once) do
6464
@course.epub_exports.create(user: @student).export_without_send_later
65-
@service = EpubExports::CreateService.new(@course, @student)
65+
@service = EpubExports::CreateService.new(@course, @student, :epub_export)
6666
end
6767

6868
it "should return true" do
@@ -72,9 +72,21 @@
7272

7373
context "when user doesn't have an active epub_export" do
7474
it "should return true" do
75-
service = EpubExports::CreateService.new(@course, @student)
75+
service = EpubExports::CreateService.new(@course, @student, :epub_export)
7676
expect(service.already_running?).to be_falsey
7777
end
7878
end
79+
80+
context "when user has an active epub_export and starts a web_zip_export" do
81+
before(:once) do
82+
@epub_export = @course.epub_exports.create(user: @student)
83+
@epub_export.export_without_send_later
84+
@service = EpubExports::CreateService.new(@course, @student, :web_zip_export)
85+
end
86+
87+
it "should return false" do
88+
expect(@service.already_running?).to be_falsey
89+
end
90+
end
7991
end
8092
end

spec/models/web_zip_export_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require 'spec_helper'
2+
3+
describe WebZipExport do
4+
before :once do
5+
course_with_teacher(active_all: true)
6+
student_in_course(active_all: true)
7+
end
8+
9+
describe "#generate" do
10+
let_once(:web_zip_export) do
11+
@course.web_zip_exports.create({
12+
user: @student
13+
}).tap do |web_zip_export|
14+
web_zip_export.update_attribute(:workflow_state, 'exported')
15+
end
16+
end
17+
18+
it "should update job_progress completion" do
19+
web_zip_export.generate_without_send_later
20+
expect(web_zip_export.job_progress.completion).to eq WebZipExport::PERCENTAGE_COMPLETE[:generating]
21+
end
22+
23+
it "should set state to generating" do
24+
web_zip_export.generate_without_send_later
25+
expect(web_zip_export.generating?).to be_truthy
26+
end
27+
end
28+
end

0 commit comments

Comments
 (0)