This commit is contained in:
tonyrewin 2023-01-17 09:19:12 +03:00
parent 261b22716b
commit 910c191b0d
4 changed files with 112 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from migration.tables.content_items import get_shout_slug
from migration.tables.content_items import migrate as migrateShout from migration.tables.content_items import migrate as migrateShout
from migration.tables.topics import migrate as migrateTopic from migration.tables.topics import migrate as migrateTopic
from migration.tables.users import migrate as migrateUser from migration.tables.users import migrate as migrateUser
from migration.tables.remarks import migrate as migrateRemark
from migration.tables.users import migrate_2stage as migrateUser_2stage from migration.tables.users import migrate_2stage as migrateUser_2stage
from orm.reaction import Reaction from orm.reaction import Reaction
from orm import init_tables from orm import init_tables
@ -135,6 +136,15 @@ async def shouts_handle(storage, args):
print("[migration] " + str(anonymous_author) + " authored by @anonymous") print("[migration] " + str(anonymous_author) + " authored by @anonymous")
async def remarks_handle(storage):
print("[migration] comments")
c = 0
for entry_remark in storage["remarks"]["data"]:
remark = await migrateRemark(entry_remark)
c += 1
print("[migration] " + str(c) + " remarks migrated")
async def comments_handle(storage): async def comments_handle(storage):
print("[migration] comments") print("[migration] comments")
id_map = {} id_map = {}
@ -169,6 +179,8 @@ async def all_handle(storage, args):
await users_handle(storage) await users_handle(storage)
await topics_handle(storage) await topics_handle(storage)
print("[migration] users and topics are migrated") print("[migration] users and topics are migrated")
await remarks_handle(storage)
print("[migration] remarks are migrated")
await shouts_handle(storage, args) await shouts_handle(storage, args)
print("[migration] migrating comments") print("[migration] migrating comments")
await comments_handle(storage) await comments_handle(storage)
@ -210,6 +222,11 @@ def data_load():
content_data = json.loads(open("migration/data/content_items.json").read()) content_data = json.loads(open("migration/data/content_items.json").read())
storage["shouts"]["data"] = content_data storage["shouts"]["data"] = content_data
print("[migration.load] " + str(len(content_data)) + " content items ") print("[migration.load] " + str(len(content_data)) + " content items ")
remarks_data = json.loads(open("migration/data/remarks.json").read())
storage["remarks"]["data"] = remarks_data
print("[migration.load] " + str(len(remarks_data)) + " remarks data ")
# fill out storage # fill out storage
for x in users_data: for x in users_data:
storage["users"]["by_oid"][x["_id"]] = x storage["users"]["by_oid"][x["_id"]] = x

View File

@ -0,0 +1,32 @@
from base.orm import local_session
from migration.extract import extract_md
from migration.html2text import html2text
from orm.remark import Remark
def migrate(entry):
print(entry)
break
remark = {
"slug": entry["slug"],
"oid": entry["_id"],
"body": extract_md(html2text(
entry['body'] + 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(
**tooltip
)
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

15
orm/remark.py Normal file
View File

@ -0,0 +1,15 @@
from datetime import datetime
from enum import Enum as Enumeration
from sqlalchemy import Column, DateTime, Enum, ForeignKey, String
from base.orm import Base
class Remark(Base):
tablename = "remark"
slug = Column(String, unique=True, nullable=False)
body = Column(String, nullable=False)
shout = Column(ForeignKey("shout.id"), nullable=True, index=True, comment="Shout")

48
resolvers/zine/remark.py Normal file
View File

@ -0,0 +1,48 @@
from datetime import datetime, timedelta, timezone
from sqlalchemy.orm import joinedload, aliased
from sqlalchemy.sql.expression import desc, asc, select, func
from base.orm import local_session
from base.resolvers import query, mutation
from base.exceptions import ObjectNotExist
from orm.remark import Remark
@mutation.field("createRemark")
@login_required
async def create_remark(_, info, slug, body):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
tt = Remark.create(slug=slug, body=body)
session.commit()
return
@mutation.field("updateRemark")
@login_required
async def update_remark(_, info, slug, body = ''):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
rmrk = session.query(Remark).where(Remark.slug == slug).one()
if body:
tt.body = body
session.add(rmrk)
session.commit()
return
@mutation.field("deleteRemark")
@login_required
async def delete_remark(_, info, slug):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
rmrk = session.query(Remark).where(Remark.slug == slug).one()
rmrk.remove()
session.commit()
return
@query.field("loadRemark")
@login_required
async def load_remark(_, info, slug):
pass