Ejemplo n.º 1
0
def post(post_id):
    """View function for post page"""

    # Form object: `Comment`
    form = CommentForm()
    # form.validate_on_submit() will be true and return the
    # data object to form instance from user enter,
    # when the HTTP request is POST
    if form.validate_on_submit():
        new_comment = Comment(id=str(uuid4()), name=form.name.data)
        new_comment.text = form.text.data
        new_comment.date = datetime.now()
        new_comment.post_id = post_id
        db.session.add(new_comment)
        db.session.commit()

    post = db.session.query(Post).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,
                           form=form,
                           recent=recent,
                           top_tags=top_tags)
Ejemplo n.º 2
0
def post(post_id):
    """View function for post page"""

    #form object:comment
    form = CommentForm()
    #当HTTP请求是POST时,提交验证通过并把用户输入返回
    if form.validate_on_submit():
        new_comment = Comment(id=str(uuid4()), name=form.name.data)
        new_comment.text = form.text.data
        new_comment.date = datetime.datetime.now()
        new_comment.post_id = post_id
        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,
                           form=form,
                           recent=recent,
                           top_tags=top_tags)
Ejemplo n.º 3
0
def play_movie(movie_id):
    form = CommentForm()
    post = Movie.query.get_or_404(movie_id)
    credit = Credits(movie_id=movie_id)
    db.session.add(credit)
    db.session.commit()
    rem_movie = get_recommendations(movie_id)
    if form.validate_on_submit():
        comm = Comment(content=form.content.data,
                       movie_id=movie_id,
                       author_user=current_user)
        db.session.add(comm)
        db.session.commit()
        flash('Your comment has been posted!', 'success')
        return redirect(url_for('play_movie', movie_id=movie_id))
    comm = Comment.query.filter_by(movie_id=movie_id).order_by(
        Comment.date_posted.desc())
    usrr = User.query.all()
    com_total = comm.count()
    movie = Movie.query.all()
    return render_template('play_movie.html',
                           com_total=com_total,
                           title=post.title,
                           usrr=usrr,
                           post=post,
                           comm=comm,
                           rem_movie=rem_movie,
                           movie=movie,
                           form=form)
Ejemplo n.º 4
0
def show_post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get("page", 1, type=int)
    comment_per_page = current_app.config["BLOG_COMMENT_PER_PAGE"]
    # 得到该文章的对应评论的第一页(没通过审核的评论不显示)
    pagination = Comment.query.with_parent(post).filter_by(reviewed=True) \
     .order_by(Comment.timestamp.asc()).paginate(page, per_page=comment_per_page)
    comments = pagination.items

    if current_user.is_authenticated:  # 用户已登录,则用管路员评论表单
        form = AdminCommentForm()
        form.author.data = current_user.name
        form.email.data = current_app.config["BLOG_EMAIL"]
        form.site.data = url_for(".index")
        from_admin = True  # 管理员评论默认通过审核
        reviewed = True

    else:
        form = CommentForm()
        from_admin = False
        reviewed = False

    # 提交评论或者回复表单后
    if form.validate_on_submit():
        author = form.author.data
        email = form.email.data
        site = form.site.data
        body = form.body.data

        comment = Comment(author=author,
                          email=email,
                          site=site,
                          body=body,
                          from_admin=from_admin,
                          reviewed=reviewed,
                          post=post)
        # 如果URL中reply参数存在,则说明是回复
        # 被回复的评论的id(这里可以是访客回复管理员消息,也可以是管理员回复游客消息)
        replied_id = request.args.get("reply")
        if replied_id:
            replied_comment = Comment.query.get_or_404(replied_id)
            comment.replied = replied_comment
            send_new_reply_email(replied_comment)
        db.session.add(comment)
        db.session.commit()

        # 不同身份用户评论后,页面闪现出的提示消息也略有不同
        if current_user.is_authenticated:  # 管理员回复就免去了向自己发送邮件的过程
            flash('成功回复!', 'success')
        else:
            flash('感谢你的回复,管理员审核后将被发布!', 'info')
            # 当有游客对某篇文章进行评论/回复时,通知管理员审核
            send_new_comment_email(post)
        return redirect(url_for(".show_post", post_id=post_id))

    return render_template("blog/post.html",
                           post=post,
                           pagination=pagination,
                           form=form,
                           comments=comments)
