I am trying to provide an editor for auth_user using SQLFORM. I also am
trying to add a checkbox to the bottom of the form to set whether or not the
user is an administrator. If so, then the user gets added to an
Administrators group when calling form.accepts().
The strange part is, the code works fine if you check the box, then click
submit, but if you try to uncheck the box, that's when you get the following
traceback:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
Traceback (most recent call last):
File "/media/psf/Python/web2py/gluon/restricted.py", line 192, in restricted
exec ccode in environment
File
"/Users/rosspeoples/Dropbox/Code/Python/web2py/applications/init/controllers/administration.py"
<http://127.0.0.1:8001/admin/edit/init/controllers/administration.py>, line
650, in <module>
File "/media/psf/Python/web2py/gluon/globals.py", line 137, in <lambda>
self._caller = lambda f: f()
File "/Users/rosspeoples/Dropbox/Code/Python/web2py/gluon/tools.py", line
2485, in f
return action(*a, **b)
File
"/Users/rosspeoples/Dropbox/Code/Python/web2py/applications/init/controllers/administration.py"
<http://127.0.0.1:8001/admin/edit/init/controllers/administration.py>, line
148, in security
return update()
File
"/Users/rosspeoples/Dropbox/Code/Python/web2py/applications/init/controllers/administration.py"
<http://127.0.0.1:8001/admin/edit/init/controllers/administration.py>, line
143, in update
return user_form(user)
File
"/Users/rosspeoples/Dropbox/Code/Python/web2py/applications/init/controllers/administration.py"
<http://127.0.0.1:8001/admin/edit/init/controllers/administration.py>, line
120, in user_form
if form.accepts(request.vars, session):
File "/media/psf/Python/web2py/gluon/sqlhtml.py", line 1203, in accepts
self.table._db(self.table._id == self.record.id).update(**fields)
File "/Users/rosspeoples/Dropbox/Code/Python/web2py/gluon/dal.py", line 5401,
in update
fields = self.db[tablename]._listify(update_fields,update=True)
File "/Users/rosspeoples/Dropbox/Code/Python/web2py/gluon/dal.py", line 4677,
in _listify
raise SyntaxError, 'Field %s does not belong to the table' % name
SyntaxError: Field is_administrator does not belong to the table
And here is the controller that is adding the checkbox:
form = SQLFORM(db.auth_user, user, showid=False)
form[0].insert(-1, TR((
TD(LABEL('Is Administrator',
_for='auth_user_is_administrator')),
TD(INPUT(_type='checkbox', _name='is_administrator',
_id='auth_user_is_administrator'))
)))
form.vars.is_administrator = False
if user:
if auth.has_membership(user_id=user.id, role='Administrators'):
form.vars.is_administrator = True
if form.accepts(request.vars, session):
admin_group =
db(db.auth_group.role=='Administrators').select().first()
db((db.auth_membership.user_id==user.id) &
(db.auth_membership.group_id==admin_group.id)).delete()
if form.vars.is_administrator:
db.auth_membership.insert(group_id=admin_group.id,
user_id=user.id)
return 'OK'
return dict(form=form)
The thing that is driving me nuts is that I am doing exactly what the book
says to do:
http://web2py.com/book/default/chapter/07#Adding-extra-form-elements-to-SQLFORM
Any thoughts? Thanks.