mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 16:02:46 +08:00
FEATURE: Option to export multiple categories using export_category method
This commit is contained in:
parent
ed16cba77f
commit
d9823f69c6
|
@ -4,10 +4,8 @@ require "import_export/topic_exporter"
|
||||||
module ImportExport
|
module ImportExport
|
||||||
class CategoryExporter < BaseExporter
|
class CategoryExporter < BaseExporter
|
||||||
|
|
||||||
def initialize(category_id)
|
def initialize(category_ids)
|
||||||
@category = Category.find(category_id)
|
@categories = Category.where(id: category_ids).or(Category.where(parent_category_id: category_ids)).to_a
|
||||||
@categories = Category.where(parent_category_id: category_id).to_a
|
|
||||||
@categories << @category
|
|
||||||
@export_data = {
|
@export_data = {
|
||||||
categories: [],
|
categories: [],
|
||||||
groups: [],
|
groups: [],
|
||||||
|
@ -17,7 +15,6 @@ module ImportExport
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
puts "Exporting category #{@category.name}...", ""
|
|
||||||
export_categories!
|
export_categories!
|
||||||
export_category_groups!
|
export_category_groups!
|
||||||
export_topics_and_users
|
export_topics_and_users
|
||||||
|
|
|
@ -11,12 +11,12 @@ module ImportExport
|
||||||
ImportExport::Importer.new(data).perform
|
ImportExport::Importer.new(data).perform
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.export_categories(include_users, filename = nil)
|
def self.export_category_structure(include_users, filename = nil)
|
||||||
ImportExport::CategoryStructureExporter.new(include_users).perform.save_to_file(filename)
|
ImportExport::CategoryStructureExporter.new(include_users).perform.save_to_file(filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.export_category(category_id, filename = nil)
|
def self.export_categories(category_ids, filename = nil)
|
||||||
ImportExport::CategoryExporter.new(category_id).perform.save_to_file(filename)
|
ImportExport::CategoryExporter.new(category_ids).perform.save_to_file(filename)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.export_topics(topic_ids, filename = nil)
|
def self.export_topics(topic_ids, filename = nil)
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
desc 'Export all the categories'
|
desc 'Export all the categories'
|
||||||
task 'export:categories', [:include_group_users, :file_name] => [:environment] do |_, args|
|
task 'export:categories', [:category_ids] => [:environment] do |_, args|
|
||||||
require "import_export/import_export"
|
require "import_export/import_export"
|
||||||
|
|
||||||
ImportExport.export_categories(args[:include_group_users], args[:file_name])
|
ImportExport.export_categories(args[:category_ids])
|
||||||
|
puts "", "Done", ""
|
||||||
|
end
|
||||||
|
|
||||||
|
desc 'Export only the structure of all categories'
|
||||||
|
task 'export:category_structure', [:include_group_users, :file_name] => [:environment] do |_, args|
|
||||||
|
require "import_export/import_export"
|
||||||
|
|
||||||
|
ImportExport.export_category_structure(args[:include_group_users], args[:file_name])
|
||||||
puts "", "Done", ""
|
puts "", "Done", ""
|
||||||
end
|
end
|
||||||
|
|
|
@ -181,16 +181,22 @@ class DiscourseCLI < Thor
|
||||||
puts 'Requests sent. Clients will refresh on next navigation.'
|
puts 'Requests sent. Clients will refresh on next navigation.'
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
|
desc "export_categories", "Export categories, all its topics, and all users who posted in those topics"
|
||||||
def export_category(category_id, filename = nil)
|
def export_categories(*category_ids)
|
||||||
raise "Category id argument is missing!" unless category_id
|
puts "Starting export of categories...", ""
|
||||||
|
|
||||||
load_rails
|
load_rails
|
||||||
load_import_export
|
load_import_export
|
||||||
ImportExport.export_category(category_id, filename)
|
ImportExport.export_categories(category_ids)
|
||||||
puts "", "Done", ""
|
puts "", "Done", ""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc "export_category", "Export a category, all its topics, and all users who posted in those topics"
|
||||||
|
def export_category(category_id)
|
||||||
|
raise "Category id argument is missing!" unless category_id
|
||||||
|
|
||||||
|
export_categories([category_id])
|
||||||
|
end
|
||||||
|
|
||||||
desc "import_category", "Import a category, its topics and the users from the output of the export_category command"
|
desc "import_category", "Import a category, its topics and the users from the output of the export_category command"
|
||||||
def import_category(filename)
|
def import_category(filename)
|
||||||
raise "File name argument missing!" unless filename
|
raise "File name argument missing!" unless filename
|
||||||
|
|
|
@ -12,12 +12,8 @@ describe ImportExport::CategoryExporter do
|
||||||
end
|
end
|
||||||
|
|
||||||
context '.perform' do
|
context '.perform' do
|
||||||
it 'raises an error when the category is not found' do
|
|
||||||
expect { ImportExport::CategoryExporter.new(100).perform }.to raise_error(ActiveRecord::RecordNotFound)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'export the category when it is found' do
|
it 'export the category when it is found' do
|
||||||
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
|
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
|
||||||
|
|
||||||
expect(data[:categories].count).to eq(1)
|
expect(data[:categories].count).to eq(1)
|
||||||
expect(data[:groups].count).to eq(0)
|
expect(data[:groups].count).to eq(0)
|
||||||
|
@ -25,16 +21,25 @@ describe ImportExport::CategoryExporter do
|
||||||
|
|
||||||
it 'export the category with permission groups' do
|
it 'export the category with permission groups' do
|
||||||
category_group = Fabricate(:category_group, category: category, group: group)
|
category_group = Fabricate(:category_group, category: category, group: group)
|
||||||
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
|
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
|
||||||
|
|
||||||
expect(data[:categories].count).to eq(1)
|
expect(data[:categories].count).to eq(1)
|
||||||
expect(data[:groups].count).to eq(1)
|
expect(data[:groups].count).to eq(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'export multiple categories' do
|
||||||
|
category2 = Fabricate(:category)
|
||||||
|
category_group = Fabricate(:category_group, category: category, group: group)
|
||||||
|
data = ImportExport::CategoryExporter.new([category.id, category2.id]).perform.export_data
|
||||||
|
|
||||||
|
expect(data[:categories].count).to eq(2)
|
||||||
|
expect(data[:groups].count).to eq(1)
|
||||||
|
end
|
||||||
|
|
||||||
it 'export the category with topics and users' do
|
it 'export the category with topics and users' do
|
||||||
topic1 = Fabricate(:topic, category: category, user_id: -1)
|
topic1 = Fabricate(:topic, category: category, user_id: -1)
|
||||||
topic2 = Fabricate(:topic, category: category, user: user)
|
topic2 = Fabricate(:topic, category: category, user: user)
|
||||||
data = ImportExport::CategoryExporter.new(category.id).perform.export_data
|
data = ImportExport::CategoryExporter.new([category.id]).perform.export_data
|
||||||
|
|
||||||
expect(data[:categories].count).to eq(1)
|
expect(data[:categories].count).to eq(1)
|
||||||
expect(data[:groups].count).to eq(0)
|
expect(data[:groups].count).to eq(0)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user