Changes between Version 36 and Version 37 of BluePrintRESTImplementation


Ignore:
Timestamp:
01/18/09 21:23:04 (16 years ago)
Author:
Fran Boon
Comment:

Use method parameters, not globals

Legend:

Unmodified
Added
Removed
Modified
  • BluePrintRESTImplementation

    v36 v37  
    2929s3.crud_strings[table]=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)
    3030}}}
    31 These optional settings can also be set, if-desired:
    32 {{{
    33 s3.listonly[table]=1    # Don't allow resources to be creatable from List view
    34 s3.undeletable[table]=1 # Don't allow resources to be deletable from List/Display views
    35 }}}
    3631
    3732This is the supporting material in {{{models/__db.py}}}:
     
    4237s3.crud_fields=Storage()
    4338s3.crud_strings=Storage()
    44 s3.listonly=Storage()
    45 s3.undeletable=Storage()
    4639
    4740def shn_crud_strings_lookup(resource):
     
    4942    return getattr(s3.crud_strings,'%s' % resource)
    5043
    51 def shn_rest_controller(module,resource):
     44def import_csv(table,file):
     45    "Import CSV file into Database. Comes from appadmin.py"
     46    import csv
     47    reader = csv.reader(file)
     48    colnames=None
     49    for line in reader:
     50        if not colnames:
     51            colnames=[x[x.find('.')+1:] for x in line]
     52            c=[i for i in range(len(line)) if colnames[i]!='id']           
     53        else:
     54            items=[(colnames[i],line[i]) for i in c]
     55            table.insert(**dict(items))
     56
     57def import_json(table,file):
     58    "Import JSON into Database."
     59    import gluon.contrib.simplejson as sj
     60    reader=sj.loads(file)
     61    # ToDo
     62    # Get column names
     63    # Insert records
     64    #table.insert(**dict(items))
     65    return
     66           
     67def shn_rest_controller(module,resource,deletable=True,listadd=True,extra=None):
    5268    """
    5369    RESTlike controller function.
     70   
     71    Provides CRUD operations for the given module/resource.
     72    Optional parameters:
     73    deletable=False: don't provide visible options for deletion
     74    listadd=False: don't provide an add form in the list view
     75    extra='field': extra field to display in the list view
    5476   
    5577    Anonymous users can Read.
     
    5779   
    5880    Auditing options for Read &/or Write.
    59 
     81   
    6082    Supported Representations:
    6183        HTML is the default (including full Layout)
     
    6688         - read-only for now
    6789        CSV (useful for synchronization)
    68          - read-only for now
     90         - List/Display/Create for now
    6991        AJAX (designed to be run asynchronously to refresh page elements)
    7092
     
    7294        Alternate Representations
    7395            JSON create/update
    74             CSV create/update
    75             SMS,XML,PDF
     96            CSV update
     97            SMS,XML,PDF,LDIF
    7698        Customisable Security Policy
    7799    """
     
    83105    else:
    84106        s3.crud_strings=shn_crud_strings_lookup(table)
    85     try:
    86         s3.deletable=not s3.undeletable[_table]
    87     except:
    88         s3.deletable=True
    89     try:
    90         s3.listadd=not s3.listonly[_table]
    91     except:
    92         s3.listadd=True
    93107   
    94108    # Which representation should output be in?
     
    111125            )
    112126        if representation=="html":
    113             if t2.logged_in and s3.deletable:
    114                 db[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)")
     127            if t2.logged_in and deletable:
     128                if extra:
     129                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="TD(db(db.gis_projection.id==%i).select()[0].%s),TD(INPUT(_type='checkbox',_class='delete_row',_name='%s',_id='%i'))" % (table.id,extra,resource,table.id))
     130                else:
     131                    db[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)")
    115132            else:
    116                 db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
     133                if extra:
     134                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="db(db.gis_projection.id==%i).select()[0].%s" % (table.id,extra))
     135                else:
     136                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
    117137            list=t2.itemize(table)
    118             if list=="None":
     138            if not list:
    119139                list=s3.crud_strings.msg_list_empty
    120140            title=s3.crud_strings.title_list
    121141            subtitle=s3.crud_strings.subtitle_list
    122             if t2.logged_in and s3.listadd:
     142            if t2.logged_in and listadd:
    123143                # Display the Add form below List
    124                 if s3.deletable:
     144                if deletable:
    125145                    # Add extra column header to explain the checkboxes
    126146                    if isinstance(list,TABLE):
     
    138158            else:
    139159                # List only
    140                 if s3.listadd:
     160                if listadd:
    141161                    add_btn=A(s3.crud_strings.label_create_button,_href=t2.action(resource,'create'),_id='add-btn')
    142162                else:
     
    151171                return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn)
    152172        elif representation=="ajax":
    153             if t2.logged_in and s3.deletable:
    154                 db[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)")
     173            if t2.logged_in and deletable:
     174                if extra:
     175                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="TD(db(db.gis_projection.id==%i).select()[0].%s),TD(INPUT(_type='checkbox',_class='delete_row',_name='%s',_id='%i'))" % (table.id,extra,resource,table.id))
     176                else:
     177                    db[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)")
    155178            else:
    156                 db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
     179                if extra:
     180                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display',extra="db(db.gis_projection.id==%i).select()[0].%s" % (table.id,extra))
     181                else:
     182                    db[table].represent=lambda table:shn_list_item(table,resource='%s' % resource,action='display')
    157183            list=t2.itemize(table)
    158             if list=="No data":
     184            if not list:
    159185                list=s3.crud_strings.msg_list_empty
    160             if s3.deletable:
     186            if deletable:
    161187                # Add extra column header to explain the checkboxes
    162188                if isinstance(list,TABLE):
     
    211237                title=s3.crud_strings.title_display
    212238                edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id]),_id='edit-btn')
    213                 if s3.deletable:
     239                if deletable:
    214240                    delete=A(T("Delete"),_href=t2.action(resource,['delete',t2.id]),_id='delete-btn')
    215241                else:
     
    289315                    elif representation=="json":
    290316                        # ToDo
     317                        # Read in POST
     318                        #file=request.body.read()
     319                        #import_json(table,file)
    291320                        item='{"Status":"failed","Error":{"StatusCode":501,"Message":"JSON creates not yet supported!"}}'
    292321                        response.view='plain.html'
    293322                        return dict(item=item)
     323                    elif representation=="csv":
     324                        # Read in POST
     325                        file=request.vars.filename.file
     326                        try:
     327                            import_csv(table,file)
     328                            reply=T('Data uploaded')
     329                        except:
     330                            reply=T('Unable to parse CSV file!')
     331                        return reply
    294332                    else:
    295333                        session.error=T("Unsupported format!")
     
    378416                    )
    379417                if representation=="html":
    380                     if t2.logged_in and s3.deletable:
     418                    if t2.logged_in and deletable:
    381419                        db[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)")
    382420                    else:
     
    407445    return shn_rest_controller(module,'shelter')
    408446}}}
     447These optional settings can also be set, if-desired:
     448{{{
     449# Don't allow resources to be creatable from List view
     450return shn_rest_controller(module,'shelter',listadd=False)
     451# Don't allow resources to be deletable from List/Display views
     452return shn_rest_controller(module,'shelter',deletable=False)
     453# Display extra field in the list view
     454return shn_rest_controller(module,'shelter',extra='epsg')
     455}}}
    409456
    410457== Views ==