From eb3679a005c09f7c843b6a7564b1b194e1c77ca6 Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 8 Oct 2019 16:18:19 -0400 Subject: [PATCH] add response type options --- saddle.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/saddle.py b/saddle.py index a57eaea..33bbcd3 100644 --- a/saddle.py +++ b/saddle.py @@ -31,6 +31,7 @@ async def index(request): return render_template("index.html", request, locals()) data = await request.post() rand_name = bool(data.get('rand_name')) + response_type = data.get('reponse_type') files = [] for filefield in data.getall('files'): @@ -59,13 +60,20 @@ async def index(request): for file in files: t = (int(request.cookies.get('userid')), file[0], file[1], expiration_date) files_insert.append(t) - async with request.app['pool'] as conn: + + async with request.app['pool'].acquire() as conn: await conn.executemany( "INSERT INTO upload (user_id, id, filename, expiration_date) " "VALUES ($1, $2, $3, $4)", files_insert) + urls = [config.upload_url + f[1] for f in files] - return render_template("result.html", request, locals()) + if response_type == 'html': + return render_template("result.html", request, locals()) + elif response_type == 'plain': + return web.Response(body='\n'.join(urls)) + elif response_type == 'json': + return web.json_response(urls) @routes.get('/gallery/{user_id}', name='gallery') @@ -76,7 +84,7 @@ async def gallery(request): except ValueError: raise web.HTTPNotFound - async with request.app['pool'] as conn: + async with request.app['pool'].acquire() as conn: uploads = await conn.fetch( "SELECT * FROM upload WHERE user_id = $1", user_id)