URL Routing question re: setting persistent arguments for use in controller functions

72 views
Skip to first unread message

Henry Nguyen

unread,
Dec 8, 2013, 3:46:49 AM12/8/13
to web...@googlegroups.com
Hello,

I have an application in which users can have sub_users. Within the app, a user can select a sub_user, at which point, all controls become targeted to that selected sub_user. For example, if I select sub_user 1 and then goto "settings/show_settings.html," I want the show_settings.html to show settings only for that sub_user 1. If I navigate to another page, I want the controller and function to again refer only to sub_user 1. I want it to stay like this until I explicitly navigate to another sub_user 2.

In the case of web2py's URL mapping scheme, I assume this would mean I would have to use 1 as the argument after every function in the URL. What's the best way to go about ensuring this? Is this even the best way?

I've looked around and I can't find any best-practice advice regarding this functionality for web2py. Interestingly enough, though, the admin app does exactly what I'm looking for. For example, if I goto appadmin and goto manage an app, I am first taken to:


Then, if I goto peek at a model file, I am taken to:


I am obviously being directed to a different function in the default controller of the admin app, but how did the myapp argument stay constant throughout that navigation?

Thank you ahead of time for any help. We're starting to rewrite our stack at my company and web2py is proving immeasurably invaluable... allowing us to develop so refreshingly quickly and confidently. Thank you all for your work on this!

Jonathan Lundell

unread,
Dec 8, 2013, 11:43:54 AM12/8/13
to web2py
On 8 Dec 2013, at 12:46 AM, Henry Nguyen <henryn...@gmail.com> wrote:

I have an application in which users can have sub_users. Within the app, a user can select a sub_user, at which point, all controls become targeted to that selected sub_user. For example, if I select sub_user 1 and then goto "settings/show_settings.html," I want the show_settings.html to show settings only for that sub_user 1. If I navigate to another page, I want the controller and function to again refer only to sub_user 1. I want it to stay like this until I explicitly navigate to another sub_user 2.

In the case of web2py's URL mapping scheme, I assume this would mean I would have to use 1 as the argument after every function in the URL. What's the best way to go about ensuring this? Is this even the best way?


Consider storing the sub_user ID in your session.

Jonathan Lundell

unread,
Dec 8, 2013, 12:28:16 PM12/8/13
to web2py
I should point out, though, that there's a downside to this. Suppose a user opens multiple tabs, and tries to work with different sub_users in different tabs. That will fail, because all the tabs share the same session.

If that's not an issue for you, sessions are the way to go. If it is, try putting the sub_user into your forms as a hidden variable, and picking it up from vars.

Henry Nguyen

unread,
Dec 9, 2013, 12:28:14 AM12/9/13
to web...@googlegroups.com
Jonathan, 

Thank you for your suggestion. Session variables should work just fine for this indeed.

Just for future reference, I had also came up with a somewhat hacky way to get my navigation menus to append the sub_user.id as an argument. Basically, I have two navigation menus: one on the left to select the sub_user and one at the top for various actions pertaining to that sub_user. In menu.py, iterate over each sub_user and add a tab for the sub_user menu, appending the sub_user.id as an argument to the URL. Then, only add the top menu items if the current sub_user.id equals the id in the URL arguments. If so, set the arguments for each of those tabs to be that sub_user.id. In other words:

for sub_user in db(db.sub_user.auth_user_id==auth.user_id).select():
    response
.sub_user_menu += [
       
(str(sub_user.first_name), False, URL('dashboard', 'index', args=str(sub_user.id)), [])]
   
if request.args(0) and str(sub_user.id) == str(request.args(0)):
        response
.dashboard_menu = [
           
(T('Tab1'), (request.function=='tab1'), URL('dashboard', 'tab1', args=str(sub_user.id)), []),
           
(T('Tab2'), (request.function=='tab2'), URL('dashboard', 'tab2', args=str(sub_user.id)), []),
           
(T('Tab3'), (request.function=='tab3'), URL('dashboard', 'tab3', args=str(sub_user.id)), [])]

Not sure how Pythonic this really is but it got the job done. Sessions will be much cleaner, though, so I'll go with that. Thank you again.
Reply all
Reply to author
Forward
0 new messages