customizing auth_user

33 views
Skip to first unread message

selecta

unread,
Oct 22, 2009, 4:21:08 PM10/22/09
to web2py-users
First of all, thanks for this awsome tool. I love it! Just discoverd
it last Friday and I already build a rocking application that will go
live soon. After having years of experience building web pages and
also 3 years of Python I was almost ready to switch to RoR but always
go stuck learing Ruby then I discovered web2py by accident and I am
hooked Thanks Thanks Thanks.

But to buissness:
I followed http://www.web2py.com/examples/default/tools#authentication
to customize my auth_user table
First of all there is a mistake in the code for # define custom tables
instead of self.settings.table_user_name it should read
auth.settings.table_user_name
Second I wanted to delete first_name and last_name to replace it by
just name however this is not possible (discovered another posting
about that later), it would be nice if this would be stated in the
docu i just linked above.
After reverting back to the original auth_user I thought I could just
hide the last_name and auto fill it with
db.auth_user.last_name.default = 'A'
db.auth_user.last_name.readable = False
db.auth_user.last_name.writable = False
but this does not work ...
Traceback (most recent call last):
File "/home/select/Dev/web2py/gluon/restricted.py", line 178, in
restricted
exec ccode in environment
File "/home/select/Dev/web2py/applications/frisur/controllers/
default.py", line 80, in <module>
File "/home/select/Dev/web2py/gluon/globals.py", line 102, in
<lambda>
self._caller = lambda f: f()
File "/home/select/Dev/web2py/applications/frisur/controllers/
default.py", line 59, in user
return dict(form=auth())
File "/home/select/Dev/web2py/gluon/tools.py", line 481, in __call__
return self.register()
File "/home/select/Dev/web2py/gluon/tools.py", line 938, in register
% form.vars
KeyError: 'last_name'

I guess I can create a custom form or overwrite the table (?? which is
the best), but I wonder why the above does not work.

Hope you can help thanks so far, you really are the bomb

mdipierro

unread,
Oct 22, 2009, 4:38:04 PM10/22/09
to web2py-users
Good point. There has to be a way to disable last name. For now you
can add

if not 'last_name' in form.vars:
form.vars.last_name=''

before line 938 in tools.py:

description = \
'group uniquely assigned to %(first_name)s %(last_name)
s'\
% form.vars

Massimo


On Oct 22, 3:21 pm, selecta <gr...@delarue-berlin.de> wrote:
> First of all, thanks for this awsome tool. I love it! Just discoverd
> it last Friday and I already build a rocking application that will go
> live soon. After having years of experience building web pages and
> also 3 years of Python I was almost ready to switch to RoR but always
> go stuck learing Ruby then I discovered web2py by accident and I am
> hooked Thanks Thanks Thanks.
>
> But to buissness:
> I followedhttp://www.web2py.com/examples/default/tools#authentication

mdipierro

unread,
Oct 22, 2009, 4:42:25 PM10/22/09
to web2py-users
Or better:

if not 'last_name' in form.vars:
description = \
'group uniquely assigned to %(first_name)s'\
% form.vars
else:
description = \
'group uniquely assigned to %(first_name)s %
(last_name)s'\
% form.vars

(adding this in trunk!)

With this change you can actually define a custom db.auth_user without
last_name but you will also need in your model:

db.auth_membership.user_id.requires = IS_IN_DB(db, '%s.id' %
auth.settings.table_user._tablename,
'%(id)s: %(first_name)s')

db.auth_event.user_id.requires = IS_IN_DB(db, '%s.id' %
auth.settings.table_user._tablename,
'%(id)s: %(first_name)s')


On Oct 22, 3:21 pm, selecta <gr...@delarue-berlin.de> wrote:
> First of all, thanks for this awsome tool. I love it! Just discoverd
> it last Friday and I already build a rocking application that will go
> live soon. After having years of experience building web pages and
> also 3 years of Python I was almost ready to switch to RoR but always
> go stuck learing Ruby then I discovered web2py by accident and I am
> hooked Thanks Thanks Thanks.
>
> But to buissness:
> I followedhttp://www.web2py.com/examples/default/tools#authentication

Wiiboy

unread,
Nov 12, 2009, 7:36:03 PM11/12/09
to web2py-users
Did you do the same with first_name as well? I'm getting the
following with SVN 1421:
Traceback (most recent call last):
File "/media/apps/web2py/gluon/restricted.py", line 184, in
restricted
exec ccode in environment
File "/media/apps/web2py/applications/main/controllers/default.py",
line 54, in <module>
File "/media/apps/web2py/gluon/globals.py", line 103, in <lambda>
self._caller = lambda f: f()
File "/media/apps/web2py/applications/main/controllers/default.py",
line 33, in user
return dict(form=auth())
File "/media/apps/web2py/gluon/tools.py", line 686, in __call__
redirect(self.url(args='login'))
File "/media/apps/web2py/gluon/tools.py", line 1137, in register
error_message=self.messages.mismatched_password)),
KeyError: 'first_name'

Wiiboy

