Changes between Version 31 and Version 32 of BluePrintRESTImplementation


Ignore:
Timestamp:
01/11/09 09:33:17 (16 years ago)
Author:
Fran Boon
Comment:

Updates for s3.crud_strings, s3.crud_fields, s3.undeletable,s3.listadd, CSV/AJAX representations

Legend:

Unmodified
Added
Removed
Modified
  • BluePrintRESTImplementation

    v31 v32  
    1010                SQLField('uuid',length=64,default=uuid.uuid4()),
    1111                SQLField('name'))
    12 # NB Beware of lambdas & %s substitution as they get evaluated when called, not when defined!
    13 db['%s' % table].represent=lambda table:shn_list_item(table,resource='shelter',action='display')
     12exec("s3.crud_fields.%s=['name']" % table)
     13db['%s' % table].exposes=s3.crud_fields['%s' % table]
     14# Moved to Controller - allows us to redefine for different scenarios (& also better MVC separation)
     15#db['%s' % table].displays=s3.crud_fields['%s' % table]
     16# NB Beware of lambdas & %s substitution as they get evaluated when called, not when defined!
     17#db['%s' % table].represent=lambda table:shn_list_item(table,resource='shelter',action='display')
    1418title_create=T('Add Shelter')
    1519title_display=T('Shelter Details')
     
    2428msg_record_deleted=T('Shelter deleted')
    2529msg_list_empty=T('No Shelters currently registered')
    26 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)' % table)
     30exec('s3.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)' % table)
    2731}}}
    2832These optional settings can also be set, if-desired:
     
    3539{{{
    3640from gluon.storage import Storage
    37 crud_strings=Storage()
     41# Keep all S3 framework-level elements stored off here, so as to avoid polluting global namespace & to make it clear which part of the framework is being interacted with
     42s3=Storage()
     43s3.crud_fields=Storage()
     44s3.crud_strings=Storage()
     45s3.listonly=Storage()
     46s3.undeletable=Storage()
    3847
    3948def shn_crud_strings_lookup(resource):
    4049    "Look up CRUD strings for a given resource based on the definitions in models/module.py."
    41     return getattr(crud_strings,'%s' % resource)
     50    return getattr(s3.crud_strings,'%s' % resource)
    4251
    4352def shn_rest_controller(module,resource):
     
    5766        CSV (useful for synchronization)
    5867         - read-only for now
     68        AJAX (designed to be run asynchronously to refresh page elements)
    5969
    6070    ToDo:
     
    6979    table=db['%s_%s' % (module,resource)]
    7080    if resource=='setting':
    71         crud_strings=shn_crud_strings_lookup(resource)
     81        s3.crud_strings=shn_crud_strings_lookup(resource)
    7282    else:
    73         crud_strings=shn_crud_strings_lookup(table)
     83        s3.crud_strings=shn_crud_strings_lookup(table)
     84    try:
     85        s3.deletable=not s3.undeletable['%s' % table]
     86    except:
     87        s3.deletable=True
     88    try:
     89        s3.listadd=not s3.listonly['%s' % table]
     90    except:
     91        s3.listadd=True
    7492   
    7593    # Which representation should output be in?
     
    92110            )
    93111        if representation=="html":
     112            if t2.logged_in and s3.deletable:
     113                db['%s' % table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="INPUT(_type='checkbox',_class='delete_row',_name='%s' % resource,_id='%i' % table.id)")
     114            else:
     115                db['%s' % table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
    94116            list=t2.itemize(table)
    95117            if list=="No data":
    96                 list=crud_strings.msg_list_empty
    97             title=crud_strings.title_list
    98             subtitle=crud_strings.subtitle_list
    99             if t2.logged_in:
     118                list=s3.crud_strings.msg_list_empty
     119            title=s3.crud_strings.title_list
     120            subtitle=s3.crud_strings.subtitle_list
     121            if t2.logged_in and s3.listadd:
     122                # Display the Add form below List
     123                if s3.deletable:
     124                    # Add extra column header to explain the checkboxes
     125                    if isinstance(list,TABLE):
     126                        list.insert(0,TR('',B('Delete?')))
    100127                form=t2.create(table)
     128                # Check for presence of Custom View
    101129                custom_view='%s_list_create.html' % resource
    102130                _custom_view=os.path.join(request.folder,'views',module,custom_view)
     
    105133                else:
    106134                    response.view='list_create.html'
    107                 addtitle=crud_strings.subtitle_create
     135                addtitle=s3.crud_strings.subtitle_create
    108136                return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle,addtitle=addtitle)
    109137            else:
    110                 add_btn=A(crud_strings.label_create_button,_href=t2.action(resource,'create'),_id='add-btn')
     138                # List only
     139                if s3.listadd:
     140                    add_btn=A(s3.crud_strings.label_create_button,_href=t2.action(resource,'create'),_id='add-btn')
     141                else:
     142                    add_btn=''
     143                # Check for presence of Custom View
    111144                custom_view='%s_list.html' % resource
    112145                _custom_view=os.path.join(request.folder,'views',module,custom_view)
     
    116149                    response.view='list.html'
    117150                return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn)
     151        elif representation=="ajax":
     152            if t2.logged_in and s3.deletable:
     153                db['%s' % table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="INPUT(_type='checkbox',_class='delete_row',_name='%s' % resource,_id='%i' % table.id)")
     154            else:
     155                db['%s' % table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
     156            list=t2.itemize(table)
     157            if list=="No data":
     158                list=s3.crud_strings.msg_list_empty
     159            if s3.deletable:
     160                # Add extra column header to explain the checkboxes
     161                if isinstance(list,TABLE):
     162                    list.insert(0,TR('',B('Delete?')))
     163            response.view='plain.html'
     164            return dict(item=list)
    118165        elif representation=="plain":
    119166            list=t2.itemize(table)
     
    149196                )
    150197            if representation=="html":
     198                try:
     199                    db['%s' % table].displays=s3.crud_fields['%s' % table]
     200                except:
     201                    pass
    151202                item=t2.display(table)
     203                # Check for presence of Custom View
    152204                custom_view='%s_display.html' % resource
    153205                _custom_view=os.path.join(request.folder,'views',module,custom_view)
     
    156208                else:
    157209                    response.view='display.html'
    158                 title=crud_strings.title_display
     210                title=s3.crud_strings.title_display
    159211                edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id]),_id='edit-btn')
    160                 delete=A(T("Delete"),_href=t2.action(resource,['delete',t2.id]),_id='delete-btn')
    161                 list_btn=A(crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
     212                if s3.deletable:
     213                    delete=A(T("Delete"),_href=t2.action(resource,['delete',t2.id]),_id='delete-btn')
     214                else:
     215                    delete=''
     216                list_btn=A(s3.crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
    162217                return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,delete=delete,list_btn=list_btn)
    163218            elif representation=="plain":
     
    215270                        )
    216271                    if representation=="html":
    217                         t2.messages.record_created=crud_strings.msg_record_created
     272                        t2.messages.record_created=s3.crud_strings.msg_record_created
    218273                        form=t2.create(table)
     274                        # Check for presence of Custom View
    219275                        custom_view='%s_create.html' % resource
    220276                        _custom_view=os.path.join(request.folder,'views',module,custom_view)
     
    223279                        else:
    224280                            response.view='create.html'
    225                         title=crud_strings.title_create
    226                         list_btn=A(crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
     281                        title=s3.crud_strings.title_create
     282                        list_btn=A(s3.crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
    227283                        return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn)
    228284                    elif representation=="plain":
     
    260316                        )
    261317                    if representation=="html":
    262                         t2.messages.record_modified=crud_strings.msg_record_modified
     318                        t2.messages.record_modified=s3.crud_strings.msg_record_modified
    263319                        form=t2.update(table,deletable=False)
     320                        # Check for presence of Custom View
    264321                        custom_view='%s_update.html' % resource
    265322                        _custom_view=os.path.join(request.folder,'views',module,custom_view)
     
    268325                        else:
    269326                            response.view='update.html'
    270                         title=crud_strings.title_update
    271                         list_btn=A(crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
     327                        title=s3.crud_strings.title_update
     328                        list_btn=A(s3.crud_strings.label_list_button,_href=t2.action(resource),_id='list-btn')
    272329                        return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn)
    273330                    elif representation=="plain":
     
    302359                            new_value=''
    303360                        )
    304                     t2.messages.record_deleted=crud_strings.msg_record_deleted
    305                     t2.delete(table,next=resource)
     361                    t2.messages.record_deleted=s3.crud_strings.msg_record_deleted
     362                    if representation=="ajax":
     363                        t2.delete(table,next='%s?format=ajax' % resource)
     364                    else:
     365                        t2.delete(table,next=resource)
    306366                else:
    307367                    t2.redirect('login',vars={'_destination':'%s/delete/%i' % (resource,t2.id)})