nova-web/web/tos.py

57 lines
1.8 KiB
Python
Raw Normal View History

2023-08-27 18:10:10 +02:00
import os
2023-09-11 14:05:12 +02:00
import ujson
2023-08-27 18:10:10 +02:00
import flask
import random
import secrets
from dotenv import load_dotenv
2023-09-11 14:05:12 +02:00
from json.decoder import JSONDecodeError
2023-08-27 18:10:10 +02:00
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'):
2023-10-16 00:51:43 +02:00
os.makedirs('data', exist_ok=True)
2023-08-27 18:10:10 +02:00
open('data/tos.json', 'w', encoding='utf8').write('{}')
try:
2023-09-11 14:05:12 +02:00
data = ujson.load(open('data/tos.json', encoding='utf8'))
except JSONDecodeError:
2023-08-27 18:10:10 +02:00
data = {}
data[code] = emoji
2023-09-11 14:05:12 +02:00
ujson.dump(data, open('data/tos.json', 'w', encoding='utf8'))
2023-08-27 18:10:10 +02:00
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
2023-09-11 14:05:12 +02:00
data = ujson.load(open('data/tos.json', encoding='utf8'))
2023-08-27 18:10:10 +02:00
del data[code]
2023-09-11 14:05:12 +02:00
ujson.dump(data, open('data/tos.json', 'w', encoding='utf8'))
2023-08-27 18:10:10 +02:00
return 'Removed.', 204
2023-08-29 21:02:50 +02:00
@app.route('/legal/<path:subpath>')
def legal_site(subpath):
emoji = None
if subpath == 'terms':
2023-09-11 14:05:12 +02:00
emoji = ujson.load(open('data/tos.json', encoding='utf8')).get(flask.request.args.get('verify'))
2023-08-29 21:02:50 +02:00
if emoji:
emoji = emoji.encode('utf8')
return flask.render_template(f'legal/{subpath}.html', verify=emoji)