formatted, linted, fixed
This commit is contained in:
@@ -1,28 +1,32 @@
|
||||
from logging import exception
|
||||
from migration.extract import extract_md, html2text
|
||||
from base.orm import local_session
|
||||
from orm import Topic, Community
|
||||
|
||||
|
||||
def migrate(entry):
|
||||
body_orig = entry.get('description', '').replace(' ', ' ')
|
||||
topic_dict = {
|
||||
'slug': entry['slug'],
|
||||
'oid': entry['_id'],
|
||||
'title': entry['title'].replace(' ', ' '), #.lower(),
|
||||
'children': [],
|
||||
'community' : Community.default_community.slug
|
||||
}
|
||||
topic_dict['body'] = extract_md(html2text(body_orig), entry['_id'])
|
||||
with local_session() as session:
|
||||
slug = topic_dict['slug']
|
||||
t: Topic = session.query(Topic).filter(Topic.slug == slug).first() or Topic.create(**topic_d) or raise Exception('topic not created') # type: ignore
|
||||
if t:
|
||||
if len(t.title) > len(topic_dict['title']):
|
||||
Topic.update(t, {'title': topic_dict['title']})
|
||||
if len(t.body) < len(topic_dict['body']):
|
||||
Topic.update(t, { 'body': topic_dict['body'] })
|
||||
session.commit()
|
||||
# print(topic.__dict__)
|
||||
rt = t.__dict__.copy()
|
||||
del rt['_sa_instance_state']
|
||||
return rt
|
||||
body_orig = entry.get("description", "").replace(" ", " ")
|
||||
topic_dict = {
|
||||
"slug": entry["slug"],
|
||||
"oid": entry["_id"],
|
||||
"title": entry["title"].replace(" ", " "),
|
||||
"children": [],
|
||||
"community": Community.default_community.slug,
|
||||
}
|
||||
topic_dict["body"] = extract_md(html2text(body_orig), entry["_id"])
|
||||
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
|
||||
|
Reference in New Issue
Block a user