Moved database and user generated content to /var/matemat

This commit is contained in:
s3lph 2018-07-11 16:04:08 +02:00
parent a6e7c244e4
commit 3a8497325f
12 changed files with 24 additions and 20 deletions

View file

@ -1,5 +1,5 @@
--- ---
image: s3lph/matemat-ci:20180710-03 image: s3lph/matemat-ci:20180711-01
stages: stages:
- test - test

View file

@ -2,6 +2,8 @@
FROM debian:buster FROM debian:buster
RUN useradd -d /home/matemat -m matemat RUN useradd -d /home/matemat -m matemat
RUN mkdir -p /var/matemat/db && chown matemat:matemat -R /var/matemat/db
RUN mkdir -p /var/matemat/upload && chown matemat:matemat -R /var/matemat/upload
RUN apt-get update -qy RUN apt-get update -qy
RUN apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools build-essential RUN apt-get install -y --no-install-recommends python3-dev python3-pip python3-coverage python3-setuptools build-essential
RUN pip3 install wheel pycodestyle mypy RUN pip3 install wheel pycodestyle mypy

View file

@ -13,4 +13,4 @@ if __name__ == '__main__':
port = int(sys.argv[1]) port = int(sys.argv[1])
# Start the web server # Start the web server
MatematWebserver(port=port).start() MatematWebserver(port=port, staticroot='/var/matemat/upload').start()

View file

