I managed to get both of those problems sorted. Thank you for your
input!
Attached below is the rough code I used:
@auth.requires_membership('administrators')
def create():
# set up groups to be put into
groups = db().select(db.auth_group.ALL)
group_fields = []
group_lookup = {}
# create the fields and a lookup table for the ids of the groups
if we want to add them
for g in groups:
name = "is_in_group_%s" % g.role
group_fields.append(Field(name, "boolean",
label=g.role.capitalize()))
group_lookup[name] = g.id
# create the form
form = SQLFORM.factory(db.auth_user,
Field("password_two", "password", requires=IS_EXPR('value==%s'
% repr(request.vars.get("password", None)),
error_message=auth.messages.mismatched_password),
label="Verify Password"),
*group_fields)
# if all good
if form.accepts(request.vars):
response.flash = 'User "%s" added successfully!' %
request.vars["username"]
# create an ignore list of input names to seperate the user
data from the rest
ignore_list = group_lookup.keys() + ["password_two",
"_formname"]
# seperate the user_data
user_data = {}
for k in request.vars:
if k not in ignore_list:
user_data[k] = request.vars[k]
# create the new user
new_user = db.auth_user.insert(**user_data)
# process the membership information
for g in group_lookup.keys():
if request.vars.get(g, None).lower() == "on":
auth.add_membership(group_lookup[g], new_user)
elif form.errors:
response.flash = 'An error has occurred.'
else:
response.flash = 'Please fill out the form.'
return dict(form=form)
On Apr 22, 3:40 pm, mdipierro <[email protected]> wrote:
> auth.resiter() form is not thought for person A to register person B
> but for person A to register itself.
>
> You can use appadmin to register another person or you have to create
> your own interface for example via
>
> form=crud.create(db.auth_user)
>
> You can also make your form more complex
>
> form=SQLFORM.factory(db.auth_user,Field('group1'),Field('group2')):
> if form.accepts(request.vars):
> insert form.vars into db.auth_user
> if form.vars.group1. auth.add_membership(....)
> if form.vars.group2. auth.add_membership(....)
>
> On Apr 21, 10:00 pm, rohfle <[email protected]> wrote:
>
> > When I said "logging on", I meant the process of login, not logging
> > events.
>
> > Elaborating on the original questions:
> > 1) For example, I login as an administrator and I register a user name
> > named bob. When I click submit to register bob, the currently logged-
> > in user gets switched from administrator to "bob" after being added to
> > the database. Can I stop this switch from occurring?
>
> > 2) I want to extend the register form from looking in HTML like:
> > [username]
> > [firstname]
> > [lastname]
> > [password]
> > [verify password]
> > <Submit>
> > to:
> > [username]
> > [firstname]
> > [lastname]
> > [password]
> > [verify password]
>
> > [x] group1
> > [ ] group2
> > <Submit>
> > where group1 and group2 come from the table auth_group.
>
> > Sorry I should have made these things clearer.
>
> > On Apr 22, 2:13 pm, mdipierro <[email protected]> wrote:
>
> > > 1) Yes. Look at the bottom of this page:
>
> > >http://www.web2py.com/book/default/section/8/2
>
> > > If you set a log message to None, the log is not performed.
>
> > > 2) Not sure I understand. You can do
>
> > > auth.settings.register_onaccept=lambda form: do_something_with(form)
>
> > > On Apr 21, 8:14 pm, rohfle <[email protected]> wrote:
>
> > > > Couple of questions about web2py Auth:
>
> > > > 1) Can I stop Auth.register from logging on as the newly created user
> > > > after registration?
> > > > 2) Is there a way to add group membership to form returned by
> > > > Auth.register?
>
> > > > --
> > > > Subscription
> > > > settings:http://groups.google.com/group/web2py/subscribe?hl=en