Esempio n. 1
0
def edit_comment(commentId: str):
    json_data = flask.request.json
    comment = Comment.get_by_id(commentId)

    # text
    text = json_data.get('text')
    if text:
        comment.text = text

    if not comment.save():
        return flask.jsonify({'msg': 'Error in saving data'}), 400
    return flask.jsonify({'msg': 'Success'}), 200
Esempio n. 2
0
def add_comment():
    json_data = flask.request.json

    # userId
    userId = json_data.get('userId')
    user = User.get_or_none(User.userId == userId)
    if not user:
        return flask.jsonify({'msg': 'Must provide valid userId'}), 400

    # recipeId
    recipeId = json_data.get('recipeId')
    recipe = Recipe.get_or_none(Recipe.id == recipeId)
    if not recipe:
        return flask.jsonify({'msg': 'Must provide valid recipeId'}), 400

    # text
    text = json_data.get('text')
    if not text:
        return flask.jsonify({'msg': 'Must provide non-empty text'}), 400

    comment = Comment(user=user, recipe=recipe, text=text)
    if not comment.save():
        return flask.jsonify({'msg': 'Error in saving data'}), 400
    return flask.jsonify({'msg': 'Success'}), 200
Esempio n. 3
0
def get_comments():
    userId = flask.request.args.get('userId')
    if not userId: return flask.jsonify({'msg': 'Must provide userId'}), 400

    recipeId = flask.request.args.get('recipeId')
    if not recipeId:
        return flask.jsonify({'msg': 'Must provide recipeId'}), 400

    query = Comment.select().join(User)
    if userId: query = query.where(User.userId == userId)
    if recipeId: query = query.where(Comment.recipe == recipeId)

    comments = [c for c in query]
    comment_dicts = [c.as_dict() for c in comments]
    return flask.jsonify({'msg': 'Success', 'data': comment_dicts}), 200
Esempio n. 4
0
def delete_comment(commentId: str):
    comment = Comment.get_by_id(commentId)
    if not comment.delete_instance():
        return flask.jsonify({'msg': 'Error in deleting data'}), 400
    return flask.jsonify({'msg': 'Success'}), 200
Esempio n. 5
0
def get_comment(commentId: str):
    comment = Comment.get_by_id(commentId)
    return flask.jsonify({'msg': 'Success', 'data': comment.as_dict()}), 200
Esempio n. 6
0
 def wrapper(commentId, *args, **kwargs):
     comment = Comment.get_or_none(Comment.id == commentId)
     if comment:
         return func(commentId, *args, **kwargs)
     else:
         return flask.jsonify({'msg': 'Comment does not exists'}), 400
Esempio n. 7
0
 def comments(self):
     from models.model_comment import Comment
     return Comment.select().where(Comment.recipe == self).order_by(
         Comment.updated_at)
Esempio n. 8
0
LikeRelation.bulk_create(likes)

# Step 8: Comments
with Path('data/comments.txt').open('r') as f:
    comment_texts = f.readlines()
    comment_texts = [c.strip() for c in comment_texts if c.strip()]

comment_now_texts = comment_texts
comments = []
for fromUserId in range(len(users)):
    for recipeId in range(len(recipes)):
        random.seed(1993 + fromUserId + recipeId)
        haveComment = random.randint(0, 10)
        if haveComment > 8:
            random.seed(2993 + fromUserId)
            numCommentTexts = random.randint(1, 3)
            random.seed(5993 + fromUserId)
            if numCommentTexts > len(comment_now_texts):
                comment_now_texts = comment_texts

            comment_selected_idxs = random.sample(
                range(len(comment_now_texts)), numCommentTexts)
            for c in comment_selected_idxs:
                comments.append(
                    Comment(user=fromUserId + 1,
                            recipe=recipeId + 1,
                            text=comment_texts[c]))
                comment_now_texts.remove(comment_texts[c])

Comment.bulk_create(comments)