From b966ce6c2472a982ce793ef251ce0b718b97e12c 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 +- resolvers/zine/profile.py | 13 ++++++------ resolvers/zine/reactions.py | 3 +-- 6 files changed, 35 insertions(+), 35 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 diff --git a/resolvers/zine/profile.py b/resolvers/zine/profile.py index 8cd68453..c70106df 100644 --- a/resolvers/zine/profile.py +++ b/resolvers/zine/profile.py @@ -162,16 +162,15 @@ async def update_profile(_, info, profile): user_id = auth.user_id with local_session() as session: user = session.query(User).filter(User.id == user_id).one() - slugowner = session.query(User).where(User.slug == profile['slug']).one() - if slugowner: - if slugowner.id != user_id: - return { - "error": "slug is used by another user" - } + if not user: + return { + "error": "canoot find user" + } user.update(profile) session.commit() return { - "error": None + "error": None, + "author": user } diff --git a/resolvers/zine/reactions.py b/resolvers/zine/reactions.py index 40c912a5..56bcafca 100644 --- a/resolvers/zine/reactions.py +++ b/resolvers/zine/reactions.py @@ -198,11 +198,10 @@ async def delete_reaction(_, info, reaction=None): auth: AuthCredentials = info.context["request"].auth with local_session() as session: - user = session.query(User).where(User.id == auth.user_id).first() r = session.query(Reaction).filter(Reaction.id == reaction).first() if not r: return {"error": "invalid reaction id"} - if r.createdBy != user.id: + if r.createdBy != auth.user_id: return {"error": "access denied"} r.deletedAt = datetime.now(tz=timezone.utc) session.commit()