@ -23,7 +23,7 @@ def admin(method: str,
if authlevel < 2: if authlevel < 2:
raise HttpException(403) raise HttpException(403)
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
user = db.get_user(uid) user = db.get_user(uid)
if method == 'POST' and 'change' in args: if method == 'POST' and 'change' in args:
handle_change(args, user, db) handle_change(args, user, db)
@ -78,8 +78,8 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No
if 'avatar' not in args: if 'avatar' not in args:
return return
avatar = bytes(args.avatar) avatar = bytes(args.avatar)
os.makedirs('./static/img/thumbnails/users/', exist_ok=True) os.makedirs('/var/matemat/upload/thumbnails/users/', exist_ok=True)
with open(f'./static/img/thumbnails/users/{user.id}.png', 'wb') as f: with open(f'/var/matemat/upload/thumbnails/users/{user.id}.png', 'wb') as f:
f.write(avatar) f.write(avatar)
except UnicodeDecodeError: except UnicodeDecodeError:
@ -111,8 +111,8 @@ def handle_admin_change(args: RequestArguments, db: MatematDatabase):
newproduct = db.create_product(name, price_member, price_non_member) newproduct = db.create_product(name, price_member, price_non_member)
if 'image' in args: if 'image' in args:
image = bytes(args.image) image = bytes(args.image)
os.makedirs('./static/img/thumbnails/products/', exist_ok=True) os.makedirs('/var/matemat/upload/thumbnails/products/', exist_ok=True)
with open(f'./static/img/thumbnails/products/{newproduct.id}.png', 'wb') as f: with open(f'/var/matemat/upload/thumbnails/products/{newproduct.id}.png', 'wb') as f:
f.write(image) f.write(image)
elif change == 'restock': elif change == 'restock':

View file

@ -13,7 +13,7 @@ def buy(method: str,
-> Union[str, bytes, PageletResponse]: -> Union[str, bytes, PageletResponse]:
if 'authenticated_user' not in session_vars: if 'authenticated_user' not in session_vars:
return RedirectResponse('/') return RedirectResponse('/')
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
uid: int = session_vars['authenticated_user'] uid: int = session_vars['authenticated_user']
user = db.get_user(uid) user = db.get_user(uid)
if 'n' in args: if 'n' in args:

View file

@ -13,7 +13,7 @@ def deposit(method: str,
-> Union[str, bytes, PageletResponse]: -> Union[str, bytes, PageletResponse]:
if 'authenticated_user' not in session_vars: if 'authenticated_user' not in session_vars:
return RedirectResponse('/') return RedirectResponse('/')
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
uid: int = session_vars['authenticated_user'] uid: int = session_vars['authenticated_user']
user = db.get_user(uid) user = db.get_user(uid)
if 'n' in args: if 'n' in args:

View file

@ -19,7 +19,7 @@ def login_page(method: str,
if method == 'GET': if method == 'GET':
return TemplateResponse('login.html') return TemplateResponse('login.html')
elif method == 'POST': elif method == 'POST':
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
try: try:
user: User = db.login(str(args.username), str(args.password)) user: User = db.login(str(args.username), str(args.password))
except AuthenticationError: except AuthenticationError:

View file

@ -12,7 +12,7 @@ def main_page(method: str,
session_vars: Dict[str, Any], session_vars: Dict[str, Any],
headers: Dict[str, str])\ headers: Dict[str, str])\
-> Union[bytes, str, PageletResponse]: -> Union[bytes, str, PageletResponse]:
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
if 'authenticated_user' in session_vars: if 'authenticated_user' in session_vars:
uid: int = session_vars['authenticated_user'] uid: int = session_vars['authenticated_user']
authlevel: int = session_vars['authentication_level'] authlevel: int = session_vars['authentication_level']

View file

@ -23,7 +23,7 @@ def modproduct(method: str,
if authlevel < 2: if authlevel < 2:
raise HttpException(403) raise HttpException(403)
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
authuser = db.get_user(auth_uid) authuser = db.get_user(auth_uid)
if not authuser.is_admin: if not authuser.is_admin:
raise HttpException(403) raise HttpException(403)
@ -47,7 +47,7 @@ def handle_change(args: RequestArguments, product: Product, db: MatematDatabase)
if change == 'del': if change == 'del':
db.delete_product(product) db.delete_product(product)
try: try:
os.remove(f'./static/img/thumbnails/products/{product.id}.png') os.remove(f'/var/matemat/upload/thumbnails/products/{product.id}.png')
except FileNotFoundError: except FileNotFoundError:
pass pass
@ -76,6 +76,6 @@ def handle_change(args: RequestArguments, product: Product, db: MatematDatabase)
if 'image' in args: if 'image' in args:
image = bytes(args.image) image = bytes(args.image)
if len(image) > 0: if len(image) > 0:
os.makedirs('./static/img/thumbnails/products/', exist_ok=True) os.makedirs('/var/matemat/upload/thumbnails/products/', exist_ok=True)
with open(f'./static/img/thumbnails/products/{product.id}.png', 'wb') as f: with open(f'/var/matemat/upload/thumbnails/products/{product.id}.png', 'wb') as f:
f.write(image) f.write(image)

View file

@ -23,7 +23,7 @@ def moduser(method: str,
if authlevel < 2: if authlevel < 2:
raise HttpException(403) raise HttpException(403)
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
authuser = db.get_user(auth_uid) authuser = db.get_user(auth_uid)
if not authuser.is_admin: if not authuser.is_admin:
raise HttpException(403) raise HttpException(403)
@ -47,7 +47,7 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No
if change == 'del': if change == 'del':
db.delete_user(user) db.delete_user(user)
try: try:
os.remove(f'./static/img/thumbnails/users/{user.id}.png') os.remove(f'/var/matemat/upload/thumbnails/users/{user.id}.png')
except FileNotFoundError: except FileNotFoundError:
pass pass
@ -83,6 +83,6 @@ def handle_change(args: RequestArguments, user: User, db: MatematDatabase) -> No
if 'avatar' in args: if 'avatar' in args:
avatar = bytes(args.avatar) avatar = bytes(args.avatar)
if len(avatar) > 0: if len(avatar) > 0:
os.makedirs('./static/img/thumbnails/users/', exist_ok=True) os.makedirs('/var/matemat/upload/thumbnails/users/', exist_ok=True)
with open(f'./static/img/thumbnails/users/{user.id}.png', 'wb') as f: with open(f'/var/matemat/upload/thumbnails/users/{user.id}.png', 'wb') as f:
f.write(avatar) f.write(avatar)

View file

@ -19,7 +19,7 @@ def touchkey_page(method: str,
if method == 'GET': if method == 'GET':
return TemplateResponse('touchkey.html', username=str(args.username), uid=int(str(args.uid))) return TemplateResponse('touchkey.html', username=str(args.username), uid=int(str(args.uid)))
elif method == 'POST': elif method == 'POST':
with MatematDatabase('test.db') as db: with MatematDatabase('/var/matemat/db/test.db') as db:
try: try:
user: User = db.login(str(args.username), touchkey=str(args.touchkey)) user: User = db.login(str(args.username), touchkey=str(args.touchkey))
except AuthenticationError: except AuthenticationError:

View file

@ -2,6 +2,8 @@
FROM debian:buster FROM debian:buster
RUN useradd -d /home/matemat -m matemat RUN useradd -d /home/matemat -m matemat
RUN mkdir -p /var/matemat/db && chown matemat:matemat -R /var/matemat/db
RUN mkdir -p /var/matemat/upload && chown matemat:matemat -R /var/matemat/upload
RUN apt-get update -qy RUN apt-get update -qy
RUN apt-get install -y --no-install-recommends sudo git docker.io python3-dev python3-pip python3-coverage python3-setuptools build-essential RUN apt-get install -y --no-install-recommends sudo git docker.io python3-dev python3-pip python3-coverage python3-setuptools build-essential
RUN pip3 install wheel pycodestyle mypy RUN pip3 install wheel pycodestyle mypy