1
0
Fork 0
forked from s3lph/matemat
matemat/matemat/webserver/pagelets/buy.py

27 lines
879 B
Python

from typing import Any, Dict, Union
from matemat.webserver import pagelet, RequestArguments, PageletResponse, RedirectResponse
from matemat.db import MatematDatabase
@pagelet('/buy')
def buy(method: str,
path: str,
args: RequestArguments,
session_vars: Dict[str, Any],
headers: Dict[str, str]) \
-> Union[str, bytes, PageletResponse]:
if 'authenticated_user' not in session_vars:
return RedirectResponse('/')
with MatematDatabase('test.db') as db:
uid: int = session_vars['authenticated_user']
user = db.get_user(uid)
if 'n' in args:
n = int(str(args.n))
else:
n = 1
if 'pid' in args:
pid = int(str(args.pid))
product = db.get_product(pid)
db.increment_consumption(user, product, n)
return RedirectResponse('/')