From bfe95966b47b1829577acd474acf33a1edff98e5 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Fri, 2 Jan 2015 12:29:05 +0530 Subject: [PATCH] better filenames for export --- app/assets/javascripts/discourse/models/export_csv.js | 2 +- app/controllers/export_csv_controller.rb | 2 +- app/jobs/regular/export_csv_file.rb | 10 ++++++---- spec/controllers/export_csv_controller_spec.rb | 6 +++--- spec/jobs/export_csv_file_spec.rb | 11 +++++------ 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/discourse/models/export_csv.js b/app/assets/javascripts/discourse/models/export_csv.js index be678c392..9d5c00d89 100644 --- a/app/assets/javascripts/discourse/models/export_csv.js +++ b/app/assets/javascripts/discourse/models/export_csv.js @@ -30,7 +30,7 @@ Discourse.ExportCsv.reopenClass({ @method export_user_list **/ exportUserList: function() { - return Discourse.ajax("/export_csv/export_entity.json", {data: {entity_type: 'admin', entity: 'user'}}); + return Discourse.ajax("/export_csv/export_entity.json", {data: {entity_type: 'admin', entity: 'user_list'}}); }, /** diff --git a/app/controllers/export_csv_controller.rb b/app/controllers/export_csv_controller.rb index c28ffe864..a8f875ede 100644 --- a/app/controllers/export_csv_controller.rb +++ b/app/controllers/export_csv_controller.rb @@ -15,7 +15,7 @@ class ExportCsvController < ApplicationController def show params.require(:id) filename = params.fetch(:id) - export_id = filename.split('_')[1].split('.')[0] + export_id = filename.split('-')[2].split('.')[0] export_initiated_by_user_id = 0 export_initiated_by_user_id = UserExport.where(id: export_id)[0].user_id unless UserExport.where(id: export_id).empty? export_csv_path = UserExport.get_download_path(filename) diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index 6c8c9e5d0..b0ae17c36 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -24,6 +24,7 @@ module Jobs def execute(args) entity = args[:entity] + @file_name = entity if entity == "user_archive" @entity_type = "user" @@ -56,12 +57,12 @@ module Jobs end end - def user_export + def user_list_export query = ::AdminUserIndexQuery.new user_data = query.find_users_query.to_a user_data.map do |user| group_names = get_group_names(user).join(';') - user_array = get_user_fields(user) + user_array = get_user_list_fields(user) user_array.push(group_names) if group_names != '' user_array end @@ -168,7 +169,7 @@ module Jobs user_archive_array end - def get_user_fields(user) + def get_user_list_fields(user) user_array = [] HEADER_ATTRS_FOR['user'].each do |attr| @@ -271,7 +272,8 @@ module Jobs def set_file_path @file = UserExport.create(export_type: @entity_type, user_id: @current_user.id) - @file_name = "export_#{@file.id}.csv" + file_name_prefix = @file_name.split('_').join('-') + @file_name = "#{file_name_prefix}-#{@file.id}.csv" # ensure directory exists dir = File.dirname("#{UserExport.base_directory}/#{@file_name}") diff --git a/spec/controllers/export_csv_controller_spec.rb b/spec/controllers/export_csv_controller_spec.rb index cb40652ee..1b9a7337a 100644 --- a/spec/controllers/export_csv_controller_spec.rb +++ b/spec/controllers/export_csv_controller_spec.rb @@ -1,7 +1,7 @@ require "spec_helper" describe ExportCsvController do - let(:export_filename) { "export_999.csv" } + let(:export_filename) { "user-archive-999.csv" } context "while logged in as normal user" do @@ -30,7 +30,7 @@ describe ExportCsvController do describe ".download" do it "uses send_file to transmit the export file" do file = UserExport.create(export_type: "user", user_id: @user.id) - file_name = "export_#{file.id}.csv" + file_name = "user-archive-#{file.id}.csv" controller.stubs(:render) export = UserExport.new() UserExport.expects(:get_download_path).with(file_name).returns(export) @@ -74,7 +74,7 @@ describe ExportCsvController do describe ".download" do it "uses send_file to transmit the export file" do file = UserExport.create(export_type: "admin", user_id: @admin.id) - file_name = "export_#{file.id}.csv" + file_name = "screened-email-#{file.id}.csv" controller.stubs(:render) export = UserExport.new() UserExport.expects(:get_download_path).with(file_name).returns(export) diff --git a/spec/jobs/export_csv_file_spec.rb b/spec/jobs/export_csv_file_spec.rb index 8c41b43bc..f0f05afc1 100644 --- a/spec/jobs/export_csv_file_spec.rb +++ b/spec/jobs/export_csv_file_spec.rb @@ -8,16 +8,16 @@ describe Jobs::ExportCsvFile do end end - let :user_header do + let :user_list_header do Jobs::ExportCsvFile.new.get_header('user') end - let :user_export do - Jobs::ExportCsvFile.new.user_export + let :user_list_export do + Jobs::ExportCsvFile.new.user_list_export end def to_hash(row) - Hash[*user_header.zip(row).flatten] + Hash[*user_list_header.zip(row).flatten] end it 'exports sso data' do @@ -25,10 +25,9 @@ describe Jobs::ExportCsvFile do user = Fabricate(:user) user.create_single_sign_on_record(external_id: "123", last_payload: "xxx", external_email: 'test@test.com') - user = to_hash(user_export.find{|u| u[0] == user.id}) + user = to_hash(user_list_export.find{|u| u[0] == user.id}) user["external_id"].should == "123" user["external_email"].should == "test@test.com" end end -