示例#1
0
    def post(self, post_id, data):
        post = Post.get_one(post_id)
        if not post:
            raise InvalidUsage(404, 'Post not found')
        comment = Comment(post_id=post.id, author_id=current_user.id, **data)
        comment.save()

        return comment
示例#2
0
def comment_create(ident):
    form = CreateCommentForm()
    tweet = Tweet.query.filter_by(identifier=ident).first_or_404()
    if form.validate_on_submit():
        comment = Comment(textbody=form.textbody.data, identifier=Comment.get_identifier(), tweet=tweet, author=current_user, recipient=tweet.author)
        comment.save()
        tweet.author.add_notification('unread_notifs_count', tweet.author.new_notifs())
        db.session.commit()
        return redirect(url_for('tweets.tweet_show', ident=ident))
    return render_template('tweets/comment_create.html', form=form)
示例#3
0
    def test_comment_deleted(self):
        user = User(**self.user)
        user.save()
        post = Post(**self.post, owner_id=user.id)
        post.save()
        comment = Comment(**self.comment, post_id=post.id, author_id=user.id)
        comment.save()

        res = self.authorized_delete(
            f"/posts/{post.id}/comments/{comment.id}",
            user
        )

        self.assertEqual(res.status_code, 200)
        self.assertEqual(res.json['deleted'], comment.id)
示例#4
0
    def test_comment_unauthorized_delete(self):
        user = User(**self.user)
        user.save()
        user2 = User('Another', 'another', '*****@*****.**', 'secret')
        user2.save()

        post = Post(**self.post, owner_id=user.id)
        post.save()
        comment = Comment(**self.comment, post_id=post.id, author_id=user.id)
        comment.save()

        res = self.authorized_delete(f"/posts/{post.id}", user2)

        self.assertEqual(res.status_code, 403)
        self.assertEqual(res.json['message'], 'Permission denied')
示例#5
0
def comment(id):
    if request.method == 'POST':
        if request.form['body'] != '':
            new_comment = Comment(body=request.form['body'],
                                  author=current_user.id,
                                  post_id=id)
            if 'image_url' in request.form:
                new_comment.image_url = request.form['image_url']
            db.session.add(new_comment)
            db.session.commit()
            flash('Successfully posted comment', 'success')
            return redirect(url_for('root.home'))
        else:
            flash('User try to post an empty comment', 'warning')
    return redirect(url_for('root.home'))
示例#6
0
def post(post_id):
    current_post = Post.query.get_or_404(post_id)
    form = CommentForm()
    #page = request.args.get('commentsPage', 1, type=int)
    comments = Comment.query.filter(Comment.post_id == post_id).order_by(
        Comment.date_posted.desc()).all()

    if request.method == 'POST':
        if current_user.is_authenticated:
            if form.validate_on_submit():
                new_comment = Comment(body=form.body.data,
                                      post_id=post_id,
                                      author=current_user)
                db.session.add(new_comment)
                db.session.commit()
                flash('Your comment has been submitted', 'success')
                return redirect(url_for('posts.post', post_id=post_id))
        else:
            flash('You need to be logged in to submit a comment.', 'warning')
            return redirect(url_for('users.login'))

    return render_template('posts/post.html',
                           title=current_post.title,
                           post=current_post,
                           form=form,
                           comments=comments)
示例#7
0
    def test_comment_unauthorized_update(self):
        new_data = {
            'contents': 'New contents'
        }
        user = User(**self.user)
        user.save()
        user2 = User('Another', 'another', '*****@*****.**', 'secret')
        user2.save()
        post = Post(**self.post, owner_id=user.id)
        post.save()
        comment = Comment(**self.comment, post_id=post.id, author_id=user.id)
        comment.save()

        res = self.authorized_put(
            f"/posts/{post.id}/comments/{comment.id}",
            user2,
            json=new_data
        )

        self.assertEqual(res.status_code, 403)
        self.assertEqual(res.json['message'], 'Permission denied')
示例#8
0
    def test_comment_updated(self):
        new_data = {
            'contents': 'New contents'
        }
        user = User(**self.user)
        user.save()
        post = Post(owner_id=user.id, **self.post)
        post.save()
        comment = Comment(**self.comment, post_id=post.id, author_id=user.id)
        comment.save()

        res = self.authorized_put(
            f"/posts/{post.id}/comments/{comment.id}",
            user,
            json=new_data
        )

        data = res.json
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['contents'], new_data['contents'])
        self.assertEqual(data['author']['id'], user.id)
示例#9
0
    def test_comments_retrieved(self):
        COMMENTS_NUM = 5
        user = User(**self.user)
        user.save()
        post = Post(owner_id=user.id, **self.post)
        post.save()
        [Comment(**self.comment, post_id=post.id, author_id=user.id).save()
            for _ in range(COMMENTS_NUM)]

        res = self.client.get(f"/posts/{post.id}/comments")

        self.assertEqual(res.status_code, 200)
        self.assertEqual(len(res.json), COMMENTS_NUM)
