core/migration/tables/comments.py

86 lines
2.9 KiB
Python
Raw Normal View History

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-10-13 17:46:30 +00:00
# users_dict = json.loads(open(abspath('migration/data/users.dict.json')).read())
# topics_dict = json.loads(open(abspath('migration/data/topics.dict.json')).read()) # old_id keyed
2021-08-20 09:27:19 +00:00
def migrate(entry):
2021-08-23 08:44:46 +00:00
'''
2021-10-09 10:08:27 +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-09-11 08:20:23 +00:00
type Comment {
id: Int!
2021-08-20 09:27:19 +00:00
author: Int!
body: String!
2021-09-11 08:20:23 +00:00
replyTo: Int!
2021-08-20 09:27:19 +00:00
createdAt: DateTime!
2021-09-11 08:20:23 +00:00
updatedAt: DateTime
shout: Int!
2021-08-20 09:27:19 +00:00
deletedAt: DateTime
deletedBy: Int
rating: Int
2021-10-13 17:46:30 +00:00
ratigns: [CommentRating]
2021-09-11 08:20:23 +00:00
views: Int
old_id: String
2021-10-13 17:46:30 +00:00
old_thread: String
2021-08-20 09:27:19 +00:00
}
2021-08-23 08:44:46 +00:00
'''
2021-10-09 10:08:27 +00:00
with local_session() as session:
2021-10-13 17:46:30 +00:00
shout = session.query(Shout).filter(Shout.old_id == entry['_id']).first()
if not shout: print(entry)
assert shout, '=== NO SHOUT IN COMMENT ERROR ==='
author = session.query(User).filter(User.old_id == entry['_id']).first()
2021-10-12 19:38:12 +00:00
comment_dict = {
2021-10-09 10:08:27 +00:00
'old_id': entry['_id'],
2021-10-13 17:46:30 +00:00
'author': author.id if author else 0,
2021-10-12 19:38:12 +00:00
'createdAt': date_parse(entry['createdAt']),
2021-10-09 10:08:27 +00:00
'body': html2text(entry['body']),
2021-10-13 17:46:30 +00:00
'shout': shout
2021-10-09 10:08:27 +00:00
}
2021-10-12 19:38:12 +00:00
if 'rating' in entry:
comment_dict['rating'] = entry['rating']
2021-10-13 17:46:30 +00:00
if entry.get('deleted'):
comment_dict['deletedAt'] = entry['updatedAt']
comment_dict['deletedBy'] = entry['updatedBy']
2021-10-12 19:38:12 +00:00
if 'thread' in entry:
comment_dict['old_thread'] = entry['thread']
2021-10-13 17:46:30 +00:00
# print(entry.keys())
2021-10-12 19:38:12 +00:00
comment = Comment.create(**comment_dict)
for comment_rating_old in entry.get('ratings',[]):
rater_id = session.query(User).filter(User.old_id == comment_rating_old['createdBy']).first()
comment_rating_dict = {
2021-10-13 17:46:30 +00:00
'value': comment_rating_old['value'],
'createdBy': rater_id or 0,
'createdAt': comment_rating_old.get('createdAt', datetime.datetime.now()),
'comment_id': comment.id
2021-10-12 19:38:12 +00:00
}
2021-10-13 17:46:30 +00:00
try:
comment_rating = CommentRating.create(**comment_rating_dict)
# TODO: comment rating append resolver
# comment['ratings'].append(comment_rating)
except Exception as e:
print(comment_rating)
pass # raise e
2021-10-12 19:38:12 +00:00
return comment