nova-web/web/tos.py
2023-08-29 21:02:50 +02:00

56 lines
1.7 KiB
Python

import os
import json
import flask
import random
import secrets
from dotenv import load_dotenv
load_dotenv()
def register(app):
@app.route('/api/tos-verification', methods=['POST'])
def tos_verification_api():
if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'):
return 'Unauthorized', 401
code = secrets.token_urlsafe(6)
emoji = random.choice(open('config/emojis.txt', encoding='utf8').read())
if not os.path.exists('data/tos.json'):
open('data/tos.json', 'w', encoding='utf8').write('{}')
try:
data = json.load(open('data/tos.json', encoding='utf8'))
except json.decoder.JSONDecodeError:
data = {}
data[code] = emoji
json.dump(data, open('data/tos.json', 'w', encoding='utf8'))
return {
'code': code,
'emoji': emoji
}
@app.route('/api/tos-verification/<code>', methods=['DELETE'])
def tos_verification_api_delete(code):
if not flask.request.headers.get('Authorization') == os.getenv('TOS_VERIFICATION_KEY'):
return 'Unauthorized', 401
data = json.load(open('data/tos.json', encoding='utf8'))
del data[code]
json.dump(data, open('data/tos.json', 'w', encoding='utf8'))
return 'Removed.', 204
@app.route('/legal/<path:subpath>')
def legal_site(subpath):
emoji = None
if subpath == 'terms':
emoji = json.load(open('data/tos.json', encoding='utf8')).get(flask.request.args.get('verify'))
if emoji:
emoji = emoji.encode('utf8')
return flask.render_template(f'legal/{subpath}.html', verify=emoji)