示例#10
0
    def create(product_id):
        comment_form = CommentForm(request.form)

        if not comment_form.validate():
            return render("products/details.html",
                          comment_open=True,
                          comment_form=comment_form,
                          product=Product.query.get(product_id))

        comment = Comment(comment_form.comment.data, product_id,
                          current_user.id)

        if not comment.save("comment save"):
            flash("Kommentin lähettäminen epäonnistui", "error")
            return render("products/details.html",
                          comment_open=True,
                          comment_form=comment_form,
                          product=Product.query.get(product_id))

        # return back with empty form
        flash("Kommentti lisätty", "success")
        return render("products/details.html",
                      comment_form=CommentForm(),
                      product=Product.query.get(product_id))
示例#11
0
def comment_object(user, dgf_object_id):
    dgf_object = DgfObject.query.filter_by(dgf_id=dgf_object_id).first()
    if dgf_object is None:
        return make_response(('Object not found', 404))

    data = request.get_json(force=True) or {}
    try:
        valid_data = CommentSchema().load(data)
    except ValidationError as err:
        return make_response((err.messages, 400))
    comment = Comment(content=valid_data["content"],
                      author=user,
                      dgf_object=dgf_object,
                      written_at=datetime.datetime.now())
    db.session.add(comment)
    db.session.commit()
    schema = CommentSchema()
    data = schema.dump(comment)
    return make_response((data, 201))
示例#12
0
def comment(id):
    if request.method == "POST":
        new_comment = Comment(user=request.get_json()['user'],
                              tour_id=id,
                              comment=request.get_json()['comment_tour'])
        db.session.add(new_comment)
        db.session.commit()
        return jsonify({
            "id": new_comment.id,
            "user": new_comment.user,
            "tour_id": new_comment.tour_id,
            "comment": new_comment.comment
        })
    if request.method == "GET":
        comments = Comment.query.filter_by(tour_id=id).order_by(
            Comment.id.desc()).all()
        return jsonify({"comment": [comment.render() for comment in comments]})
    if request.method == "DELETE":
        remove_comment = Comment.query.filter_by(id=id).first()
        if remove_comment.user == request.get_json()['user']:
            db.session.delete(remove_comment)
            db.session.commit()
            return jsonify({"state": "success"})
        return jsonify({'state': 'notUser'})
示例#13
0
def add_comments():
    u1 = User.query.filter_by(username="******").first()
    u2 = User.query.filter_by(username="******").first()
    u3 = User.query.filter_by(username="******").first()
    p1 = Project.query.filter_by(code="SSWIKI").first()
    p2 = Project.query.filter_by(code="CRYPTO").first()
    p3 = Project.query.filter_by(code="RAINGEN").first()
    i1 = Issue.query.filter_by(project_code=p1.code, title="Fix TOC").first()

    # comments on issue
    issue_ecode = Entity.ISSUE.value
    c1 = Comment(
        created_by=u1.id,
        entity_id=i1.id,
        entity_code=issue_ecode,
        body="Does the draft contain everything? Is there more to come?",
        created_on=datetime.now() - timedelta(hours=6, seconds=22),
    )
    c2 = Comment(
        created_by=u2.id,
        entity_id=i1.id,
        entity_code=issue_ecode,
        body="I think they're waiting on a few suggestions.",
        created_on=datetime.now() - timedelta(hours=5, seconds=58),
    )

    # comments on project
    project_ecode = Entity.PROJECT.value
    c3 = Comment(
        created_by=u2.id,
        created_on=p2.created_on + timedelta(hours=1),
        entity_id=p2.id,
        entity_code=project_ecode,
        body="Corporate isn't too keen on DOGE. Just a heads up.",
    )
    c4 = Comment(
        created_by=u1.id,
        created_on=p2.created_on + timedelta(hours=2),
        entity_id=p2.id,
        entity_code=project_ecode,
        body=
        "I just checked, there's some disagreement about how useful it will be but they want us to do a feasibility study anyway.",
    )
    c5 = Comment(
        created_by=u3.id,
        created_on=p2.created_on + timedelta(hours=2, minutes=5),
        entity_id=p2.id,
        entity_code=project_ecode,
        body=
        "It's unknown territory for the whole board. Let's get a prelim out to them ASAP.",
    )
    c6 = Comment(
        created_by=u2.id,
        created_on=p3.created_on + timedelta(hours=1, minutes=20),
        entity_id=p3.id,
        entity_code=project_ecode,
        body="To put it mildly, this study is garbage. How is this a project?",
    )
    c7 = Comment(
        created_by=u3.id,
        created_on=p3.created_on + timedelta(hours=1, minutes=40),
        entity_id=p3.id,
        entity_code=project_ecode,
        body=
        "Well, there's a paying client. Apparently corporate is getting top dollar for this, so we should at least give them something.",
    )

    db.session.add_all([c1, c2, c3, c4, c5, c6, c7])
    db.session.commit()
示例#14
0
def post_comments():
    new_comments = Comment(request.form['comment'], request.form['author'],
                           datetime.utcnow())
    db.session.add(new_comments)
    db.session.commit()
    return jsonify(new_comments.toDictionary())
示例#15
0
from src.api import db
from src.models import Comment
from datetime import datetime

db.create_all()
db.session.add(Comment('Test Comment', 'admin', datetime.utcnow()))
db.session.commit()