Ejemplo n.º 5
0
def fake_comments(count=500):
	for i in range(count):
		comment = Comment(
			author=fake.name(),
			email=fake.email(),
			site=fake.url(),
			body=fake.sentence(),
			timestamp=fake.date_time_this_year(),
			reviewed=True,
			post=Post.query.get(random.randint(1, Post.query.count()))
		)
		db.session.add(comment)

	salt = int(count * 0.1)  # 50 条
	for i in range(salt):
		# 未审核通过的评论
		comment = Comment(
			author=fake.name(),
			email=fake.email(),
			site=fake.url(),
			body=fake.sentence(),
			timestamp=fake.date_time_this_year(),
			reviewed=False,
			post=Post.query.get(random.randint(1, Post.query.count()))
		)
		db.session.add(comment)

		# 管理员的评论
		comment = Comment(
			author='刘知安',
			email='*****@*****.**',
			site='https://github.com/LiUzHiAn',
			body=fake.sentence(),
			timestamp=fake.date_time_this_year(),
			from_admin=True,
			reviewed=True,
			post=Post.query.get(random.randint(1, Post.query.count()))
		)
		db.session.add(comment)
	db.session.commit()

	# 50条追评
	for i in range(salt):
		comment = Comment(
			author=fake.name(),
			email=fake.email(),
			site=fake.url(),
			body=fake.sentence(),
			timestamp=fake.date_time_this_year(),
			reviewed=True,
			replied=Comment.query.get(random.randint(1, Comment.query.count())),
			post=Post.query.get(random.randint(1, Post.query.count()))
		)
		db.session.add(comment)
	db.session.commit()
Ejemplo n.º 6
0
def new_post_comment(post_id):
    form = CommentForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        comment = Comment(content=form.content.data,
                          com_author=current_user,
                          post=post)
        Comment.save(comment)
        flash('Your comment has been posted!', 'success')
        return redirect(url_for('main.home'))
    return render_template('new_post_comment.html',
                           title='New comment',
                           form=form,
                           post=post)
Ejemplo n.º 7
0
def new_comment(posts_id):
    posts = Post.query.filter_by(id = posts_id).first()
    form = CommentForm()

    if form.validate_on_submit():
        comment = form.comment.data

        new_comment = Comment(comment_content=comment,user_id=current_user.id, posts_id=posts_id)

        new_comment.save_comment()

        return redirect(url_for('comments.index'))
    title='New Comment'
    return render_template('new_comment.html',title=title,comment_form = form,posts_id=posts_id)
Ejemplo n.º 8
0
def post(post_id):
    page = request.args.get('page', 1, type=int)
    post = Post.query.get_or_404(post_id)
    comments = Comment.query\
        .filter_by(
            post=post
        )\
        .order_by(
            Comment.date_posted.desc()
        )\
        .paginate(
            per_page=6,
            page=page
        )
    form = CommentForm()
    if form.validate_on_submit():
        new_comment = Comment(content=form.content.data,
                              post_id=post.id,
                              user_id=current_user.id)
        db.session.add(new_comment)
        db.session.commit()

        flash('Comentário adicionado', '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)
def post(post_id):
    post = Post.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    comments = Comment.query.filter_by(reference=post.quirk).paginate(
        page=page, per_page=5)
    form = AnswerForm()
    if post.posttype == "Report" or post.posttype == "Announcement":
        return render_template("post.html", title=post.assignment, post=post)
    elif post.posttype == "Injury":
        return render_template("injurypost.html",
                               title=post.assignment,
                               post=post)
    elif post.posttype == "Question":
        if form.validate_on_submit():
            comment = Comment(commenter=current_user.username,
                              comment=form.content.data,
                              reference=post.quirk)
            db.session.add(comment)
            db.session.commit()
            post.reviewedornot = True
            db.session.commit()
            return redirect(url_for("post", post_id=post.id))
        return render_template("studypost.html",
                               title=post.assignment,
                               post=post,
                               comments=comments,
                               legend="Add your comment here",
                               form=form)
Ejemplo n.º 10
0
def post(post_id):
    # get_or_404() method gets the post with the post_id and if it doesn't exist it returns a 404 error (
    # page doesn't exist)
    view_post = Post.query.get_or_404(post_id)
    form = CommentForm()

    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          post=view_post,
                          author=current_user._get_current_object()
                          )  # current_user._get_current_object()
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been added', 'success')
        return redirect(url_for('.post', post_id=view_post.id, page=-1))

    page = request.args.get('page', 1, type=int)
    if page == -1:
        page = (view_post.comments.count() -
                1) // current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1

    pagination = view_post.comments.order_by(Comment.timestamp.asc()).paginate(
        page,
        per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
        error_out=False)
    comments = pagination.items

    return render_template('post.html',
                           title=view_post.title,
                           post=view_post,
                           form=form,
                           comments=comments,
                           pagination=pagination)
