예제 #1
0
def api_comments(*, page='1'):
    page_index = get_page_index(page)
    num = yield from Comment.findNumber()
    p = Page(num, page_index)
    if num==0:
        return dict(page=p, comments=())
    comments = yield from Comment.findAll(orderBy='created_at desc', limit=(p.offset, p.limit))
    return dict(page=p, comments=comments)
예제 #2
0
def api_comments(*, page='1'):
    page_index = get_page_index(page)
    num = yield from Comment.findNumber()
    p = Page(num, page_index)
    if num == 0:
        return dict(page=p, comments=())
    comments = yield from Comment.findAll(orderBy='created_at desc',
                                          limit=(p.offset, p.limit))
    return dict(page=p, comments=comments)
예제 #3
0
def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = yield from Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id, user_id=user.id, user_name=user.name, user_image=user.image, content=content.strip())
    yield from comment.save()
    return comment
예제 #4
0
    def post(self):
        json_data = json.loads(request.data)
        if not json_data:
            return {'message': 'No input data provided'}, 400
        # Validate and deserialize input
        data, errors = comment_schema.load(json_data)
        if errors:
            return {"status": "error", "data": errors}, 422
        category_id = Category.query.filter_by(id=data['category_id']).first()
        if not category_id:
            return {
                'status': 'error',
                'message': 'comment category not found'
            }, 400
        if 'score' in json_data:
            return {
                'status': 'error',
                'message': 'Score can not be added when commenting'
            }, 422
        comment = Comment(category_id=data['category_id'],
                          comment=data['comment'],
                          score=0)
        db.session.add(comment)
        db.session.commit()

        result = comment_schema.dump(comment).data

        return {'status': "success", 'data': result}, 201
예제 #5
0
def api_delete_comment(id, request):
    check_admin(request)
    c = yield from Comment.find(id)
    if c is None:
        raise APIResourceNotFoundError('Comment')
    yield from c.remove()
    return dict(id=id)
예제 #6
0
def api_create_comment(id, request, *, content):
    user = request.__user__
    if user is None:
        raise APIPermissionError('Please signin first.')
    if not content or not content.strip():
        raise APIValueError('content')
    blog = yield from Blog.find(id)
    if blog is None:
        raise APIResourceNotFoundError('Blog')
    comment = Comment(blog_id=blog.id,
                      user_id=user.id,
                      user_name=user.name,
                      user_image=user.image,
                      content=content.strip())
    yield from comment.save()
    return comment
예제 #7
0
def post(id):
    from Model import db, Comment
    post = Post.query.get_or_404(id)
    form = CommentForm()
    if form.validate_on_submit():
        comment = Comment(body=form.body.data,
                          post=post,
                          author=current_user._get_current_object())

        db.session.add(comment)
        db.session.commit()
        flash('你的评论已发布')
        return redirect(url_for('.post', id=post.id, page=-1))

    page = request.args.get('page', 1, type=int)
    if page == -1:
        page=(post.comments.count()-1)// \
                current_app.config['FLASKY_COMMENTS_PER_PAGE']+1
    pagination = 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',
                           posts=[post],
                           form=form,
                           comments=comments,
                           pagination=pagination)
예제 #8
0
    def post(self):
        current_app.logger.info('Processing Comment POST')
        json_data = request.get_json(force=True)
        if not json_data:
            current_app.logger.error('No input data given to Comment POST')
            return {'message': 'No input data provided', 'error': 'true'}, 400
        # Validate and deserialize input
        data, errors = comment_schema.load(json_data)
        if errors:
            current_app.logger.error('Bad data given to Comment POST')
            return {"status": "error", "data": errors}, 422
        entry_id = Entry.query.filter_by(id=data['entry_id']).first()
        if not entry_id:
            current_app.logger.error('Parent entry missing in Comment POST')
            return {'status': 'error', 'message': 'comment entry not found', 'error': 'true'}, 400
        comment = Comment(
            entry_id=data['entry_id'], 
            comment=data['comment']
            )
        db.session.add(comment)
        db.session.commit()

        result = comment_schema.dump(comment).data

        current_app.logger.info('Successful Comment POST')
        return {'status': "success", 'data': result}, 201

    # You can add the other methods here...
