From e38ab0a0664ce5eba668cf089bde29b1d15a9bfb Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Tue, 17 Jan 2023 12:11:18 +0300 Subject: [PATCH] [api] remarks & drafts/collabs + migrations --- migration/__init__.py | 7 ++++--- migration/tables/remarks.py | 41 +++++++++++++++++++------------------ orm/remark.py | 4 ++-- resolvers/create/collab.py | 2 +- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/migration/__init__.py b/migration/__init__.py index 0737f6c5..c59e3ec4 100644 --- a/migration/__init__.py +++ b/migration/__init__.py @@ -140,7 +140,7 @@ async def remarks_handle(storage): print("[migration] comments") c = 0 for entry_remark in storage["remarks"]["data"]: - remark = await migrateRemark(entry_remark) + remark = await migrateRemark(entry_remark, storage) c += 1 print("[migration] " + str(c) + " remarks migrated") @@ -179,9 +179,9 @@ async def all_handle(storage, args): await users_handle(storage) await topics_handle(storage) print("[migration] users and topics are migrated") - await remarks_handle(storage) - print("[migration] remarks are migrated") await shouts_handle(storage, args) + print("[migration] remarks...") + await remarks_handle(storage) print("[migration] migrating comments") await comments_handle(storage) # export_email_subscriptions() @@ -202,6 +202,7 @@ def data_load(): "cats": [], "tags": [], }, + "remarks": {"data": []}, "users": {"by_oid": {}, "by_slug": {}, "data": []}, "replacements": json.loads(open("migration/tables/replacements.json").read()), } diff --git a/migration/tables/remarks.py b/migration/tables/remarks.py index 247d183f..78f52c92 100644 --- a/migration/tables/remarks.py +++ b/migration/tables/remarks.py @@ -4,27 +4,28 @@ from migration.html2text import html2text from orm.remark import Remark -def migrate(entry): +def migrate(entry, storage): + post_oid = entry['contentItem'] + print(post_oid) + shout_dict = storage['shouts']['by_oid'].get(post_oid) remark = { - "slug": entry["slug"], - "oid": entry["_id"], - "body": extract_md(html2text( - entry['body'] + entry['textAfter'] or '' + \ - entry['textBefore'] or '' + \ - entry['textSelected'] or '' - ), entry["_id"]) + "shout": shout_dict['id'], + "body": extract_md( + html2text(entry['body']), + entry['_id'] + ), + "desc": extract_md( + html2text( + entry['textAfter'] or '' + \ + entry['textBefore'] or '' + \ + entry['textSelected'] or '' + ), + entry["_id"] + ) } with local_session() as session: - slug = remark["slug"] - rmrk = session.query(Remark).filter(Remark.slug == slug).first() or Remark.create( - **remark - ) - if not rmrk: - raise Exception("no rmrk!") - if rmrk: - Remark.update(rmrk, remark) - session.commit() - rt = tt.__dict__.copy() - del rt["_sa_instance_state"] - return rt + rmrk = Remark.create(**remark) + session.commit() + del rmrk["_sa_instance_state"] + return rmrk diff --git a/orm/remark.py b/orm/remark.py index 89a6494a..9432a3f5 100644 --- a/orm/remark.py +++ b/orm/remark.py @@ -8,8 +8,8 @@ from base.orm import Base class Remark(Base): - tablename = "remark" + __tablename__ = "remark" - slug = Column(String, unique=True, nullable=False) body = Column(String, nullable=False) + desc = Column(String, default='') shout = Column(ForeignKey("shout.id"), nullable=True, index=True, comment="Shout") diff --git a/resolvers/create/collab.py b/resolvers/create/collab.py index 3fe4a169..f8b1c8a7 100644 --- a/resolvers/create/collab.py +++ b/resolvers/create/collab.py @@ -72,7 +72,7 @@ async def update_draft(_, info, draft_input): } else: draft_input["updatedAt"] = datetime.now(tz=timezone.utc) - collab.update(**draft_input) + collab.update(draft_input) session.commit() # TODO: email notify