def details(connection, id, related):

    thread = dbConnector.select_query(
        connection,
        'SELECT date, forum, id, isClosed, isDeleted, message, slug, title, user, dislikes, likes, points, posts FROM thread WHERE id = %s;',
        ( id, )
    )

    if len(thread) == 0:
        raise Exception('Thread not founded')

    thread = thread[0]
    thread = {
        'date': str(thread[0]),
        'forum': thread[1],
        'id': thread[2],
        'isClosed': bool(thread[3]),
        'isDeleted': bool(thread[4]),
        'message': thread[5],
        'slug': thread[6],
        'title': thread[7],
        'user': thread[8],
        'dislikes': thread[9],
        'likes': thread[10],
        'points': thread[11],
        'posts': thread[12],
    }

    if "user" in related:
        thread["user"] = user.details(connection, thread["user"])
    if "forum" in related:
        thread["forum"] = forum.details(
            connection=connection, short_name=thread["forum"], related=[])

    return thread
def forum_details():
    connection = dbConnector.connect()
    params = functions.get_json(request)
    try:
        functions.check(params, ["forum"])
        response = forum.details(
            connection, params["forum"], functions.get_related(params))
    except Exception, e:
        connection.close()
        return json.dumps({"code": 3, "response": e.message})
示例#3
0
def details(connection, details_id, related):
    post = post_query(connection, details_id)
    if post is None:
        raise Exception("no post with id = " + details_id)

    if "user" in related:
        post["user"] = user.details(connection, post["user"])
    if "forum" in related:
        post["forum"] = forum.details(connection, post["forum"], [])
    if "thread" in related:
        post["thread"] = thread.details(connection, post["thread"], [])

    return post
def list(connection, required, optional, related):

    global threads
    query = "SELECT date, dislikes, forum, id, isClosed, isDeleted, likes, message, points, posts, slug, title, user FROM thread WHERE "

    if 'forum' in required:
        query += "forum = " + "\'" + str(required["forum"][0]) + "\'"
    if 'user' in required:
        query += "user = "******"\'" + str(required["user"][0]) + "\'"

    if 'since' in optional:
        since = optional["since"][0]
        query += " AND date >= " + "\'" + str(since) + "\'"

    if 'order' in optional:
        query += " ORDER BY date " + "".join(optional["order"])

    if 'limit' in optional:
        query += " LIMIT " + "".join(optional["limit"])

    try:
        threads = dbConnector.select_query(connection, query, ())
    except Exception as e:
        print (e.message)

    response = []
    if threads != ():
        for k in threads:
            k = {
                'date': str(k[0]),
                'dislikes': k[1],
                'forum': k[2],
                'id': k[3],
                'isClosed': bool(k[4]),
                'isDeleted': bool(k[5]),
                'likes': k[6],
                'message': k[7],
                'points': k[8],
                'posts': k[9],
                'slug': k[10],
                'title': k[11],
                'user': k[12]
            }
            if "user" in related:
                k["user"] = user.details(connection, k["user"])
            if "forum" in related:
                k["forum"] = forum.details(
                    connection=connection, short_name=k["forum"], related=[])
            response.append(k)

    return response
示例#5
0
def posts_list(connection, 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 + " = " + "'" + str("".join(identifier)) + "'"
    )

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

    query += " ORDER BY date " + "".join(params["order"])

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

    parameters += tuple("")
    query_res = dbConnector.select_query(connection, query, parameters)

    post_list = []
    for post in query_res:
        responseDict = {
            "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:
            responseDict["user"] = user.details(connection, responseDict["user"])
        if "forum" in related:
            responseDict["forum"] = forum.details(connection, short_name=responseDict["forum"], related=[])
        if "thread" in related:
            responseDict["thread"] = thread.details(connection, id=responseDict["thread"], related=[])
        post_list.append(responseDict)
    return post_list