Ejemplo n.º 1
0
Archivo: blog.py Proyecto: ccfr32/www
    def post(self, action='', post_id=0):
        '''
        编辑或者提交新文章
        '''
        if action == 'delete':
            try:
                post = Post.query.get_or_404(post_id)
                post.permissions.delete.test(self.identity, 401)
                db.session.delete(post)
                db.session.commit()
                detail = {'id': post_id}
                self.reply(0, u'删除成功', **detail)
                return
            except Exception as e:
                self.reply(1, str(e))
                return

        # 默认都是编辑或者修改
        # 0 表示不公开, 1表示公开
        public = self.get_argument("public", '0') == '1'
        tags = self.get_argument("tags", '')
        title = self.get_argument("title", '')
        if not title:
            self.reply(1, u'请输入标题')
            return
        content = self.get_argument("content", '')
        if not content:
            self.reply(1, u'请输入内容')
            return

        # 如果是编辑
        post = None
        if action == 'edit':
            try:
                post = Post.query.get_or_404(post_id)
            except Exception as e:
                self.reply(1, u'找不到该文章')
                return
            # 标题不能重复
            if  post.title != title and Post.query.get_by_title(title):
                self.reply(1, u'标题已经存在')
                return
        else:  # 默认都是提交新文章
            if Post.query.get_by_title(title):
                self.reply(1, u'标题已经存在')
                return 
            post = Post(author_id=self.current_user.id)
        post.title = title
        post.content = content
        post.public = public
        post.tags = tags
        db.session.add(post)
        db.session.commit()
        detail = {'id': post.id}
        if action == 'edit':
            self.reply(0, u'修改成功', **detail)
        else:
            self.reply(0, u'保存成功', **detail)
Ejemplo n.º 2
0
def submit():

    form = PostForm()

    if form.validate_on_submit():

        post = Post(author=g.user)
        form.populate_obj(post)

        db.session.add(post)
        db.session.commit()

        flash(_("Posting success"), "success")

        return redirect(post.url)

    return render_template("blog/submit.html", form=form)
Ejemplo n.º 3
0
    def post(self):

        form = self.forms.PostForm(self.request.arguments)

        if form.validate():

            post = Post(author_id=self.current_user.id)
            form.populate_obj(post)

            db.session.add(post)
            db.session.commit()

            # redirect
            next_url = form.next.data
            if not next_url:
                next_url = post.url
            self.redirect(next_url)
            return

        self.render("blog/post.html", form=form)
        return
Ejemplo n.º 4
0
Archivo: blog.py Proyecto: yhyan/www
    def post(self, action='', post_id=0):
        '''
        编辑或者提交新文章
        '''
        if action == 'delete':
            try:
                post = Post.query.get_or_404(post_id)
                post.permissions.delete.test(self.identity, 401)
                db.session.delete(post)
                db.session.commit()
                detail = {'id': post_id}
                self.reply(0, u'删除成功', **detail)
                return
            except Exception as e:
                self.reply(1, str(e))
                return

        # 默认都是编辑或者修改
        # 0 表示不公开, 1表示公开
        public = self.get_argument("public", '0') == '1'
        tags = self.get_argument("tags", '')
        title = self.get_argument("title", '')
        version = int(self.get_argument('version', '0'))
        if not title:
            self.reply(1, u'请输入标题')
            return
        html = self.get_argument("html", "")
        markdown = self.get_argument("markdown", "")
        if not html or not markdown:
            self.reply(1, u'请输入内容')
            return

        if tags == '20':
            # 日记
            try:
                datetime.strptime(title, "%Y.%m.%d")
            except:
                self.reply(1, u'日记的标题格式为%Y.%m.%d')
                return
        # 如果是编辑
        post = None
        if action == 'edit':
            try:
                post = Post.query.get_or_404(post_id)
            except Exception as e:
                self.reply(1, u'找不到该文章')
                return
            if version < post.version():
                self.reply(1, u'版本太旧')
                return
            # 标题不能重复
            if  post.title != title and Post.query.get_by_title(title):
                self.reply(1, u'标题已经存在')
                return
        else:  # 默认都是提交新文章
            if Post.query.get_by_title(title):
                self.reply(1, u'标题已经存在')
                return 
            post = Post(author_id=self.current_user.id)
        post.title = title
        post.html = html
        post.markdown = markdown
        post.public = public
        post.tags = tags
        db.session.add(post)
        db.session.commit()
        detail = {'id': post.id, 'version': post.version()}
        if action == 'edit':
            self.reply(0, u'修改成功', **detail)
        else:
            self.reply(0, u'保存成功', **detail)