Migrate questions, answers and comments

Votes are missing though.
parent d630d700
......@@ -50,6 +50,10 @@ class Migrator:
"""Start the migration process"""
self.migrate_users()
self.migrate_user_logins()
self.migrate_tags()
self.migrate_nodes("question")
self.migrate_nodes("answer")
self.migrate_nodes("comment")
def migrate_users(self):
"""Migrate users to Askbot"""
......@@ -136,3 +140,104 @@ class Migrator:
# Update the next ID value
self.set_next_id("django_authopenid_userassociation", "id", last_id + 1)
def migrate_tags(self):
"""Migrate tags"""
existing = [n[0] for n in self.askbot_query("SELECT id FROM tag;")]
last_id = 0
for tag in self.osqa.table_items("forum_tag", what="tags", order="id"):
last_id = tag["id"]
if tag["id"] in existing:
continue
new = self.models.Tag()
new.id = tag["id"]
new.name = tag["name"]
new.created_by_id = tag["created_by_id"]
new.used_count = tag["used_count"]
new.save()
self.set_next_id("tag", "id", last_id + 1)
def migrate_nodes(self, node):
"""Migrate questions"""
# Load existing nodes
existing = [n[0] for n in self.askbot_query(
"SELECT id FROM askbot_post WHERE post_type = %s;", node
)]
existing_threads = [n[0] for n in self.askbot_query(
"SELECT id FROM askbot_thread;", node
)]
last_id = 0
last_thread_id = 0
table = "forum_node WHERE node_type = '%s'" % node
for node in self.osqa.table_items(table, what="%ss" % node, order="id"):
last_id = node["id"]
# Check if the node already exists
if node["id"] in existing:
continue
author = self.models.User.objects.get(id=node["author_id"])
# If it's a question, create a new thread
if node["node_type"] == "question":
last_thread_id = node["id"]
# Do this only if the thread doesn't exist
if node["id"] not in existing_threads:
thread = self.models.Thread()
thread.id = node["id"]
thread.title = node["title"]
last_activity_by = node["author_id"]
if node["last_activity_by_id"]:
last_activity_by = node["last_activity_by_id"]
thread.last_activity_by_id = last_activity_by
thread.last_activity_at = node["last_activity_at"]
thread.view_count = node["extra_count"]
thread.tagnames = node["tagnames"]
thread.save()
# Migrate post data
post = self.models.Post()
post.post_type = node["node_type"]
post.id = node["id"]
post.author_id = node["author_id"]
post.text = node["body"]
post.added_at = node["added_at"]
# Attach parents
if node["parent_id"]:
post.parent_id = node["parent_id"]
if node["node_type"] == "question":
post.thread_id = node["id"]
elif node["node_type"] == "answer":
post.thread_id = node["parent_id"]
elif node["node_type"] == "comment":
post.thread_id = self.models.Post.objects \
.get(id=node["parent_id"]) \
.thread_id
post.save()
post.parse_and_save(author)
# Mark answers as accepted if needed
if node["marked"] and node["node_type"] == "answer":
post.thread.accepted_answer = post
post.endorsed = True
post.endorsed_at = self.django.utils.timezone.now()
post.thread.save()
post.add_revision(
author = author,
revised_at = node["added_at"],
text = node["body"],
comment = "",
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment