I have a client requirement for tables in my database to be prefixed
with 'tbl_' and to have an ID field caled recid. For legacy purposes,
auth_user is implemented as a VIEW on an existing table.
For this reason, my use of the Auth module is becoming quite
customised.
I had presumed that including the following arguments in the table
definition would map the table's ID to the correct field.
Field('recid','id')
primarykey=['recid']
However, I am getting this error:
Traceback (most recent call last):
File "gluon/restricted.py", line 173, in restricted
exec ccode in environment
File "/var/www/web2py/web2py.1.76.5/applications/MyApp/models/
db.py", line 104, in <module>
migrate=False)
File "gluon/sql.py", line 1263, in define_table
**dict(primarykey=args['primarykey']))
File "gluon/sql.py", line 2105, in __init__
field.requires = sqlhtml_validators(field)
File "gluon/sql.py", line 465, in sqlhtml_validators
requires = validators.IS_IN_DB(field._db,referenced.id,
File "gluon/sql.py", line 1573, in __getattr__
return dict.__getitem__(self,key)
KeyError: 'id'
Here is an example of the customisation that I have made in db/
models.py :
# tbl_auth_user
auth.settings.table_user_name = 'view_auth_user'
passfield = auth.settings.password_field
table = db.define_table(
auth.settings.table_user_name,
Field('recid','id'),
Field('first_name', length=50, default='',
label=auth.messages.label_first_name),
Field('last_name', length=50, default='',
label=auth.messages.label_last_name),
Field('long_name', length=50, default='', label='Long Name'),
Field('email', length=512, default='',
label=auth.messages.label_email),
Field('username', length=15, default='', unique=True),
Field(passfield, 'password', length=512,
readable=False,
label=auth.messages.label_password),
Field('registration_key', length=512,
writable=False, readable=False, default='',
label=auth.messages.label_registration_key),
primarykey=['recid'],
migrate=False,
format='%(first_name)s %(last_name)s (%(id)s)'
)
table.first_name.requires = \
IS_NOT_EMPTY(error_message=auth.messages.is_empty)
table.last_name.requires = \
IS_NOT_EMPTY(error_message=auth.messages.is_empty)
#table[passfield].requires = [CRYPT(key=auth.settings.hmac_key)]
table.email.requires = \
[IS_EMAIL(error_message=auth.messages.invalid_email),
IS_NOT_IN_DB(db, '%s.email' % auth.settings.table_user_name)]
table.username.requires = IS_NOT_IN_DB(db, table.username)
table.registration_key.default = ''
auth.settings.table_user = db[auth.settings.table_user_name]
view_auth_user has the following fields:
RECID
FIRST_NAME
LAST_NAME
LONG_NAME
PASSWORD2
USERNAME
EMAIL
REGISTRATION_KEY
The traceback falls over on line 104 of modesl/db.py which is the end
of db.define_table call below:
# auth_membership
auth.settings.table_membership_name = 'tbl_auth_membership'
table = db.define_table(
auth.settings.table_membership_name,
Field('recid','id'),
Field('user_id', auth.settings.table_user,
label=auth.messages.label_user_id),
Field('group_id', auth.settings.table_group,
label=auth.messages.label_group_id),
primarykey=['recid'],
migrate=False)
table.user_id.requires = IS_IN_DB(db, '%s.recid' %
auth.settings.table_user_name,
'%(first_name)s %(last_name)s (%(recid)s)')
table.group_id.requires = IS_IN_DB(db, '%s.recid' %
auth.settings.table_group_name,
'%(role)s (%(recid)s)')
auth.settings.table_membership =
db[auth.settings.table_membership_name]
Regards,
Chris Guest
--
To unsubscribe, reply using "remove me" as the subject.