예제 #1
0
    def post(self, id=None):
        if request.form.get('publish') and not id:
            post = Post()
        elif request.form.get('draft') and not id:
            post = Draft()
        elif request.form.get('publish') and id:
            post = Post.objects(id=id).first()
            if not post:
                post = Post()
        elif request.form.get('draft') and not id:
            post = Draft.objects(id=id).first() or Draft.objects(
                post=Post.objects(id=id).first()).first()
            if not post:
                post = Draft()
        if self.form.validate_on_submit():
            if request.form.get('draft'):
                post.title = self.form.title.data
                post.abstract = self.form.abstract.data
                post.raw = self.form.raw.data
                post.author = User.objects(id=current_user.get_id()).first()
                post.category = self.form.category.data
                post.tags = [
                    tag.strip() for tag in self.form.tags_.data.split(',')
                ]
                if id:
                    post.post = Post.objects(id=id).first()
                post.save()
            if request.form.get('publish'):
                post.title = self.form.title.data
                post.abstract = self.form.abstract.data
                post.raw = self.form.raw.data
                post.author = User.objects(id=current_user.get_id()).first()
                post.category = self.form.category.data
                post.tags = [
                    tag.strip() for tag in self.form.tags_.data.split(',')
                ]
                post.save()
            # 创建博客统计表
            if request.form.get('publish') and post.id != id:
                post_statistics = PostStatistics()
                post_statistics.post = post
                post_statistics.save()
                tracker = Tracker()
                tracker.post = post
                tracker.save()

            if request.form.get('publish') and self.form.from_draft.data:
                draft = Draft.objects(id=id).first() or Draft.objects(
                    post=Post.objects(id=id).first()).first()
                draft.delete()
                return redirect(url_for('common.post_detail', id=post.id))
            elif request.form.get('publish'):
                return redirect(url_for('common.post_detail', id=post.id))
            elif not id and request.form.get('draft'):
                return redirect(url_for('admin.add_post'))
            return self.get()
        return self.get()
예제 #2
0
 def post(self, username):
     user = User.objects(username=username).first()
     if self.form.validate_on_submit():
         user.role = self.form.role.data
         user.save()
         flash('修改权限成功')
         return redirect(url_for('admin.role', username=username))
     return self.get(username)
예제 #3
0
 def post(self, id):
     form = CommentForm()
     if form.validate_on_submit():
         comment = Comment()
         comment.raw = form.raw.data
         # comment.author = User.objects(username=current_user.get('username', None)).first() or None
         comment.post = Post.objects.get_or_404(id=id)
         if not current_user.get_id():
             comment.guest_user = '******' % uuid.uuid1()
             comment.author = None
         else:
             comment.author = User.objects(id=current_user.get_id()).first()
         comment.save()
     return redirect(url_for('common.post_detail', id=id))
예제 #4
0
    def delete(self, username):
        user = User.objects(username=username).first()

        # 删除 user 指向它的 post 和 comment 都需要修改
        posts = Post.objects(author=user).all()
        if posts:
            for post in posts:
                post.author = None
                post.save()

        commemts = Comment.objects(author=user).all()
        if commemts:
            for comment in commemts:
                comment.author = None
                comment.save()

        user.delete()
        if request.args.get('ajax'):
            return 'success'
        return redirect(url_for('admin.users'))
예제 #5
0
    def get(self):
        post = self.post
        if g.identity.can(admin_permission):
            post = self.post
        else:
            post = self.post.filter(author=User.objects(
                id=current_user.get_id()).first())

        # page 不写道路由中,作为参数访问
        try:
            current_page = request.args.get('page', 1, type=int)
        except:
            current_page = 1

        # paginate能实现分页显示
        posts = post.paginate(page=current_page, per_page=PER_PAGE)

        total = math.ceil(len(post) / PER_PAGE)

        return render_template('admin/posts.html', posts=posts, total=total)
예제 #6
0
 def get(self, username):
     user = User.objects(username=username).first()
     data = {'user': user}
     data['form'] = self.form
     return render_template('admin/role.html', **data)