예제 #1
0
파일: forms.py 프로젝트: iamsixer/mdwiki-1
class PostForm(FlaskForm):
    title = StringField('标题', validators=[DataRequired()])
    content = TextAreaField('内容', validators=[DataRequired()])
    tags = StringField('tags')
    location = HiddenField('location', validators=[DataRequired()])
    createAt = HiddenField('createAt', default=util.getNowFmtDate())
    modifyAt = HiddenField('createAt', default=util.getNowFmtDate())
    fileModifyAt = HiddenField('fileModifyAt', default=util.getNowFmtDate())
예제 #2
0
파일: vo.py 프로젝트: iamsixer/mdwiki-1
 def __init__(self,
              location,
              title='',
              content='',
              summary='',
              createAt=None,
              modifyAt=None,
              author=''):
     self.location = location
     self.title = title
     self.content = content
     self.summary = summary
     self.createAt = datetime.strptime(createAt or util.getNowFmtDate(),
                                       '%Y-%m-%d %H:%M:%S')
     self.modifyAt = datetime.strptime(modifyAt or util.getNowFmtDate(),
                                       '%Y-%m-%d %H:%M:%S')
     self.author = author
예제 #3
0
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')