johnmahugu

web2py how-edit-user-profile-and-membership-in-one-view

Apr 1st, 2015
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 KB | None | 0 0
  1. originally posted on my coding blog: http://brunorocha.org/python/web2py/web2py-manage-users-and-membership-in-the-same-form.html
  2.  
  3.  
  4. web2py - manage users and membership in the same form
  5.  
  6. As requested by user of Stack Overflow.
  7. http://stackoverflow.com/questions/11992749/web2py-how-edit-user-profile-and-membership-in-one-view
  8. How to manage users and memberships at the same form
  9.  
  10. NOTE: You have to register the first admin user first, because to manage users and memberships we require to be admin
  11.  
  12. For the purpose of the example we are going to use the file controllers/default.py accessible at the url localhost:8000/YOURAPP/default
  13.  
  14. A grid to list your users
  15.  
  16. this is the list admins will see when hit http://..../default/list_users
  17.  
  18. The user list grid
  19.  
  20. 1 - Put on the default.py file
  21.  
  22. #@auth.requires_membership("admin") # uncomment to enable security
  23. def list_users():
  24. btn = lambda row: A("Edit", _href=URL('manage_user', args=row.auth_user.id))
  25. db.auth_user.edit = Field.Virtual(btn)
  26. rows = db(db.auth_user).select()
  27. headers = ["ID", "Name", "Last Name", "Email", "Edit"]
  28. fields = ['id', 'first_name', 'last_name', "email", "edit"]
  29. table = TABLE(THEAD(TR(*[B(header) for header in headers])),
  30. TBODY(*[TR(*[TD(row[field]) for field in fields]) \
  31. for row in rows]))
  32. table["_class"] = "table table-striped table-bordered table-condensed"
  33. return dict(table=table)
  34.  
  35. With generic views will see this
  36.  
  37. 2 - The edit links to manage_users
  38.  
  39. Now accessing http://..../default/list_users you are going to see the grid showing all users, now if you click on the edit link on grid it goes to manage_user function we defined on btn = lambda row: A("Edit", _href=URL('manage_user', args=row.auth_user.id))
  40.  
  41. Create this two functions in the same controller
  42. The user form
  43.  
  44. #@auth.requires_membership("admin") # uncomment to enable security
  45. def manage_user():
  46. user_id = request.args(0) or redirect(URL('list_users'))
  47. form = SQLFORM(db.auth_user, user_id).process()
  48. membership_panel = LOAD(request.controller,
  49. 'manage_membership.html',
  50. args=[user_id],
  51. ajax=True)
  52. return dict(form=form,membership_panel=membership_panel)
  53.  
  54. On the above function we are going to create two objects form which is the form to edit the user object, also we create membership_panel which is an ajax panel to load the manage_membership inside it and ajax managed.
  55.  
  56. note: that this function takes user_id from request.args(0) then if it is not provided it redirects back to thelist_users
  57. The membership panel
  58.  
  59. #@auth.requires_membership("admin") # uncomment to enable security
  60. def manage_membership():
  61. user_id = request.args(0) or redirect(URL('list_users'))
  62. db.auth_membership.user_id.default = int(user_id)
  63. db.auth_membership.user_id.writable = False
  64. form = SQLFORM.grid(db.auth_membership.user_id == user_id,
  65. args=[user_id],
  66. searchable=False,
  67. deletable=False,
  68. details=False,
  69. selectable=False,
  70. csv=False,
  71. user_signature=False) # change to True in production
  72. return form
  73.  
  74. note that on the manage_membership we are returning the form directly, so we can input it inside the ajax panel membership_panel
  75. The manage_user view
  76.  
  77. 3 - Create an html file in YOURAPP/views/default/manage_user.html
  78.  
  79. {{extend 'layout.html'}}
  80. <h4> Edit The user </h4>
  81. {{=form}}
  82. <hr>
  83. <h4> User membership </h4>
  84. {{=membership_panel}}
  85.  
  86. The end result
  87. User Form
  88.  
  89. Add membership
  90.  
  91. Done, using web2py 2.0 (trunk)
  92.  
  93.  
  94. auth users membership ajax
  95. Share
  96. Related slices
  97.  
  98. AJAX adding child records to parent
  99. Integrating Linkedin with web2py
  100. Star rating plugin (Improved version)
  101. Ajax Live Search (auto-complete)
  102. Upload Progress in web2py
  103.  
  104. Comments (7)
  105.  
  106. Login to post
  107.  
  108. order by: newest oldest upvoted downvoted
  109.  
  110.  
  111. 0
  112. sif-baksh-10957 6 months ago
  113.  
  114. Many thanks Bruno Rocha, this is great.
  115.  
  116. 0
  117. web2pyslices 1 year ago
  118.  
  119. Updated the code. added .process() in the form and also user_signature=False to the grid.
  120.  
  121.  
  122.  
  123. Thanks
  124.  
  125. 0
  126. marc-garcia-10770 1 year ago
  127.  
  128. @jkumar There are code missing, you have to create also a view as "default/list_users" (Or whatever the function name that creates the table) and in the view you have to type this:
  129.  
  130.  
  131.  
  132. {{extend 'layout.html'}}
  133.  
  134. {{=table}}
  135.  
  136.  
  137. Regards
  138. replies (1)
  139. web2pyslices 1 year ago
  140.  
  141. Yes, that was a change in web2py.
  142.  
  143. You can create the new view in 'default/list_users.html' or optionally you can just set the generic patterns to *
  144.  
  145. response.generic_patterns = ['*'] in db.py
  146.  
  147.  
  148. 0
  149. jkumar 1 year ago
  150.  
  151. I am trying the same with auth_group and "my_custom_table".
  152.  
  153. I am not getting the drop down to map the tables.
  154.  
  155.  
  156.  
  157. Here is my membership table.
  158. db.define_table("devices_membership",
  159. Field("devices_config_id", "integer", db.devices_config),
  160. Field("auth_group_id", "integer", db.auth_group))
  161.  
  162.  
  163. 0
  164. zandar 1 year ago
  165.  
  166. The menbership panel does not work, is there code missing?
  167.  
  168. 0
  169. jose-soto-11237 2 years ago
  170.  
  171. Thanks for the post!!!
  172.  
  173. I just test it in web2py version 2.2.1. To be able to visualize the buttons (Edit and Add) in manage_menbership table. I have added: user_signature=False to the SQLFORM.grid.
  174.  
  175. 0
  176. andrew-webster-10407 2 years ago
  177.  
  178. Excellent work - implementing this on my own site now!
Advertisement
Add Comment
Please, Sign In to add comment