def post_delete(path): isAdmin=util.checkAdmin() if isAdmin: post=Post.query.filter_by(location=path).first() else: post=Post.query.filter_by(location=path,userId=str(current_user.id)).first() path=util.urlDirPathFormat(path) abspath=util.getAbsPostPath(path) if post or os.path.exists(abspath): if post: log.debug(post.location+post.userId) db.session.delete(post) if os.path.exists(abspath): os.remove(abspath) term=dict(fieldName='location',text=path) try: searchutil.deleteDocument([term]) #db.session.commit() utilpost.delete_post_cache(abspath) flash("删除成功!", "success") except AttributeError as e: flash("发生内部错误 %s" % str(e),'danger') else: flash("文章不存在或无权限编辑!","warning") return render_template('hintInfo.html')
def postlistByTag(tagName, curNum): if not tagName.strip() or not curNum or curNum < 1: flash("参数错误!", "warning") return render_template('hintInfo.html') paginate = Post.query.filter(Post.tags.any(name=tagName)).order_by( Post.location).paginate(curNum, 15, False) cur_posts = paginate.items posts = list() for cur_post in cur_posts: abspath = util.getAbsPostPath(cur_post.location) if not os.path.exists(abspath): continue with open(abspath, 'r', encoding='utf-8') as f: content = f.read() conSplit = content.split('\n\n', 1) if len(conSplit) < 2: metaStr = '' postContent = conSplit[0] else: metaStr = conSplit[0] postContent = conSplit[1] meta = util.parsePostMeta(metaStr) summary = postContent[:200] posts.append({ 'location': cur_post.location, 'title': meta['title'], 'summary': summary, 'meta': meta }) return render_template('tagsPostList.html', tagName=tagName, paginate=paginate, posts=posts)
def post_save(): form=PostForm() if form.validate_on_submit(): checked,location=util.checkPostLocation(form.location.data) if not checked: flash('文章地址不合法', 'danger') return render_template('hintInfo.html') postExist=os.path.exists(util.getAbsPostPath(location)) post=Post.query.filter_by(location=location).first() isAdmin=util.checkAdmin() isNew=True if not post else False if isNew and postExist and not isAdmin: flash("文章早已存在,您没有权限操作",'info') return redirect(url_for('.post_get',path=location)) meta=dict(title=form.title.data,author=current_user.username or current_user.email, createAt=form.createAt.data,location=location ,modifyAt=util.getNowFmtDate()) if isNew: meta['createAt']=util.getNowFmtDate() post=Post() post.location = location post.userId = current_user.id db.session.add(post) # else: # flash('error location for post!','danger') # return abspath=util.getAbsPostPath(post.location) if(form.fileModifyAt.data) and os.path.exists(abspath): orginModifyAt=datetime.fromtimestamp(os.stat(abspath).st_mtime).strftime('%Y-%m-%d %H:%M:%S') #如果文件修改时间对不上,说明文件已经被修改过了 if form.fileModifyAt.data!=orginModifyAt: flash("保存失败,在您编辑文章过程中,文件已经被修改过了!") return render_template('hintInfo.html') tags=form.tags.data.lower().split(',') tagsList=[] if isinstance(tags,list): for tagStr in tags: tagObj=Tag.query.filter_by(name=tagStr).first() if not tagObj: # db.session.add(Tag(tagStr)) tagsList.append(Tag(tagStr)) else: # db.session.add(tagObj) tagsList.append(tagObj) else: tagObj = Tag.query.filter_by(name=tags[0]).first() if not tagObj: # db.session.add(Tag(tagStr)) tagsList.append(Tag(tags[0])) else: # db.session.add(tagObj) tagsList.append(tagObj) #post=db.session.merge(post) #print(post in db.session) post.tags=tagsList abspath=util.getAbsPostPath(post.location) if post.location.find(os.sep)>0: dir=abspath.rsplit(os.sep,1)[0] if not os.path.exists(dir): os.makedirs(dir) with open(abspath,'w+',encoding='UTF-8') as f: # 这一步很坑,一定要去掉\r,否则每次编辑器显示都会多出空行 content = form.content.data.replace('\r', '') # print(meta) f.write(util.fmtPostMeta(meta)+content) postvo=vo.SearchPostVo(content=content,summary=content[:100],**meta) if isNew: searchutil.indexDocument([postvo]) else: searchutil.updateDocument([postvo]) utilpost.releasePostLock(post.location) utilpost.delete_post_cache(abspath) return redirect(url_for('.post_get',path=post.location)) flash('保存失败', 'danger') return render_template('hintInfo.html')