-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathmigrate_from_pbb2.py
More file actions
85 lines (69 loc) · 2.67 KB
/
Copy pathmigrate_from_pbb2.py
File metadata and controls
85 lines (69 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding:utf-8 -*-
from __future__ import unicode_literals
import datetime
from pymongo import MongoClient
from gather import create_app
from gather.extensions import db
from gather.account.models import Account
from gather.node.models import Node
from gather.topic.models import Topic, Reply
app = create_app()
mongo_database = MongoClient()["forum"]
def role(num):
if num == 1:
return "user"
elif num >= 5:
return "admin"
return "staff"
def timestamp_to_datetime(t):
return datetime.datetime.fromtimestamp(t)
def main():
for pbb_member in mongo_database.members.find(sort=[('created', 1)]):
account = Account(
username=pbb_member["name"].lower(),
email=pbb_member["email"],
website=pbb_member["website"],
description=pbb_member["description"],
role=role(pbb_member["role"]),
created=timestamp_to_datetime(pbb_member["created"]),
password="need-to-reset"
)
print "Migrating Account %s" % account.username
account.create_password(str(pbb_member["_id"]))
db.session.add(account)
db.session.commit()
for pbb_node in mongo_database.nodes.find():
node = Node(
name=pbb_node["title"],
slug=pbb_node["name"],
description=pbb_node["description"]
)
db.session.add(node)
db.session.commit()
for pbb_topic in mongo_database.topics.find(sort=[('last_reply_time', 1)]):
topic = Topic(
title=pbb_topic["title"],
content=pbb_topic["content"],
author=Account.query.filter_by(username=pbb_topic["author"].lower()).first(),
node=Node.query.filter_by(slug=pbb_topic["node"]).first(),
created=timestamp_to_datetime(pbb_topic["created"]),
updated=timestamp_to_datetime(pbb_topic["last_reply_time"])
)
print "Migrating Topic %s by %s" % (topic.title, topic.author.username)
db.session.add(topic)
db.session.commit()
for pbb_reply in mongo_database.replies.find({'topic': str(pbb_topic["_id"])},
sort=[('index', 1)]):
reply = Reply(
content=pbb_reply["content"],
author=Account.query.filter_by(username=pbb_reply["author"].lower()).first(),
topic=topic,
created=timestamp_to_datetime(pbb_reply["created"])
)
print "Migrating Reply %s by %s" % (reply.content, reply.author.username)
db.session.add(reply)
db.session.commit()
with app.app_context():
db.drop_all()
db.create_all()
main()