def post_get(path): checked,path=util.checkPostLocation(path) if not checked: flash("路径参数错误",'danger') return render_template('hintInfo.html') else: abspath=os.path.join(current_app.config['PAGE_DIR'],path)+".md" if not os.path.exists(abspath): flash("页面不存在!",'danger') return render_template('hintInfo.html',canCreate=True,location=path) else: # with open(abspath,encoding='UTF-8') as f: # content=f.read() # #title=content.split('\n\n',1)[0] # #content=content.split('\n\n',1)[1] # md_ext=util.Constant.md_ext # md=markdown.Markdown(output_format='html5',encoding='utf-8',extensions=md_ext) # html=util.html_clean(md.convert(content)) # # toc=md.toc # meta=md.Meta html,toc,meta=utilpost.get_post_content(abspath) log.debug('meta %s' % meta) post=Post.query.get(path) if not post: post=Post(location=path) # user=User.query.get(post.userId) tagNames=[] if post.tags: tagNames=[tag.name for tag in post.tags] return render_template('page.html',content=html,toc=toc,title=meta.get('title',' ')[0],post=post,author=meta.get('author',' ')[0] ,meta=meta,tagNames=tagNames)
def imageIndex(): topPathlist=['upload','upload_bak','dokuwiki'] path=request.args.get('path','upload') if path: isValid,path=util.checkPostLocation(path) if not isValid: raise ArgsErrorException('路径参数错误') imgDir=util.getAbsDataItemPath(path) if not (os.path.exists(imgDir) and os.path.isdir(imgDir)): raise ArgsErrorException('路径参数错误') images=list() dirs=list() curPage=int(request.args.get('curPage',1)) pageSize=int(request.args.get('pageSize',10)) for filename in os.listdir(imgDir): if os.path.isdir(imgDir+os.sep+filename): tdir=dict(path=path+os.sep+filename,name=filename,link='/data/'+path+'/'+filename) dirs.append(tdir) continue if os.path.splitext(filename)[1][1:] in ['png','jpg','bmp','gif','jpeg']: img=dict(path=path+os.sep+filename,link='/data/'+path+'/'+filename) images.append(img) if request.is_xhr: return jsonify(images[(curPage-1)*pageSize:curPage*pageSize]) else: return render_template('picManager.html',topPathlist=topPathlist,breadcrumbs=path.replace('/',os.sep).split(os.sep),path=path,dirs=dirs,images=images[:10],maxPage=len(images)//pageSize+1)
def post_new(path): checked,path=util.checkPostLocation(path) if not checked: flash("路径参数错误",'danger') return render_template('hintInfo.html') else: if utilpost.isPostLocked(path): flash("文章已被锁定,其他人正在编辑,您暂时无法编辑.") return render_template('hintInfo.html') utilpost.createPostLock(path) abspath=os.path.join(current_app.config['PAGE_DIR'],path)+".md" if not os.path.exists(abspath): form=PostForm() form.location.data=path flash('文章不存在,新建文章','info') return render_template('editor.html',title='新建文章',action=url_for('.post_save'),form=form,isPost=True)
def post_get(path): checked, path = util.checkPostLocation(path) if not checked: flash("路径参数错误", 'danger') return render_template('hintInfo.html') else: abspath = os.path.join(current_app.config['PAGE_DIR'], path) + ".md" if not os.path.exists(abspath): flash("页面不存在!", 'danger') return render_template('hintInfo.html', canCreate=True, location=path) else: with open(abspath, encoding='UTF-8') as f: content = f.read() #title=content.split('\n\n',1)[0] #content=content.split('\n\n',1)[1] md_ext = util.Constant.md_ext md = markdown.Markdown(output_format='html5', encoding='utf-8', extensions=md_ext) html = md.convert(content) toc = md.toc meta = md.Meta log.debug('meta %s' % meta) post = Post.query.get(path) if not post: post = {'location': path} # user=User.query.get(post.userId) return render_template('page.html', content=html, toc=toc, title=meta.get('title', ' ')[0], post=post, author=meta.get('author', ' ')[0], meta=meta)
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')