Пример #1
0
def post(post_id):
    form = CommentForm()

    if form.validate_on_submit():
        new_comment = Comment()
        new_comment.name = form.name.data
        new_comment.text = form.text.data
        new_comment.post_id = post_id
        new_comment.date = datetime.datetime.now()

        db.session.add(new_comment)
        db.session.commit()

    post = Post.query.get_or_404(post_id)
    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    recent, top_tags = sidebar_data()

    return render_template(
        'post.html',
        post=post,
        tags=tags,
        comments=comments,
        recent=recent,
        top_tags=top_tags,
        form=form
    )
Пример #2
0
def post(post_id):
    form = CommentForm()

    if form.validate_on_submit():
        new_comment = Comment()
        new_comment.name = form.name.data
        new_comment.text = form.text.data
        new_comment.post_id = post_id
        new_comment.date = datetime.datetime.now()

        db.session.add(new_comment)
        db.session.commit()

    post = Post.query.get_or_404(post_id)
    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    recent, top_tags = sidebar_data()

    return render_template('post.html',
                           post=post,
                           tags=tags,
                           comments=comments,
                           recent=recent,
                           top_tags=top_tags,
                           form=form)
Пример #3
0
def post(post_id, page=1):
    form = CommentForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            comment = Comment()
            comment.name = form.name.data
            comment.text = form.text.data
            comment.post_id = post_id
            comment.date = datetime.datetime.now()

            db.session.add(comment)
            db.session.commit()
            return redirect(url_for('post', post_id=post.id))

    post = Post.query.get_or_404(post_id)
    tags = post.tags

    comments = post.comments.order_by(Comment.date.desc()
        ).paginate(page, 10)
    recent, top_tags = sidebar_data()

    return render_template(
        'post.html',
        post=post,
        tags=tags,
        comments=comments,
        recent=recent,
        top_tags=top_tags,
        form=form
    )
Пример #4
0
def post(post_id):
    form = CommentForm()
    if form.validate_on_submit():
        new_comment = Comment()
        new_comment.name = form.name.data
        new_comment.text = form.text.data
        new_comment.post_id = post_id
        new_comment.date = datetime.datetime.now()
        db.session.add(new_comment)
        db.session.commit()
        form.name.data = None
        form.text.data = None
    post = Post.query.get_or_404(post_id)
    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    recent, top_tags = sidebar_data()

    if request.method == 'POST':
        return redirect(url_for('blog.post', post_id=post_id))
    else:
        return render_template('blog/post.html',
                               post=post,
                               tags=tags,
                               comments=comments,
                               recent=recent,
                               top_tags=top_tags,
                               form=form)
Пример #5
0
def article(post_id):
    post = Post.query.filter_by(id=post_id).first()
    form = CommentForm()
    if form.validate_on_submit():
        newcomment = Comment()
        newcomment.name = current_user.name
        newcomment.text = form.content.data
        newcomment.post_id = post.id
        newcomment.user_id = current_user.id
        newcomment.date = datetime.now()
        db.session.add(newcomment)
        db.session.commit()
        return redirect(url_for('.article', post_id=post.id))

    comments = Comment.query.filter_by(post_id=post_id).order_by(
        Comment.date).all()

    if post is None:
        return redirect(url_for('.listarticle'))
    else:
        title = post.title
        text = post.text
        publish_date = post.publish_date
        user_id = post.user_id
        user = User.query.filter_by(id=user_id).first()

        return render_template('post.html',
                               form=form,
                               comments=comments,
                               text=text,
                               username=user.username,
                               publish_date=publish_date,
                               siteheading=title,
                               backgroundpic='/static/img/post2_bg.jpg',
                               post=post)
Пример #6
0
    def setUp(self):
        db.app = test_app
        db.create_all()
        user = User()
        user.username = self.username
        user.set_password(self.password)
        db.session.add(user)

        comment = Comment()
        comment.name = self.comment_name
        comment.text = self.comment_text

        tag = Tag()
        tag.title = self.tag_title

        post = Post()
        post.title = self.post_title
        post.text = self.post_text
        post.publish_date = self.post_publish_date

        # add relationships to other tables
        post.user = user
        post.tags = [tag]
        post.comments = [comment]

        db.session.add(user)
        db.session.add(comment)
        db.session.add(tag)
        db.session.add(post)
        db.session.commit()
Пример #7
0
    def post(self, post_id=None,comment_id=None):
        print "In commentPostAPI"
        print "post_id is %s" %post_id
        print "comment_id is %s" %comment_id
        if comment_id or not post_id:
            abort(400)
        else:
            args = comment_post_parser.parse_args(strict=True)
            user = User.verify_auth_token(args['token'])
            post = Post.query.filter_by(id=post_id).first()
            print "user is %s,post is %s" %(user,post)
            if not user or not post:
                abort(401)
            if user.username == 'admin123':
                new_comment = Comment()
                new_comment.user = args['name']
                new_comment.date = datetime.datetime.now()
                new_comment.text = args['text']
                new_comment.post_id = post_id
            else:
                comment_user = args['name']

                if user.username != comment_user:
                    abort(401)
                new_comment = Comment()
                new_comment.name = user.username
                new_comment.date = datetime.datetime.now()
                new_comment.text = args['text']
                new_comment.post_id = post_id

            db.session.add(new_comment)
            db.session.commit()
            return new_comment.id, 201
