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.

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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).
An issue has been submitted to fix the bug. You can use the above workaround for now.
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
meant form.vars not form.user
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="[email protected]", password=db.auth_user.password.requires[0]('mypassword')[0]) auth.add_membership('admin', db.auth_user(new_user_id) ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.