2022-08-11 09:14:12 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from dateutil.parser import parse as date_parse
|
|
|
|
from orm import Reaction, User
|
2022-08-11 09:59:35 +00:00
|
|
|
from base.orm import local_session
|
2022-08-11 10:06:31 +00:00
|
|
|
from migration.html2text import html2text
|
2022-08-11 09:14:12 +00:00
|
|
|
from orm.reaction import ReactionKind
|
2022-08-17 09:07:14 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
2022-08-11 09:14:12 +00:00
|
|
|
|
|
|
|
ts = datetime.now()
|
|
|
|
|
|
|
|
def migrate(entry, storage):
|
|
|
|
'''
|
|
|
|
{
|
|
|
|
"_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"
|
|
|
|
}
|
|
|
|
|
|
|
|
->
|
|
|
|
|
|
|
|
type Reaction {
|
|
|
|
id: Int!
|
|
|
|
shout: Shout!
|
|
|
|
createdAt: DateTime!
|
|
|
|
createdBy: User!
|
|
|
|
updatedAt: DateTime
|
|
|
|
deletedAt: DateTime
|
|
|
|
deletedBy: User
|
|
|
|
range: String # full / 0:2340
|
|
|
|
kind: ReactionKind!
|
|
|
|
body: String
|
|
|
|
replyTo: Reaction
|
|
|
|
stat: Stat
|
|
|
|
old_id: String
|
|
|
|
old_thread: String
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
reaction_dict = {}
|
2022-08-11 11:22:10 +00:00
|
|
|
reaction_dict['createdAt'] = ts if not entry.get('createdAt') else date_parse(entry.get('createdAt'))
|
|
|
|
print('[migration] reaction original date %r' % entry.get('createdAt'))
|
2022-08-11 09:14:12 +00:00
|
|
|
# print('[migration] comment date %r ' % comment_dict['createdAt'])
|
|
|
|
reaction_dict['body'] = html2text(entry.get('body', ''))
|
|
|
|
reaction_dict['oid'] = entry['_id']
|
|
|
|
if entry.get('createdAt'): reaction_dict['createdAt'] = date_parse(entry.get('createdAt'))
|
|
|
|
shout_oid = entry.get('contentItem')
|
|
|
|
if not shout_oid in storage['shouts']['by_oid']:
|
|
|
|
if len(storage['shouts']['by_oid']) > 0:
|
|
|
|
return shout_oid
|
|
|
|
else:
|
|
|
|
print('[migration] no shouts migrated yet')
|
|
|
|
raise Exception
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
with local_session() as session:
|
|
|
|
author = session.query(User).filter(User.oid == entry['createdBy']).first()
|
|
|
|
shout_dict = storage['shouts']['by_oid'][shout_oid]
|
|
|
|
if shout_dict:
|
|
|
|
reaction_dict['shout'] = shout_dict['slug']
|
|
|
|
reaction_dict['createdBy'] = author.slug if author else 'discours'
|
|
|
|
reaction_dict['kind'] = ReactionKind.COMMENT
|
|
|
|
|
|
|
|
# creating reaction from old comment
|
2022-08-13 16:19:16 +00:00
|
|
|
day = (reaction_dict.get('createdAt') or ts).replace(hour=0, minute=0, second=0, microsecond=0)
|
2022-08-11 09:14:12 +00:00
|
|
|
reaction = Reaction.create(**reaction_dict)
|
2022-08-17 09:07:14 +00:00
|
|
|
ReactedStorage.increment(reaction)
|
2022-08-11 09:14:12 +00:00
|
|
|
|
|
|
|
reaction_dict['id'] = reaction.id
|
|
|
|
for comment_rating_old in entry.get('ratings',[]):
|
|
|
|
rater = session.query(User).filter(User.oid == comment_rating_old['createdBy']).first()
|
|
|
|
reactedBy = rater if rater else session.query(User).filter(User.slug == 'noname').first()
|
|
|
|
re_reaction_dict = {
|
|
|
|
'shout': reaction_dict['shout'],
|
|
|
|
'replyTo': reaction.id,
|
|
|
|
'kind': ReactionKind.LIKE if comment_rating_old['value'] > 0 else ReactionKind.DISLIKE,
|
|
|
|
'createdBy': reactedBy.slug if reactedBy else 'discours'
|
|
|
|
}
|
|
|
|
cts = comment_rating_old.get('createdAt')
|
|
|
|
if cts: re_reaction_dict['createdAt'] = date_parse(cts)
|
|
|
|
try:
|
|
|
|
# creating reaction from old rating
|
2022-08-13 16:19:16 +00:00
|
|
|
rr = Reaction.create(**re_reaction_dict)
|
2022-08-17 09:07:14 +00:00
|
|
|
ReactedStorage.increment(rr)
|
2022-08-13 16:19:16 +00:00
|
|
|
|
2022-08-11 09:14:12 +00:00
|
|
|
except Exception as e:
|
|
|
|
print('[migration] comment rating error: %r' % re_reaction_dict)
|
|
|
|
raise e
|
|
|
|
else:
|
|
|
|
print('[migration] error: cannot find shout for comment %r' % reaction_dict)
|
|
|
|
return reaction
|
|
|
|
|
|
|
|
def migrate_2stage(rr, old_new_id):
|
|
|
|
reply_oid = rr.get('replyTo')
|
|
|
|
if not reply_oid: return
|
|
|
|
new_id = old_new_id.get(rr.get('oid'))
|
|
|
|
if not new_id: return
|
|
|
|
with local_session() as session:
|
|
|
|
comment = session.query(Reaction).filter(Reaction.id == new_id).first()
|
|
|
|
comment.replyTo = old_new_id.get(reply_oid)
|
|
|
|
comment.save()
|
|
|
|
session.commit()
|
|
|
|
if not rr['body']: raise Exception(rr)
|