Пример #8
0
def post(post_id):
    form = CommentForm()
    if form.validate_on_submit():
        new_comment = Comment()
        new_comment.name = form.name.data
        new_comment.text = form.text.data
        new_comment.post_id = post_id
        new_comment.date = datetime.now()
        db.session.add(new_comment)
        db.session.commit()
        return redirect(url_for('.post', post_id=post_id))
    post = Post.query.get_or_404(post_id)
    # 添加阅读量
    post.read = post.read + 1
    db.session.add(post)
    db.session.commit()

    tags = post.tags
    comments = post.comments.order_by(Comment.date.desc()).all()
    # 是否有编辑权限
    permission = Permission(UserNeed(post.user.id))
    is_edit = permission.can() or admin_permission.can()
    if g.is_login:
        form.name.data = current_user.username
    return render_template('post.html',
                           post=post,
                           tags=tags,
                           is_edit=is_edit,
                           comments=comments,
                           form=form)
Пример #9
0
def post(post_id):
    form = CommentForm()
    post = Post.objects(id=post_id).get_or_404()
    if form.validate_on_submit():
        new_comment = Comment()
        new_comment.name = form.name.data
        new_comment.text = form.text.data
        new_comment.date = datetime.datetime.now()
        post.comments.append(new_comment)
        post.save()
    tags = post.tags
    comments = post.comments

    return with_sidebar_render('post.html',
                               post=post,
                               tags=tags,
                               comments=comments,
                               form=form)
Пример #10
0
def commentarticle(post_id):
    form = CommentForm()
    post = Post.query.filter_by(id=post_id).first()
    if form.validate_on_submit():
        newcomment = Comment()
        newcomment.name = current_user.name
        newcomment.text = form.content.data
        newcomment.post_id = post.id
        newcomment.user_id = current_user.id
        newcomment.date = datetime.now()
        db.session.add(newcomment)
        db.session.commit()
        return redirect(url_for('.commentarticle', post_id=post.id))

    comments = Comment.query.filter_by(post_id=post_id).order_by(
        Comment.date).all()
    time = datetime.now()

    return render_template('post/commentarticle.html',
                           time=time,
                           form=form,
                           post=post,
                           comments=comments,
                           backgroundpic='/static/img/post-bg.jpg')
Пример #11
0
def insert_data():
    with app.app_context():
        # 不需要在这里创建库,应该使用数据库升级命令`db upgrade`来创建库
        # db.create_all()

        # 这里设定了3种角色
        role_admin = Role(name='admin')
        role_admin.description = "administrator role"
        role_poster = Role(name='poster')
        role_poster.description = "the registered user role"
        role_default = Role(name='default')
        role_default.description = 'the unregistered user role'
        db.session.add(role_admin)
        db.session.add(role_poster)
        db.session.add(role_default)

        # add User
        admin = User(username='******')
        admin.email = '*****@*****.**'
        admin.password = '******'
        admin.confirmed = True
        admin.roles.append(role_admin)
        admin.roles.append(role_poster)
        admin.roles.append(role_default)
        db.session.add(admin)

        user01 = User(username='******')
        user01.email = '*****@*****.**'
        user01.password = '******'
        user01.confirmed = True
        user01.roles.append(role_poster)
        user01.roles.append(role_default)
        db.session.add(user01)

        user02 = User(username='******')
        user02.email = '*****@*****.**'
        user02.password = '******'
        user02.confirmed = True
        user02.roles.append(role_poster)
        user02.roles.append(role_default)
        db.session.add(user02)

        # add Tag and Post
        tag_one = Tag('Python')
        tag_two = Tag('Flask')
        tag_three = Tag('SQLAlechemy')
        tag_four = Tag('Jinja')
        tag_list = [tag_one, tag_two, tag_three, tag_four]

        s = "Example Text"

        for i in xrange(1, 101):
            new_post = Post("Post {}".format(i))
            if i % 2:
                new_post.user = user01
            else:
                new_post.user = user02
            new_post.publish_date = datetime.datetime.utcnow()
            new_post.text = s
            new_post.tags = random.sample(tag_list, random.randint(1, 3))
            db.session.add(new_post)

        # add comment
        comment01 = Comment()
        comment01.name = 'comment01'
        comment01.text = 'comment text'
        comment01.post_id = 99
        comment01.date = datetime.datetime.utcnow()
        db.session.add(comment01)

        comment02 = Comment()
        comment02.name = 'comment02'
        comment02.text = 'comment text'
        comment02.post_id = 100
        comment02.date = datetime.datetime.utcnow()
        db.session.add(comment02)

        db.session.commit()