예제 #9
0
    def post(self):
        print('hello post function')
        json_data = request.get_json(force=True)
        print(json_data)
        if not json_data:
            return {'message': 'No input data provided'}, 400
        # Validate and deserialize input
        data, errors = comment_schema.load(json_data)
        if errors:
            return errors, 422
        category_id = Category.query.filter_by(id=data['category_id']).first()
        print(category_id)
        if not category_id:
            return {
                'status': 'error',
                'message': 'comment category not found'
            }, 400
        comment = Comment(category_id=json_data['category_id'],
                          comment=json_data['comment'])
        print(comment)
        db.session.add(comment)
        db.session.commit()

        result = comment_schema.dump(comment).data
        print(result)

        return {'status': "success", 'data': result}, 201
예제 #10
0
def api_delete_comment(id, request):
    check_admin(request)
    c = yield from Comment.find(id)
    if c is None:
        raise APIResourceNotFoundError('Comment')
    yield from c.remove()
    return dict(id=id)
예제 #11
0
def get_blog(id):
    blog = yield from Blog.find(id)
    comments = yield from Comment.findAll('blog_id=?', [id],
                                          orderBy='created_at desc')
    for c in comments:
        c.html_content = text2html(c.content)
    blog.html_content = markdown2.markdown(blog.content)
    return {'__template__': 'blog.html', 'blog': blog, 'comments': comments}
예제 #12
0
def get_blog(id):
    blog = yield from Blog.find(id)
    comments = yield from Comment.findAll('blog_id=?', [id], orderBy='created_at desc')
    for c in comments:
        c.html_content =  text2html(c.content)
    blog.html_content = markdown2.markdown(blog.content)
    return {
        '__template__': 'blog.html',
        'blog': blog,
        'comments': comments        
    }
예제 #13
0
 def post(self):
     json_data = request.get_json(force=True)
     if not json_data:
         return {'message': 'No input data provided'}, 400
     # Validate and deserialize input
     data, errors = comment_schema.load(json_data)
     if errors:
         return {"status": "error", "data": errors}, 422
     category_id = Category.query.filter_by(id=data['category_id']).first()
     if not category_id:
         return {
             'status': 'error',
             'message': 'comment category not\
              found'
         }, 4
     comment = Comment(comment=json_data['comment'],
                       category_id=json_data['category_id'])
     db.session.add(comment)
     db.session.commit()
     result = comment_schema.dump(comment).data
     return {"status": 'success', 'data': result}, 201
예제 #14
0
    def post(self):
        json_data = request.get_json(force=True)

        if not json_data or not 'category_id' in json_data or not 'comment' in json_data:
            return {"error": "No input data provided"}, 400

        print(json_data)

        category = Category.query.filter_by(
            id=json_data['category_id']).first()

        if not category:
            return {'message': 'Category does not exists!'}, 400

        comment = Comment(comment=json_data['comment'],
                          category_id=json_data['category_id'])

        db.session.add(comment)
        db.session.commit()

        result = comment_schema.dump(comment)

        return {"message": "Saved successfully!", "data": result}, 201
예제 #15
0
    Category(name='Philosophy'),
    Category(name='Leadership'),
    Category(name='Marriage'),
    Category(name='Cooking'),
    Category(name='Technology'),
    Category(name='Spiritual')
]

languages = [
    Language(name='Spanish', code="SP"),
    Language(name='French', code='FR'),
    Language(name='German', code='GR')
]

comments = [
    Comment(comment='I know what I am doing!', category_id=6, score=0),
    Comment(comment='I want to pupu!', category_id=1, score=0),
    Comment(comment='Toyota is my best car', category_id=2, score=0),
    Comment(comment='I love shits', category_id=3, score=0),
    Comment(comment='He is a devoted man', category_id=4, score=0),
    Comment(comment='Wishes are not horses', category_id=5, score=0),
    Comment(comment='My wife is my best friend', category_id=7, score=0),
    Comment(comment='I love Nigeria jollof', category_id=8, score=0),
    Comment(comment='You have to learn the version 12.3.0',
            category_id=9,
            score=0),
    Comment(comment='I can see the hidden things of your mind',
            category_id=10,
            score=0),
    Comment(comment='This is just an extra joke', category_id=3, score=0)
]