71 lines
1.8 KiB
Python
Executable File
71 lines
1.8 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
An email management frontend.
|
|
"""
|
|
import asyncio
|
|
|
|
from aiohttp import web
|
|
import jinja2
|
|
import aiohttp_jinja2
|
|
from aiohttp_jinja2 import render_template
|
|
import uvloop
|
|
import asyncpg
|
|
from passlib.hash import argon2
|
|
|
|
import forms
|
|
import config
|
|
import buckler_aiohttp
|
|
|
|
uvloop.install()
|
|
routes = web.RouteTableDef()
|
|
|
|
@routes.get('/', name='index')
|
|
@routes.post('/', name='index')
|
|
async def index(request):
|
|
"""The index page."""
|
|
user_id = int(request.cookies.get('userid'))
|
|
server_domain = config.server_domain
|
|
result = {}
|
|
|
|
async with request.app['pool'].acquire() as conn:
|
|
user_data = await conn.fetchrow(
|
|
"SELECT * FROM virtual_users WHERE buckler_id = $1",
|
|
user_id)
|
|
|
|
if not user_data and request.method == 'GET':
|
|
return render_template('index.html', request, locals())
|
|
|
|
if request.method == 'POST':
|
|
data = await request.post()
|
|
form_type = data.get('form_type')
|
|
if form_type == 'change_password':
|
|
result = await forms.change_password(request, user_data)
|
|
elif form_type == 'new_email':
|
|
result = await forms.new_email(request)
|
|
if result['ok']:
|
|
email = request['meta']['username'] + '@' +config.server_domain
|
|
user_data = {'email': email}
|
|
|
|
return render_template('index.html', request, locals())
|
|
|
|
|
|
async def init_app():
|
|
"""Initializes the application."""
|
|
app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
|
|
aiohttp_jinja2.setup(
|
|
app,
|
|
trim_blocks=True,
|
|
lstrip_blocks=True,
|
|
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)
|
|
return app_wrap
|
|
|
|
if __name__ == "__main__":
|
|
app = asyncio.run(init_app())
|
|
aiohttp.web.run_app(app, host='0.0.0.0', port=5050)
|