From 86a896f78b1b104a02199575670805b3f3110f2c Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 17 Nov 2020 12:46:58 -0500 Subject: [PATCH] add database connection --- config.py.template | 11 +++++++++-- stickup.py | 8 +++++++- templates/index.html | 3 +++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/config.py.template b/config.py.template index e47060d..e305868 100644 --- a/config.py.template +++ b/config.py.template @@ -7,10 +7,17 @@ eg. https://example.com/stickup. """ server_domain = 'steelbea.me' url_prefix = '/stickup' - buckler = { 'url': "https://steelbea.me/buckler", 'app_id': 1, - 'app_key': """password""", + 'app_key': r"""password""", 'login_url': "https://steelbea.me/buckler/login", } + +mailserver_db = { + 'database': 'mailserver', + 'user': 'mailserver', + 'password': r"""password""", + 'host': 'localhost', + 'port': 5432, +} diff --git a/stickup.py b/stickup.py index 204e5ab..562c5aa 100755 --- a/stickup.py +++ b/stickup.py @@ -9,6 +9,7 @@ import jinja2 import aiohttp_jinja2 from aiohttp_jinja2 import render_template import uvloop +import asyncpg import config import buckler_aiohttp @@ -19,13 +20,17 @@ routes = web.RouteTableDef() @routes.get('/', name='index') async def index(request): """The index page.""" + async with request.app['pool'].acquire() as conn: + user_id = int(request.cookies.get('userid')) + email_addresses = await conn.fetch( + "SELECT email FROM virtual_users WHERE buckler_id = $1", + user_id) return render_template('index.html', request, locals()) async def init_app(): """Initializes the application.""" app = web.Application(middlewares=[buckler_aiohttp.buckler_session]) - #app = web.Application() aiohttp_jinja2.setup( app, trim_blocks=True, @@ -33,6 +38,7 @@ async def init_app(): undefined=jinja2.StrictUndefined, loader=jinja2.FileSystemLoader('templates'), ) + app['pool'] = await asyncpg.create_pool(**config.mailserver_db) app.router.add_routes(routes) app_wrap = web.Application() app_wrap.add_subapp(config.url_prefix, app) diff --git a/templates/index.html b/templates/index.html index dbf9b54..2146c17 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,5 +7,8 @@

This is a stickup!

+ {% for record in email_addresses %} +
Your email address is {{ record['email'] }}
+ {% endfor %}