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 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
def user_details():

    connection = dbConnector.connect()
    params = functions.get_json(request)

    try:
        functions.check(params, ["user"])
        userr = user.details(connection, params["user"])
    except Exception as e:
        connection.close()
        return json.dumps({"code": 1, "response": e.message})

    connection.close()
    return json.dumps({"code": 0, "response": userr})
def details(connection, short_name, related):
    forum = dbConnector.select_query(
        connection, 'SELECT id, name, short_name, user FROM forum WHERE short_name = %s', (
            short_name,)
    )

    if len(forum) == 0:
        raise Exception("Forum " + short_name + " not found")

    forum = serialise_f(forum)

    if "user" in related:
        forum["user"] = user.details(connection, forum["user"])
    return forum
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
def forum_listUsers(connection, forum_shortname, optional):

    global posts
    query = "SELECT user.id, user.name, user.email FROM user " \
        "WHERE user.email IN (SELECT DISTINCT user FROM post WHERE forum = \'" + \
        str(forum_shortname) + "\')"
    if "since_id" in optional:
        query += " AND user.id >= " + str(optional["since_id"][0])
    if "order" in optional:
        query += " ORDER BY user.name " + str(optional["order"][0])
    if "limit" in optional:
        query += " LIMIT " + str(optional["limit"][0])

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

    response = []
    for post in posts:
        res = user.details(connection, str(post[2]))
        response.append(res)

    return response