Version 23 (modified by 16 years ago) ( diff ) | ,
---|
Implementation for the BluePrintREST:
Model
models/module.py
resource='shelter' single=resource.capitalize() # NB May need manual fixing! plural=single+'s' title_create=T('Add %s' % single) title_display=T('%s Details' % single) title_list=T('List %s' % plural) title_update=T('Edit %s' % single) subtitle_create=T('Add New %s' % single) subtitle_list=T('%s' % plural) label_list_button=T('List %s' % plural) label_create_button=T('Add %s' % single) msg_record_created=T('%s added' % single) msg_record_modified=T('%s updated' % single) msg_record_deleted=T('%s deleted' % single) msg_list_empty=T('No %s currently registered' % plural) exec('crud_strings.%s=Storage(title_create=title_create, title_display=title_display, title_list=title_list, title_update=title_update, subtitle_create=subtitle_create, subtitle_list=subtitle_list, label_list_button=label_list_button, label_create_button=label_create_button, msg_record_created=msg_record_created, msg_record_modified=msg_record_modified, msg_record_deleted=msg_record_deleted, msg_list_empty=msg_list_empty)' % resource)
models/_db.py
def shn_crud_strings_lookup(resource): "Look up CRUD strings for a given resource." return getattr(crud_strings,'%s' % resource) def shn_rest_controller(module,resource): """ RESTlike controller function. Anonymous users can Read. Authentication required for Create/Update/Delete. ToDo: Alternate Representations Search method Customisable Security Policy """ table=db['%s_%s' % (module,resource)] crud_strings=shn_crud_strings_lookup(resource) if len(request.args)==0: # No arguments => default to list (or list_create if logged_in) list=t2.itemize(table) if list=="No data": list=crud_strings.msg_list_empty title=crud_strings.title_list subtitle=crud_strings.subtitle_list if t2.logged_in: form=t2.create(table) response.view='list_create.html' addtitle=crud_strings.subtitle_create return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle,addtitle=addtitle) else: add_btn=A(crud_strings.label_create_button,_href=t2.action(resource,'create')) response.view='list.html' return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn) else: method=request.args[0] if request.args[0].isdigit(): # 1st argument is ID not method => display. # Default format (representation) is full HTML page item=t2.display(table) response.view='display.html' title=crud_strings.title_display edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id])) list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,list_btn=list_btn) else: if method=="create": if t2.logged_in: t2.messages.record_created=crud_strings.msg_record_created form=t2.create(table) response.view='create.html' title=crud_strings.title_create list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) else: t2.redirect('login',vars={'_destination':'%s/create' % resource}) elif method=="display": t2.redirect(resource,args=t2.id) elif method=="update": if t2.logged_in: t2.messages.record_modified=crud_strings.msg_record_modified form=t2.update(table) response.view='update.html' title=crud_strings.title_update list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) else: t2.redirect('login',vars={'_destination':'%s/update/%i' % (resource,t2.id)}) elif method=="delete": if t2.logged_in: t2.messages.record_deleted=crud_strings.msg_record_deleted t2.delete(table,next=resource) return else: t2.redirect('login',vars={'_destination':'%s/delete/%i' % (resource,t2.id)}) else: # Unsupported method! t2.redirect(resource)
Simplified module table:
resource='shelter' table=module+'_'+resource db.define_table(table, SQLField('modified_on','datetime',default=now), SQLField('uuid',length=64,default=uuid.uuid4()), SQLField('name')) db['%s' % table].represent=lambda table:shn_list_item(table,resource=resource,action='display')
Controller
def shelter(): "RESTful CRUD controller" return shn_rest_controller(module,'shelter')
Views
create.html
{{extend 'layout.html'}} {{try:}} {{=H2(title)}} {{except:}} {{pass}} {{include 'key.html'}} <div class='form-container'> {{try:}} {{=form}} {{except:}} {{pass}} </div> <p> </p> {{try:}} {{=list_btn}} {{except:}} {{pass}}
display.html
{{extend 'layout.html'}} {{try:}} {{=H2(title)}} {{except:}} {{pass}} {{try:}} {{=edit}} {{except:}} {{pass}} <div class='item-container'> {{try:}} {{=item}} {{except:}} {{pass}} </div> <p> </p> {{try:}} {{=list_btn}} {{except:}} {{pass}}
list.html
{{extend 'layout.html'}} {{try:}} {{=H2(title)}} {{except:}} {{pass}} {{try:}} {{=H3(subtitle)}} {{except:}} {{pass}} <div id='list-container'> {{try:}} {{=list}} {{except:}} {{pass}} </div> <p> </p> {{try:}} {{=add_btn}} {{except:}} {{pass}}
list_create.html
{{extend 'layout.html'}} {{try:}} {{=H2(title)}} {{except:}} {{pass}} {{try:}} {{=H3(subtitle)}} {{except:}} {{pass}} <div id='list-container'> {{try:}} {{=list}} {{except:}} {{pass}} </div> <p> </p> {{try:}} {{=H3(addtitle)}} {{except:}} {{pass}} <div class='form-container'> {{try:}} {{=form}} {{except:}} {{pass}} </div> {{include 'key.html'}}
update.html
{{extend 'layout.html'}} {{try:}} {{=H2(title)}} {{except:}} {{pass}} {{include 'key.html'}} <div class='form-container'> {{try:}} {{=form}} {{except:}} {{pass}} </div> <p> </p> {{try:}} {{=list_btn}} {{except:}} {{pass}}
key.html
<p><b>{{=T('Key')}}:</b><b class='red'> * </b> - {{=T('Fields tagged with a star')}} (<span class='red'> * </span>) {{=T('are mandatory and must be filled')}}.</p>
Note:
See TracWiki
for help on using the wiki.