Update Events (#10)
This update restructures the events page code and provides additional functionality. - Overwrite old entries on re-submission of previously used emails - Build data and stats caches on launch for use in submission checks and stats regeneration - Update to form content - Move stats function to separate utils file Reviewed-on: ayo/website#10 Co-authored-by: Jimmy Vargo <james@ayo.tokyo> Co-committed-by: Jimmy Vargo <james@ayo.tokyo>
This commit is contained in:
parent
b2bf426820
commit
dae67ce653
8 changed files with 128 additions and 53 deletions
|
|
@ -1,75 +1,62 @@
|
|||
from flask import Flask, request, render_template
|
||||
from datetime import datetime
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from flask import Flask, request, render_template
|
||||
|
||||
from form_content import FormContent
|
||||
from utils import get_data_stats
|
||||
|
||||
app = Flask(__name__)
|
||||
save_path = Path('data.txt')
|
||||
all_keys = set(FormContent.times | FormContent.topics | FormContent.languages) | { 'name', 'email', 'topic-other' }
|
||||
save_path = Path('data.json')
|
||||
data_cache = { entry['email']: entry for entry in json.loads(save_path.read_text()) }\
|
||||
if save_path.exists() else {}
|
||||
stats_cache = get_data_stats(data_cache)
|
||||
|
||||
def get_data_stats() -> dict:
|
||||
data = {}
|
||||
summed = {
|
||||
key: {
|
||||
'sum': 0,
|
||||
'total': 0,
|
||||
'percent': 0
|
||||
} for key in all_keys
|
||||
}
|
||||
summed['topic-other'] = set()
|
||||
MAX_FILE_SIZE = 1_000_000
|
||||
MAX_REQUEST_LENGTH = 1_000
|
||||
|
||||
if save_path.exists():
|
||||
for raw_line in save_path.read_text().split('\n'):
|
||||
if raw_line:
|
||||
line = json.loads(raw_line)
|
||||
data[line['email']] = line
|
||||
for line in data.values():
|
||||
for key, value in dict.items(line):
|
||||
if key == 'topic-other' and value is not None:
|
||||
summed['topic-other'] = summed['topic-other'] | { value }
|
||||
if type(value) is bool:
|
||||
sum = summed[key]['sum'] if key in summed else 0
|
||||
sum = sum + 1 if value is True else sum
|
||||
total = summed[key]['total'] + 1 if key in summed else 1
|
||||
summed[key] = {
|
||||
'sum': sum,
|
||||
'total': total,
|
||||
'percent': f'{((sum / total) * 100):.1f}'
|
||||
}
|
||||
summed['topic-other'] = list(summed['topic-other'])
|
||||
return summed
|
||||
|
||||
stats_cache = (0, get_data_stats())
|
||||
|
||||
@app.route('/')
|
||||
def form():
|
||||
return render_template('form.html', times=FormContent.times, topics=FormContent.topics, languages=FormContent.languages)
|
||||
return render_template('form.html',
|
||||
times=FormContent.times,
|
||||
topics=FormContent.topics,
|
||||
background=FormContent.background,
|
||||
languages=FormContent.languages)
|
||||
|
||||
|
||||
@app.route('/submit', methods=['POST'])
|
||||
def submit():
|
||||
if save_path.exists() and save_path.stat().st_size > 1_000_000:
|
||||
if save_path.exists() and save_path.stat().st_size > MAX_FILE_SIZE:
|
||||
return { 'error': 'Database full.' }, 500
|
||||
if len(request.data) > 1000:
|
||||
if len(request.data) > MAX_REQUEST_LENGTH:
|
||||
return { 'error': 'Request too long.' }, 400
|
||||
|
||||
data = request.get_json()
|
||||
|
||||
if set(data) != all_keys:
|
||||
if set(data) != FormContent.all_keys:
|
||||
return { 'error': 'Invalid request format.' }, 400
|
||||
|
||||
with save_path.open(mode='a') as file:
|
||||
file.write(f'{json.dumps(data)}\n')
|
||||
data['date_submitted'] = datetime.strftime(datetime.now(), '%Y/%m/%d, %H:%M:%S')
|
||||
|
||||
data_cache[data['email']] = data
|
||||
save_path.write_text(json.dumps(list(data_cache.values()), indent='\t'))
|
||||
|
||||
global stats_cache
|
||||
stats_cache = get_data_stats(data_cache)
|
||||
return {}, 200
|
||||
|
||||
|
||||
@app.route('/stats')
|
||||
def stats():
|
||||
global stats_cache
|
||||
if save_path.exists() and save_path.stat().st_mtime > stats_cache[0]:
|
||||
stats_cache = (save_path.stat().st_mtime, get_data_stats())
|
||||
return render_template('stats.html', times=FormContent.times, topics=FormContent.topics,
|
||||
languages=FormContent.languages, data=stats_cache[1])
|
||||
return render_template('stats.html',
|
||||
times=FormContent.times,
|
||||
topics=FormContent.topics,
|
||||
background=FormContent.background,
|
||||
languages=FormContent.languages,
|
||||
data=stats_cache)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(host='0.0.0.0', port=9700, debug=False)
|
||||
app.run(host='0.0.0.0', port=9700, debug=False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue