Redirect after profile update

99 views
Skip to first unread message

Ron Chatterjee

unread,
May 16, 2016, 11:59:04 PM5/16/16
to web2py-users
Anyone aware of anything similar to


auth.settings.register_onupdate?

I have splitted up the auth_user to buyer and seller. I redirect one group by the following after registration

auth.settings.register_onaccept = lambda form: after_registration(form)


I would like to do similar after the profile update.

But I can't seem to find


auth.settings.register_onupdate


Thanks in advance.

villas

unread,
May 17, 2016, 4:37:37 AM5/17/16
to web2py-users
There is:

profile_next  and  profile_onaccept

Ron Chatterjee

unread,
May 17, 2016, 9:54:52 AM5/17/16
to web2py-users
Profile onaccept works but my logic is wrong I am guessing because it escapes and always end up going to index page

Basically I am trying to update the form (which I call form2) with the info that was used to create db.consumer

in db I have
auth.settings.profile_onaccept = lambda form: profile_update(form)

db.define_table("consumer",
                Field("seller_profile", 'reference auth_user', readable = False, writable = False, widget=SQLFORM.widgets.options.widget, requires= IS_EMPTY_OR(IS_IN_DB(db,db.auth_user.id))),
                 Field("profile_statement", "text", label='Profile hightlights*',requires=IS_NOT_EMPTY(),comment='Write few things to summarize your profile'))

That brings it to the controller
def profile_update(form):
    form2 = [];
    db.consumer.seller_profile.default = auth.user_id
    if form.vars.user_type != 'Seller':
        redirect(URL('default','index'))   
           
    else:
        query  = (db.consumer.created_by==auth.user_id);
        profile= db(query).select();           
        form2 = SQLFORM(db.consumer,profile[0].id,deletable=True, submit_button='Update Profile').process()
        if form2.accepted:
            session.flash = T('Profile is modified')
    return dict(form2 = form2)


in view profile_update I have {{=form2}}

Any thoughts?

On Tuesday, May 17, 2016 at 4:37:37 AM UTC-4, villas wrote:
There is:

profile_next  and  profile_onaccept

villas

unread,
May 17, 2016, 11:50:17 AM5/17/16
to web2py-users
Why not use auth.settings.profile_onaccept or profile_next to simply redirect to another url.  You can easily access your auth info,  create a new form,  or even post auth info into another table.

It isn't clear what you are trying to do.

If you need better access to auth.profile function then you could use auth.profile() feature as per book:
def myprofile(): return dict(form=auth.profile())

If you need to update or create a consumer record,  then do that in a completely different function.  Simply redirect there setting args or vars. 

If you want your auth information available,  you can always access it like this:  auth.user.last_name

Also don't forget that db.auth_user is a table like any other.  You can access it and read or write information,  if that's what you'd like to do.

Ron Chatterjee

unread,
May 17, 2016, 12:03:39 PM5/17/16
to web2py-users
Sorry, wasn't too clear. I posted the code....What I am trying to do is, I have a table call consumer. It has a profile statement. 

auth.settings.register_onaccept = lambda form: after_registration(form) 

I use this to create that profile. 

I would like that to be updated when someone update their profile. 

Alfonso Serra

unread,
May 17, 2016, 3:14:09 PM5/17/16
to web...@googlegroups.com
Instead on relying on callbacks you could implement the user() controller in default.py
login, logout, register, profile, and so on are passed as request.args

in default.py, something like:
def user():
 
if auth.user:
   
if request.args[0] == "profile":
     
if user.type == "seller":
          redirect
(URL("sellerprofile"))
     
else:
          redirect
(URL("buyerprofile"))
     
 
return dict(form=auth())


villas

unread,
May 18, 2016, 6:50:52 AM5/18/16
to web2py-users
Hi Ron

Whilst I am still not totally clear in what you are trying to do,  it seems that your code to create your form2 is not written correctly.  Maybe you can change it a little and simply redirect to this similar function ...

def consumer_update():
    if auth.user.user_type != 'Seller':

        redirect
(URL('default','index'))

    query  
= (db.consumer.created_by==auth.user_id);
    consumer_rec
= db(query).select().first()

    db
.consumer.seller_profile.default = auth.user_id
    form2
= SQLFORM(db.consumer, consumer_rec, deletable=True, submit_button='Update Consumer Profile')
   
if form2.process().accepted:

          session
.flash = T('Profile is modified')

          redirect
(URL('index'))
   
elif form2.errors:
          response
.flash = 'form has errors'
   
return dict(form2=form2)



Ron Chatterjee

unread,
May 18, 2016, 10:16:24 AM5/18/16
to web...@googlegroups.com
I thank you for your help villas and much appreciated. I guess the only two difference between your approach and mine is:

(1)
profile= db(query).select(); 
profile[0].id 

 consumer_rec = db(query).select().first()

I could be wrong but I believe they works the same. 

(2)
And

You have:

    form2 = SQLFORM(db.consumer, consumer_rec, deletable=True, submit_button='Update Consumer Profile')

  if form2.process().accepted:

I did. 

  form2 = SQLFORM(db.consumer,profile[0].id,deletable=True, submit_button='Update Profile').process()

if form2.accepted:

 Am I correct?

villas

unread,
May 18, 2016, 5:38:22 PM5/18/16
to web2py-users
Hi Ron, 
I said it was a similar function for good reason - I am simply trying to make corrections and help you get your code working :)
One point is that you don't need to use profile_onaccept,  you can simply redirect after using the normal auth.profile().
Best wishes.

Ron Chatterjee

unread,
May 20, 2016, 8:49:34 PM5/20/16
to web2py-users
Thank you Villas and Also Al for your input. I found a work around. 

villas

unread,
May 21, 2016, 4:36:12 AM5/21/16
to web2py-users
Hi Ron, That's great,  but please also consider leaving a note of what solution you found.  Many users refer to these threads and if they read the whole thing and it just end with 'I found another solution',  this can be frustrating. 
Many thanks and best wishes.
Reply all
Reply to author
Forward
0 new messages