Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Quick List/Update/Create table snippet
- Recipe by Benigno Calvo (benigno) on 2009-12-09 in Controller
- Views (841)Favorite (0)Like (0)Dislike (0)Subscribe (0)
- I do not particularly like having a function for each of the standard operations (select, update, create) on a single table. Moreso when I am prototyping or just want quick cruds. Not only do they clutter the controller a lot but they also end up being nasty URIs. So here is a little piece of very basic code, that I find myself reusing again and again. I hope it is useful for you too.
- Share
- Quick Table Management Snippet
- Just a little snippet that I find myself using again and again, to quickly create navigation to List, update and create tables:
- In your controller:
- def mylink(field, type, ref):
- """
- Returns a URL to link to in crud.selects.
- """
- return URL(r=request, args=[field])
- def table_name():
- """
- Maintain all the table_name records in the application
- Manage, list, create and update projects.
- """
- # SELECT if no args
- if len(request.args)<1:
- return dict(form=crud.select(db.table_name,linkto=mylink))
- # If 1 arg and it is 'create' then CREATE
- elif len(request.args)==1 and request.args[0]=='create':
- return dict(form=crud.create(db.table_name,next=URL(request.application,request.controller,'table_name')))
- # If 1 arg and its integer, EDIT it.
- elif len(request.args)==1:
- try:
- i=int(request.args[0])
- except:
- redirect(URL(r=request,f='index'))
- return dict(form=crud.update(db.table_name,i,next=URL(request.application,request.controller,'table_name')))
- else:
- redirect(URL(r=request,f='index'))
- This leaves the following URLs for your table_name actions:
- /appname/controller/table_name will display a SELECT of the table.
- /appname/controller/table_name/create will CREATE a new record for the table.
- /appname/controller/table_name/edit/ID will EDIT table_name.id == ID
- Closing up
- I really hope that this little snippet saves some of your time for quickly prototyping apps.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement