Beispiel #1
0
def add_default_post_comments(user, posts):
    for post in posts:
        comment1 = Comment(content=f'I commented on my own post...',
                           user_id=user.id,
                           post_id=post.id)
        comment2 = Comment(content=f'Some comments here and there',
                           user_id=user.id,
                           post_id=post.id)
        db.session.add(comment1)
        db.session.add(comment2)
    db.session.commit()
Beispiel #2
0
    def post(self, id):
        args = request.json
        print(args)
        comment = Comment(topic_id=id,
                          parent_comment_id=args['parent_comment_id'],
                          content=args['content'],
                          created_by=self.db.query(User).first())

        self.db.add(comment)
        self.db.commit()
        return Response(json.dumps({
            'id':
            str(comment.id),
            'content':
            comment.content,
            'parent_id':
            str(comment.parent_comment_id)
            if comment.parent_comment_id else None,
            'created_by': {
                'id': str(comment.created_by_id),
                'email': comment.created_by.email,
            },
            'num_upvotes':
            int(comment.num_upvotes),
            'num_downvotes':
            int(comment.num_downvotes),
            'hotness_score':
            float(comment.hotness_score),
            'created_at':
            comment.created_at.isoformat(),
            'updated_at':
            comment.updated_at.isoformat(),
        }),
                        mimetype='application/json'), 201
Beispiel #3
0
def subreddit_post_comments(subreddit_id, post_id):
    post = find_post(subreddit_id, post_id)
    form = CommentForm(request.form)

    if request.method == 'GET':
        return render_template('single_post.html', post=post, form=form)

    if form.validate():
        comment = Comment(content=request.form.get('content'),
                          user_id=current_user.id,
                          post_id=post.id)
        db.session.add(comment)
        db.session.commit()
    return redirect(
        url_for('subreddits.subreddit_post',
                subreddit_id=subreddit_id,
                post_id=post_id))