Changes between Version 36 and Version 37 of BluePrintRESTImplementation
- Timestamp:
- 01/18/09 21:23:04 (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
BluePrintRESTImplementation
v36 v37 29 29 s3.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) 30 30 }}} 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 view34 s3.undeletable[table]=1 # Don't allow resources to be deletable from List/Display views35 }}}36 31 37 32 This is the supporting material in {{{models/__db.py}}}: … … 42 37 s3.crud_fields=Storage() 43 38 s3.crud_strings=Storage() 44 s3.listonly=Storage()45 s3.undeletable=Storage()46 39 47 40 def shn_crud_strings_lookup(resource): … … 49 42 return getattr(s3.crud_strings,'%s' % resource) 50 43 51 def shn_rest_controller(module,resource): 44 def 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 57 def 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 67 def shn_rest_controller(module,resource,deletable=True,listadd=True,extra=None): 52 68 """ 53 69 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 54 76 55 77 Anonymous users can Read. … … 57 79 58 80 Auditing options for Read &/or Write. 59 81 60 82 Supported Representations: 61 83 HTML is the default (including full Layout) … … 66 88 - read-only for now 67 89 CSV (useful for synchronization) 68 - read-onlyfor now90 - List/Display/Create for now 69 91 AJAX (designed to be run asynchronously to refresh page elements) 70 92 … … 72 94 Alternate Representations 73 95 JSON create/update 74 CSV create/update75 SMS,XML,PDF 96 CSV update 97 SMS,XML,PDF,LDIF 76 98 Customisable Security Policy 77 99 """ … … 83 105 else: 84 106 s3.crud_strings=shn_crud_strings_lookup(table) 85 try:86 s3.deletable=not s3.undeletable[_table]87 except:88 s3.deletable=True89 try:90 s3.listadd=not s3.listonly[_table]91 except:92 s3.listadd=True93 107 94 108 # Which representation should output be in? … … 111 125 ) 112 126 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)") 115 132 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') 117 137 list=t2.itemize(table) 118 if list=="None":138 if not list: 119 139 list=s3.crud_strings.msg_list_empty 120 140 title=s3.crud_strings.title_list 121 141 subtitle=s3.crud_strings.subtitle_list 122 if t2.logged_in and s3.listadd:142 if t2.logged_in and listadd: 123 143 # Display the Add form below List 124 if s3.deletable:144 if deletable: 125 145 # Add extra column header to explain the checkboxes 126 146 if isinstance(list,TABLE): … … 138 158 else: 139 159 # List only 140 if s3.listadd:160 if listadd: 141 161 add_btn=A(s3.crud_strings.label_create_button,_href=t2.action(resource,'create'),_id='add-btn') 142 162 else: … … 151 171 return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn) 152 172 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)") 155 178 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') 157 183 list=t2.itemize(table) 158 if list=="No data":184 if not list: 159 185 list=s3.crud_strings.msg_list_empty 160 if s3.deletable:186 if deletable: 161 187 # Add extra column header to explain the checkboxes 162 188 if isinstance(list,TABLE): … … 211 237 title=s3.crud_strings.title_display 212 238 edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id]),_id='edit-btn') 213 if s3.deletable:239 if deletable: 214 240 delete=A(T("Delete"),_href=t2.action(resource,['delete',t2.id]),_id='delete-btn') 215 241 else: … … 289 315 elif representation=="json": 290 316 # ToDo 317 # Read in POST 318 #file=request.body.read() 319 #import_json(table,file) 291 320 item='{"Status":"failed","Error":{"StatusCode":501,"Message":"JSON creates not yet supported!"}}' 292 321 response.view='plain.html' 293 322 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 294 332 else: 295 333 session.error=T("Unsupported format!") … … 378 416 ) 379 417 if representation=="html": 380 if t2.logged_in and s3.deletable:418 if t2.logged_in and deletable: 381 419 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)") 382 420 else: … … 407 445 return shn_rest_controller(module,'shelter') 408 446 }}} 447 These optional settings can also be set, if-desired: 448 {{{ 449 # Don't allow resources to be creatable from List view 450 return shn_rest_controller(module,'shelter',listadd=False) 451 # Don't allow resources to be deletable from List/Display views 452 return shn_rest_controller(module,'shelter',deletable=False) 453 # Display extra field in the list view 454 return shn_rest_controller(module,'shelter',extra='epsg') 455 }}} 409 456 410 457 == Views ==