write reports to file

fixes PS-120

test plan

*reports should all still generate

Change-Id: I0a0dc8fdf298a6bd803bb52a4562a548ef727c6b
Reviewed-on: https://gerrit.instructure.com/17242
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: Tyler Belnap <tyler@instructure.com>
This commit is contained in:
Nathan Mills 2013-01-29 10:59:55 -07:00
parent e48636b8bf
commit 2e0613d34b
3 changed files with 36 additions and 16 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright (C) 2011 - 2012 Instructure, Inc.
# Copyright (C) 2011 - 2013 Instructure, Inc.
#
# This file is part of Canvas.
#
@ -52,6 +52,17 @@ module Canvas::AccountReports
end
end
def self.generate_file_name(account_report, ext)
"#{account_report.report_type}_#{Time.now.strftime('%d_%b_%Y')}_#{account_report.id}_.#{ext}"
end
def self.generate_file(account_report)
temp = Tempfile.open(generate_file_name(account_report, "csv"))
filepath = temp.path
temp.close
filepath
end
def self.message_recipient(account_report, message, csv=nil)
user = account_report.user
account = account_report.account
@ -59,7 +70,7 @@ module Canvas::AccountReports
notification = Notification.by_name("Report Generation Failed") if !csv
attachment = nil
if csv.is_a? Hash
filename = "#{account_report.report_type}_#{Time.now.strftime('%d_%b_%Y')}_#{account_report.id}_.zip"
filename = generate_file_name(account_report, "zip")
temp = Tempfile.open(filename)
filepath = temp.path
temp.close
@ -75,12 +86,22 @@ module Canvas::AccountReports
elsif csv
require 'action_controller'
require 'action_controller/test_process.rb'
filename = "#{account_report.report_type}_#{Time.now.strftime('%d_%b_%Y')}_#{account_report.id}_.csv"
f = Tempfile.open(filename)
f << csv
f.close
filepath = f.path
filetype = 'text/csv'
ext = csv !~ /\n/ && File.extname(csv)
case ext
when ".csv"
filename = File.basename(csv);
filepath = csv
filetype = 'text/csv'
when ".zip"
filetype = 'application/zip'
else
filename = generate_file_name(account_report, "csv")
f = Tempfile.open(filename)
f << csv
f.close
filepath = f.path
filetype = 'text/csv'
end
end
if filename
attachment = account.attachments.create!(

View File

@ -121,7 +121,8 @@ module Canvas::AccountReports
'attempt', 'outcome score', 'course name', 'course id', 'course sis id', 'assignment url']
# Generate the CSV report
result = FasterCSV.generate do |csv|
filename = Canvas::AccountReports.generate_file(@account_report)
FasterCSV.open(filename, "w") do |csv|
csv << headers
students.find_each do |row|
row['assignment url'] =
@ -144,8 +145,7 @@ module Canvas::AccountReports
'account_reports.default.outcome.message',
"Student-assignment-outcome mapping report successfully generated for %{account_name}",
:account_name => @account.name),
result)
result
filename)
end
# retrieve the list of courses for the account
@ -198,8 +198,8 @@ module Canvas::AccountReports
AND caa.course_section_id=course_sections.id
)", @account.id])
end
result = FasterCSV.generate do |csv|
filename = Canvas::AccountReports.generate_file(@account_report)
FasterCSV.open(filename, "w") do |csv|
csv << ['student name', 'student id', 'student sis', 'course', 'course id', 'course sis', 'section',
'section id', 'section sis', 'term', 'term id', 'term sis','current score', 'final score']
students.find_each do |student|
@ -222,8 +222,7 @@ module Canvas::AccountReports
end
end
Canvas::AccountReports.message_recipient(@account_report, I18n.t('account_reports.default.grade.message',"Grade export successfully generated for term %{term_name}", :term_name => name), result)
result
Canvas::AccountReports.message_recipient(@account_report, I18n.t('account_reports.default.grade.message',"Grade export successfully generated for term %{term_name}", :term_name => name), filename)
end
end
end

View File

@ -40,7 +40,7 @@ module ReportsSpecHelper
result
end
else
all_parsed = FasterCSV.parse(csv_report).to_a
all_parsed = FasterCSV.parse(account_report.attachment.open).to_a
all_parsed[1..-1].sort_by { |r| r.values_at(*sort_columns).join }
end
end