Beispiel #1
0
def save_blog_img(filename, form):
    current_time = get_current_time()
    current_time = current_time.split(' ')[0]
    create_path(basedir + '/uploads/image/' + current_time)
    # 将博客示例图片存储到对应的文件夹中
    form.blog_img_file.data.save(basedir + '/uploads/image/' + current_time + '/' + filename)
    blog_img_path = '/backend/blog/img/' + current_time + '/' + filename
    return blog_img_path
Beispiel #2
0
def blog_create():
    form = PostForm()
    drafts = DraftBlog.query.filter_by(tag=1).all()
    draft_id = request.args.get('draft-id')
    if request.method == 'GET':
        if draft_id:
            c_draft = DraftBlog.query.get_or_404(draft_id)
            form.title.data = c_draft.title
            form.body.data = c_draft.content
            form.brief_content.data = c_draft.brief
        return render_template('backend/create-blog.html',
                               drafts=drafts,
                               form=form,
                               draft_id=draft_id)

    if form.validate_on_submit():
        # 获取表单中信息
        title = form.title.data
        content = form.body.data
        introduce = form.brief_content.data
        filename = form.blog_img_file.data.filename
        _type = form.blog_type.data
        level = form.blog_level.data
        bs = BeautifulSoup(content, 'html.parser')
        catalogue = [
            link.get('id') for link in bs.find_all('a') if link.get('id')
        ]

        current_time = get_current_time()
        current_time = current_time.split(' ')[0]
        create_path(basedir + '/uploads/image/' + current_time)
        # 将博客示例图片存储到对应的文件夹中
        form.blog_img_file.data.save(basedir + '/uploads/image/' +
                                     current_time + '/' + filename)
        blog_img_path = '/backend/blog/img/' + current_time + '/' + filename

        cate = BlogType.query.filter_by(id=_type).first()
        state = States.query.get_or_404(1)
        blg = Blog(title=title,
                   type_id=cate.id,
                   introduce=introduce,
                   content=content,
                   pre_img=blog_img_path,
                   is_private=level - 1,
                   state=state)
        cate.counts += 1
        update_contribution()
        db.session.add(blg)
        db.session.add(PostContent(content=str(catalogue), post_id=blg.id))
        db.session.commit()
        return redirect(url_for('blog_bp.index'))
    else:
        flash('不能提交包含空的表单!', 'danger')
        return render_template('backend/create-blog.html',
                               form=form,
                               drafts=drafts,
                               draft_id=draft_id)
Beispiel #3
0
def blog_content_edit(blog_id):
    form = EditPostForm()
    blog = Blog.query.get_or_404(blog_id)
    if form.validate_on_submit():
        history_content = blog.content
        filename = form.blog_img_file.data.filename
        if filename != '':
            blog_img_path = save_blog_img(filename, form)
            blog.pre_img = blog_img_path

        blog.content = form.body.data
        blog.title = form.title.data
        type = form.blog_type.data
        cate = BlogType.query.filter_by(id=type).first()
        blog.type_id = cate.id
        blog.introduce = form.brief_content.data
        blog.update_time = datetime.datetime.now()
        bs = BeautifulSoup(form.body.data, 'html.parser')
        catalogue = [
            link.get('id') for link in bs.find_all('a') if link.get('id')
        ]
        post_cate = PostContent.query.filter_by(post_id=blog.id).first()
        if post_cate:
            post_cate.content = str(catalogue)
        else:
            db.session.add(PostContent(content=str(catalogue),
                                       post_id=blog.id))

        update_contribution()
        history_file_path = basedir + '/history/' + get_md5(
            get_current_time()) + '.txt'
        with open(history_file_path, 'w', encoding='utf-8') as f:
            f.write(history_content)
        bh = BlogHistory(blog_id=blog.id,
                         save_path=history_file_path,
                         timestamps=datetime.datetime.now())
        db.session.add(bh)
        db.session.commit()

        return redirect(url_for('blog_bp.blog_article', blog_id=blog_id))

    form.title.data = blog.title
    form.blog_type.data = blog.type_id
    form.brief_content.data = blog.introduce
    form.body.data = blog.content
    return render_template('backend/editBlogContent.html', form=form)
Beispiel #4
0
def update_github_avatar():
    try:
        link = rd.get('avatar')
        res = requests.get(link, timeout=30)
        md5 = get_md5(str(get_current_time()))
        with open(basedir + '/uploads/github/{}.png'.format(md5), 'wb') as f:
            f.write(res.content)

        # 移除上一次任务存储的图片
        pre_md5 = rd.get('pre_md5')
        if pre_md5:
            import os
            os.remove(basedir + '/uploads/github/{}.png'.format(pre_md5))

        # 更新redis中local_avatar的值
        rd.set('local_avatar', '/tool/github/{}.png'.format(md5))
        rd.set('pre_md5', md5)
        logger.info('更新github头像成功!')
    except Exception as e:
        logger.error('更新github头像失败!失败原因:\n' + traceback.format_exc())
Beispiel #5
0
def blog_create():
    form = PostForm()
    if request.method == 'GET':
        return render_template('backend/createBlog.html', form=form)

    if form.validate_on_submit():
        # 获取表单中信息
        title = form.title.data
        type = form.blog_type.choices[int(form.blog_type.data) - 1][0]
        level = form.blog_level.data
        content = form.body.data
        introduce = form.brief_content.data
        filename = form.blog_img_file.data.filename

        current_time = get_current_time()
        current_time = current_time.split(' ')[0]
        create_path(basedir + '/uploads/image/' + current_time)
        # 将博客示例图片存储到对应的文件夹中
        form.blog_img_file.data.save(basedir + '/uploads/image/' +
                                     current_time + '/' + filename)
        blog_img_path = '/backend/blog/img/' + current_time + '/' + filename

        cate = BlogType.query.filter_by(id=type).first()
        state = States.query.get_or_404(1)
        blg = Blog(title=title,
                   type_id=cate.id,
                   introduce=introduce,
                   content=content,
                   pre_img=blog_img_path,
                   is_private=level - 1,
                   state=state)
        cate.counts += 1
        db.session.add(blg)
        db.session.commit()

        return redirect(url_for('blog_bp.index'))
    else:
        flash('不能提交包含空的表单!', 'danger')
        return render_template('backend/createBlog.html', form=form)