コード例 #1
0
ファイル: routes.py プロジェクト: grantIee/cs1998
def create_comment(post_id):
    '''
    file: ./documentation/create_comment.yml
    '''

    post = Post.query.filter_by(id=post_id).first()
    if post is not None:
        request_body = json.loads(request.data)
        # Code here checks for blank body requests / @beforerequests checks for None body requests
        if not request_body.get('text') == '' and not request_body.get(
                'username') == '':
            comment = Comment(text=request_body.get('text'),
                              username=request_body.get('username'),
                              post_id=post_id)
            post.comments.append(comment)
            db.session.add(comment)
            db.session.commit()
            return json.dumps({
                'success': True,
                'data': comment.serialize()
            }), 201
        return json.dumps({
            'success': False,
            'error': 'invalid body format'
        }), 412
    return json.dumps({'success': False, 'error': 'Post not found!'}), 404
コード例 #2
0
def create_comment(user_id, post_id, body):
    comment = Comment(timestamp=datetime.datetime.now(),
                      content=body.get("content"),
                      edited=False,
                      user_id=user_id,
                      post_id=post_id)
    db.session.add(comment)
    db.session.commit()
    return comment.serialize()
コード例 #3
0
ファイル: fake_data.py プロジェクト: superArlen/blog
 def fake_comment(self, count: int = 100):
     """构造虚拟评论"""
     for i in range(count):
         comment_obj = Comment(
             author=self.fake.name(),
             content=self.fake.sentence(),
             post=session.query(Post).get(random.randint(1, session.query(Post).count())),
         )
         session.add(comment_obj)
     session.commit()
コード例 #4
0
ファイル: fake_data.py プロジェクト: superArlen/blog
 def fake_reply(self, count: int = 50):
     # 回复评论
     for i in range(count):
         reply_obj = Comment(
             author=self.fake.name(),
             content=self.fake.sentence(),
             post=session.query(Post).get(random.randint(1, session.query(Post).count())),
             replied=session.query(Comment).get(random.randint(1, session.query(Comment).count()))
         )
         session.add(reply_obj)
     session.commit()
