mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 19:46:55 +08:00
FEATURE: category and topic importer can support data from different sources using IMPORT_SOURCE
This commit is contained in:
parent
aec5de451b
commit
214fdad155
|
@ -39,14 +39,15 @@ module ImportExport
|
|||
|
||||
def import_categories
|
||||
id = @export_data[:category].delete(:id)
|
||||
import_id = "#{id}#{import_source}"
|
||||
|
||||
parent = CategoryCustomField.where(name: 'import_id', value: id.to_s).first.try(:category)
|
||||
parent = CategoryCustomField.where(name: 'import_id', value: import_id).first.try(:category)
|
||||
|
||||
unless parent
|
||||
permissions = @export_data[:category].delete(:permissions_params)
|
||||
parent = Category.new(@export_data[:category])
|
||||
parent.user_id = @topic_importer.new_user_id(@export_data[:category][:user_id]) # imported user's new id
|
||||
parent.custom_fields["import_id"] = id
|
||||
parent.custom_fields["import_id"] = import_id
|
||||
parent.permissions = permissions.present? ? permissions : { "everyone" => CategoryGroup.permission_types[:full] }
|
||||
parent.save!
|
||||
set_category_description(parent, @export_data[:category][:description])
|
||||
|
@ -54,14 +55,15 @@ module ImportExport
|
|||
|
||||
@export_data[:subcategories].each do |cat_attrs|
|
||||
id = cat_attrs.delete(:id)
|
||||
existing = CategoryCustomField.where(name: 'import_id', value: id.to_s).first.try(:category)
|
||||
import_id = "#{id}#{import_source}"
|
||||
existing = CategoryCustomField.where(name: 'import_id', value: import_id).first.try(:category)
|
||||
|
||||
unless existing
|
||||
permissions = cat_attrs.delete(:permissions_params)
|
||||
subcategory = Category.new(cat_attrs)
|
||||
subcategory.parent_category_id = parent.id
|
||||
subcategory.user_id = @topic_importer.new_user_id(cat_attrs[:user_id])
|
||||
subcategory.custom_fields["import_id"] = id
|
||||
subcategory.custom_fields["import_id"] = import_id
|
||||
subcategory.permissions = permissions.present? ? permissions : { "everyone" => CategoryGroup.permission_types[:full] }
|
||||
subcategory.save!
|
||||
set_category_description(subcategory, cat_attrs[:description])
|
||||
|
@ -79,5 +81,9 @@ module ImportExport
|
|||
def import_topics
|
||||
@topic_importer.import_topics
|
||||
end
|
||||
|
||||
def import_source
|
||||
@_import_source ||= "#{ENV['IMPORT_SOURCE'] || ''}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -18,14 +18,15 @@ module ImportExport
|
|||
|
||||
def import_users
|
||||
@export_data[:users].each do |u|
|
||||
import_id = "#{u[:id]}#{import_source}"
|
||||
existing = User.with_email(u[:email]).first
|
||||
if existing
|
||||
if existing.custom_fields["import_id"] != u[:id]
|
||||
existing.custom_fields["import_id"] = u[:id]
|
||||
if existing.custom_fields["import_id"] != import_id
|
||||
existing.custom_fields["import_id"] = import_id
|
||||
existing.save!
|
||||
end
|
||||
else
|
||||
u = create_user(u, u[:id]) # see ImportScripts::Base
|
||||
u = create_user(u, import_id) # see ImportScripts::Base
|
||||
end
|
||||
end
|
||||
self
|
||||
|
@ -37,13 +38,15 @@ module ImportExport
|
|||
print t[:title]
|
||||
|
||||
first_post_attrs = t[:posts].first.merge(t.slice(*(TopicExporter::TOPIC_ATTRS - [:id, :category_id])))
|
||||
|
||||
first_post_attrs[:user_id] = new_user_id(first_post_attrs[:user_id])
|
||||
first_post_attrs[:category] = new_category_id(t[:category_id])
|
||||
|
||||
first_post = PostCustomField.where(name: "import_id", value: first_post_attrs[:id]).first.try(:post)
|
||||
import_id = "#{first_post_attrs[:id]}#{import_source}"
|
||||
first_post = PostCustomField.where(name: "import_id", value: import_id).first&.post
|
||||
|
||||
unless first_post
|
||||
first_post = create_post(first_post_attrs, first_post_attrs[:id])
|
||||
first_post = create_post(first_post_attrs, import_id)
|
||||
end
|
||||
|
||||
topic_id = first_post.topic_id
|
||||
|
@ -51,10 +54,17 @@ module ImportExport
|
|||
t[:posts].each_with_index do |post_data, i|
|
||||
next if i == 0
|
||||
print "."
|
||||
existing = PostCustomField.where(name: "import_id", value: post_data[:id]).first.try(:post)
|
||||
post_import_id = "#{post_data[:id]}#{import_source}"
|
||||
existing = PostCustomField.where(name: "import_id", value: post_import_id).first&.post
|
||||
unless existing
|
||||
create_post(post_data.merge(topic_id: topic_id,
|
||||
user_id: new_user_id(post_data[:user_id])), post_data[:id]) # see ImportScripts::Base
|
||||
# see ImportScripts::Base
|
||||
create_post(
|
||||
post_data.merge(
|
||||
topic_id: topic_id,
|
||||
user_id: new_user_id(post_data[:user_id])
|
||||
),
|
||||
post_import_id
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -65,12 +75,16 @@ module ImportExport
|
|||
end
|
||||
|
||||
def new_user_id(external_user_id)
|
||||
ucf = UserCustomField.where(name: "import_id", value: external_user_id.to_s).first
|
||||
ucf = UserCustomField.where(name: "import_id", value: "#{external_user_id}#{import_source}").first
|
||||
ucf ? ucf.user_id : Discourse::SYSTEM_USER_ID
|
||||
end
|
||||
|
||||
def new_category_id(external_category_id)
|
||||
CategoryCustomField.where(name: "import_id", value: external_category_id).first.category_id rescue nil
|
||||
CategoryCustomField.where(name: "import_id", value: "#{external_category_id}#{import_source}").first.category_id rescue nil
|
||||
end
|
||||
|
||||
def import_source
|
||||
@_import_source ||= "#{ENV['IMPORT_SOURCE'] || ''}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user