Ejemplo n.º 1
0
def post(id):
    if post := Post.query.get(id):
        post.views += 1
        db.session.commit()
        form = CommentForm()
        if form.validate_on_submit():
            name, email, content = (Markup.striptags(form.name.data),
                                    Markup.striptags(form.email.data),
                                    Markup.striptags(form.content.data))
            db.session.add(
                Comment(title=name, email=email, content=content, post_id=id))
            db.session.commit()
            s = f'Дякую, {Markup.striptags(form.name.data)}, за коментар :)'
            flash(s, 'success')
            return redirect(url_for('posts.post', id=id))
        elif form.is_submitted():
            flash("Будь ласка, заповніть всі поля відповідно до вимог.",
                  "error")
        related_posts = Post.query.filter_by(cat_id=post.cat_id).limit(5)
        return render_template('post.html',
                               title=post.title,
                               categories=Category.query.all(),
                               post=post,
                               months=months,
                               form=form,
                               related_posts=related_posts)
Ejemplo n.º 2
0
def new_comment(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comments(post_id=post_id, author_id=post.author.id, content=form.content.data)
        db.session.add(comment)
        db.session.commit()
        flash('The comment has been created', 'success')
        return redirect(url_for('posts.post', post_id=post.id))

    return render_template('add_comment.html', title='New Comment', form=form, legend='New Comment')
Ejemplo n.º 3
0
def comment(slug):
    post = Post.query.filter(Post.slug == slug).first_or_404()

    form = CommentForm(formdata=request.form)

    if form.validate():
        current_app.logger.exception('Something wrong')
        comment = Comment(post_id=post.id, name=form.name.data,
                          text=form.text.data)
        db.session.add(comment)
        db.session.commit()

    return redirect(url_for('posts.post_detail', slug=post.slug))
Ejemplo n.º 4
0
def post_detail(slug):
    post = Post.query.filter(Post.slug == slug).first_or_404()
    tags = post.tags
    comments = post.comments
    form = CommentForm()
    return render_template('posts/post_detail.html', post=post, tags=tags,
                           comments=comments, form=form)
Ejemplo n.º 5
0
def post(post_id):
    post = Post.objects.get_or_404(id=post_id)
    form = CommentForm()
    print(form.validate_on_submit())
    if form.validate_on_submit():
        content = form.content.data
        user = User.objects.get_or_404(email=current_user.email)
        comment = Comment(post=post, author=user, content=content)
        comment.save()
    form.content.data = ""
    comments = Comment.objects(post=post)
    return render_template('post.html',
                           title='Post',
                           post=post,
                           comments=comments,
                           form=form)
Ejemplo n.º 6
0
def update_comment(post_id, comment_id):
    form = CommentForm()
    post = Post.objects().get_or_404(id=post_id)
    comment = Comment.objects().get(id=comment_id)
    comment.comment = form.comment.data
    comment.save()
    flash("Comment updated", "success")
    return redirect(url_for("posts.post", post_id=post.id))
Ejemplo n.º 7
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    comments = Comment.query.filter_by(topic=post).order_by(
        Comment.date_commented.desc())
    form = CommentForm()
    if form.validate_on_submit():
        newcomment = Comment(commenter=current_user,
                             topic=post,
                             content=form.content.data)
        db.session.add(newcomment)
        db.session.commit()
        flash('Your comment has been added!', 'success')
    return render_template('post.html',
                           title=post.title,
                           post=post,
                           comments=comments,
                           form=form,
                           legend='Add Comment')
Ejemplo n.º 8
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.body.data,
                          author=current_user.username,
                          post_id=post.id)
        db.session.add(comment)
        db.session.commit()
        flash('Comment published', 'success')
        return redirect(
            url_for('posts.post', post_id=post.id, comments=post.comments))
    return render_template('post.html',
                           pokemons=post.pokemons,
                           post=post,
                           form=form,
                           comments=post.comments,
                           legend="Comments")
Ejemplo n.º 9
0
def comment_update(post_id, comment_id):
    post = Post.query.get_or_404(post_id)
    comment = Comments.query.get_or_404(comment_id)

    if post.author != current_user and 'Admin' not in current_user.roles_names:
        abort(403)

    form = CommentForm()

    if form.validate_on_submit():
        comment.content = form.content.data
        db.session.commit()
        flash('The comment has been updated.', 'success')
        return redirect(url_for('posts.post', post_id=post.id))

    #form will be filled with actual data
    elif request.method == 'GET':
        form.content.data = comment.content

    return render_template('add_comment.html', title='Update Comment', form=form, legend='Update Comment')
Ejemplo n.º 10
0
def article(slug):
    #this route allows the user to visit a page that contains a specific post, and contols the addition of comments
    posts = Post.query.filter_by(slug=slug).first_or_404()
    post = Post.query.all()
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(name=bleach.clean(form.name.data),
                          website=bleach.clean(form.website.data),
                          content=bleach.clean(form.content.data),
                          post_id=posts.id)
        db.session.add(comment)
        db.session.commit()
        flash(
            'Hi, thanks for your letting us know your thought! We have just received your comment and it has been submitted to our moderators for review.',
            'success')
        return redirect(url_for('main.home'))
    return render_template('article.html',
                           posts=posts,
                           form=form,
                           title=posts.title,
                           post=post)
Ejemplo n.º 11
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    comments = Comments.query.filter_by(post_id=post.id).order_by(
        Comments.time_posted.desc()).all()
    if request.method == 'POST':
        comment = Comments(name=form.name.data,
                           email=form.email.data,
                           message=form.message.data,
                           post_id=post.id)
        db.session.add(comment)
        # post.comment += 1
        flash('Your comment has been submitted!', 'success')
        return redirect(url_for('posts.post', post_id=post.id))
    return render_template('post.html',
                           title=post.title,
                           post=post,
                           form=form,
                           comments=comments)
Ejemplo n.º 12
0
def post(post_id):
    post = Post.objects().get_or_404(id=post_id)
    likes = len(post.user_likes)
    is_liked = False
    comments = Comment.objects(post=post)
    if current_user.is_authenticated and post in current_user.liked_posts:
        is_liked = True
    form = CommentForm()
    if request.method == "POST":
        comment = Comment(comment=form.comment.data,
                          comment_author=current_user.id,
                          post=post)
        comment.save()
        flash("Comment added", "success")
        return redirect(url_for("posts.post", post_id=post.id))
    return render_template("posts/post.html",
                           title=post.title,
                           post=post,
                           is_liked=is_liked,
                           likes=likes,
                           form=form,
                           comments=comments)