This snippet gives a web2py scaffolding form for student attendance Copyright (C) 2013 Alan Etkin This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
The model
DAYS = ["2013-01-22", "2013-02-22", "2013-03-22", "2013-04-22", "2013-05-22", "2013-06-22"] STUDENTS = dict([(student.id, "%s %s" % (student.first_name, student.last_name)) for student in db(db.auth_user).select()]) db.define_table("attendance", Field("student", "reference auth_user"), Field("day", requires=IS_IN_SET(DAYS))) ATTENDANCES = ["%s_%s" % (attendance.day, attendance.student) for attendance in db(db.attendance).select()] def attended(day, student_id): s = "%s_%s" % (day, student_id) return s in ATTENDANCES
The controller
def attendance(): head = THEAD(TR([TD(), *TD(day) for day in DAYS])) body = TBODY(*[TR(TD("%s %s" % (student.first_name, student.last_name)), *[TD(INPUT(value=attended(day, student.id), _type="checkbox", _name="%s_%s" % (day, student.id))) for day in DAYS]) for student in STUDENTS]) form = FORM(TABLE(head, body)) if form.process().accepted: # Here you must check changes and apply them to the db for k, v in form.vars.iteritems(): day, student_id = k.split("_") if (not k in ATTENDANCES) and v: db.attendance.insert(student=student_id, day=day) elif k in ATTENDANCES and not v: db((db.attendance.student==student_id)&\ (db.attendance.day==day)).delete() return dict(form=form)
The view
{{=form}}
Comments (1)
0
samuel-bonilla-11088 11 years ago
good...