-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpong.py
172 lines (143 loc) · 5.12 KB
/
pingpong.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import sqlite3
from datetime import date
from flask import Flask, render_template, request, redirect, url_for, g
from pingpong.models.day import Day
from pingpong.models import week
from pingpong.models.ping_pong_tables import (
get_ping_pong_tables,
get_ping_pong_table
)
from pingpong.models.booking import Booking
from pingpong.models.booking_store import BookingStore, get_booking
from pingpong.models.booker import Booker
# Creación del objeto aplicación
app = Flask(__name__)
# Configuración
# True para el modo de depuración.
DEBUG = True
# Ubicación de la base de datos.
DB = 'db/pingpong.sqlite'
# Cargamos la configuración.
app.config.from_object(__name__)
# Ruta por defecto
@app.route('/')
def home():
"""
Obtiene el día correspondiente a hoy.
"""
today_date = date.today()
return list_bookings(today_date.year,
today_date.month,
today_date.day)
@app.route('/bookings/<int:year_num>/<int:month_num>/<int:day_num>.html')
def list_bookings(year_num, month_num, day_num):
"""
Obtiene el día correspondiente a los parámetros definidos
en la URL.
"""
day_date = date(year_num, month_num, day_num)
day_week = week.from_date(day_date)
day = Day(day_week, day_date)
ping_pong_tables = get_ping_pong_tables()
booking_store = BookingStore(g.db_con)
bookings = booking_store.get_bookings(day)
ping_pong_tables_data = [{
'ping_pong_table': ping_pong_table,
'time_slots': [
{
'time_slot': time_slot,
'booking': get_booking(time_slot,
ping_pong_table,
bookings)
}
for time_slot in day.time_slots
]
}
for ping_pong_table in ping_pong_tables]
view_data = {
'day_data': {
'day': day,
'tennis_tables_data': ping_pong_tables_data
}
}
return render_template('list_bookings.html',
view_data=view_data)
@app.route('/bookings/<int:year_num>/<int:month_num>/<int:day_num>/<time>/<table>.html',
methods=['GET', 'POST'])
def create_booking(year_num, month_num, day_num, time, table):
"""
Si request.method == 'GET': Muestra un formulario para introducir
los datos para crear una nueva reserva.
Si request.method == 'POST': Crea una nueva reserva.
"""
day_date = date(year_num, month_num, day_num)
day_week = week.from_date(day_date)
day = Day(day_week, day_date)
time_slot = day.get_time_slot(time)
ping_pong_table = get_ping_pong_table(table)
if request.method == 'POST':
booker = Booker(
request.form['booking_booker_name'],
request.form['booking_booker_email']
)
booking = Booking(
time_slot,
ping_pong_table,
booker,
request.form['booking_notes']
)
booking_store = BookingStore(g.db_con)
booking_store.save_booking(booking)
url = url_for('list_bookings',
year_num=year_num,
month_num=month_num,
day_num=day.date.day)
return redirect(url)
else:
return render_template('create_booking.html',
time_slot=time_slot,
table=ping_pong_table)
@app.route('/bookings/delete/<int:pk>.html',
methods=['GET', 'POST'])
def delete_booking(pk):
"""
Si request.method == 'GET': Muestra un resumen de la reserva
a cancelar.
Si request.method == 'POST': Elimina la reserva correspondiente
la clave primaria pk.
"""
booking_store = BookingStore(g.db_con)
booking = booking_store.get_booking_by_pk(pk)
if request.method == 'POST':
booking_store.delete_booking(pk)
day_date = booking.time_slot.week_day.date
url = url_for('list_bookings',
year_num=day_date.year,
month_num=day_date.month,
day_num=day_date.day)
return redirect(url)
else:
return render_template('delete_booking.html',
booking=booking)
@app.before_request
def load_db_connection():
"""
Crea una conexión con la base de datos y la
guarda en la lista de variables globales.
"""
g.db_con = sqlite3.connect(DB)
g.db_con.row_factory = sqlite3.Row
@app.teardown_request
def close_db_connection(exception):
"""
Cierra la conexión con la base de datos.
:param exception: La excepción, si existe, que ha causado
la destrucción de la petición.
"""
db_con = g.get('db_con', None)
if db_con is not None:
db_con.close()
if __name__ == '__main__':
app.run(debug=DEBUG)