wiki:BluePrintRESTImplementation

Version 7 (modified by Fran Boon, 13 years ago) ( diff )

--

Implementation for the BluePrintREST:

# RESTful controller function
# NB No login required:
# Anonymous users can Read/Create
# Authentication required for Update/Delete
def shelter():
    table=db.cr_shelter
    if request.args:
        method=request.args[0]
        try:
            # 1st argument is ID not method => display
            id = int(method)
            item=t2.display(table)
            response.view='display.html'
            title=T('Shelter Details')
            edit=A(T("Edit"),_href=t2.action('shelter',['update',t2.id]))
            list=A(T("List Shelters"),_href=t2.action('shelter'))
            return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,list=list)
        except:
            if method=="create":
                t2.messages.record_created=T("Shelter added")
                form=t2.create(table)
                response.view='create.html'
                title=T('Add Shelter')
                list=A(T("List Shelters"),_href=t2.action('shelter'))
                return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list=list)
            elif method=="display":
                item=t2.display(table)
                response.view='display.html'
                title=T('Shelter Details')
                edit=A(T("Edit"),_href=t2.action('shelter',['update',t2.id]))
                list=A(T("List Shelters"),_href=t2.action('shelter'))
                return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,list=list)
            elif method=="update":
                if t2.logged_in:
                    t2.messages.record_modified=T("Shelter updated")
                    form=t2.update(table)
                    response.view='update.html'
                    title=T('Edit Shelter')
                    list=A(T("List Shelters"),_href=t2.action('shelter'))
                    return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list=list)
                else:
                    t2.redirect('login')
            elif method=="delete":
                if t2.logged_in:
                    t2.messages.record_deleted=T("Shelter deleted")
                    t2.delete(table,next='shelter')
                    return
                else:
                    t2.redirect('login')
            else:
                # Invalid!
                return
    else:
        # No arguments => default to list
        list=t2.itemize(table)
        if list=="No data":
            list="No Shelters currently registered."
        response.view='list.html'
        title=T('List Shelters')
        subtitle=T('Shelters')
        add=A(T("Add Shelter"),_href=t2.action('shelter','create'))
        return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add=add)
Note: See TracWiki for help on using the wiki.