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 info_edit(photo_id): form = EditPhotoInfoForm() photo = Photo.query.get_or_404(photo_id) if form.validate_on_submit(): photo.title = form.photo_title.data photo.description = form.photo_desc.data update_contribution() db.session.commit() return redirect(url_for('.photo_edit')) form.photo_title.data = photo.title form.photo_desc.data = photo.description return render_template('backend/editPhotoInfo.html', form=form)
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_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)