def view_question(post_id):
    post = Question.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    comments = Comment.query.filter_by(reference=post.quirk).paginate(
        page=page, per_page=5)
    image_file = current_user.image_file
    user_level = current_user.level
    form = AnswerForm()
    if current_user.level == ('Repairman' or 'Administrator'):
        return render_template("studypost.html",
                               title=post.title,
                               user_level=user_level,
                               image_file=image_file,
                               post=post,
                               comments=comments)
    else:
        if form.validate_on_submit():
            comment = Comment(commenter=current_user.username,
                              comment=form.content.data,
                              commenttitle=form.title.data,
                              reference=post.quirk)
            db.session.add(comment)
            db.session.commit()
            post.reviewedornot = True
            db.session.commit()
            return redirect(url_for("home", post_id=post.id))
        return render_template("view_question.html",
                               title=post.title,
                               form=form,
                               user_level=user_level,
                               post=post,
                               comments=comments,
                               image_file=image_file,
                               legend="Add your comment here")
Ejemplo n.º 12
0
def reply_comment(post_id, comment_id):
    post = Post.query.get_or_404(post_id)
    # comment = Comment.query.get(comment_id)
    parent = Comment.query.get(comment_id)
    path = str(comment_id)
    db.session.commit()
    form = AddCommentForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            comment = Comment(body=form.body.data,
                              post_id=post.id,
                              parent_id=comment_id,
                              depth=parent.depth + 1,
                              path=path)
            db.session.add(comment)
            db.session.commit()
            child = Comment.query.get(comment.id)
            # print(child)
            child.path = str(parent.path) + '.' + str(child.id)
            db.session.commit()
            flash("Your reply has been added to the post", "success")
            return redirect(url_for("main.home"))
    return render_template("reply_comment.html",
                           title="Comment Post",
                           form=form,
                           post_id=post_id,
                           comment_id=comment_id)
Ejemplo n.º 13
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    comments = Comment.query.filter_by(post_id_comm=post_id).all()
    likes = Like.query.filter_by(post_id=post_id,
                                 username=current_user.username)
    # likes_2 = Like.query.filter_by(post_id=post_id, username=current_user.username)
    number_of_likes = Like.query.filter_by(post_id=post_id).count()

    form = CommentForm()
    if request.method == 'GET':
        post.view += 1
        db.session.commit()

    if request.method == 'POST':
        if form.validate_on_submit():
            new_comment = Comment(text_comment=form.comment_text.data,
                                  post_id_comm=post_id,
                                  user_id_comm=current_user.id)
            db.session.add(new_comment)
            db.session.commit()
            return redirect(url_for('home'))

    return render_template('post.html',
                           title=post.title,
                           post=post,
                           form=form,
                           comments=comments,
                           likes=likes,
                           number_of_likes=number_of_likes)
Ejemplo n.º 14
0
def post(post_id):
    page = request.args.get('page', 1, type=int)
    post = Post.query.get_or_404(post_id)
    #Post.query.paginate(page = page, per_page = 10)
    comments = Comment.query.filter_by(post_id=post_id)
    comments = comments.paginate(page=page, per_page=20)
    #comments = Comment.query.paginate(page = page, per_page = 20)
    form = CommentForm()

    if form.validate_on_submit():

        comment = Comment(content=form.content.data,
                          author=current_user,
                          post_id=post_id)
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been created!', 'success')
        #return redirect(url_for('home'))

    return render_template('post.html',
                           title=post.title,
                           post=post,
                           comments=comments,
                           form=form,
                           legend='Comments')
