No Description

index.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from datetime import datetime
  2. from flask import (
  3. Blueprint, flash, g, redirect, render_template, request, url_for
  4. )
  5. from werkzeug.exceptions import abort
  6. from app.db import get_db
  7. from app.forms import TurnForm
  8. from app.email import send_email
  9. from app import app
  10. bp = Blueprint('index', __name__)
  11. @bp.route('/', methods=('GET', 'POST'))
  12. def index():
  13. form = TurnForm()
  14. if form.validate_on_submit():
  15. station_id = request.form['station']
  16. db = get_db()
  17. db.execute(
  18. 'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?',
  19. (station_id,)
  20. )
  21. turn = db.execute(
  22. 'SELECT last_turn FROM station WHERE s_id=?',
  23. (station_id,)
  24. ).fetchone()[0]
  25. db.execute(
  26. 'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)'
  27. ' VALUES (?, ?, ?, ?, ?)',
  28. (form.cName.data, form.cEmail.data, turn, '{0:%H:%M}'.format(datetime.now()), station_id)
  29. )
  30. station_info = db.execute(
  31. 'SELECT nombre_empleado, oficina FROM station WHERE s_id=?',
  32. (station_id,)
  33. ).fetchone()
  34. db.commit()
  35. flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'], turn))
  36. send_email('Your UPR turn',
  37. sender=app.config['MAIL_USERNAME'],
  38. recipients=[form.cEmail.data],
  39. text_body='Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'],
  40. station_info['oficina'],
  41. turn),
  42. html_body='<h3>Your turn for {} ({}) is number {}</h3>'.format(station_info['nombre_empleado'],
  43. station_info['oficina'],
  44. turn))
  45. return redirect(url_for('index'))
  46. db = get_db()
  47. stations = db.execute(
  48. 'SELECT s_id, nombre_empleado, oficina FROM station'
  49. ' ORDER BY oficina ASC'
  50. ).fetchall()
  51. return render_template('index.html', stations=stations, form=form)