I have defined a custom auth.settings.table_user_name implementation
that includes one extra field and specifies a value for the format
argument. In this case format is a callback function which displays
the user's name with extra formatting based on membership in certain
auth groups. This option works wonderfully with SQLFORM fields and
with appadmin displays of the auth_membership table, but I seem to be
missing something about its functionality elsewhere.
(This is in the context of a demo chat application utilizing the
web2py_comet functionality). I have another table which is defined
like this:
db.define_table('chat',
Field('speaker', db.auth_user, default=auth.user_id,
writable=False, readable=False),
Field('statement', 'text', requires=IS_NOT_EMPTY()),
Field('deleted', db.auth_user, writable=False, readable=False))
db.chat.speaker.requires = IS_IN_DB(db, db.auth_user.id)
db.chat.deleted.requires = IS_IN_DB(db, db,auth_user.id)
My controller for displaying the lines uses this logic
rawlines=db(db.chat.deleted==None).select(limitby=(0,10),orderby=~db.chat.id)
lines=[]
for line in rawlines:
lines.append(DIV(line.speaker, B("> "), line.statement,
_id=line.id))
However, when I do that, I get the user's ID instead of the user's
name as formatted by my format callback method. Thus the chat output
looks like
1> Hi there
1> How is everyone doing?
2> We're great, how about you?
1> Oh, I'm absolutely fantastic.
In order to get the properly formatted user's name, I have to
explicitly call my format method, making the last line of the above
controller instead read
lines.append(DIV(name_format(line.speaker), B("> "),
line.statement, _id=line.id))
This defeats most of the purpose I had in using the automatic
format=name_format argument to the define_table call. Am I missing
something here? What am I doing incorrectly? As I scale out of my
demo chatroom, I want to make sure user names are consistently
displayed throughout my application by means of the format argument to
the auth_user table, rather than trying to remember to call
name_format(user) everywhere. Any advice?
--Greg