core/migration/tables/topics.py

29 lines
1.0 KiB
Python
Raw Normal View History

2022-08-22 14:56:51 +00:00
from logging import exception
2022-08-11 10:06:31 +00:00
from migration.extract import extract_md, html2text
2022-08-11 09:59:35 +00:00
from base.orm import local_session
2022-08-11 09:14:12 +00:00
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']
2022-08-22 14:56:51 +00:00
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()
2022-08-11 09:14:12 +00:00
# print(topic.__dict__)
2022-08-22 14:56:51 +00:00
rt = t.__dict__.copy()
2022-08-11 09:14:12 +00:00
del rt['_sa_instance_state']
return rt