def test_question_comment(self):
        """
        测试评论添加删除
        以及回复添加删除
        :return:
        """
        u = User.create(username='******', password='******')
        u1 = User.create(username='******', password='******')
        question = Question.create(author=u, description='dwdwdgg?')
        self.assertTrue(question.undelete_comments.count() == 0)
        self.assertTrue(question.comments.count() == 0)
        c = Comment.create(author=u1,
                           question=question,
                           topic_type='question',
                           body='fdjhfjdj')
        c1 = Comment.create(author=u1,
                            question=question,
                            topic_type='question',
                            body='lgkjgfdjhfjdj')
        self.assertTrue(question.comments.count() == 2)
        self.assertTrue(question.undelete_comments.count() == 2)
        r = Reply.create(comment=c, author=u, body='kllfdd')
        r1 = Reply.create(comment=c, author=u1, user=u, body='ldkkkfg')
        self.assertTrue(c.replies.count() == 2)

        u.delete_comment(c)  # 先删除评论
        self.assertTrue(c.replies.count() == 2)  # 不会影响回复
        self.assertTrue(question.comments.count() == 2)
        self.assertTrue(question.undelete_comments.count() == 1)

        u.delete_reply(r)  # 删除一条回复
        self.assertTrue(c.replies.count() == 1)
        u.delete_reply(r1)  #删除最后一条回复
        self.assertNotIn(c, question.comments.all())  #c不应该在所有评论里面
def test_submissionsentiment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)

    c1 = Comment.create(session, **MOCK_COMMENT1)
    c1_sentiment = comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **MOCK_COMMENT2)
    c2_sentiment = comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    comments = session.query(Comment).options(joinedload("sentiment")).all()

    comment_sentiments = []
    for c in comments:
        comment_sentiments.append({"polarity": c.sentiment.polarity, "subjectivity": c.sentiment.subjectivity})

    submission_sentiment = comment_sentiment_avg(comment_sentiments)
    submission_sentiment.update({"submission_id": s.id})

    ss = SubmissionSentiment.create(session, **submission_sentiment)

    submission_sentiments = session.query(SubmissionSentiment).all()
    ss1 = submission_sentiments[0]

    # test object form
    assert ss1.id == 1
    assert ss1.submission_id == s.id
    for k in submission_sentiment.keys():
        assert getattr(ss1, k) == submission_sentiment[k]

    # test values
    assert ss1.polarity < 0.5 and ss1.polarity > -0.5
    assert ss1.subjectivity > 0.8
def test_comment_sentiment_avg(session):
    s = Submission.create(session, **const.MOCK_SUBMISSION)

    c1 = Comment.create(session, **const.MOCK_COMMENT1)
    c1_sentiment = sentiment.comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **const.MOCK_COMMENT2)
    c2_sentiment = sentiment.comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    comments = session.query(Comment).\
        options(joinedload('sentiment')).\
        all()

    comment_sentiments = []
    for c in comments:
        comment_sentiments.append({
            "polarity": c.sentiment.polarity,
            "subjectivity": c.sentiment.subjectivity
        })

    csa = sentiment.comment_sentiment_avg(comment_sentiments)
    csa.update({'submission_id': s.id})

    expected_keys = ['submission_id', 'polarity', 'subjectivity']
    assert sorted(csa.keys()) == sorted(expected_keys)

    assert isinstance(csa['polarity'], float)
    assert isinstance(csa['subjectivity'], float)
def test_commentsentiment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)

    c1 = Comment.create(session, **MOCK_COMMENT1)
    c1_sentiment = comment_sentiment(c1)
    c1s = CommentSentiment.create(session, **c1_sentiment)

    c2 = Comment.create(session, **MOCK_COMMENT2)
    c2_sentiment = comment_sentiment(c2)
    c2s = CommentSentiment.create(session, **c2_sentiment)

    # test object form
    for k in c1_sentiment.keys():
        assert getattr(c1s, k) == c1_sentiment[k]

    # test relationship
    assert c1.sentiment == c1s

    # test values
    assert c1s.id == 1
    assert c1s.polarity > 0.5
    assert c1s.subjectivity > 0.8
    assert c2s.id == 2
    assert c2s.polarity < -0.5
    assert c2s.subjectivity > 0.8
Exemple #5
0
    def post(self, post_id):
        content = self.get_argument('content', None)

        if content is None:
            raise exceptions.EmptyFields()
        else:
            users, content = at_content(content)
            username = self.current_user
            comment = yield gen.maybe_future(
                Comment.create(username, post_id, content))

            # Update gold.
            update_gold.apply_async(('sb_2l', username, post_id))
            update_gold.apply_async(('comment', username))
            if users:
                update_gold.apply_async(('be_comment', users))

            # Notify users: be comment, someone @you.
            notify.apply_async(
                ('comment', username, post_id, content))
            if users:
                notify.apply_async(
                    ('at', username, users, post_id, content))

            raise gen.Return(comment.to_dict())
