예제 #1
0
def details():
    content = get_json(request)
    required_data = ["thread"]
    related = related_exists(content)
    if 'thread' in related:
        return json.dumps({"code": 3, "response": "error"})
    try:
        choose_required(data=content, required=required_data)
        thread = threads.details(id=content["thread"], related=related)
    except Exception as e:
        return json.dumps({"code": 1, "response": (e.message)})
    return json.dumps({"code": 0, "response": thread})
예제 #2
0
파일: posts.py 프로젝트: saidgadjiev/dbapi
def details(connect,details_id, related):
    post = post_query(connect,details_id)
    if post is None:
        raise Exception("no post with id = " + details_id)

    if "user" in related:
        post["user"] = users.details(connect,post["user"])
    if "forum" in related:
        post["forum"] = forums.details(connect=connect,short_name=post["forum"], related=[])
    if "thread" in related:
        post["thread"] = threads.details(connect=connect,id=post["thread"], related=[])

    return post
예제 #3
0
파일: thread.py 프로젝트: niggor/DB_TP
def details():
    content = get_json(request)
    required_data = ["thread"]
    related = related_exists(content)
    if 'thread' in related:
        return json.dumps({"code": 3, "response": "error"})
    try:
        choose_required(data=content, required=required_data)
        thread = threads.details(id=content["thread"], related=related)
        #print('thread is deleted')
        #print(thread['isDeleted'])
        #if thread['isDeleted'] == True:
        #    thread['posts'] = 0
        #    print(thread['posts'])
    except Exception as e:
        return json.dumps({"code": 1, "response": (e.message)})
    return json.dumps({"code": 0, "response": thread})
예제 #4
0
파일: posts.py 프로젝트: saidgadjiev/dbapi
def posts_list(connect, entity, params, identifier, related=[]):
    query = "SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, isHighlighted, isSpam, likes, message, " \
            "parent, points, thread, user FROM post WHERE " + entity + " = %s "

    parameters = [identifier]
    if "since" in params:
        query += " AND date >= %s"
        parameters.append(params["since"])

    query += " ORDER BY date " + params["order"]

    if "limit" in params:
        query += " LIMIT " + str(params["limit"])

    post_ids = DBconnect.select_query(connect=connect,query=query, params=parameters)
    post_list = []
    for post in post_ids:
        pf = {
                'date': str(post[0]),
                'dislikes': post[1],
                'forum': post[2],
                'id': post[3],
                'isApproved': bool(post[4]),
                'isDeleted': bool(post[5]),
                'isEdited': bool(post[6]),
                'isHighlighted': bool(post[7]),
                'isSpam': bool(post[8]),
                'likes': post[9],
                'message': post[10],
                'parent': post[11],
                'points': post[12],
                'thread': post[13],
                'user': post[14],
            }
        if "user" in related:
            pf["user"] = users.details(connect,pf["user"])
        if "forum" in related:
            pf["forum"] = forums.details(connect,short_name=pf["forum"], related=[])
        if "thread" in related:
            pf["thread"] = threads.details(connect,id=pf["thread"], related=[])
        post_list.append(pf)
    return post_list
예제 #5
0
def posts_list(entity, params, identifier, related=[]):
    # if entity == "forum":
    #     DBconnect.exist(entity="forum", identifier="short_name", value=identifier)
    # if entity == "thread":
    #     DBconnect.exist(entity="thread", identifier="id", value=identifier)
    # if entity == "user":
    #     DBconnect.exist(entity="user", identifier="email", value=identifier)
    query = "SELECT date, dislikes, forum, id, isApproved, isDeleted, isEdited, isHighlighted, isSpam, likes, message, " \
            "parent, points, thread, user, path FROM post WHERE " + entity + " = %s "

    parameters = [identifier]
    if "since" in params:
        query += " AND date >= %s"
        parameters.append(params["since"])

    if "sort" in params:
        if params["sort"] == "flat":
            query += " ORDER BY date "
        elif params["sort"] == "tree":
            if params["order"] == "desc":
                query += " ORDER BY  SUBSTRING_INDEX(path, '.', 2) DESC, TRIM(LEADING SUBSTRING_INDEX(path, '.', 2) FROM path) "
            else:
                query += " ORDER BY path "
            if "limit" in params:
                query += " LIMIT " + str(params["limit"])
        else:
            bound_query = "SELECT SUBSTRING_INDEX(MIN(t.path), '.', 2) AS left_bound FROM "\
                "(SELECT path FROM post WHERE thread = %s AND parent IS NULL ORDER BY path " + params["order"] or ""
            if "limit" in params:
                bound_query += " LIMIT " + str(params["limit"])
            bound_query += ") AS t;"
            left = DBconnect.select_query(bound_query, (identifier, ))[0][0]
            bound_query = "SELECT MAX(t.path) AS left_bound FROM "\
                "(SELECT path FROM post WHERE thread = %s AND parent IS NULL ORDER BY path " + params["order"] or ""
            if "limit" in params:
                bound_query += " LIMIT " + str(params["limit"])
            bound_query += ") AS t;"
            right = DBconnect.select_query(bound_query, (identifier, ))[0][0]

            query += " AND SUBSTRING_INDEX(path, '.', 2) BETWEEN %s AND %s ORDER BY " + \
                params["order"] == "desc" and " SUBSTRING_INDEX(path, '.', 2) DESC, " \
                "TRIM(LEADING SUBSTRING_INDEX(path, '.', 2) FROM path) " or " path "
            parameters.append(left)
            parameters.append(right)
    else:
        if "order" in params:
            query += " ORDER BY date " + params["order"]
        else:
            query += " ORDER BY date DESC"
        if "limit" in params:
            query += " LIMIT " + str(params["limit"])

    post_ids = DBconnect.select_query(query=query, params=parameters)
    post_list = []

    if "sort" in params and params["sort"] != "flat":
        for post in post_ids:
            path = post[15].split('.')[1:]
            if len(path) == 1:
                pf = {
                    'date': str(post[0]),
                    'dislikes': post[1],
                    'forum': post[2],
                    'id': post[3],
                    'isApproved': bool(post[4]),
                    'isDeleted': bool(post[5]),
                    'isEdited': bool(post[6]),
                    'isHighlighted': bool(post[7]),
                    'isSpam': bool(post[8]),
                    'likes': post[9],
                    'message': post[10],
                    'parent': post[11],
                    'points': post[12],
                    'thread': post[13],
                    'user': post[14],
                    'childs': walk(post_ids, path[0], 2)
                }
                post_list.append(pf)
    else:
        for post in post_ids:
            pf = {
                'date': str(post[0]),
                'dislikes': post[1],
                'forum': post[2],
                'id': post[3],
                'isApproved': bool(post[4]),
                'isDeleted': bool(post[5]),
                'isEdited': bool(post[6]),
                'isHighlighted': bool(post[7]),
                'isSpam': bool(post[8]),
                'likes': post[9],
                'message': post[10],
                'parent': post[11],
                'points': post[12],
                'thread': post[13],
                'user': post[14],
            }
            if "user" in related:
                pf["user"] = users.details(pf["user"])
            if "forum" in related:
                pf["forum"] = forums.details(short_name=pf["forum"], related=[])
            if "thread" in related:
                pf["thread"] = threads.details(id=pf["thread"], related=[])
            post_list.append(pf)
    return post_list