wiki:BluePrintRESTImplementation

Version 12 (modified by Fran Boon, 14 years ago) ( diff )

key.html is Internationalised

Implementation for the BluePrintREST:

Model

Simplified:

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

# RESTful controller function
# Anonymous users can Read
# Authentication required for Create/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":
                if t2.logged_in:
                    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)
                else:
                    t2.redirect('login',vars={'_destination':'shelter/create'})
            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',vars={'_destination':'shelter/update/%i' % t2.id})
            elif method=="delete":
                if t2.logged_in:
                    t2.messages.record_deleted=T("Shelter deleted")
                    t2.delete(table,next='shelter')
                    return
                else:
                    t2.redirect('login',vars={'_destination':'shelter/delete/%i' % t2.id})
            else:
                # Invalid!
                return
    else:
        # No arguments => default to list
        list=t2.itemize(table)
        if list=="No data":
            list="No Shelters currently registered."
        title=T('List Shelters')
        subtitle=T('Shelters')
        if t2.logged_in:
            form=t2.create(table)
            response.view='list_create.html'
            addtitle=T('Add Shelter')
            return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle,addtitle=addtitle)
        else:
            add=A(T("Add Shelter"),_href=t2.action('shelter','create'))
            response.view='list.html'
            return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add=add)

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}}
{{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}}
{{except:}}
{{pass}}

list.html

{{extend 'layout.html'}}

{{try:}}
 {{=H2(title)}}
{{except:}}
{{pass}}
<div class='list-container'>
{{try:}}
 {{=H3(subtitle)}}
{{except:}}
{{pass}}
{{try:}}
 {{=list}}
{{except:}}
{{pass}}
</div>
<p>&nbsp;</p>
{{try:}}
 {{=add}}
{{except:}}
{{pass}}

list_create.html

{{extend 'layout.html'}}

{{try:}}
 {{=H2(title)}}
{{except:}}
{{pass}}
<div class='list-container'>
{{try:}}
 {{=H3(subtitle)}}
{{except:}}
{{pass}}
{{try:}}
 {{=list}}
{{except:}}
{{pass}}
</div>
<p>&nbsp;</p>
{{try:}}
 {{=H3(addtitle)}}
{{except:}}
{{pass}}
{{include 'key.html'}}
<div class='form-container'>
{{try:}}
 {{=form}}
{{except:}}
{{pass}}
</div>

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}}
{{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.