Beispiel #1
0
def init_blog_db():
    blog1 = Blog(title="954.二倍数对数组")
    blog1.url_id = gen_blog_url_id()

    cat1 = BlogCategory(name="LeetCode")
    cat2 = BlogCategory(name="Python3")
    cat3 = BlogCategory(name="AI&ML")
    cat4 = BlogCategory(name="C++")

    blog1.category = cat1

    db.session.add_all([blog1])
    db.session.add_all([cat1, cat2, cat3, cat4])
    db.session.commit()
Beispiel #2
0
def create_blog(cat_id):
    if request.method == 'GET':
        cat_all = BlogCategory.query.filter().all()
        blog_cat = BlogCategory.query.filter(BlogCategory.id == int(cat_id)).first()

        dicts = {
            'cat_all': cat_all,
            'note_cat': blog_cat,
            'catid': cat_id
        }
        return render_template('blog/create_blog.html', **dicts)
    else:
        title = request.form.get('docTitle')
        if title:
            new_blog = Blog(title=title)
            new_blog.url_id = gen_blog_url_id()

            username = session.get('username')
            # 指定用户
            if username:
                user = User.query.filter(User.username == username).first()
            else:
                user = User.query.filter().first()
            new_blog.user = user
            # 设定类别
            note_cat = BlogCategory.query.filter(BlogCategory.id == int(cat_id)).first()
            new_blog.category = note_cat

            # 记录内容
            content = request.form.get('docContent')
            path = FILESERVER + PATHSEP + 'blogs' + PATHSEP + cat_id + secure_filename(''.join(lazy_pinyin(note_cat.name))) + PATHSEP
            full_filename = path + secure_filename(''.join(lazy_pinyin(title))) + '.md'
            if not os.path.exists(path):
                os.makedirs(path)
            sec_writefile(full_filename, content)
            new_blog.content_path = full_filename
            # 更新数据库
            db.session.add(new_blog)
            db.session.commit()

        return redirect(url_for('main.blogs_cat', note_catid=int(cat_id)))