コード例 #1
0
async def handle_get_posts(request):
    thread_slug_or_id = request.match_info['slug_or_id']
    limit = request.rel_url.query.get('limit', False)
    desc = request.rel_url.query.get('desc', False)
    since = request.rel_url.query.get('since', False)
    sort = request.rel_url.query.get('sort', 'flat')

    connection_pool = request.app['pool']
    connection = connection_pool.getconn()
    cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

    cursor.execute(Thread.query_get_thread(thread_slug_or_id))
    result = cursor.fetchone()
    if not result:
        connection_pool.putconn(connection)
        return web.json_response(
            status=404, data={"message": "Can't find user with id #42\n"})
    thread_id = result['id']
    cursor.execute(Post.query_get_posts(thread_id, since, sort, desc, limit))
    result = cursor.fetchall()

    for item in result:
        item['created'] = item['created'].astimezone().isoformat()

    connection_pool.putconn(connection)
    return web.json_response(status=200, data=result)
コード例 #2
0
async def handle_get_details(request):
    thread_slug_or_id = request.match_info['slug_or_id']

    connection_pool = request.app['pool']
    connection = connection_pool.getconn()
    cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

    cursor.execute(Thread.query_get_thread(thread_slug_or_id))
    result = cursor.fetchone()
    if not result:
        connection_pool.putconn(connection)
        return web.json_response(
            status=404, data={"message": "Can't find user with id #42\n"})

    data = result
    data['created'] = data['created'].astimezone().isoformat()
    connection_pool.putconn(connection)
    return web.json_response(status=200, data=data)