コード例 #5
0
def create_comment(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if post is None:
        return json.dumps({'success': False, 'error': 'Post not found!'}), 404
    comment_body = json.loads(request.data)
    comment = Comment(text=comment_body.get('text'),
                      username=comment_body.get('username'),
                      post_id=post.id)
    post.comments.append(comment)
    db.session.add(comment)
    db.session.commit()
    return json.dumps({'success': True, 'data': comment.serialize()}), 201
コード例 #6
0
def create_comment(userid, postid):
    post_body = json.loads(request.data)
    try:
        comment = Comment(text=post_body.get('text'),
                          user_id=userid,
                          post_id=postid)
        db.session.add(comment)
        db.session.commit()

        return json.dumps({'success': True, 'data': comment.serialize()}), 201

    except KeyError as e:
        return json.dumps({'success': False, 'data': 'Invalid input'}), 404
コード例 #7
0
def create_comment(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if not post:
        return failure_response('Post not found')
    body = json.loads(request.data)
    name = body.get('name')
    description = body.get('description')
    if not body:
        return failure_response('Missing required field')
    new_comment = Comment(netid = body.get("netid"), name = name, description = description)
    post.comments.append(new_comment)
    db.session.add(new_comment)
    db.session.commit()
    return success_response(new_comment.serialize(), 201)
コード例 #8
0
def comment_post(post_id):
    """Vote on a post."""
    post_body = json.loads(request.data)
    post = Post.query.filter_by(id=post_id).first()
    if post is not None:
        comment = Comment(text=post_body['text'],
                          username=post_body['username'],
                          post_id=post.id)
        post.comment_count = post.comment_count + 1
        post.comments.append(comment)
        db.session.add(comment)
        db.session.commit()
        return json.dumps({'success': True, 'data': post.serialize()}), 200
    return json.dumps({'success': False, 'error': 'Invalid post!'}), 404
コード例 #9
0
def post_com(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if post is not None:
        com_body = json.loads(request.data)
        com = Comment(
            content=com_body.get("content"),
            username=com_body.get("username"),
            post_id=post_id,
        )
        post.comments.append(com)
        db.session.add(com)
        db.session.commit()
        return json.dumps({"success": True, "data": com.serialize()})
    return json.dumps({"success": False, "error": "Post not found"}), 404
コード例 #10
0
ファイル: app.py プロジェクト: ljgreenhill/BearMarket
def comment_on_item(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if post is None:
        return failure_response('Item not found')
    if post.active != None and post.active != True:
        return failure_response('Item inactive')
    body = json.loads(request.data)
    if body.get('content') is None:
        return failure_response('No message provided')
    if not logged_in(current_user):
        return failure_response('User not logged in')
    new_comment = Comment(sender=current_user.id,
                          content=body.get('content'),
                          post=post_id)
    db.session.add(new_comment)
    db.session.commit()
    return success_response(post.serialize())
コード例 #11
0
def add_post_comment(post_id):
    post = Post.query.filter_by(id=post_id, active=True).first()
    if post is None:
        return nopost()
    comment_body = extract(request)
    if 'text' not in comment_body or 'token' not in comment_body:
        return missing()
    uid = token_to_uid(comment_body)
    if uid is None:
        return invalid_token()
    comment = Comment(text=comment_body.get('text'),
                      uid=uid,
                      post_id=post_id,
                      creation_time=time.time())
    activate(uid)
    post.comments.append(comment)
    db.session.add(comment)
    db.session.commit()
    return json.dumps({'success': True, 'data': comment.serialize()}), 201
コード例 #12
0
def post_comment(post_id):
    post = Post.query.filter_by(id=post_id).first()
    if not post:
        return json.dumps({'success': False, 'error': 'Post not found'}), 404

    post_body = json.loads(request.data)
    body_comment = post_body.get('body')
    username = post_body.get('username')
    user = User.query.filter_by(username=username).first()
    if not user:
        return json.dumps({'success': False, 'error': 'user not found'}), 404
    user_id = user.id
    newComment = Comment(body_comment=body_comment,
                         time_stamp=datetime.now(),
                         date=date.today(),
                         user_id=user_id,
                         post_id=post_id)
    db.session.add(newComment)
    db.session.commit()
    return json.dumps({'success': True, 'data': newComment.serialize()}), 201
コード例 #13
0
ファイル: app.py プロジェクト: danielorourke/Ithaca-Trails
def create_comment(trail_id, review_id):
    trail = Trail.query.filter_by(id=trail_id, ).first()
    if trail is None:
        return failure_response('Trail not found!')
    review = Review.query.filter_by(id=review_id, trail=trail).first()
    if review is None:
        return failure_response('Review not found!')
    body = json.loads(request.data)

    new_comment = Comment(body=body.get('body'),
                          username=body.get('username'),
                          review_id=review_id,
                          review=review)

    if new_comment.body is None or new_comment.username is None:
        return failure_response('Could not create comment!')
    review.comments.append(new_comment)
    db.session.add(new_comment)
    db.session.commit()
    return success_response(new_comment.serialize(), 201)
コード例 #14
0
 def storeData(self, res):
     comment = Comment(comment=res)
     comment.save()
コード例 #15
0
from db import Genre, Film, User, Comment

path = "kinopoisk.sql"


def convertToBinaryData(filename):
    # Convert digital data to binary format
    with open(filename, 'rb') as file:
        blobData = file.read()
    return blobData


def write_file(data, filename):
    # Convert binary data to proper format and write it on Hard Disk
    with open(filename, 'wb') as file:
        file.write(data)


genresTable = Genre(path)
films = Film(path)
users = User(path)
commentTable = Comment(path)

tupleGenres = []
genre_map = {}
listGenres = genresTable.get_genres()
for i in listGenres:
    genre_map[i[1]] = i[0]
    tupleGenres.append(i[1])
コード例 #16
0
db.session.commit()
print('Added Question')

## create Answer resource to db
answer1 = Answer(
    question_id=1,  # has to exist
    answer_text='test_answer_text',
    explanation_text='test_explanation_text',
    is_correct=1)
db.session.add(answer1)
db.session.commit()
print('Added Answer')

## create Comment resource to db
comment1 = Comment(
    user_id=1,  # has to exist
    question_id=1,  # has to exist
    comment_text='test_comment_text')
db.session.add(comment1)
db.session.commit()
print('Added Comment')

## create Quiz resource to db
quiz1 = Quiz(user_id=1,
             created=datetime.now(),
             completed=datetime.now(),
             result='test_result',
             number_of_questions=1)
db.session.add(quiz1)
db.session.commit()
print('Added Quiz')