Added rudimentary support for differente subjects and helper scripts.
This commit is contained in:
80
code.py
80
code.py
@@ -4,23 +4,61 @@
|
||||
import web
|
||||
|
||||
urls = (
|
||||
'/', 'Index'
|
||||
'/', 'Index',
|
||||
'/[0-9]+', 'Subject'
|
||||
)
|
||||
|
||||
render = web.template.render('templates/')
|
||||
|
||||
db = web.database(dbn = 'mysql', user = '', pw = '', db = '')
|
||||
|
||||
COOKIE_SUBJECT = "weblabs_cookie_subject"
|
||||
|
||||
def _get_schedule_list():
|
||||
schedules = db.query("SELECT sched_id, description FROM schedules WHERE sched_id < 8 ORDER BY sched_id ASC")
|
||||
cookie = web.cookies().get(COOKIE_SUBJECT)
|
||||
|
||||
if cookie is None or int(cookie) <= 1:
|
||||
return []
|
||||
|
||||
schedules = db.query("SELECT sched_id, description FROM schedules WHERE sched_id > 1 AND subject_id = " + str(cookie) + " " +
|
||||
"ORDER BY sched_id ASC")
|
||||
|
||||
lst = []
|
||||
for s in schedules:
|
||||
lst.append((s['sched_id'], s['description']))
|
||||
|
||||
return lst
|
||||
|
||||
def _get_subject_list():
|
||||
subjects = db.query("SELECT subject_id, name FROM subjects WHERE subject_id > 1 ORDER BY subject_id ASC")
|
||||
|
||||
lst = []
|
||||
for s in schedules:
|
||||
lst.append((s['sched_id'], s['description']))
|
||||
for s in subjects:
|
||||
lst.append((s['subject_id'], s['name']))
|
||||
|
||||
return lst
|
||||
|
||||
class Index:
|
||||
form = web.form.Form(
|
||||
web.form.Dropdown(
|
||||
'asignatura',
|
||||
_get_subject_list(),
|
||||
description = "Asignatura inscrita"
|
||||
),
|
||||
web.form.Button('Seleccionar')
|
||||
)
|
||||
|
||||
def GET(self):
|
||||
return render.index(self.form())
|
||||
|
||||
def POST(self):
|
||||
form = self.form()
|
||||
form.validates()
|
||||
subject_id = form.d.asignatura
|
||||
web.setcookie(COOKIE_SUBJECT, subject_id)
|
||||
raise web.seeother('/' + str(subject_id))
|
||||
|
||||
class Subject:
|
||||
|
||||
form = web.form.Form(
|
||||
web.form.Textbox(
|
||||
@@ -38,21 +76,45 @@ class Index:
|
||||
),
|
||||
web.form.Dropdown(
|
||||
'horario',
|
||||
_get_schedule_list(),
|
||||
[],
|
||||
description = "Horario a inscribir:"
|
||||
),
|
||||
web.form.Button('Registrar horario')
|
||||
)
|
||||
|
||||
|
||||
def GET(self):
|
||||
cookie = web.cookies().get(COOKIE_SUBJECT)
|
||||
|
||||
def validate_cookie(cookie):
|
||||
found = False
|
||||
|
||||
subjects = db.query("SELECT subject_id FROM subjects WHERE subject_id > 1 ORDER BY subject_id ASC")
|
||||
for s in subjects:
|
||||
if int(cookie) == int(s['subject_id']):
|
||||
found = True
|
||||
break
|
||||
|
||||
return found
|
||||
|
||||
if cookie is None or not validate_cookie(cookie):
|
||||
raise web.seeother('/')
|
||||
|
||||
|
||||
schedules = _get_schedule_list()
|
||||
form = self.form()
|
||||
form.horario.args = schedules
|
||||
|
||||
schedules = db.query(
|
||||
"SELECT schedules.sched_id, schedules.description, schedules.capacity, rooms.name " +
|
||||
"FROM schedules " +
|
||||
"INNER JOIN rooms ON schedules.room_id = rooms.room_id " +
|
||||
"ORDER BY schedules.sched_id ASC"
|
||||
"AND schedules.subject_id = " + str(cookie) + " " +
|
||||
"ORDER BY schedules.sched_id ASC "
|
||||
)
|
||||
|
||||
return render.index(schedules, self.form(), None)
|
||||
subject_name = db.query("SELECT name FROM subjects WHERE subject_id = " + str(cookie))
|
||||
|
||||
return render.subject(subject_name[0]['name'], schedules, form, None)
|
||||
|
||||
def POST(self):
|
||||
schedules = db.query(
|
||||
@@ -67,7 +129,7 @@ class Index:
|
||||
if not form.validates():
|
||||
return render.index(
|
||||
schedules,
|
||||
self.form,
|
||||
self.form(),
|
||||
"No deje los campos vacíos.<br/>La cédula debe ser un número."
|
||||
)
|
||||
|
||||
|
Reference in New Issue
Block a user