mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 23:58:31 -05:00
Merge pull request #3102 from techAPJ/patch-2
Add username and date-time in exported file name
This commit is contained in:
commit
dc801bb442
5 changed files with 21 additions and 12 deletions
|
@ -15,7 +15,7 @@ class ExportCsvController < ApplicationController
|
||||||
def show
|
def show
|
||||||
params.require(:id)
|
params.require(:id)
|
||||||
filename = params.fetch(:id)
|
filename = params.fetch(:id)
|
||||||
export_id = filename.split('-')[2].split('.')[0]
|
export_id = filename.split('-')[-1].split('.')[0]
|
||||||
export_initiated_by_user_id = 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_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)
|
export_csv_path = UserExport.get_download_path(filename)
|
||||||
|
|
|
@ -260,8 +260,12 @@ module Jobs
|
||||||
|
|
||||||
|
|
||||||
def set_file_path
|
def set_file_path
|
||||||
file_name_prefix = @file_name.split('_').join('-')
|
if @entity == "user_archive"
|
||||||
@file = UserExport.create(export_type: file_name_prefix, user_id: @current_user.id)
|
file_name_prefix = "#{@file_name.split('_').join('-')}-#{current_user.username}-#{Time.now.strftime("%y%m%d-%H%M%S")}"
|
||||||
|
else
|
||||||
|
file_name_prefix = "#{@file_name.split('_').join('-')}-#{Time.now.strftime("%y%m%d-%H%M%S")}"
|
||||||
|
end
|
||||||
|
@file = UserExport.create(file_name: file_name_prefix, user_id: @current_user.id)
|
||||||
@file_name = "#{file_name_prefix}-#{@file.id}.csv"
|
@file_name = "#{file_name_prefix}-#{@file.id}.csv"
|
||||||
|
|
||||||
# ensure directory exists
|
# ensure directory exists
|
||||||
|
|
|
@ -12,7 +12,7 @@ class UserExport < ActiveRecord::Base
|
||||||
def self.remove_old_exports
|
def self.remove_old_exports
|
||||||
expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a
|
expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a
|
||||||
expired_exports.map do |expired_export|
|
expired_exports.map do |expired_export|
|
||||||
file_name = "#{expired_export.export_type}-#{expired_export.id}.csv.gz"
|
file_name = "#{expired_export.file_name}-#{expired_export.id}.csv.gz"
|
||||||
file_path = "#{UserExport.base_directory}/#{file_name}"
|
file_path = "#{UserExport.base_directory}/#{file_name}"
|
||||||
|
|
||||||
if File.exist?(file_path)
|
if File.exist?(file_path)
|
||||||
|
@ -33,7 +33,7 @@ end
|
||||||
# Table name: user_exports
|
# Table name: user_exports
|
||||||
#
|
#
|
||||||
# id :integer not null, primary key
|
# id :integer not null, primary key
|
||||||
# export_type :string(255) not null
|
# file_name :string(255) not null
|
||||||
# user_id :integer not null
|
# user_id :integer not null
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
|
|
5
db/migrate/20150115172310_rename_user_export_column.rb
Normal file
5
db/migrate/20150115172310_rename_user_export_column.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class RenameUserExportColumn < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
rename_column :user_exports, :export_type, :file_name
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,7 +1,7 @@
|
||||||
require "spec_helper"
|
require "spec_helper"
|
||||||
|
|
||||||
describe ExportCsvController do
|
describe ExportCsvController do
|
||||||
let(:export_filename) { "user-archive-999.csv.gz" }
|
let(:export_filename) { "user-archive-codinghorror-150115-234817-999.csv.gz" }
|
||||||
|
|
||||||
|
|
||||||
context "while logged in as normal user" do
|
context "while logged in as normal user" do
|
||||||
|
@ -16,7 +16,7 @@ describe ExportCsvController do
|
||||||
|
|
||||||
it "should not enqueue export job if rate limit is reached" do
|
it "should not enqueue export job if rate limit is reached" do
|
||||||
Jobs::ExportCsvFile.any_instance.expects(:execute).never
|
Jobs::ExportCsvFile.any_instance.expects(:execute).never
|
||||||
UserExport.create(export_type: "user", user_id: @user.id)
|
UserExport.create(file_name: "user-archive-codinghorror-150116-003249", user_id: @user.id)
|
||||||
xhr :post, :export_entity, entity: "user_archive", entity_type: "user"
|
xhr :post, :export_entity, entity: "user_archive", entity_type: "user"
|
||||||
expect(response).not_to be_success
|
expect(response).not_to be_success
|
||||||
end
|
end
|
||||||
|
@ -29,8 +29,8 @@ describe ExportCsvController do
|
||||||
|
|
||||||
describe ".download" do
|
describe ".download" do
|
||||||
it "uses send_file to transmit the export file" do
|
it "uses send_file to transmit the export file" do
|
||||||
file = UserExport.create(export_type: "user", user_id: @user.id)
|
file = UserExport.create(file_name: "user-archive-codinghorror-150116-003249", user_id: @user.id)
|
||||||
file_name = "user-archive-#{file.id}.csv.gz"
|
file_name = "user-archive-codinghorror-150116-003249-#{file.id}.csv.gz"
|
||||||
controller.stubs(:render)
|
controller.stubs(:render)
|
||||||
export = UserExport.new()
|
export = UserExport.new()
|
||||||
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
||||||
|
@ -65,7 +65,7 @@ describe ExportCsvController do
|
||||||
|
|
||||||
it "should not rate limit export for staff" do
|
it "should not rate limit export for staff" do
|
||||||
Jobs.expects(:enqueue).with(:export_csv_file, has_entries(entity: "staff_action", user_id: @admin.id))
|
Jobs.expects(:enqueue).with(:export_csv_file, has_entries(entity: "staff_action", user_id: @admin.id))
|
||||||
UserExport.create(export_type: "admin", user_id: @admin.id)
|
UserExport.create(file_name: "screened-email-150116-010145", user_id: @admin.id)
|
||||||
xhr :post, :export_entity, entity: "staff_action", entity_type: "admin"
|
xhr :post, :export_entity, entity: "staff_action", entity_type: "admin"
|
||||||
expect(response).to be_success
|
expect(response).to be_success
|
||||||
end
|
end
|
||||||
|
@ -73,8 +73,8 @@ describe ExportCsvController do
|
||||||
|
|
||||||
describe ".download" do
|
describe ".download" do
|
||||||
it "uses send_file to transmit the export file" do
|
it "uses send_file to transmit the export file" do
|
||||||
file = UserExport.create(export_type: "admin", user_id: @admin.id)
|
file = UserExport.create(file_name: "screened-email-150116-010145", user_id: @admin.id)
|
||||||
file_name = "screened-email-#{file.id}.csv.gz"
|
file_name = "screened-email-150116-010145-#{file.id}.csv.gz"
|
||||||
controller.stubs(:render)
|
controller.stubs(:render)
|
||||||
export = UserExport.new()
|
export = UserExport.new()
|
||||||
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
UserExport.expects(:get_download_path).with(file_name).returns(export)
|
||||||
|
|
Loading…
Reference in a new issue