Example #1
0
def detail(postid):
    form = CommentsForm()
    if form.validate_on_submit():
        if not current_user.is_authenticated:
            flash('登录才能评论哦')
            return redirect(url_for('users.login'))
        else:
            user = current_user._get_current_object()
            post = Posts.query.get(postid)
            comment = Comments(content=form.content.data, user=user, post=post)
            db.session.add(comment)
            return redirect(url_for('main.detail', postid=postid))
    post = Posts.query.get(postid)
    # 阅读数+1
    post.views += 1
    db.session.add(post)
    # 最热 最新 博文
    Topposts = Posts.query.order_by(db.desc(Posts.views))[:5]
    Newposts = Posts.query.order_by(db.desc(Posts.timestamp))[:5]
    # 博文的评论
    comments = Comments.query.filter_by(post=post).order_by(
        db.desc(Comments.timestamp))
    return render_template('common/detail.html',
                           post=post,
                           Newposts=Newposts,
                           Topposts=Topposts,
                           form=form,
                           comments=comments)
Example #2
0
def lookup_report(request, id):
    if request.method == "POST":
        form = CommentsForm(request.POST)
        if form.is_valid():
            complaint = Complaints.objects.get(id=id)
            print "form is valid"
            user_id = request.user.id
            complaint_id = complaint.id
            comments = form.cleaned_data['comments']
            ThisComment = Comments(user_id=user_id, comments=comments, complaints_id=complaint_id)
    #                ThisComment = Comments(user=request.user, complaints=complaint, comments=comments)
            ThisComment.save()
        complaint = Complaints.objects.get(id=id)
        form = CommentsForm()
        data = {
            'complaint': complaint,
            'form': form,
        }
        return render(request, "lookup.html", data)
    else:
        currentUrl = request.get_full_path()
        request.session['currentUrl'] = currentUrl
        print request.session['currentUrl']
        complaint = Complaints.objects.get(id=id)
        form = CommentsForm()
        data = {
            'complaint': complaint,
            'form': form,
        }
        return render(request, "lookup.html", data)
Example #3
0
def comments(username, p_id):
    user = query_db(
        'SELECT * FROM Users WHERE username="******";'.format(username), one=True)
    if user == None:
        flash('You are not logged in')
        return redirect(url_for('index'))
    elif user['password'] == session.get('password'):
        form = CommentsForm()
        if form.is_submitted():
            user = query_db(
                'SELECT * FROM Users WHERE username="******";'.format(username),
                one=True)
            query_db(
                'INSERT INTO Comments (p_id, u_id, comment, creation_time) VALUES({}, {}, "{}", \'{}\');'
                .format(p_id, user['id'], form.comment.data, datetime.now()))
        post = query_db('SELECT * FROM Posts WHERE id={};'.format(p_id),
                        one=True)
        all_comments = query_db(
            'SELECT DISTINCT * FROM Comments AS c JOIN Users AS u ON c.u_id=u.id WHERE c.p_id={} ORDER BY c.creation_time DESC;'
            .format(p_id))
        return render_template('comments.html',
                               title='Comments',
                               username=username,
                               form=form,
                               post=post,
                               comments=all_comments)
    else:
        return redirect(url_for('stream', username=session.get('username')))
Example #4
0
def post(id):
    article = Articles.query.get_or_404(id)
    posts = Articles.query.order_by(Articles.date_posted.desc()).all()
    comments = article.userscomments
    form = CommentsForm()
    if form.validate_on_submit():
        comment = Comments(comment=form.comments.data,user_id=current_user.id,article_id=article.id)
        db.session.add(comment)
        db.session.commit()
        flash('Your have successfully added your comment!', 'success')
        return redirect(url_for('post',id = id))
    return render_template('post.html', title=article.title, article = article,form=form,posts=posts,comments=comments)
Example #5
0
def film_info(film_name):
    post = Cinema.query.filter_by(name=film_name).first_or_404()
    form = CommentsForm()
    users = User
    user_comment = Comments.query.all()
    if form.validate_on_submit():
        add_comment = Comments(body=form.text.data, user_id=current_user.id)
        db.session.add(add_comment)
        db.session.commit()
    return render_template('film_info.html',
                           post=post,
                           form=form,
                           comments=user_comment,
                           user=users)
Example #6
0
def submitComments():
    form = CommentsForm()
    print("Hello 1")
    if form.validate_on_submit():
        print("Hello 2")
        comment = Comment(
            name=form.name.data,
            comment=form.comment.data)  #commentID=form.commentID.data)
        db.session.add(comment)
        db.session.commit()
        flash('Your comment has been posted!')
        print("Hello 3")
        return redirect(url_for('viewOtherComments'))
    print("Hello 4")
    return render_template('comments.html', title='Comments', form=form)
Example #7
0
def updatecomment(commentid,articleid):
    article = Articles.query.get_or_404(str(articleid))
    comment = Comments.query.get_or_404(str(commentid))
    if comment.writer != current_user:
        abort(403)

    form = CommentsForm()
    if form.validate_on_submit():
        comment.comment = form.comments.data
        db.session.commit()
        flash('Your comment has been updated!', 'success')
        return redirect(url_for('post',id = article.id))
    elif request.method == 'GET':
        form.comments.data = comment.comment
    return render_template('post.html', title = 'Update comment', form = form,article=article)