Ejemplo n.º 15
0
def comment_post(post_id):
    
    post = Post.query.get_or_404(post_id)
    
    print(post,request.method == "POST", request.form['user_comment'])
    
    if post and request.method == "POST":
        # print("adding")
        comment = Comment(commentor = current_user.username,comment = request.form['user_comment'], post__id = post_id)
        db.session.add(comment)
        db.session.commit()

        if current_user!=post.author:
            now_utc = datetime.now(timezone('UTC'))
            now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
            
            if current_user.id != post.author.id:
                notify = Notify(username = post.author.username, title = "comment", text = "You got a comment", post_id = post_id)
                db.session.add(notify)

            add_time1 = Timeline(username=current_user.username,title="You commented a post",text=current_user.username+" commented a post named "+post.title+" at "+now_asia.strftime("%I:%M %p"), time_am_pm = now_asia.strftime("%I:%M %p"))
            add_time2 = Timeline(username=post.author.username,title="You got a new comment",text=post.author.username+"'s post named "+post.title+" got a new comment by "+current_user.username+" at "+now_asia.strftime("%I:%M %p"), time_am_pm = now_asia.strftime("%I:%M %p"))
            db.session.add(add_time1)
            db.session.add(add_time2)
            db.session.commit()
            
    no_of_comments = len(Comment.query.filter_by(post__id = post_id).all())

    return {'comment_status': "done",'comments':no_of_comments}
Ejemplo n.º 16
0
def post(slug):
    post = Post.query.filter_by(slug=slug).first_or_404()
    news = Post.query.order_by(
        Post.date_posted.desc()).filter_by(category="News").first()
    reviews = Post.query.order_by(
        Post.date_posted.desc()).filter_by(category="Reviews").first()
    commentary = Post.query.order_by(
        Post.date_posted.desc()).filter_by(category="Commentary").first()
    comments = Comment.query.order_by(
        Comment.date_posted.desc()).filter_by(post_id=post.id).all()
    headImg = url_for('static',
                      filename='upload/media/images/head_Images/' +
                      post.headImg)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.comment.data,
                          user_id=current_user.id,
                          post_id=post.id)
        db.session.add(comment)
        db.session.commit()
        flash('Comment added', 'success')
        return redirect(url_for('main.post', slug=post.slug))
    print("slika: " + post.headImg)
    print("lokacija: " + headImg)
    return render_template('post.html',
                           title=post.title,
                           post=post,
                           news=news,
                           reviews=reviews,
                           commentary=commentary,
                           comments=comments,
                           form=form,
                           headImg=headImg)
Ejemplo n.º 17
0
def view_question(post_id):
    post = Question.query.get_or_404(post_id)
    page = request.args.get('page', 1, type=int)
    comments = Comment.query.filter_by(reference=post.quirk).paginate(
        page=page, per_page=5)
    if current_user.level != 'student':
        return render_template("studypost.html",
                               title=post.assignment,
                               post=post,
                               comments=comments)
    else:
        form = AnswerForm()
        if form.validate_on_submit():
            comment = Comment(commenter=current_user.username,
                              comment=form.content.data,
                              commenttitle=form.title.data,
                              reference=post.quirk)
            db.session.add(comment)
            db.session.commit()
            post.reviewedornot = True
            db.session.commit()
            return redirect(url_for("post", post_id=post.id))
        return render_template("studypost.html",
                               title=post.assignment,
                               post=post,
                               comments=comments,
                               legend="Add your comment here",
                               form=form)
Ejemplo n.º 18
0
def posts_comments(post_id):

    post = Post.query.filter_by(id=post_id).one()
    # comments=Comment.get_comments(post_id)
    comments=Comment.get_comments(post_id)


    return render_template('post_comments.html', post=post, comments=comments, post_id=post.id)
Ejemplo n.º 19
0
def comment(post_id):
   data = request.get_json()
   name = data['name']
   email = data['email']
   message = data['message']
   comment = Comment(name=name, email=email, message=message, post_id=post_id)
   db.session.add(comment)
   db.session.commit()
   return jsonify('comment recievd!')
