Brak opisu

index.py 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. bp = Blueprint('index', __name__)
  9. @bp.route('/', methods=('GET', 'POST'))
  10. def index():
  11. form = TurnForm()
  12. if form.validate_on_submit():
  13. station_id = request.form['station']
  14. db = get_db()
  15. db.execute(
  16. 'UPDATE station SET last_turn = last_turn + 1 WHERE s_id=?',
  17. (station_id,)
  18. )
  19. turn = db.execute(
  20. 'SELECT last_turn FROM station WHERE s_id=?',
  21. (station_id,)
  22. ).fetchone()[0]
  23. db.execute(
  24. 'INSERT INTO turno (cName, cEmail, turn, timeArrival, station)'
  25. ' VALUES (?, ?, ?, ?, ?)',
  26. (form.cName.data, form.cEmail.data, turn, '{0:%H:%M}'.format(datetime.now()), station_id)
  27. )
  28. station_info = db.execute(
  29. 'SELECT nombre_empleado, oficina FROM station WHERE s_id=?',
  30. (station_id,)
  31. ).fetchone()
  32. db.commit()
  33. flash('Your turn for {} ({}) is number {}'.format(station_info['nombre_empleado'], station_info['oficina'], turn))
  34. return redirect(url_for('index'))
  35. db = get_db()
  36. stations = db.execute(
  37. 'SELECT s_id, nombre_empleado, oficina FROM station'
  38. ' ORDER BY oficina ASC'
  39. ).fetchall()
  40. return render_template('index.html', stations=stations, form=form)