Esempio n. 1
0
def create_a_comment():
    author_id     = int(request.headers.get('userid'))
    subscriber_id = request.form.get('suid')
    pid           = int(request.form.get('pid'))
    content       = replace(request.form.get('content'))
    if content.strip() == '':
        # No empty content allowed
        response = ErrorResponse()
        response.error['errorCode'] = '108'
        response.error['errorMsg'] = 'content cannot be empty'
        return jsonify(response.__dict__)
    sql = "INSERT INTO comments(content, pid, uid, subscriber) VALUES ('{}', '{}', '{}', '{}')" \
        .format(content, pid, author_id, subscriber_id)
    if VERBOSE:
        print("insert query:" + sql)
    query_mod(sql, DB)
    # Get the generated comment
    sql = "SELECT users.user_name,comments.* FROM comments, users " \
          "WHERE users.user_id = comments.uid AND pid = {} AND uid = {} AND subscriber = '{}' " \
          "ORDER BY timestamp DESC LIMIT 1" .format(pid, author_id, subscriber_id)
    if VERBOSE:
        print("get post_id query:" + sql)
    indicator = query_fetch(sql, DB)
    response = PostList()
    if indicator:
        print()
        response.data = indicator
    else:
        response = ErrorResponse()
        response.error['errorCode'] = '106'
        response.error['errorMsg'] = 'Somehow comment is not posted.'
    return jsonify(response.__dict__)
Esempio n. 2
0
def edit_a_comment(cid=None):
    uid  = int(request.headers.get('userid'))
    content = replace(request.form.get('content')) # could be a problem
    sql = "SELECT uid FROM comments WHERE cid = '{}'" .format(cid)
    indicator = query_fetch(sql, DB)
    # authentication
    if indicator:
        if uid != indicator['uid']:
            response = ErrorResponse()
            response.error['errorCode'] = '104'
            response.error['errorMsg'] = "No authority."
            return jsonify(response.__dict__)
    else:
        response = ErrorResponse()
        response.error['errorCode'] = '105'
        response.error['errorMsg'] = 'cid does not exist.'
        return jsonify(response.__dict__)

    # modification
    if content.strip() == '':
        # No empty content
        response = ErrorResponse()
        response.error['errorCode'] = '108'
        response.error['errorMsg'] = 'content cannot be empty.'
    sql = "UPDATE comments SET content='{}', timestamp = (CURRENT_TIMESTAMP) WHERE cid='{}'" \
        .format(content, cid)
    if VERBOSE:
        print(sql)
    query_mod(sql, DB)

    # get the changed comment
    sql = "SELECT * FROM comments WHERE cid = '{}'" .format(cid)
    if VERBOSE:
        print("get post_id query:" + sql)
    indicator = query_fetch(sql, DB)
    response = PostList()
    if indicator:
        response.data = indicator
    else:
        response = ErrorResponse()
        response.error['errorCode'] = '106'
        response.error['errorMsg'] = 'Somehow comment is not posted.'
    return jsonify(response.__dict__)