wiki:BluePrintRESTImplementation

Version 19 (modified by Fran Boon, 16 years ago) ( diff )

New Implementation with shn_rest_controller() broken out :)

Implementation for the BluePrintREST:

Model

def shn_crud_strings_lookup(resource):
    "Look up CRUD strings for a given resource."
    if resource=='shelter':
        return Storage(title_create=T('Add Shelter'),
            title_display=T('Organisation Details'),
            title_list=T('List Shelters'),
            title_update=T('Edit Shelter'),
            subtitle_list=T('Shelters'),
            subtitle_create=T('Add New Shelter'),
            label_list_button=T('List Shelters'),
            label_create_button=T('Add Shelter'),
            msg_record_created=T('Shelter added'),
            msg_record_modified=T('Shelter updated'),
            msg_record_deleted=T('Shelter deleted'),
            msg_list_empty=T('No Shelters currently registered'))
    else:
        # Undefined Resource!
        return

def shn_rest_controller(module,resource):
    """
    RESTful 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:

db.define_table('cr_shelter',
                SQLField('modified_on','datetime',default=now),
                SQLField('uuid',length=64,default=uuid.uuid4()),
                SQLField('name'))
db.cr_shelter.represent=lambda table:shn_list_item(table,resource='shelter',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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</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>&nbsp;</p>
{{try:}}
 {{=list_btn}}
{{except:}}
{{pass}}

key.html

<p><b>{{=T('Key')}}:</b><b class='red'> * </b> - {{=T('Fields tagged with a star')}} &#040;<span class='red'> * </span>&#041; {{=T('are mandatory and must be filled')}}.</p>
Note: See TracWiki for help on using the wiki.