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
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)
def add_photo(): form = AddPhotoForm() if form.validate_on_submit(): tags = form.tags.data title = form.photo_title.data img_file = form.img_file.data.filename img_file = str(current_user.id) + img_file desc = form.photo_desc.data # 相片保存在当前日期的文件夹中 folder = str(datetime.now()).split(' ')[0] create_path(basedir + '/uploads/gallery/' + folder) form.img_file.data.save(basedir + '/uploads/gallery/' + folder + '/' + img_file) # 云服务器带宽过低,当图片太大在相册加载太慢,所以这里生成相片缩略图 > 1M if os.path.getsize(basedir + '/uploads/gallery/' + folder + '/' + img_file) > \ current_app.config.get('PHOTO_NEED_RESIZE'): small_img = generate_thumbnail(basedir + '/uploads/gallery/' + folder + '/' + img_file) small_img.save(basedir + '/uploads/gallery/' + folder + '/' + 'small' + img_file) small_path = '/gallery/' + folder + '/' + 'small' + img_file else: small_path = '/gallery/' + folder + '/' + img_file img_path = '/gallery/' + folder + '/' + img_file photo = Photo(title=title, description=desc, save_path=img_path, save_path_s=small_path) # 将tag信息添加到相片中 for name in tags.split(): tag = Tag.query.filter_by(name=name).first() if tag is None: tag = Tag(name=name) db.session.add(tag) db.session.commit() if tag not in photo.tags: photo.tags.append(tag) db.session.commit() update_contribution() db.session.commit() flash('相片添加完成~', 'success') return redirect(url_for('gallery_bp.index')) return render_template('backend/add-photo.html', form=form)
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)