All right. I guess I was just confused by the wording.
I guess what I was really asking for was the ability to rename the required
field names. For example instead of 'email' I wanted to use 'username'.
The reason I didn't use 'email' from the start is that I am trying to
integrate web2py with an existing (production) database that uses
'username' instead of 'email'. We're actually redesigining our whole DB
schema, so we're renaming 'username' to 'email' anyway, so it doesn't
matter to me.
But shouldn't people who already have tables and want to use web2py be able
to rename the default required field names? This would help people with
existing user tables but different field names for email, password, etc.
For example, if the 'user' table has 'password_field' instead of
'password', 'user_email' instead of 'email', etc.
Thanks for all the help
On Thursday, May 31, 2012 3:23:17 PM UTC-7, Massimo Di Pierro wrote:
>
> Not sure I understand. If auth.define_tables(username=True) web2py defined
> this
>
> db.auth_user.username.requires = \
> [IS_MATCH('[\w\.\-]+'),
> IS_NOT_IN_DB(db, db.auth_user.username)]
>
> On Thursday, 31 May 2012 15:36:59 UTC-5, Osman Masood wrote:
>>
>> Thanks for the prompt response. Looks like according to the book, using
>> 'username' should be allowed though:
>> By default, web2py uses email for login. If instead you want to log in
>> using username set auth.define_tables(username=True)
>> Also:
>> If you add a field called "username", it will be used in place of "email"
>> for login. If you do, you will need to add a validator as well:
>>
>> 1.
>>
>> auth_table.username.requires = IS_NOT_IN_DB(db, auth_table.username)
>>
>>
>> (from http://web2py.com/books/default/chapter/29/9)
>>
>> I can add in the functionality into tools.py, if you want.
>> Thanks
>>
>> On Thursday, May 31, 2012 1:30:11 PM UTC-7, Massimo Di Pierro wrote:
>>>
>>> The email field is required. If you omit it there is nowhere to send
>>> lost-password messages.
>>>
>>> Anyway. You just need:
>>>
>>> auth.define_tables(username=True, signature=False, migrate=False)
>>>
>>> You do not need all the code before that line.
>>>
>>> On Thursday, 31 May 2012 15:09:39 UTC-5, Osman Masood wrote:
>>>>
>>>> Hi,
>>>> I have a custom table, and I'm trying to use a 'username' field instead
>>>> of an 'email' field (since the book says if there's a username field, it
>>>> will be used instead of the email field).
>>>> However, whenever I try the 'forgot my username' function, it gives me
>>>> an error, saying:
>>>> <type 'exceptions.KeyError'> 'email'
>>>> (Looks like it's trying to access the email field, which of course
>>>> doesn't exist.)
>>>>
>>>> Here's my code:
>>>> auth.settings.table_user_name = 'user'
>>>> db.define_table(auth.settings.table_user_name,
>>>> Field('username', length=60, unique=True, label='Email address'), #
>>>> required
>>>> Field('password', 'password', length=512, writable=False,
>>>> readable=False, label='Password'), # required
>>>> Field('registration_key', length=512, writable=False,
>>>> readable=False, default=''), # required
>>>> Field('reset_password_key', length=512, writable=False,
>>>> readable=False, default=''), # required
>>>> Field('registration_id', length=512, writable=False,
>>>> readable=False, default=''), # required
>>>> format='%(username)s')
>>>>
>>>> ## do not forget validators
>>>> custom_auth_table = db[auth.settings.table_user_name] # get the
>>>> custom_auth_table
>>>> custom_auth_table.password.requires = [ CRYPT(key="mykey",
>>>> digest_alg="sha512") ]
>>>> custom_auth_table.username.requires = [
>>>> IS_EMAIL(error_message=auth.messages.invalid_email),
>>>> IS_NOT_IN_DB(db, custom_auth_table.username)] # we need this so
>>>> that web2py uses username as the email address. unique is not good enough
>>>> auth.settings.table_user = custom_auth_table # tell auth to use
>>>> custom_auth_table
>>>>
>>>> ## configure auth policy
>>>> auth.settings.registration_requires_verification = True
>>>> auth.settings.registration_requires_approval = True
>>>> auth.settings.reset_password_requires_verification = True
>>>> auth.messages.verify_email = 'Click on the link http://' + \
>>>> request.env.http_host + \
>>>> URL(r=request,c='default',f='user',args=['verify_email']) + \
>>>> '/%(key)s to verify your email'
>>>> auth.messages.reset_password = 'Click on the link http://' + \
>>>> request.env.http_host + \
>>>> URL(r=request,c='default',f='user',args=['reset_password']) + \
>>>> '/%(key)s to reset your password'
>>>>
>>>> auth.define_tables(username=True, signature=False, migrate=False)
>>>>
>>>> ## configure email
>>>> mail=auth.settings.mailer
>>>> mail.settings.server = 'smtp.gmail.com:587' or 'logging'
>>>> mail.settings.sender = '[email protected]'
>>>> mail.settings.login = '[email protected]:mypass'
>>>>
>>>>
>>>> And here's the error traceback:
>>>>
>>>> Traceback (most recent call last):
>>>> File "/home/www-data/web2py/gluon/restricted.py", line 205, in restricted
>>>> exec ccode in environment
>>>> File
>>>> "/home/www-data/web2py/applications/Talent_Web/controllers/default.py"
>>>> <https://ec2-204-236-184-137.us-west-1.compute.amazonaws.com/admin/default/edit/Talent_Web/controllers/default.py>,
>>>> line 73, in <module>
>>>> File "/home/www-data/web2py/gluon/globals.py", line 175, in <lambda>
>>>> self._caller = lambda f: f()
>>>> File
>>>> "/home/www-data/web2py/applications/Talent_Web/controllers/default.py"
>>>> <https://ec2-204-236-184-137.us-west-1.compute.amazonaws.com/admin/default/edit/Talent_Web/controllers/default.py>,
>>>> line 34, in user
>>>> return dict(form=auth())
>>>> File "/home/www-data/web2py/gluon/tools.py", line 1196, in __call__
>>>> return getattr(self,args[0])()
>>>> File "/home/www-data/web2py/gluon/tools.py", line 2158, in
>>>> retrieve_username
>>>> old_requires = table_user.email.requires
>>>> File "/home/www-data/web2py/gluon/dal.py", line 7180, in __getattr__
>>>> return self[key]
>>>> File "/home/www-data/web2py/gluon/dal.py", line 7120, in __getitem__
>>>> return dict.__getitem__(self, str(key))
>>>> KeyError: 'email'
>>>>
>>>>