コード例 #1
0
ファイル: api.py プロジェクト: zbaolong/originblog
    def post(self, slug):
        """修改文章内容"""
        post = Post.objects.get_or_404(slug=slug)
        # 只有管理员或文章作者才有权限修改文章
        if not current_user.is_admin and post.author != current_user._get_current_object(
        ):
            abort(403)

        form = PostForm()
        if form.validate_on_submit():
            post.title = form.title.data
            post.abstract = form.abstract.data
            post.weight = form.weight.data
            post.raw_content = form.raw_content.data
            post.category = form.category.data if form.category.data else None
            post.tags = form.tags.data.split() if form.tags.data else None
            post.type = form.type.data
            # 修改文章包括标题不会更改slug,以确保链接的永久
            post.save()

            flash('Post updated.', 'success')
            post_type = post.type
            endpoints = {'post': 'blog.show_post', 'page': 'blog.show_page'}
            return redirect(url_for(endpoints[post_type], slug=post.slug))
        return self.get(slug, form)
コード例 #2
0
ファイル: api.py プロジェクト: zbaolong/originblog
    def post(self):
        """增加新文章"""
        form = PostForm()

        if form.validate_on_submit():
            title = form.title.data
            abstract = form.abstract.data
            weight = form.weight.data
            raw_content = form.raw_content.data
            category = form.category.data if form.category.data else None
            tags = form.tags.data.split() if form.tags.data else None
            type = form.type.data
            post = Post(title=title,
                        abstract=abstract,
                        weight=weight,
                        raw_content=raw_content,
                        category=category,
                        tags=tags,
                        type=type)
            post.author = current_user._get_current_object()
            if post.author.is_admin:
                post.from_admin = True
            # 保存文章到数据库时,注意处理slug相同的情况
            try:
                post.save()
            except NotUniqueError:
                post.slug += str(int(time.time()))
                post.save()

            # 初始化文章的统计数据
            post_statistic = PostStatistic(post=post, post_type=post.type)
            post_statistic.verbose_count_base = random.randint(500, 5000)
            post_statistic.save()

            # 发送文章发布的信号
            post_published.send(current_app._get_current_object(), post=post)

            flash('Post published.', 'success')
            # 跳转到对应端点
            endpoints = {'post': 'blog.show_post', 'page': 'blog.show_page'}
            return redirect(url_for(endpoints[post.type], slug=post.slug))
        templates = {
            'post': 'admin/new_post.html',
            'page': 'admin/new_page.html'
        }
        return render_template(templates.get(form.type.data,
                                             'admin/new_post.html'),
                               form=form)
コード例 #3
0
ファイル: api.py プロジェクト: zbaolong/originblog
    def get(self, slug, form=None):
        """获取文章内容与编辑表单"""
        post = Post.objects.get_or_404(slug=slug)
        # 只有管理员或文章作者才有权限修改文章内容
        if not current_user.is_admin and post.author != current_user._get_current_object(
        ):
            abort(403)

        if not form:
            form = PostForm()
            form.title.data = post.title
            form.abstract.data = post.abstract
            form.weight.data = post.weight
            form.raw_content.data = post.raw_content
            form.category.data = post.category
            form.tags.data = ' '.join(post.tags)
            form.type.data = post.type

        templates = {
            'post': 'admin/edit_post.html',
            'page': 'admin/edit_page.html'
        }
        return render_template(templates.get(post.type,
                                             'admin/edit_post.html'),
                               slug=slug,
                               form=form)
コード例 #4
0
def new_page():
    form = PostForm()
    form.type.data = 'page'
    return render_template('admin/new_page.html', form=form)