Exemple #6
0
def favorite_comments(username, id):
    s = search()
    if s:
        return s
    user = User.query.filter_by(username=username).first()
    favorite = Favorite.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment.create(author=current_user._get_current_object(),
                                 body=form.body.data,
                                 favorite=favorite,
                                 topic_type='favorite')
        db.session.add(comment)
        db.session.commit()
        flash('评论添加成功')
        return redirect(
            url_for('.favorite_comments',
                    username=user.username,
                    id=favorite.id))
    page = request.args.get('page', 1, type=int)
    pagination = favorite.comments.order_by(Comment.timestamp.desc()).paginate(
        page,
        per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'],
        error_out=False)
    comments = pagination.items
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   user=user,
                   favorite=favorite)
    return render_template('favorite/favorite_comments.html', **context)
Exemple #7
0
def post(id):
    """文章详情页"""
    s = search()
    if s:
        return s
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if post.author != current_user:
        post.browsed()
    if form.validate_on_submit():
        comment = Comment.create(author=current_user._get_current_object(),
                                 post=post,
                                 body=form.body.data,
                                 topic_type='post')
        db.session.add(comment)
        db.session.commit()
        flash('评论添加成功', 'success')
        return redirect(url_for('.post', id=post.id))
    page = request.args.get('page', 1, type=int)
    pagination = post.comments.filter_by(topic_type='post'). \
        order_by(Comment.timestamp.desc()).paginate(page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'],
                                                    error_out=False)
    comments = pagination.items
    if post.disable_comment:
        return render_template('post/post.html', post=post)
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   post=post)
    return render_template('post/post.html', **context)
Exemple #8
0
    def append_comment(self, _id, user, comment):

        order = self.read(_id)

        comment = Comment.create(user=user, text=comment)
        OrderComment.create(order=order, comment=comment)

        return True
def test_comment_model(session):
    s = Submission.create(session, **MOCK_SUBMISSION)
    c1 = Comment.create(session, **MOCK_COMMENT1)
    c2 = Comment.create(session, **MOCK_COMMENT2)

    db_submissions = session.query(Submission).all()
    db_comments = session.query(Comment).all()

    assert db_submissions[0].comments[0] == c1
    assert db_submissions[0].comments[1] == c2
    assert len(db_comments) == 2

    db_c1 = db_comments[0]
    for k in MOCK_COMMENT1.keys():
        assert getattr(db_c1, k) == MOCK_COMMENT1[k]

    # test relationship
    assert s.comments == db_comments
Exemple #10
0
def question_comments(id):
    s = search()
    if s:
        return s
    form = CommentForm()
    question = Question.query.get_or_404(id)
    page = request.args.get('page', 1, type=int)
    if form.validate_on_submit():
        Comment.create(author=current_user._get_current_object(), question=question,
                       body=form.body.data, topic_type='question')
        question_comment_add.send(question)
        flash('评论添加成功', category='success')
        return redirect(url_for('.question_comments', id=question.id))

    pagination = question.comments.filter_by(topic_type='question').order_by(Comment.timestamp.desc()).paginate(
        page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False
    )
    comments = pagination.items
    context = dict(pagination=pagination, comments=comments, question=question, form=form)
    return render_template('question/question_comments.html', **context)
def test_comment_sentiment(session):
    s = Submission.create(session, **const.MOCK_SUBMISSION)

    c = Comment.create(session, **const.MOCK_COMMENT1)
    cs = sentiment.comment_sentiment(c)

    expected_keys = ['comment_id', 'polarity', 'subjectivity']
    assert sorted(cs.keys()) == sorted(expected_keys)

    assert isinstance(cs['polarity'], float)
    assert isinstance(cs['subjectivity'], float)
Exemple #12
0
def create(id, commenter_id: user_id, commenter, editors_pick, asset, content,
           ip_address, parent, created):
    comment = Comment.create(id=id,
                             commenter_id=commenter_id,
                             commenter=commenter,
                             editors_pick=editors_pick,
                             asset=asset,
                             content=content,
                             ip_address=ip_address,
                             parent=parent,
                             created=created)
    return comment.id
Exemple #13
0
def answer_comments(id):
    s = search()
    if s:
        return s
    answer = Answer.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        Comment.create(author=current_user._get_current_object(),
                       answer=answer,
                       topic_type='answer',
                       body=form.body.data)
        answer_comment_add.send(answer)
        flash('评论添加成功', 'success')
        return redirect(url_for('.answer_comments', id=answer.id))
    page = request.args.get('page', 1, type=int)
    pagination = answer.comments.filter_by(topic_type='answer').order_by(Comment.timestamp.desc()). \
        paginate(page, per_page=current_app.config['ZHIDAO_COMMENT_PER_PAGE'], error_out=False)
    comments = pagination.items
    context = dict(form=form,
                   pagination=pagination,
                   comments=comments,
                   answer=answer)
    return render_template('answer/answer_comments.html', **context)
    def create(self, user, text):

        comment = Comment.create(text=text, user=user)

        return comment