Esempio n. 1
0
def vote_post():
    cur = mysql.connection.cursor()
    try:
        json_obj = json.loads(json.dumps(request.json))
        post_id = json_obj["post"]
        vote = str(json_obj["vote"])
    except Exception:
        return json.dumps(responses_codes[2])
    if not post_id or not vote:
        return json.dumps(responses_codes[1])
    if vote not in {'1', '-1'}:
        return json.dumps(responses_codes[2])
    try:
        if vote == '1':
            cur.execute(
                '''UPDATE Post SET points = points + 1, likes = likes + 1 WHERE id = '%s' '''
                % post_id)
        else:
            cur.execute(
                '''UPDATE Post SET points = points - 1, dislikes = dislikes + 1 WHERE id = '%s' '''
                % post_id)
    except Exception:
        return json.dumps(responses_codes[5])
    response = get_post_entity([], post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {"code": 0, "response": response}
    return json.dumps(result, sort_keys=True)
Esempio n. 2
0
def vote_post():
    cur = mysql.connection.cursor()
    try:
        json_obj = json.loads(json.dumps(request.json))
        post_id = json_obj["post"]
        vote = str(json_obj["vote"])
    except Exception:
        return json.dumps(responses_codes[2])
    if not post_id or not vote:
        return json.dumps(responses_codes[1])
    if vote not in {'1', '-1'}:
        return json.dumps(responses_codes[2])
    try:
        if vote == '1':
            cur.execute('''UPDATE Post SET points = points + 1, likes = likes + 1 WHERE id = '%s' ''' % post_id)
        else:
            cur.execute('''UPDATE Post SET points = points - 1, dislikes = dislikes + 1 WHERE id = '%s' ''' % post_id)
    except Exception:
        return json.dumps(responses_codes[5])
    response = get_post_entity([], post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {
        "code": 0,
        "response": response
    }
    return json.dumps(result, sort_keys=True)
Esempio n. 3
0
def update_post():
    cur = mysql.connection.cursor()
    try:
        json_obj = json.loads(json.dumps(request.json))
        post_id = json_obj["post"]
        message = json_obj["message"]
    except Exception:
        return json.dumps(responses_codes[2])
    if not post_id or not message:
        return json.dumps(responses_codes[1])
    try:
        cur.execute('''SELECT message, isEdited FROM Post WHERE id = '%s' ''' %
                    post_id)
        res = cur.fetchone()
        isEdited = int(res[1])
        if res[0] != message:
            isEdited = 1

        cur.execute(
            '''UPDATE Post SET message='%s', isEdited='%s' WHERE id = '%s' '''
            % (
                message,
                isEdited,
                post_id,
            ))
    except Exception:
        return json.dumps(responses_codes[5])
    response = get_post_entity([], post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {"code": 0, "response": response}
    return json.dumps(result, sort_keys=True)
Esempio n. 4
0
def update_post():
    cur = mysql.connection.cursor()
    try:
        json_obj = json.loads(json.dumps(request.json))
        post_id = json_obj["post"]
        message = json_obj["message"]
    except Exception:
        return json.dumps(responses_codes[2])
    if not post_id or not message:
        return json.dumps(responses_codes[1])
    try:
        cur.execute('''SELECT message, isEdited FROM Post WHERE id = '%s' ''' % post_id)
        res = cur.fetchone()
        isEdited = int(res[1])
        if res[0] != message:
            isEdited = 1

        cur.execute('''UPDATE Post SET message='%s', isEdited='%s' WHERE id = '%s' ''' % (message, isEdited, post_id,))
    except Exception:
        return json.dumps(responses_codes[5])
    response = get_post_entity([], post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {
        "code": 0,
        "response": response
    }
    return json.dumps(result, sort_keys=True)
Esempio n. 5
0
def detail_post():
    related = request.args.getlist("related")
    post_id = int(request.args.get("post"))
    if not post_id:
        return json.dumps(responses_codes[2], sort_keys=True)
    response = get_post_entity(related, post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {"code": 0, "response": response}
    return json.dumps(result, sort_keys=True)
Esempio n. 6
0
def detail_post():
    related = request.args.getlist("related")
    post_id = int(request.args.get("post"))
    if not post_id:
        return json.dumps(responses_codes[2], sort_keys=True)
    response = get_post_entity(related, post_id)
    if response in responses_codes:
        return json.dumps(response, sort_keys=True)
    result = {
        "code": 0,
        "response": response
    }
    return json.dumps(result, sort_keys=True)
Esempio n. 7
0
def listPosts_thread():
    tree_posts_list = []
    cur = mysql.connection.cursor()

    thread_id = request.args.get("thread")
    if not thread_id:
        return json.dumps(responses_codes[2], sort_keys=True)
    sort = request.args.get("sort")
    since = request.args.get("since")
    limit = request.args.get("limit")
    order = request.args.get("order")

    if sort is None or sort == 'flat':
        results = {
            "code": 0,
            "response": list_posts([], since, limit, order, "thread", thread_id)
        }
        return json.dumps(results, sort_keys=True)

    if sort not in ['flat', 'tree', 'parent_tree']:
        return json.dumps(responses_codes[2], sort_keys=True)

    query = '''SELECT path FROM Post WHERE thread = '%s' '''
    query_params = (thread_id,)
    if since:
        query += "AND date >= '%s' "
        query_params += (since,)

    cur.execute(query % query_params)
    for x in cur.fetchall():
        tree_posts_list.append(x[0])

    tree_posts_list = natural_sort(tree_posts_list)

    if order is None or order == 'desc':
        tree_posts_list = sorted(tree_posts_list, key=lambda k: int(k.split('.')[0]), reverse=True)

    if limit and int(limit) <= len(tree_posts_list):
        n = int(limit)
    else:
        n = len(tree_posts_list)

    x = 0
    result_set = []
    if sort == 'tree':
        while x < n:
            split_list = tree_posts_list[x].split('.')
            result_set.append(get_post_entity([], int(split_list[len(split_list)-1])))
            x+=1
    else:
        i = 0
        prev_firs_id = tree_posts_list[0].split('.')[0]
        while x < n and i < len(tree_posts_list):
            split_list = tree_posts_list[i].split('.')
            if split_list[0] != prev_firs_id:
                x += 1
            if x < n:
                result_set.append(get_post_entity([], int(split_list[len(split_list)-1])))
            prev_firs_id = tree_posts_list[i].split('.')[0]
            i+=1

    results = {
        "code": 0,
        "response": result_set
    }
    return json.dumps(results, sort_keys=True)
Esempio n. 8
0
def listPosts_thread():
    tree_posts_list = []
    cur = mysql.connection.cursor()

    thread_id = request.args.get("thread")
    if not thread_id:
        return json.dumps(responses_codes[2], sort_keys=True)
    sort = request.args.get("sort")
    since = request.args.get("since")
    limit = request.args.get("limit")
    order = request.args.get("order")

    if sort is None or sort == 'flat':
        results = {
            "code": 0,
            "response": list_posts([], since, limit, order, "thread", thread_id)
        }
        return json.dumps(results, sort_keys=True)

    if sort not in ['flat', 'tree', 'parent_tree']:
        return json.dumps(responses_codes[2], sort_keys=True)

    query = '''SELECT path FROM Post WHERE thread = '%s' '''
    query_params = (thread_id,)
    if since:
        query += "AND date >= '%s' "
        query_params += (since,)

    cur.execute(query % query_params)
    for x in cur.fetchall():
        tree_posts_list.append(x[0])

    tree_posts_list = natural_sort(tree_posts_list)

    if order is None or order == 'desc':
        tree_posts_list = sorted(tree_posts_list, key=lambda k: int(k.split('.')[0]), reverse=True)

    if limit and int(limit) <= len(tree_posts_list):
        n = int(limit)
    else:
        n = len(tree_posts_list)

    x = 0
    result_set = []
    if sort == 'tree':
        while x < n:
            split_list = tree_posts_list[x].split('.')
            result_set.append(get_post_entity([], int(split_list[len(split_list)-1])))
            x += 1
    else:
        if tree_posts_list != []:
            i = 0
            prev_firs_id = tree_posts_list[0].split('.')[0]
            while x < n and i < len(tree_posts_list):
                split_list = tree_posts_list[i].split('.')
                if split_list[0] != prev_firs_id:
                    x+=1
                if x < n:
                    result_set.append(get_post_entity([], int(split_list[len(split_list)-1])))
                prev_firs_id = tree_posts_list[i].split('.')[0]
                i+=1

    results = {
        "code": 0,
        "response": result_set
    }
    return json.dumps(results, sort_keys=True)