nova-web/web/tos.py
NovaOSS Admins a1543f9a4f Fixes
2023-10-15 22:51:43 +00:00

57 lines
1.8 KiB
Python
Executable file

import os
import ujson
import flask
import random
import secrets
from dotenv import load_dotenv
from json.decoder import JSONDecodeError
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'):
os.makedirs('data', exist_ok=True)
open('data/tos.json', 'w', encoding='utf8').write('{}')
try:
data = ujson.load(open('data/tos.json', encoding='utf8'))
except JSONDecodeError:
data = {}
data[code] = emoji
ujson.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 = ujson.load(open('data/tos.json', encoding='utf8'))
del data[code]
ujson.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 = ujson.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)