def upgrade_blog(): print('upgrade tag ...') tags = session.execute('select * from tags;') Tag.bulk_save([Tag(id=i.id, name=i.name) for i in tags]) print('upgrade category ...') categories = session.execute('select * from categories;') Category.bulk_save([Category(id=i.id, name=i.name) for i in categories]) print('upgrade blog ...') blogs = session.execute('select * from blogs;') Blog.bulk_save([ Blog(id=blog.id, title=blog.title, content=blog.content, content_type=blog.content_type, is_copy=blog.is_copy, category_id=blog.category_id, user_id=blog.author_id, **date(blog)) for blog in blogs ]) print('upgrade tag_blog ...') tag_blogs = session.execute('select * from tag_blog;') db.engine.execute(tag_blog.insert(), [{ 'tag_id': i.tags_id, 'blog_id': i.blogs_id } for i in tag_blogs])
def write_to_blog(attr, file_type='org'): _author = attr['author'] _category = attr['category'] _tags = attr['tags'].split(',') _title = attr['title'] _date = attr['date'] _content = attr['content'] user = User.query.filter_by(username=_author).first() if not user: user = User(username=_author, email='*****@*****.**') user.set_password('123123') user.save() category = Category.query.filter_by(name=_category).first() if not category: category = Category(name=_category) category.save() tags = [] for _tag in _tags: tag = Tag.query.filter_by(name=_tag).first() if not tag: tag = Tag(name=_tag) tag.save() tags.append(tag) try: _date = datetime.strptime(_date, '%Y-%m-%d'), except ValueError: _date = datetime.strptime(_date, '%Y-%m-%d %H:%M:%S'), blog = Blog.query.filter_by(title=_title).first() if not blog: blog = Blog( title=_title, category_id=category.id, content=_content, created_at=_date, tags=tags, content_type='1' if file_type == 'org' else '0', user_id=user.id) blog.save() return blog
def random_init(): def random_sep(n=6): sep = [' '] * n sep.append("\n") sep.append("\n\n") return choice(sep) def random_word(n=20, sep=True): word = ''.join(sample(ascii_letters + digits, randrange(2, n))) if not sep: return word return word + random_sep() random_users = [ User(username=random_word(12, False), password=random_word(12, False), email=random_word(15, False)) for _ in range(4) ] random_tags = [Tag(name=random_word(12, False)) for i in range(15)] random_categories = [ Category(name=random_word(12, False)) for i in range(5) ] db.session.bulk_save_objects(random_users) db.session.bulk_save_objects(random_tags) db.session.bulk_save_objects(random_categories) db.session.commit() random_users = User.query.all() random_tags = Tag.query.all() for category in Category.query.all(): print(category, category.id) random_blogs = [ Blog(category_id=category.id, title=random_word(20, False), content=' '.join([random_word() for _ in range(1000)]), user_id=choice(random_users).id, tags=[choice(random_tags) for _ in range(randrange(1, 4))]) for _ in range(randrange(3, 15)) ] db.session.bulk_save_objects(random_blogs) db.session.commit() random_timelines = [ TimeLine(content=' '.join([random_word() for _ in range(100)]), user_id=choice(random_users).id, is_hidden=choice([True, False])) for _ in range(100) ] db.session.bulk_save_objects(random_timelines) db.session.commit()