2022-11-13 17:58:47 +00:00
|
|
|
import json
|
2023-10-26 17:56:42 +00:00
|
|
|
import re
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
2022-08-11 09:14:12 +00:00
|
|
|
from dateutil.parser import parse as date_parse
|
2022-09-17 18:12:14 +00:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
2022-08-11 09:14:12 +00:00
|
|
|
from transliterate import translit
|
2023-10-26 17:56:42 +00:00
|
|
|
|
2022-08-11 09:59:35 +00:00
|
|
|
from base.orm import local_session
|
2022-11-26 15:19:45 +00:00
|
|
|
from migration.extract import extract_html, extract_media
|
2022-08-11 09:14:12 +00:00
|
|
|
from orm.reaction import Reaction, ReactionKind
|
2023-10-26 17:56:42 +00:00
|
|
|
from orm.shout import Shout, ShoutReactionsFollower, ShoutTopic
|
|
|
|
from orm.topic import Topic, TopicFollower
|
2022-09-19 13:50:43 +00:00
|
|
|
from orm.user import User
|
2022-11-18 18:22:10 +00:00
|
|
|
from services.stat.viewed import ViewedStorage
|
2022-08-11 09:14:12 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
OLD_DATE = "2016-03-05 22:22:00.350000"
|
2022-11-23 14:09:35 +00:00
|
|
|
ts = datetime.now(tz=timezone.utc)
|
2022-08-11 09:14:12 +00:00
|
|
|
type2layout = {
|
2022-11-13 05:14:28 +00:00
|
|
|
"Article": "article",
|
2022-11-13 04:49:20 +00:00
|
|
|
"Literature": "literature",
|
2023-10-10 13:37:28 +00:00
|
|
|
"Music": "music",
|
2022-09-03 10:50:14 +00:00
|
|
|
"Video": "video",
|
|
|
|
"Image": "image",
|
2022-08-11 09:14:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-30 19:47:34 +00:00
|
|
|
anondict = {"slug": "anonymous", "id": 1, "name": "Аноним"}
|
2022-12-13 12:01:42 +00:00
|
|
|
discours = {"slug": "discours", "id": 2, "name": "Дискурс"}
|
2022-11-30 19:47:34 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-08-11 09:14:12 +00:00
|
|
|
def get_shout_slug(entry):
|
2022-09-03 10:50:14 +00:00
|
|
|
slug = entry.get("slug", "")
|
|
|
|
if not slug:
|
|
|
|
for friend in entry.get("friendlySlugs", []):
|
|
|
|
slug = friend.get("slug", "")
|
|
|
|
if slug:
|
|
|
|
break
|
2022-11-30 19:47:34 +00:00
|
|
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
2022-09-03 10:50:14 +00:00
|
|
|
return slug
|
|
|
|
|
2022-08-11 09:14:12 +00:00
|
|
|
|
2022-09-18 22:11:26 +00:00
|
|
|
def create_author_from_app(app):
|
2023-01-25 11:55:12 +00:00
|
|
|
user = None
|
2022-12-13 12:01:42 +00:00
|
|
|
userdata = None
|
2023-01-31 17:17:02 +00:00
|
|
|
# check if email is used
|
|
|
|
if app['email']:
|
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).where(User.email == app['email']).first()
|
|
|
|
if not user:
|
|
|
|
# print('[migration] app %r' % app)
|
|
|
|
name = app.get('name')
|
|
|
|
if name:
|
|
|
|
slug = translit(name, "ru", reversed=True).lower()
|
|
|
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
|
|
|
print('[migration] created slug %s' % slug)
|
|
|
|
# check if slug is used
|
|
|
|
if slug:
|
2023-01-25 11:55:12 +00:00
|
|
|
user = session.query(User).where(User.slug == slug).first()
|
2023-01-31 17:17:02 +00:00
|
|
|
|
|
|
|
# get slug from email
|
2023-01-25 11:55:12 +00:00
|
|
|
if user:
|
2023-01-31 17:17:02 +00:00
|
|
|
slug = app['email'].split('@')[0]
|
2023-01-25 06:32:59 +00:00
|
|
|
user = session.query(User).where(User.slug == slug).first()
|
2023-01-31 17:17:02 +00:00
|
|
|
# one more try
|
|
|
|
if user:
|
|
|
|
slug += '-author'
|
|
|
|
user = session.query(User).where(User.slug == slug).first()
|
|
|
|
|
|
|
|
# create user with application data
|
|
|
|
if not user:
|
|
|
|
userdata = {
|
|
|
|
"username": app["email"],
|
|
|
|
"email": app["email"],
|
|
|
|
"name": app.get("name", ""),
|
|
|
|
"emailConfirmed": False,
|
|
|
|
"slug": slug,
|
|
|
|
"createdAt": ts,
|
|
|
|
"lastSeen": ts,
|
|
|
|
}
|
|
|
|
# print('[migration] userdata %r' % userdata)
|
|
|
|
user = User.create(**userdata)
|
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
|
|
|
userdata['id'] = user.id
|
|
|
|
|
|
|
|
userdata = user.dict()
|
2022-12-13 12:01:42 +00:00
|
|
|
return userdata
|
2023-01-31 17:17:02 +00:00
|
|
|
else:
|
|
|
|
raise Exception("app is not ok", app)
|
2022-09-18 22:11:26 +00:00
|
|
|
|
|
|
|
|
2022-12-13 12:01:42 +00:00
|
|
|
async def create_shout(shout_dict):
|
2022-09-18 18:10:57 +00:00
|
|
|
s = Shout.create(**shout_dict)
|
2023-01-25 06:32:59 +00:00
|
|
|
author = s.authors[0]
|
2022-09-18 18:10:57 +00:00
|
|
|
with local_session() as session:
|
2023-10-26 17:56:42 +00:00
|
|
|
srf = (
|
|
|
|
session.query(ShoutReactionsFollower)
|
|
|
|
.where(ShoutReactionsFollower.shout == s.id)
|
|
|
|
.filter(ShoutReactionsFollower.follower == author.id)
|
|
|
|
.first()
|
|
|
|
)
|
2022-09-18 18:10:57 +00:00
|
|
|
if not srf:
|
2022-12-13 12:01:42 +00:00
|
|
|
srf = ShoutReactionsFollower.create(shout=s.id, follower=author.id, auto=True)
|
2022-09-18 18:10:57 +00:00
|
|
|
session.add(srf)
|
|
|
|
session.commit()
|
2022-11-30 19:47:34 +00:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
2022-12-13 12:01:42 +00:00
|
|
|
async def get_user(entry, storage):
|
2023-01-25 11:55:12 +00:00
|
|
|
app = entry.get("application")
|
|
|
|
userdata = None
|
2023-01-25 15:00:39 +00:00
|
|
|
user_oid = None
|
2023-01-25 11:55:12 +00:00
|
|
|
if app:
|
|
|
|
userdata = create_author_from_app(app)
|
2023-01-31 17:17:02 +00:00
|
|
|
else:
|
2023-01-25 15:00:39 +00:00
|
|
|
user_oid = entry.get("createdBy")
|
|
|
|
if user_oid == "0":
|
|
|
|
userdata = discours
|
|
|
|
elif user_oid:
|
|
|
|
userdata = storage["users"]["by_oid"].get(user_oid)
|
2023-01-31 17:17:02 +00:00
|
|
|
if not userdata:
|
|
|
|
print('no userdata by oid, anonymous')
|
2023-01-25 15:00:39 +00:00
|
|
|
userdata = anondict
|
|
|
|
print(app)
|
2022-12-13 12:01:42 +00:00
|
|
|
# cleanup slug
|
2023-01-25 15:00:39 +00:00
|
|
|
if userdata:
|
|
|
|
slug = userdata.get("slug", "")
|
|
|
|
if slug:
|
|
|
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
|
|
|
userdata["slug"] = slug
|
|
|
|
else:
|
|
|
|
userdata = anondict
|
2022-12-13 12:01:42 +00:00
|
|
|
|
|
|
|
user = await process_user(userdata, storage, user_oid)
|
|
|
|
return user, user_oid
|
2022-09-18 18:10:57 +00:00
|
|
|
|
2023-01-25 15:00:39 +00:00
|
|
|
|
2022-08-18 06:12:46 +00:00
|
|
|
async def migrate(entry, storage):
|
2022-12-13 12:01:42 +00:00
|
|
|
author, user_oid = await get_user(entry, storage)
|
2022-09-03 10:50:14 +00:00
|
|
|
r = {
|
|
|
|
"layout": type2layout[entry["type"]],
|
|
|
|
"title": entry["title"],
|
2023-10-26 17:56:42 +00:00
|
|
|
"authors": [
|
|
|
|
author,
|
|
|
|
],
|
2022-11-29 11:51:06 +00:00
|
|
|
"slug": get_shout_slug(entry),
|
|
|
|
"cover": (
|
2023-10-26 17:56:42 +00:00
|
|
|
"https://images.discours.io/unsafe/" + entry["thumborId"]
|
|
|
|
if entry.get("thumborId")
|
|
|
|
else entry.get("image", {}).get("url")
|
2022-11-29 11:51:06 +00:00
|
|
|
),
|
2023-10-25 20:38:22 +00:00
|
|
|
"visibility": "public" if entry.get("published") else "community",
|
2022-11-29 11:51:06 +00:00
|
|
|
"publishedAt": date_parse(entry.get("publishedAt")) if entry.get("published") else None,
|
|
|
|
"deletedAt": date_parse(entry.get("deletedAt")) if entry.get("deletedAt") else None,
|
|
|
|
"createdAt": date_parse(entry.get("createdAt", OLD_DATE)),
|
|
|
|
"updatedAt": date_parse(entry["updatedAt"]) if "updatedAt" in entry else ts,
|
2023-10-09 10:52:13 +00:00
|
|
|
"createdBy": author.id,
|
2022-12-13 12:01:42 +00:00
|
|
|
"topics": await add_topics_follower(entry, storage, author),
|
2023-10-26 17:56:42 +00:00
|
|
|
"body": extract_html(entry, cleanup=True),
|
2022-09-03 10:50:14 +00:00
|
|
|
}
|
2022-11-27 08:19:38 +00:00
|
|
|
|
2022-11-29 11:51:06 +00:00
|
|
|
# main topic patch
|
|
|
|
r['mainTopic'] = r['topics'][0]
|
2022-11-27 08:19:38 +00:00
|
|
|
|
2022-11-29 11:51:06 +00:00
|
|
|
# published author auto-confirm
|
2022-09-03 10:50:14 +00:00
|
|
|
if entry.get("published"):
|
2022-11-16 09:23:32 +00:00
|
|
|
with local_session() as session:
|
|
|
|
# update user.emailConfirmed if published
|
|
|
|
author.emailConfirmed = True
|
|
|
|
session.add(author)
|
|
|
|
session.commit()
|
2022-11-27 08:19:38 +00:00
|
|
|
|
2022-11-29 11:51:06 +00:00
|
|
|
# media
|
2022-11-27 08:19:38 +00:00
|
|
|
media = extract_media(entry)
|
2022-11-29 11:51:06 +00:00
|
|
|
r["media"] = json.dumps(media, ensure_ascii=True) if media else None
|
2022-11-27 08:19:38 +00:00
|
|
|
|
2022-11-29 11:51:06 +00:00
|
|
|
# ----------------------------------- copy
|
2022-11-27 08:19:38 +00:00
|
|
|
shout_dict = r.copy()
|
|
|
|
del shout_dict["topics"]
|
2022-12-13 12:01:42 +00:00
|
|
|
|
2022-11-27 08:19:38 +00:00
|
|
|
try:
|
|
|
|
# save shout to db
|
2022-11-30 19:47:34 +00:00
|
|
|
shout_dict["oid"] = entry.get("_id", "")
|
2022-12-13 12:01:42 +00:00
|
|
|
shout = await create_shout(shout_dict)
|
2022-11-27 08:19:38 +00:00
|
|
|
except IntegrityError as e:
|
2022-11-30 19:47:34 +00:00
|
|
|
print('[migration] create_shout integrity error', e)
|
2022-12-13 12:01:42 +00:00
|
|
|
shout = await resolve_create_shout(shout_dict)
|
2022-11-27 08:19:38 +00:00
|
|
|
except Exception as e:
|
|
|
|
raise Exception(e)
|
|
|
|
|
2022-11-30 19:47:34 +00:00
|
|
|
# udpate data
|
|
|
|
shout_dict = shout.dict()
|
2023-10-26 17:56:42 +00:00
|
|
|
shout_dict["authors"] = [
|
|
|
|
author.dict(),
|
|
|
|
]
|
2022-11-30 19:47:34 +00:00
|
|
|
|
2022-11-27 08:19:38 +00:00
|
|
|
# shout topics aftermath
|
|
|
|
shout_dict["topics"] = await topics_aftermath(r, storage)
|
|
|
|
|
|
|
|
# content_item ratings to reactions
|
|
|
|
await content_ratings_to_reactions(entry, shout_dict["slug"])
|
|
|
|
|
|
|
|
# shout views
|
2023-10-26 17:56:42 +00:00
|
|
|
await ViewedStorage.increment(
|
|
|
|
shout_dict["slug"], amount=entry.get("views", 1), viewer='old-discours'
|
|
|
|
)
|
2022-11-27 08:19:38 +00:00
|
|
|
# del shout_dict['ratings']
|
|
|
|
|
|
|
|
storage["shouts"]["by_oid"][entry["_id"]] = shout_dict
|
2022-11-29 11:51:06 +00:00
|
|
|
storage["shouts"]["by_slug"][shout_dict["slug"]] = shout_dict
|
2022-11-27 08:19:38 +00:00
|
|
|
return shout_dict
|
|
|
|
|
|
|
|
|
2022-11-30 19:47:34 +00:00
|
|
|
async def add_topics_follower(entry, storage, user):
|
2022-11-27 08:19:38 +00:00
|
|
|
topics = set([])
|
|
|
|
category = entry.get("category")
|
|
|
|
topics_by_oid = storage["topics"]["by_oid"]
|
2023-10-26 17:56:42 +00:00
|
|
|
oids = [
|
|
|
|
category,
|
|
|
|
] + entry.get("tags", [])
|
2022-11-27 08:19:38 +00:00
|
|
|
for toid in oids:
|
|
|
|
tslug = topics_by_oid.get(toid, {}).get("slug")
|
|
|
|
if tslug:
|
|
|
|
topics.add(tslug)
|
|
|
|
ttt = list(topics)
|
2022-09-19 15:24:43 +00:00
|
|
|
# add author as TopicFollower
|
|
|
|
with local_session() as session:
|
2022-11-30 19:47:34 +00:00
|
|
|
for tpcslug in topics:
|
2022-11-19 11:35:34 +00:00
|
|
|
try:
|
2022-11-30 19:47:34 +00:00
|
|
|
tpc = session.query(Topic).where(Topic.slug == tpcslug).first()
|
2022-12-01 08:12:48 +00:00
|
|
|
if tpc:
|
2023-10-26 17:56:42 +00:00
|
|
|
tf = (
|
|
|
|
session.query(TopicFollower)
|
|
|
|
.where(TopicFollower.follower == user.id)
|
|
|
|
.filter(TopicFollower.topic == tpc.id)
|
|
|
|
.first()
|
|
|
|
)
|
2022-12-01 08:12:48 +00:00
|
|
|
if not tf:
|
2023-10-26 17:56:42 +00:00
|
|
|
tf = TopicFollower.create(topic=tpc.id, follower=user.id, auto=True)
|
2022-12-01 08:12:48 +00:00
|
|
|
session.add(tf)
|
|
|
|
session.commit()
|
2022-11-19 11:35:34 +00:00
|
|
|
except IntegrityError:
|
2022-11-30 19:47:34 +00:00
|
|
|
print('[migration.shout] hidden by topic ' + tpc.slug)
|
2022-11-27 08:19:38 +00:00
|
|
|
# main topic
|
|
|
|
maintopic = storage["replacements"].get(topics_by_oid.get(category, {}).get("slug"))
|
|
|
|
if maintopic in ttt:
|
|
|
|
ttt.remove(maintopic)
|
|
|
|
ttt.insert(0, maintopic)
|
|
|
|
return ttt
|
2022-09-19 15:24:43 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-12-13 12:01:42 +00:00
|
|
|
async def process_user(userdata, storage, oid):
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
2023-01-25 15:00:39 +00:00
|
|
|
uid = userdata.get("id") # anonymous as
|
|
|
|
if not uid:
|
|
|
|
print(userdata)
|
|
|
|
print("has no id field, set it @anonymous")
|
|
|
|
userdata = anondict
|
|
|
|
uid = 1
|
2022-12-13 12:01:42 +00:00
|
|
|
user = session.query(User).filter(User.id == uid).first()
|
|
|
|
if not user:
|
2022-09-03 10:50:14 +00:00
|
|
|
try:
|
2022-11-30 19:47:34 +00:00
|
|
|
slug = userdata["slug"].lower().strip()
|
|
|
|
slug = re.sub('[^0-9a-zA-Z]+', '-', slug)
|
|
|
|
userdata["slug"] = slug
|
2022-09-03 10:50:14 +00:00
|
|
|
user = User.create(**userdata)
|
2022-09-18 22:11:26 +00:00
|
|
|
session.add(user)
|
|
|
|
session.commit()
|
2022-09-17 18:12:14 +00:00
|
|
|
except IntegrityError:
|
2022-12-13 12:01:42 +00:00
|
|
|
print(f"[migration] user creating with slug {userdata['slug']}")
|
|
|
|
print("[migration] from userdata")
|
|
|
|
print(userdata)
|
2022-11-30 19:47:34 +00:00
|
|
|
raise Exception("[migration] cannot create user in content_items.get_user()")
|
2022-12-13 12:01:42 +00:00
|
|
|
if user.id == 946:
|
|
|
|
print("[migration] ***************** ALPINA")
|
|
|
|
if user.id == 2:
|
|
|
|
print("[migration] +++++++++++++++++ DISCOURS")
|
2022-11-30 19:47:34 +00:00
|
|
|
userdata["id"] = user.id
|
|
|
|
userdata["createdAt"] = user.createdAt
|
|
|
|
storage["users"]["by_slug"][userdata["slug"]] = userdata
|
|
|
|
storage["users"]["by_oid"][oid] = userdata
|
2022-12-13 12:01:42 +00:00
|
|
|
if not user:
|
|
|
|
raise Exception("could not get a user")
|
|
|
|
return user
|
2022-11-27 08:19:38 +00:00
|
|
|
|
|
|
|
|
2022-12-13 12:01:42 +00:00
|
|
|
async def resolve_create_shout(shout_dict):
|
2022-11-27 08:19:38 +00:00
|
|
|
with local_session() as session:
|
|
|
|
s = session.query(Shout).filter(Shout.slug == shout_dict["slug"]).first()
|
|
|
|
bump = False
|
|
|
|
if s:
|
2022-12-13 12:01:42 +00:00
|
|
|
if s.createdAt != shout_dict['createdAt']:
|
2022-11-27 08:19:38 +00:00
|
|
|
# create new with different slug
|
|
|
|
shout_dict["slug"] += '-' + shout_dict["layout"]
|
|
|
|
try:
|
2022-12-13 12:01:42 +00:00
|
|
|
await create_shout(shout_dict)
|
2022-11-27 08:19:38 +00:00
|
|
|
except IntegrityError as e:
|
|
|
|
print(e)
|
|
|
|
bump = True
|
2022-09-03 10:50:14 +00:00
|
|
|
else:
|
2022-11-27 08:19:38 +00:00
|
|
|
# update old
|
|
|
|
for key in shout_dict:
|
|
|
|
if key in s.__dict__:
|
|
|
|
if s.__dict__[key] != shout_dict[key]:
|
2023-10-26 17:56:42 +00:00
|
|
|
print("[migration] shout already exists, but differs in %s" % key)
|
2022-11-27 08:19:38 +00:00
|
|
|
bump = True
|
|
|
|
else:
|
|
|
|
print("[migration] shout already exists, but lacks %s" % key)
|
|
|
|
bump = True
|
|
|
|
if bump:
|
|
|
|
s.update(shout_dict)
|
|
|
|
else:
|
|
|
|
print("[migration] something went wrong with shout: \n%r" % shout_dict)
|
|
|
|
raise Exception("")
|
|
|
|
session.commit()
|
2022-11-30 19:47:34 +00:00
|
|
|
return s
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-11-27 08:19:38 +00:00
|
|
|
|
|
|
|
async def topics_aftermath(entry, storage):
|
|
|
|
r = []
|
|
|
|
for tpc in filter(lambda x: bool(x), entry["topics"]):
|
2022-09-03 10:50:14 +00:00
|
|
|
oldslug = tpc
|
|
|
|
newslug = storage["replacements"].get(oldslug, oldslug)
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
if newslug:
|
|
|
|
with local_session() as session:
|
2022-12-13 12:01:42 +00:00
|
|
|
shout = session.query(Shout).where(Shout.slug == entry["slug"]).first()
|
|
|
|
new_topic = session.query(Topic).where(Topic.slug == newslug).first()
|
2022-11-29 12:36:46 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
shout_topic_old = (
|
|
|
|
session.query(ShoutTopic)
|
2022-11-30 19:47:34 +00:00
|
|
|
.join(Shout)
|
|
|
|
.join(Topic)
|
|
|
|
.filter(Shout.slug == entry["slug"])
|
|
|
|
.filter(Topic.slug == oldslug)
|
2022-09-03 10:50:14 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if shout_topic_old:
|
2022-11-30 06:27:12 +00:00
|
|
|
shout_topic_old.update({"topic": new_topic.id})
|
2022-09-03 10:50:14 +00:00
|
|
|
else:
|
|
|
|
shout_topic_new = (
|
|
|
|
session.query(ShoutTopic)
|
2022-11-30 19:47:34 +00:00
|
|
|
.join(Shout)
|
|
|
|
.join(Topic)
|
|
|
|
.filter(Shout.slug == entry["slug"])
|
|
|
|
.filter(Topic.slug == newslug)
|
2022-09-03 10:50:14 +00:00
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if not shout_topic_new:
|
|
|
|
try:
|
2023-10-26 17:56:42 +00:00
|
|
|
ShoutTopic.create(**{"shout": shout.id, "topic": new_topic.id})
|
2022-09-05 02:09:44 +00:00
|
|
|
except Exception:
|
2022-09-03 10:50:14 +00:00
|
|
|
print("[migration] shout topic error: " + newslug)
|
|
|
|
session.commit()
|
2022-11-27 08:19:38 +00:00
|
|
|
if newslug not in r:
|
|
|
|
r.append(newslug)
|
2022-09-03 10:50:14 +00:00
|
|
|
else:
|
|
|
|
print("[migration] ignored topic slug: \n%r" % tpc["slug"])
|
|
|
|
# raise Exception
|
2022-11-27 08:19:38 +00:00
|
|
|
return r
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-11-27 08:19:38 +00:00
|
|
|
|
|
|
|
async def content_ratings_to_reactions(entry, slug):
|
2022-09-03 10:50:14 +00:00
|
|
|
try:
|
2022-10-14 09:25:45 +00:00
|
|
|
with local_session() as session:
|
|
|
|
for content_rating in entry.get("ratings", []):
|
2022-09-03 10:50:14 +00:00
|
|
|
rater = (
|
2023-10-26 17:56:42 +00:00
|
|
|
session.query(User).filter(User.oid == content_rating["createdBy"]).first()
|
2022-11-29 11:51:06 +00:00
|
|
|
) or User.default_user
|
2022-11-30 19:47:34 +00:00
|
|
|
shout = session.query(Shout).where(Shout.slug == slug).first()
|
2022-11-29 11:51:06 +00:00
|
|
|
cts = content_rating.get("createdAt")
|
|
|
|
reaction_dict = {
|
|
|
|
"createdAt": date_parse(cts) if cts else None,
|
|
|
|
"kind": ReactionKind.LIKE
|
|
|
|
if content_rating["value"] > 0
|
|
|
|
else ReactionKind.DISLIKE,
|
2022-11-30 19:47:34 +00:00
|
|
|
"createdBy": rater.id,
|
2023-10-26 17:56:42 +00:00
|
|
|
"shout": shout.id,
|
2022-11-29 11:51:06 +00:00
|
|
|
}
|
|
|
|
reaction = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.shout == reaction_dict["shout"])
|
|
|
|
.filter(Reaction.createdBy == reaction_dict["createdBy"])
|
|
|
|
.filter(Reaction.kind == reaction_dict["kind"])
|
|
|
|
.first()
|
2022-09-03 10:50:14 +00:00
|
|
|
)
|
2022-11-29 11:51:06 +00:00
|
|
|
if reaction:
|
|
|
|
k = ReactionKind.AGREE if content_rating["value"] > 0 else ReactionKind.DISAGREE
|
|
|
|
reaction_dict["kind"] = k
|
|
|
|
reaction.update(reaction_dict)
|
|
|
|
session.add(reaction)
|
|
|
|
else:
|
|
|
|
rea = Reaction.create(**reaction_dict)
|
|
|
|
session.add(rea)
|
|
|
|
# shout_dict['ratings'].append(reaction_dict)
|
2022-10-14 09:25:45 +00:00
|
|
|
|
|
|
|
session.commit()
|
2022-09-05 02:09:44 +00:00
|
|
|
except Exception:
|
2022-11-29 11:51:06 +00:00
|
|
|
print("[migration] content_item.ratings error: \n%r" % content_rating)
|