2021-10-12 19:38:12 +00:00
|
|
|
from dateutil.parser import parse as date_parse
|
2021-10-09 10:08:27 +00:00
|
|
|
import json
|
2021-10-13 17:46:30 +00:00
|
|
|
import datetime
|
2021-10-09 10:08:27 +00:00
|
|
|
from os.path import abspath
|
2021-10-12 19:38:12 +00:00
|
|
|
from orm import Shout, Comment, CommentRating, User
|
2021-10-09 10:08:27 +00:00
|
|
|
from orm.base import local_session
|
|
|
|
from migration.html2text import html2text
|
2021-08-20 09:27:19 +00:00
|
|
|
|
2021-12-13 07:50:33 +00:00
|
|
|
def migrate(entry, shouts_by_oid):
|
2021-12-08 07:28:38 +00:00
|
|
|
'''
|
|
|
|
{
|
|
|
|
"_id": "hdtwS8fSyFLxXCgSC",
|
|
|
|
"body": "<p>",
|
|
|
|
"contentItem": "mnK8KsJHPRi8DrybQ",
|
|
|
|
"createdBy": "bMFPuyNg6qAD2mhXe",
|
|
|
|
"thread": "01/",
|
|
|
|
"createdAt": "2016-04-19 04:33:53+00:00",
|
|
|
|
"ratings": [
|
|
|
|
{ "createdBy": "AqmRukvRiExNpAe8C", "value": 1 },
|
|
|
|
{ "createdBy": "YdE76Wth3yqymKEu5", "value": 1 }
|
|
|
|
],
|
|
|
|
"rating": 2,
|
|
|
|
"updatedAt": "2020-05-27 19:22:57.091000+00:00",
|
|
|
|
"updatedBy": "0"
|
|
|
|
}
|
2021-10-09 10:08:27 +00:00
|
|
|
|
2021-12-08 07:28:38 +00:00
|
|
|
->
|
2021-10-09 10:08:27 +00:00
|
|
|
|
2021-12-08 07:28:38 +00:00
|
|
|
type Comment {
|
|
|
|
id: Int!
|
|
|
|
author: Int!
|
|
|
|
body: String!
|
|
|
|
replyTo: Int!
|
|
|
|
createdAt: DateTime!
|
|
|
|
updatedAt: DateTime
|
|
|
|
shout: Int!
|
|
|
|
deletedAt: DateTime
|
|
|
|
deletedBy: Int
|
|
|
|
ratings: [CommentRating]
|
|
|
|
views: Int
|
|
|
|
}
|
|
|
|
'''
|
2021-12-13 07:50:33 +00:00
|
|
|
|
|
|
|
shout_old_id = entry['contentItem']
|
|
|
|
if not shout_old_id in shouts_by_oid:
|
|
|
|
return
|
|
|
|
shout = shouts_by_oid[shout_old_id]
|
|
|
|
|
2021-12-08 07:28:38 +00:00
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(User).filter(User.old_id == entry['createdBy']).first()
|
|
|
|
comment_dict = {
|
|
|
|
'author': author.id if author else 0,
|
|
|
|
'createdAt': date_parse(entry['createdAt']),
|
|
|
|
'body': html2text(entry['body']),
|
2021-12-13 07:50:33 +00:00
|
|
|
'shout': shout["slug"]
|
2021-12-08 07:28:38 +00:00
|
|
|
}
|
|
|
|
if entry.get('deleted'):
|
|
|
|
comment_dict['deletedAt'] = date_parse(entry['updatedAt'])
|
|
|
|
comment_dict['deletedBy'] = str(entry['updatedBy'])
|
|
|
|
if entry.get('updatedAt'):
|
|
|
|
comment_dict['updatedAt'] = date_parse(entry['updatedAt'])
|
|
|
|
# comment_dict['updatedBy'] = str(entry.get('updatedBy', 0)) invalid keyword for Comment
|
|
|
|
# print(comment_dict)
|
|
|
|
comment = Comment.create(**comment_dict)
|
|
|
|
comment_dict['id'] = comment.id
|
|
|
|
comment_dict['ratings'] = []
|
|
|
|
comment_dict['old_id'] = entry['_id']
|
|
|
|
# print(comment)
|
|
|
|
for comment_rating_old in entry.get('ratings',[]):
|
|
|
|
rater = session.query(User).filter(User.old_id == comment_rating_old['createdBy']).first()
|
|
|
|
if rater and comment:
|
|
|
|
comment_rating_dict = {
|
|
|
|
'value': comment_rating_old['value'],
|
2022-01-14 12:19:57 +00:00
|
|
|
'createdBy': rater.slug,
|
2021-12-08 07:28:38 +00:00
|
|
|
'comment_id': comment.id
|
|
|
|
}
|
|
|
|
cts = comment_rating_old.get('createdAt')
|
|
|
|
if cts: comment_rating_dict['createdAt'] = date_parse(cts)
|
|
|
|
try:
|
|
|
|
comment_rating = CommentRating.create(**comment_rating_dict)
|
|
|
|
comment_dict['ratings'].append(comment_rating_dict)
|
|
|
|
except Exception as e:
|
|
|
|
print(comment_rating_dict)
|
|
|
|
raise e
|
|
|
|
return comment_dict
|
|
|
|
|
|
|
|
def migrate_2stage(entry, id_map):
|
|
|
|
old_reply_to = entry.get('replyTo')
|
|
|
|
if not old_reply_to:
|
|
|
|
return
|
|
|
|
old_id = entry['_id']
|
2021-12-13 07:50:33 +00:00
|
|
|
if not old_id in id_map:
|
|
|
|
return
|
|
|
|
id = id_map[old_id]
|
2021-12-08 07:28:38 +00:00
|
|
|
with local_session() as session:
|
|
|
|
comment = session.query(Comment).filter(Comment.id == id).first()
|
|
|
|
reply_to = id_map.get(old_reply_to)
|
|
|
|
comment.replyTo = reply_to
|
|
|
|
session.commit()
|