unread,
Nov 12, 2009, 8:34:36 PM11/12/09
to web2py-users
Actually, this doesn't work for me at all. I added a first_name
field, and now I get a KeyError: last_name

?

Thadeus Burgess

unread,
Nov 12, 2009, 9:11:39 PM11/12/09
to web...@googlegroups.com
Auth facilities expect/require first_name and last_name.

-Thadeus

Wiiboy

unread,
Nov 12, 2009, 9:20:01 PM11/12/09
to web2py-users
Did I misunderstand Massimo above, or did he say you didn't?

Thadeus Burgess

unread,
Nov 12, 2009, 9:36:54 PM11/12/09
to web...@googlegroups.com
He said that you HAVE to have first_name and last_name in your auth table.

His work around still requires you to have these columns in your table... and just places them with defaulted values of empty strings.

-Thadeus

Wiiboy

unread,
Nov 13, 2009, 12:52:00 AM11/13/09
to web2py-users
Ah. Ok.

mdipierro

unread,
Nov 13, 2009, 1:50:32 AM11/13/09
to web2py-users
The latest web2py stable actually addressed this and it no longer
requires first_name and last_name.

On Nov 12, 11:52 pm, Wiiboy <jordon...@gmail.com> wrote:
> Ah.  Ok.

Wiiboy

unread,
Nov 13, 2009, 9:19:41 AM11/13/09
to web2py-users
How do I fix the KeyErrors then?

mdipierro

unread,
Nov 13, 2009, 9:50:12 AM11/13/09
to web2py-users
You should not get the keyerrors in the latest web2py. If you do,
please send me the traceback.

Wiiboy

unread,
Nov 13, 2009, 7:42:43 PM11/13/09
to web2py-users
SVN 1433 -- No first_name or last_name defined anywhere:
Traceback (most recent call last):
File "/media/apps/web2py/gluon/restricted.py", line 184, in
restricted
exec ccode in environment
File "/media/apps/web2py/applications/main/controllers/default.py",
line 54, in <module>
File "/media/apps/web2py/gluon/globals.py", line 103, in <lambda>
self._caller = lambda f: f()
File "/media/apps/web2py/applications/main/controllers/default.py",
line 33, in user
return dict(form=auth())
File "/media/apps/web2py/gluon/tools.py", line 694, in __call__
return self.register()
File "/media/apps/web2py/gluon/tools.py", line 1145, in register
description = self.messages.group_description % form.vars
KeyError: 'first_name'

mdipierro

unread,
Nov 13, 2009, 7:49:15 PM11/13/09
to web2py-users
You need to add

auth.messages.group_description="group for use %(id)s"

Wiiboy

unread,
Nov 13, 2009, 7:57:02 PM11/13/09
to web2py-users
Oh. Cool. Thanks.

mdipierro

unread,
Nov 13, 2009, 8:42:58 PM11/13/09
to web2py-users
perhaps this should be default.

Wiiboy

unread,
Nov 13, 2009, 9:05:25 PM11/13/09
to web2py-users
Ya =)

Wiiboy

unread,
Nov 15, 2009, 10:29:51 PM11/15/09
to web2py-users
Ahh, on trying to DB-Admin the table auth_membership, I get this:
Traceback (most recent call last):
File "/media/apps/web2py/gluon/restricted.py", line 184, in
restricted
exec ccode in environment
File "/media/apps/web2py/applications/main/controllers/appadmin.py",
line 261, in <module>
File "/media/apps/web2py/gluon/globals.py", line 103, in <lambda>
self._caller = lambda f: f()
File "/media/apps/web2py/applications/main/controllers/appadmin.py",
line 109, in insert
form = SQLFORM(db[table], ignore_rw=ignore_rw)
File "/media/apps/web2py/gluon/sqlhtml.py", line 613, in __init__
inp = self.widgets.options.widget(field, default)
File "/media/apps/web2py/gluon/sqlhtml.py", line 199, in widget
options = requires0.options()
File "/media/apps/web2py/gluon/validators.py", line 348, in options
self.build_set()
File "/media/apps/web2py/gluon/validators.py", line 335, in
build_set
records = self.dbset.select(*self.fields, **dd)
File "/media/apps/web2py/gluon/sql.py", line 2937, in select
return self.parse(db,rows,self.colnames)
File "/media/apps/web2py/gluon/sql.py", line 2956, in parse
field = table[fieldname]
File "/media/apps/web2py/gluon/sql.py", line 1497, in __getitem__
return dict.__getitem__(self, str(key))
KeyError: 'first_name'

Wiiboy

unread,
Nov 15, 2009, 10:42:23 PM11/15/09
to web2py-users
It looks like it's line 792 in tools.py....

mdipierro

unread,
Nov 16, 2009, 3:27:59 AM11/16/09
to web2py-users
You need:

auth.user_id.requires = IS_IN_DB(db, 'auth_user.id', '%(id)s')

I am hesitant to make this the default. I think it is usefult to see
the actual name of the user in the dropdown (current default)
Reply all
Reply to author
Forward
0 new messages