def comments(p_id):
    form = CommentsForm()
    if form.validate_on_submit():
        preparedQuery = 'INSERT INTO Comments (p_id, u_id, comment, creation_time) VALUES(?, ?, ?, ?);'
        data = (p_id, current_user.id, form.comment.data, datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
        safe_query(preparedQuery, data)

    preparedQuery = 'SELECT * FROM Posts WHERE id=?;'
    post = safe_query(preparedQuery, (p_id,), one=True)

    preparedQuery = 'SELECT DISTINCT * FROM Comments AS c '\
    'JOIN Users AS u ON c.u_id=u.id '\
    'WHERE c.p_id=?'\
    'ORDER BY c.creation_time DESC;'
    all_comments = safe_query(preparedQuery, (p_id,))
    return render_template('comments.html', title='Comments', form=form, post=post, comments=all_comments)
Example #9
0
def comments(username, p_id):
    if username != current_user.username:
        return redirect(current_user.username)

    user = User.query.filter_by(username = username).first()
    form = CommentsForm()
    if form.is_submitted():
       

        comment = Comment(p_id=p_id, u_id=user.id, comment = form.comment.data, creation_time = datetime.now())
        db.session.add(comment)
        db.session.commit()

    post = Post.query.filter_by(id = p_id).first()
    if not post:
        return error()
    all_comments = db.session.query(Comment, User, Post).join(User, User.id == Comment.u_id).join(Post, Post.id == Comment.p_id).filter(Comment.p_id == post.id).all()
    all_comments.sort(key=sortComments, reverse=True)
    return render_template('comments.html', title='Comments', username=username, form=form, post=post, comments=all_comments)
Example #10
0
def comments(username, p_id):
    form = CommentsForm()
    if form.is_submitted():
        user = query_db('SELECT * FROM Users WHERE username= ?;', (username, ),
                        one=True)
        query_db(
            'INSERT INTO Comments (p_id, u_id, comment, creation_time) VALUES(?, ?, ?, ?);',
            (p_id, user['id'], form.comment.data, datetime.now()))

    post = query_db('SELECT * FROM Posts WHERE id= ?;', (p_id, ), one=True)
    all_comments = query_db(
        'SELECT DISTINCT * FROM Comments AS c JOIN Users AS u ON c.u_id=u.id WHERE c.p_id= ? ORDER BY c.creation_time DESC;',
        (p_id, ))
    return render_template('comments.html',
                           title='Comments',
                           username=username,
                           form=form,
                           post=post,
                           comments=all_comments)
Example #11
0
def quadcopter(username):
    user = User.query.filter_by(username=username).first_or_404()
    form = CommentsForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your comment is now live')
        return redirect(url_for('quadcopter', username=user.username))
    page = request.args.get('page', 1, type=int)
    posts = Post.query.order_by(Post.timestamp.desc()).paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('quadcopter', username = user.username, page = posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('quadcopter', username = user.username, page = posts.prev_num) \
        if posts.has_prev else None
    return render_template('quadcopter.html',
                           title='Quadcopter',
                           user=user,
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Example #12
0
def comments(username, p_id):
    if (current_user.username != username):
        return redirect(
            url_for('comments', username=current_user.username, p_id=p_id))
    else:
        form = CommentsForm()
        if form.is_submitted():
            comment = sanitizeStr(form.comment.data)
            # Dont post anything if form is empty
            if comment == '':
                return redirect(
                    url_for('comments',
                            username=current_user.username,
                            p_id=p_id))

            user = query_db('SELECT * FROM Users WHERE username=?',
                            username,
                            one=True)
            query_db(
                'INSERT INTO Comments (p_id, u_id, comment, creation_time) VALUES(?, ?, ?, ?)',
                p_id, user['id'], comment, datetime.now())
            return redirect(
                url_for(
                    'comments', username=current_user.username,
                    p_id=p_id))  # this clears the form after successfull post.

        post = query_db('SELECT * FROM Posts WHERE id=?', p_id, one=True)
        all_comments = query_db(
            'SELECT DISTINCT * FROM Comments AS c JOIN Users AS u ON c.u_id=u.id WHERE c.p_id=? ORDER BY c.creation_time DESC',
            p_id)
        return render_template('comments.html',
                               title='Comments',
                               username=username,
                               form=form,
                               post=post,
                               comments=all_comments)
Example #13
0
def post(id):
    form = CommentsForm(request.form)
    post = PostService.get_one(id)
    cs = CommentService.get_comments(id)

    if request.method == "GET":
        return render_template('web/post.html', form=form, post=post, cs=cs)

    post_id = int(id)
    name = form.name.data
    email = form.email.data
    comments = form.comments.data

    try:
        comment = CommentService.add_comment(post_id=post_id,
                                             name=name,
                                             email=email,
                                             comments=comments)
        flash('Add a comment successful!')
    except:
        flash('Failed to add a comment!')

    return render_template('web/post.html', form=form, post=post, cs=cs)