Exemplo n.º 1
0
def getDetailedPost(id, related):
    cursor = connection.cursor()
    try:
        cursor.execute(
            "SELECT "
            "posts.date AS date, "
            "posts.dislikes AS dislikes, "
            "forums.short_name AS forum, "
            "posts.id AS id, "
            "posts.isApproved AS isApproved, "
            "posts.isDeleted AS isDeleted, "
            "posts.isEdited AS isEdited, "
            "posts.isHighlighted AS isHighlighted, "
            "posts.isSpam AS isSpam, "
            "posts.likes AS likes, "
            "posts.message AS message, "
            "posts.post_id AS parent, "
            "posts.likes - posts.dislikes AS points, "
            "posts.thread_id AS thread, "
            "users.email AS user "
            "FROM posts RIGHT JOIN threads ON posts.thread_id = threads.id "
            "RIGHT JOIN forums ON threads.forum_id = forums.id "
            "JOIN users ON posts.user_id = users.id "
            "WHERE posts.id = %s """, (id,))
        post = dictfetchone(cursor)
        cursor.close()
        if not post:
            raise Exception("Post doesn't exist")
        if 'user' in related:
            from technopark_db_api_app.queries import user_queries
            post['user'] = user_queries.getDetailedUser(post['user'])
        if 'thread' in related:
            from technopark_db_api_app.queries import thread_queries
            post['thread'] = thread_queries.getDetailedThread(post['thread'],[])
        if 'forum' in related:
            from technopark_db_api_app.queries import forum_queries
            post['forum'] = forum_queries.getDetailedForum(post['forum'], [])

        post['isApproved'] = bool(post['isApproved'])
        post['isDeleted'] = bool(post['isDeleted'])
        post['isEdited'] = bool(post['isEdited'])
        post['isHighlighted'] = bool(post['isHighlighted'])
        post['isSpam'] = bool(post['isSpam'])
        post['date'] = post['date'].strftime("%Y-%m-%d %H:%M:%S")

    except Exception:
        raise
    else:
        return post
    finally:
        cursor.close()
Exemplo n.º 2
0
def details(request):
    try:
        parameters = request.GET.dict()
        validate_required_parameters(parameters, ['forum'])
        validate_optional_parameters(parameters, ['related'], [[]])
        forum = forum_queries.getDetailedForum(parameters['forum'], parameters['related'])
        response_json = {
            'code': 0,
            'response': forum,
        }
    except Exception as e:
        response_json = {
            'code': 1,
            'response': str(e),
        }
    return HttpResponse(json.dumps(response_json, ensure_ascii=False), content_type='application/json')
Exemplo n.º 3
0
def getDetailedThread(id, related):
    cursor = connection.cursor()
    try:
        cursor.execute(
            "SELECT "
            "threads.date AS date, "
            "threads.dislikes AS dislikes, "
            "forums.short_name AS forum, "
            "threads.id AS id, "
            "threads.isClosed AS isClosed, "
            "threads.isDeleted AS isDeleted, "
            "threads.likes AS likes, "
            "threads.message AS message, "
            "threads.likes - threads.dislikes AS points, "
            "COUNT(posts.id) AS posts, "
            "threads.slug AS slug, "
            "threads.title AS title, "
            "users.email AS user "
            "FROM forums JOIN threads ON forums.id = threads.forum_id "
            "JOIN users ON threads.user_id = users.id "
            "JOIN posts ON posts.thread_id = threads.id "
            "WHERE threads.id = %s""", (id,))

        thread = dictfetchone(cursor)
        cursor.close()
        thread['isClosed'] = bool(thread['isClosed'])
        thread['isDeleted'] = bool(thread['isDeleted'])
        thread['date'] = thread['date'].strftime("%Y-%m-%d %H:%M:%S")

        if 'user' in related:
            from technopark_db_api_app.queries import user_queries
            thread['user'] = user_queries.getDetailedUser(thread['user'])

        if 'forum' in related:
            from technopark_db_api_app.queries import forum_queries
            thread['forum'] = forum_queries.getDetailedForum(thread['forum'], [])

    except Exception:
        raise
    else:
        return thread
    finally:
        cursor.close()