netclock/countdown/views.py

37 lines
1.2 KiB
Python
Raw Permalink Normal View History

2020-10-26 10:07:42 +00:00
from flask import render_template, request, redirect, url_for, session
2020-09-12 10:58:38 +00:00
2020-09-12 17:51:47 +00:00
from . import app
from . import forms
2020-09-13 13:07:11 +00:00
from .countdown import Cache
2020-09-12 10:58:38 +00:00
2020-10-26 10:07:42 +00:00
@app.route('', methods=['GET', 'POST'])
2020-09-13 13:07:11 +00:00
def create():
session.permanent = True
if not session.get('created_countdowns'):
session['created_countdowns'] = []
2020-09-12 10:58:38 +00:00
2020-09-12 17:51:47 +00:00
form = forms.CountdownAdminForm(request.form)
2020-09-12 10:58:38 +00:00
if request.method == 'POST' and form.validate():
2020-10-26 10:07:42 +00:00
cache = Cache.get_instance()
2020-09-13 13:07:11 +00:00
total = form.seconds.data or 0
total += (form.minutes.data or 0) * 60
total += (form.hours.data or 0) * 60 * 60
2020-09-13 18:04:03 +00:00
countdown = cache.add_countdown(total)
session['created_countdowns'].append(str(countdown['id']))
2020-09-13 13:07:11 +00:00
session.modified = True
return redirect(url_for('countdown.created'))
return render_template('countdown/create.html', form=form, clock=None)
2020-10-26 10:07:42 +00:00
2020-09-13 13:07:11 +00:00
@app.route('/mine', methods=['GET'])
def created():
2020-09-13 18:04:03 +00:00
return render_template('countdown/created.html', countdowns=session.get('created_countdowns') or [])
2020-09-13 13:07:11 +00:00
2020-10-26 10:07:42 +00:00
2020-09-13 13:07:11 +00:00
@app.route('/<uuid:countdown_id>', methods=['GET'])
def view(countdown_id):
2020-09-13 18:04:03 +00:00
return render_template('countdown/view.html', countdown_id=countdown_id)