Ejemplo n.º 20
0
def comments(post_id):
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.content.data, user_id=current_user.id, post_id=post_id)
        db.session.add(comment)
        db.session.commit()
        flash('Commented!', 'success')
        return redirect(url_for('comments',post_id=post_id))
    comments = Comment.query.filter_by(post_id=post_id).all()
    return render_template('comments.html', comments=comments,legend='Comments',form=form)
Ejemplo n.º 21
0
def comment_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = AddCommentForm()
    if form.validate_on_submit():
        db.create_all()
        if current_user.is_authenticated:
            comment = Comment(body=form.body.data,
                              post_id=post_id,
                              username=current_user.username,
                              comment_user_id=current_user.id)
        else:
            comment = Comment(body=form.body.data,
                              post_id=post_id,
                              username='******')
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been added to the post', 'success')
        return redirect(url_for('post', post_id=post_id))
    return render_template('post.html', title=post.title, form=form, post=post)
Ejemplo n.º 22
0
def comment(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm(request.form)
    if form.validate_on_submit():
        comment = Comment(content=form.content.data,
                          author=current_user,
                          post=post)
        db.session.add(comment)
        db.session.commit()
        return Response(status=200)
    return Response(status=500)
Ejemplo n.º 23
0
def new_comment(post_id):
    post = Post.query.get(post_id)
    form = CommentForm()
    if True:
        comment = Comment(content=form.content.data,
                          author=current_user,
                          post_id=post.id)
        db.session.add(comment)
        db.session.commit()
        form = CommentForm()
        return redirect(url_for('post', post_id=post.id))
Ejemplo n.º 24
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    comments = Comment.query.all()
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.content.data, author=current_user)
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been created!', 'success')
        return redirect(url_for('home'))
    return render_template('post.html', post=post, form=form, comments=comments)
Ejemplo n.º 25
0
def comment_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = AddCommentForm()
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          post_id=post_id,
                          author=current_user)
        db.session.add(comment)
        db.session.commit()
        flash("Your comment has been added to the post", "success")
        return redirect(url_for("post", post_id=post.id))
Ejemplo n.º 26
0
def comment_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    user = current_user
    if form.validate_on_submit():
        comment = Comment(content=form.content.data, post_id=post.id, user_id=current_user.username, image=user.image_file)
        db.session.add(comment)
        db.session.commit()
        flash("Comentario añadido", "success")
        return redirect(url_for("post", post_id=post.id))
    return render_template("comment_post.html", title="Opina", form=form)
Ejemplo n.º 27
0
def post(post_id):
    post = Post.query.get_or_404(post_id)
    comments = Comment.query.filter_by(post=post).order_by(Comment.date_posted.desc())

    form = CommentForm()
    if form.validate_on_submit() and current_user:
        comment = Comment(content=form.content.data, author=current_user, post=post)
        db.session.add(comment)
        db.session.commit()
        flash('Comment has been created', '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.º 28
0
def post_display(post_id):
	test_id = post_id
	post = Post.query.get(int(post_id))
	coms = post.comments
	form = CommentForm()
	x = current_user.id
	if form.validate_on_submit():
		comment = Comment(text=form.text.data, user = x)
		db.session.add(comment)
		comment.posted_on.append(post)
		db.session.commit()
		flash('Comment added succesfully', 'success')
		return redirect(url_for('home'))
	return render_template('display_post.html', title='post.id', post = post, comments = coms, form = form)
Ejemplo n.º 29
0
def comment_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.content.data,
                          post=post,
                          creator=current_user)
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for("post", post_id=post.id))
    return render_template("comment.html",
                           post=post,
                           legend="Create Comment",
                           form=form)
Ejemplo n.º 30
0
def comment_post(post_id):

    post = Post.query.get_or_404(post_id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.content.data, commented_post=Post.query.get(post_id), commenter=current_user)
        db.session.add(comment)
        db.session.commit()

        flash('Your comment have been created!', 'success')
        
        return redirect(url_for('post', post_id=post.id))

    return render_template('comment_post.html', title='Comment Post', form=form, post=post)