wiki:BluePrintRESTImplementation

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

Now get back to where you were after login :)

Implementation for the BluePrintREST:

# RESTful controller function
# Anonymous users can Read
# Authentication required for Create/Update/Delete
def organisation():
    table=db.or_organisation
    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('Organisation Details')
            edit=A(T("Edit"),_href=t2.action('organisation',['update',t2.id]))
            list=A(T("List Organisations"),_href=t2.action('organisation'))
            return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,list=list)
        except:
            if method=="create":
                if t2.logged_in:
                    t2.messages.record_created=T("Organisation added")
                    form=t2.create(table)
                    response.view='create.html'
                    title=T('Add Organisation')
                    list=A(T("List Organisations"),_href=t2.action('organisation'))
                    return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list=list)
                else:
                    t2.redirect('login',vars={'_destination':'organisation/create'})
            elif method=="display":
                item=t2.display(table)
                response.view='display.html'
                title=T('Organisation Details')
                edit=A(T("Edit"),_href=t2.action('organisation',['update',t2.id]))
                list=A(T("List Organisations"),_href=t2.action('organisation'))
                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("Organisation updated")
                    form=t2.update(table)
                    response.view='update.html'
                    title=T('Edit Organisation')
                    list=A(T("List Organisations"),_href=t2.action('organisation'))
                    return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list=list)
                else:
                    t2.redirect('login',vars={'_destination':'organisation/update/%i' % t2.id})
            elif method=="delete":
                if t2.logged_in:
                    t2.messages.record_deleted=T("Organisation deleted")
                    t2.delete(table,next='organisation')
                    return
                else:
                    t2.redirect('login',vars={'_destination':'organisation/delete/%i' % t2.id})
            else:
                # Invalid!
                return
    else:
        # No arguments => default to list
        list=t2.itemize(table)
        if list=="No data":
            list="No Organisations currently registered."
        title=T('List Organisations')
        subtitle=T('Organisations')
        if t2.logged_in:
            form=t2.create(table)
            response.view='list_create.html'
            return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle)
        else:
            add=A(T("Add Organisation"),_href=t2.action('organisation','create'))
            response.view='list.html'
            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.