| 7 | | single=resource.capitalize() |
| 8 | | # NB May need manual fixing! |
| 9 | | plural=single+'s' |
| 10 | | title_create=T('Add %s' % single) |
| 11 | | title_display=T('%s Details' % single) |
| 12 | | title_list=T('List %s' % plural) |
| 13 | | title_update=T('Edit %s' % single) |
| 14 | | subtitle_create=T('Add New %s' % single) |
| 15 | | subtitle_list=T('%s' % plural) |
| 16 | | label_list_button=T('List %s' % plural) |
| 17 | | label_create_button=T('Add %s' % single) |
| 18 | | msg_record_created=T('%s added' % single) |
| 19 | | msg_record_modified=T('%s updated' % single) |
| 20 | | msg_record_deleted=T('%s deleted' % single) |
| 21 | | msg_list_empty=T('No %s currently registered' % plural) |
| | 7 | table=module+'_'+resource |
| | 8 | db.define_table(table, |
| | 9 | SQLField('modified_on','datetime',default=now), |
| | 10 | SQLField('uuid',length=64,default=uuid.uuid4()), |
| | 11 | 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') |
| | 14 | title_create=T('Add Shelter') |
| | 15 | title_display=T('Shelter Details') |
| | 16 | title_list=T('List Shelters') |
| | 17 | title_update=T('Edit Shelter') |
| | 18 | subtitle_create=T('Add New Shelter') |
| | 19 | subtitle_list=T('Shelters') |
| | 20 | label_list_button=T('List Shelters') |
| | 21 | label_create_button=T('Add Shelter') |
| | 22 | msg_record_created=T('Shelter added') |
| | 23 | msg_record_modified=T('Shelter updated') |
| | 24 | msg_record_deleted=T('Shelter deleted') |
| | 25 | msg_list_empty=T('No Shelters currently registered') |
| 50 | | list=t2.itemize(table) |
| 51 | | if list=="No data": |
| 52 | | list=crud_strings.msg_list_empty |
| 53 | | title=crud_strings.title_list |
| 54 | | subtitle=crud_strings.subtitle_list |
| 55 | | if t2.logged_in: |
| 56 | | form=t2.create(table) |
| 57 | | response.view='list_create.html' |
| 58 | | addtitle=crud_strings.subtitle_create |
| 59 | | return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle,addtitle=addtitle) |
| | 73 | if representation=="html": |
| | 74 | list=t2.itemize(table) |
| | 75 | if list=="No data": |
| | 76 | list=crud_strings.msg_list_empty |
| | 77 | title=crud_strings.title_list |
| | 78 | subtitle=crud_strings.subtitle_list |
| | 79 | if t2.logged_in: |
| | 80 | form=t2.create(table) |
| | 81 | response.view='list_create.html' |
| | 82 | addtitle=crud_strings.subtitle_create |
| | 83 | return dict(module_name=module_name,modules=modules,options=options,list=list,form=form,title=title,subtitle=subtitle,addtitle=addtitle) |
| | 84 | else: |
| | 85 | add_btn=A(crud_strings.label_create_button,_href=t2.action(resource,'create')) |
| | 86 | response.view='list.html' |
| | 87 | return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn) |
| | 88 | elif representation=="plain": |
| | 89 | list=t2.itemize(table) |
| | 90 | response.view='plain.html' |
| | 91 | return dict(item=list) |
| | 92 | elif representation=="json": |
| | 93 | list=db().select(table.ALL).json() |
| | 94 | response.view='plain.html' |
| | 95 | return dict(item=list) |
| 61 | | add_btn=A(crud_strings.label_create_button,_href=t2.action(resource,'create')) |
| 62 | | response.view='list.html' |
| 63 | | return dict(module_name=module_name,modules=modules,options=options,list=list,title=title,subtitle=subtitle,add_btn=add_btn) |
| 64 | | |
| | 97 | session.error=T("Unsupported format!") |
| | 98 | redirect(URL(r=request,f=resource)) |
| 69 | | # Default format (representation) is full HTML page |
| 70 | | item=t2.display(table) |
| 71 | | response.view='display.html' |
| 72 | | title=crud_strings.title_display |
| 73 | | edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id])) |
| 74 | | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| 75 | | return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,list_btn=list_btn) |
| | 103 | if representation=="html": |
| | 104 | item=t2.display(table) |
| | 105 | response.view='display.html' |
| | 106 | title=crud_strings.title_display |
| | 107 | edit=A(T("Edit"),_href=t2.action(resource,['update',t2.id])) |
| | 108 | delete=A(T("Delete"),_href=t2.action(resource,['delete',t2.id])) |
| | 109 | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| | 110 | return dict(module_name=module_name,modules=modules,options=options,item=item,title=title,edit=edit,delete=delete,list_btn=list_btn) |
| | 111 | elif representation=="plain": |
| | 112 | item=t2.display(table) |
| | 113 | response.view='plain.html' |
| | 114 | return dict(item=item) |
| | 115 | elif representation=="json": |
| | 116 | item=db(table.id==t2.id).select(table.ALL).json() |
| | 117 | response.view='plain.html' |
| | 118 | return dict(item=item) |
| | 119 | else: |
| | 120 | session.error=T("Unsupported format!") |
| | 121 | redirect(URL(r=request,f=resource)) |
| 79 | | t2.messages.record_created=crud_strings.msg_record_created |
| 80 | | form=t2.create(table) |
| 81 | | response.view='create.html' |
| 82 | | title=crud_strings.title_create |
| 83 | | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| 84 | | return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) |
| | 125 | if representation=="html": |
| | 126 | t2.messages.record_created=crud_strings.msg_record_created |
| | 127 | form=t2.create(table) |
| | 128 | response.view='create.html' |
| | 129 | title=crud_strings.title_create |
| | 130 | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| | 131 | return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) |
| | 132 | elif representation=="plain": |
| | 133 | form=t2.create(table) |
| | 134 | response.view='plain.html' |
| | 135 | return dict(item=form) |
| | 136 | elif representation=="json": |
| | 137 | # ToDo |
| | 138 | item='{"Status":"failed","Error":{"StatusCode":501,"Message":"JSON creates not yet supported!"}}' |
| | 139 | response.view='plain.html' |
| | 140 | return dict(item=item) |
| | 141 | else: |
| | 142 | session.error=T("Unsupported format!") |
| | 143 | redirect(URL(r=request,f=resource)) |
| 91 | | t2.messages.record_modified=crud_strings.msg_record_modified |
| 92 | | form=t2.update(table) |
| 93 | | response.view='update.html' |
| 94 | | title=crud_strings.title_update |
| 95 | | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| 96 | | return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) |
| | 150 | if representation=="html": |
| | 151 | t2.messages.record_modified=crud_strings.msg_record_modified |
| | 152 | form=t2.update(table,deletable=False) |
| | 153 | response.view='update.html' |
| | 154 | title=crud_strings.title_update |
| | 155 | list_btn=A(crud_strings.label_list_button,_href=t2.action(resource)) |
| | 156 | return dict(module_name=module_name,modules=modules,options=options,form=form,title=title,list_btn=list_btn) |
| | 157 | elif representation=="plain": |
| | 158 | form=t2.update(table,deletable=False) |
| | 159 | response.view='plain.html' |
| | 160 | return dict(item=form) |
| | 161 | elif representation=="json": |
| | 162 | # ToDo |
| | 163 | item='{"Status":"failed","Error":{"StatusCode":501,"Message":"JSON updates not yet supported!"}}' |
| | 164 | response.view='plain.html' |
| | 165 | return dict(item=item) |
| | 166 | else: |
| | 167 | session.error=T("Unsupported format!") |
| | 168 | redirect(URL(r=request,f=resource)) |
| 107 | | # Unsupported method! |
| 108 | | t2.redirect(resource) |
| 109 | | }}} |
| 110 | | |
| 111 | | Simplified module table: |
| 112 | | {{{ |
| 113 | | resource='shelter' |
| 114 | | table=module+'_'+resource |
| 115 | | db.define_table(table, |
| 116 | | SQLField('modified_on','datetime',default=now), |
| 117 | | SQLField('uuid',length=64,default=uuid.uuid4()), |
| 118 | | SQLField('name')) |
| 119 | | db['%s' % table].represent=lambda table:shn_list_item(table,resource='shelter',action='display') |
| 120 | | }}} |
| 121 | | NB Beware of lambdas & %s substitution as they get evaluated when called, not when defined! |
| | 178 | session.error=T("Unsupported method!") |
| | 179 | redirect(URL(r=request,f=resource)) |
| | 180 | }}} |