from datetime import datetime from flask import ( Blueprint, flash, g, redirect, render_template, request, url_for ) from werkzeug.exceptions import abort from app.db import get_db from app.forms import TurnForm from app.email import send_email from app import app bp = Blueprint('index', __name__) @bp.route('/', methods=('GET', 'POST')) def index(): form = TurnForm() if form.validate_on_submit(): station_id = request.form['station'] db = get_db() db.execute( 'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?', (station_id,) ) turn = db.execute( 'SELECT last_turn FROM station WHERE s_id=?', (station_id,) ).fetchone()[0] db.execute( 'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)' ' VALUES (?, ?, ?, ?, ?)', (form.cName.data, form.cEmail.data, turn, '{0:%H:%M}'.format(datetime.now()), station_id) ) station_info = db.execute( 'SELECT nombre_empleado, oficina FROM station WHERE s_id=?', (station_id,) ).fetchone() db.commit() flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'], turn)) send_email('Your UPR turn', sender=app.config['MAIL_USERNAME'], recipients=[form.cEmail.data], text_body='Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'], turn), html_body='

Your turn for {} ({}) is number {}

'.format(station_info['nombre_empleado'], station_info['oficina'], turn)) return redirect(url_for('index')) db = get_db() stations = db.execute( 'SELECT s_id, nombre_empleado, oficina FROM station' ' ORDER BY oficina ASC' ).fetchall() return render_template('index.html', stations=stations, form=form)