0

I would like to register users programmatically, so I can test my application with multiple users.

The way I thought of doing this was:

if db( db.auth_user ).count() == 0:
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    for letter in alphabet:
        db.auth_user.validate_and_insert( first_name=letter , last_name=letter , email='%s@%s.com'%(letter , letter) , username=letter , password='qwer' )

This however doesn't set the other tables about group membership etc.

evan54
  • 3,585
  • 5
  • 34
  • 61

2 Answers2

3

Use:

from gluon.storage import Storage
onaccept = auth.settings.register_onaccept.pop() # Disable registration callback.
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for letter in alphabet:
    user = auth.register_bare(first_name=letter, last_name=letter,
                              email='%s@%s.com' % (letter, letter),
                              username=letter, password='qwer')
    user and onaccept(Storage(vars=user))
auth.settings.register_onaccept = [onaccept] # Restore registration callback.

The above temporarily disables the register_onaccept callback, as a bug in register_bare prevents it from working properly. Instead, it is called manually after the registration, passing in the user object (which is the full user record, including the id field).

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • This almost worked. I have an `auth.settings.register_onaccept` function which takes `form` as a variable and uses `form.vars.id`. But in this case `form.vars` isn't defined. Any hints? – evan54 Feb 21 '16 at 00:20
  • I would say this is a bug in `auth.register_bare` -- it passes a dictionary of the user fields to `register_onaccept`, but it does not include the `id` (and there is no `.vars` attribute). I would say either re-write the callback to deal with the alternative input (and if you need the id, get it by doing a lookup based on the email), or disable the callback and instead call it manually, passing it the returned user object (the answer has been updated with this latter approach). – Anthony Feb 21 '16 at 05:37
  • An [issue](https://github.com/web2py/web2py/issues/1192) has been submitted to fix the bug. You can use the above workaround for now. – Anthony Feb 21 '16 at 05:48
  • thank you, I ended up doing a db lookup on the username inside the onaccept function in case the form.user isn't defined. It seems that what is passed in `form` is a Storage object with keys `first_name`, `last_name` etc – evan54 Feb 21 '16 at 12:13
  • meant `form.vars` not `form.user` – evan54 Feb 21 '16 at 12:43
1

You can look here and here, basicly for add a user and assign a group you can:

new_user_id = db.auth_user.insert(first_name="Admin",
                                  last_name="Username",
                                  email="mye...@address.com",
                                  password=db.auth_user.password.requires[0]('mypassword')[0])
auth.add_membership('admin', db.auth_user(new_user_id) )
Community
  • 1
  • 1
Cesar
  • 707
  • 3
  • 13