core/migration/tables/topics.py

32 lines
1.0 KiB
Python
Raw Permalink Normal View History

2022-08-11 09:59:35 +00:00
from base.orm import local_session
2022-11-26 15:19:45 +00:00
from migration.html2text import html2text
2022-11-19 11:35:34 +00:00
from orm import Topic
2022-08-11 09:14:12 +00:00
2022-09-04 17:20:38 +00:00
2022-08-11 09:14:12 +00:00
def migrate(entry):
2022-09-04 17:20:38 +00:00
body_orig = entry.get("description", "").replace(" ", " ")
topic_dict = {
"slug": entry["slug"],
"oid": entry["_id"],
2022-11-28 20:29:02 +00:00
"title": entry["title"].replace(" ", " "),
2023-10-30 21:00:55 +00:00
"body": html2text(body_orig),
2022-09-04 17:20:38 +00:00
}
2022-11-28 20:29:02 +00:00
2022-09-04 17:20:38 +00:00
with local_session() as session:
slug = topic_dict["slug"]
topic = session.query(Topic).filter(Topic.slug == slug).first() or Topic.create(
**topic_dict
)
if not topic:
raise Exception("no topic!")
if topic:
if len(topic.title) > len(topic_dict["title"]):
Topic.update(topic, {"title": topic_dict["title"]})
if len(topic.body) < len(topic_dict["body"]):
Topic.update(topic, {"body": topic_dict["body"]})
session.commit()
# print(topic.__dict__)
rt = topic.__dict__.copy()
del rt["_sa_instance_state"]
return rt