Changes between Version 1 and Version 2 of DeveloperGuidelines
- Timestamp:
- 12/20/08 03:14:01 (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DeveloperGuidelines
v1 v2 1 1 Web2Py: 2 *http://mdp.cti.depaul.edu/examples/static/cookbook.pdf3 *http://mdp.cti.depaul.edu/examples/static/web2py_cheatsheet.pdf2 - http://mdp.cti.depaul.edu/examples/static/cookbook.pdf 3 - http://mdp.cti.depaul.edu/examples/static/web2py_cheatsheet.pdf 4 4 5 5 MVC (like Rails) … … 11 11 Python functions in /controllers/module.py 12 12 e.g. 13 {{{ 13 14 def list_records(): 14 15 list=t2.itemize(table) 15 16 return dict (list=list) 16 17 }}} 17 18 View 18 19 HTML/Javascript templates in /views/module/function.html … … 24 25 25 26 Python: 26 *Slow start27 - http://openbookproject.net/thinkcs/python/english2e/28 *Quick start29 - http://diveintopython.org/30 *v.Quick start!31 32 27 - Slow start 28 - http://openbookproject.net/thinkcs/python/english2e/ 29 - Quick start 30 - http://diveintopython.org/ 31 - v.Quick start! 32 - Indentation matters (use 4 spaces instead of Tabs) 33 - OOP - everything is an object 33 34 34 35 T2 is used for AAA & simplified CRUD (inc Conflict Detection) … … 39 40 Create Record: 40 41 Controller 42 {{{ 41 43 def add_record(): 42 44 form=t2.create(db.table) 43 45 return dict(form=form) 46 }}} 44 47 View 45 48 views/module/add_record.html 49 {{{ 46 50 {{=form}} 51 }}} 47 52 Display Record: 48 53 Controller 54 {{{ 49 55 def display_record(): 50 56 item=t2.display(db.table) 51 57 return dict(item=item) 58 }}} 52 59 View 53 60 views/module/display_record.html 61 {{{ 54 62 {{=item}} 63 }}} 55 64 Update Record: 56 65 Controller 66 {{{ 57 67 def display_record(): 58 68 form=t2.update(db.table) 59 69 return dict(form=form) 70 }}} 60 71 View 61 72 views/module/add_record.html 73 {{{ 62 74 {{=form}} 75 }}} 63 76 Delete Record: 64 77 Controller 78 {{{ 65 79 def delete_record(): 66 80 db(db.table.id==t2.id).delete() 67 81 response.confirmation=T("Record deleted") 68 82 response.view="module/list_records.html" 83 }}} 69 84 View 70 85 normally not used (reuse existing list_records view) … … 72 87 73 88 Populate the side navigation Menus by adding this to each controller: 89 {{{ 74 90 module='module' 75 91 # Current Module (for sidebar title) … … 79 95 # List Options (from which to build Menu for this Module) 80 96 options=db(db['%s_menu_option' % module].enabled=='Yes').select(db['%s_menu_option' % module].ALL,orderby=db['%s_menu_option' % module].priority) 97 }}} 81 98 82 99 Each function needs to return these values to the view: … … 84 101 85 102 List output can be made more functional by adding this to your table definitions in models/db.py: 86 db.table.represent=lambda table: A(table.display_field,_href=t2.action('display_table',table.id))103 {{{db.table.represent=lambda table: A(table.display_field,_href=t2.action('display_table',table.id))}}} 87 104 Form labels can be set in a translatable manner using: 88 db.table.field.label=T("label")105 {{{db.table.field.label=T("label")}}} 89 106 Form field can be made to use a TEXTAREA by marking the field as being type 'text': 90 SQLField('field','text'),107 {{{SQLField('field','text'),}}} 91 108 Form field can be made to use a SELECT dropdown by setting the field as a lookup to another table: 92 SQLField('field',db.othertable),109 {{{SQLField('field',db.othertable),}}} 93 110 Set this to use a different field than 'id' in the views (e.g. 'name') using: 94 db.table.field.requires=IS_NULL_OR(IS_IN_DB(db,'othertable.id','othertable.name'))111 {{{db.table.field.requires=IS_NULL_OR(IS_IN_DB(db,'othertable.id','othertable.name'))}}} 95 112 Form field being required can be marked using: 96 db.table.field.comment=SPAN("*",_class="req")113 {{{db.table.field.comment=SPAN("*",_class="req")}}} 97 114 Help for a form field can be set using: 98 A(SPAN("[Help]"),_class="popupLink",_id="tooltip",_title=T("Help|This is what this field is for.")) 115 {{{A(SPAN("[Help]"),_class="popupLink",_id="tooltip",_title=T("Help|This is what this field is for."))}}} 116 99 117 100 118 Conflict Detection: … … 105 123 Controller 106 124 Each controller which incldues protected functions needs this: 125 {{{ 107 126 def login(): 108 127 response.view='login.html' … … 115 134 @t2.requires_login('login') 116 135 def function(): 117 136 }}} 118 137 119 138 How to add a new module? … … 122 141 Add module to db.module: 123 142 http://127.0.0.1:8000/sahana/appadmin/select/db?query=db.module.id%3E0 124 Create a table to store the module's menu options to /models/ db.py143 Create a table to store the module's menu options to /models/module.py 125 144 db.module_menu_option 126 Add tables to /models/db.py 145 Add tables to /models/module.py 146 db.module_table 127 147 Controller 128 148 Add CRUD functions for these tables to /views/module.py … … 136 156 137 157 Advanced Tricks: 138 *Python debugging139 - use Web2Py shell: http://www.vimeo.com/879939140 - http://docs.python.org/library/doctest.html141 - http://docs.python.org/library/bdb.html142 *CSS & Javascript debugging143 - use Firebug: http://code.google.com/support/bin/answer.py?answer=77412&topic=12044144 *jQuery:145 - http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx146 *Ext:147 - http://extjs.com/learn/Tutorial:Introduction_to_Ext_2.0148 - http://extjs.com/learn/Manual:Basic_Application_Design149 - http://extjs.com/forum/showthread.php?t=26728150 - http://extjs.com/learn/Ext_FAQ_Debugging158 - Python debugging 159 - use Web2Py shell: http://www.vimeo.com/879939 160 - http://docs.python.org/library/doctest.html 161 - http://docs.python.org/library/bdb.html 162 - CSS & Javascript debugging 163 - use Firebug: http://code.google.com/support/bin/answer.py?answer=77412&topic=12044 164 - jQuery: 165 - http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx 166 - Ext: 167 - http://extjs.com/learn/Tutorial:Introduction_to_Ext_2.0 168 - http://extjs.com/learn/Manual:Basic_Application_Design 169 - http://extjs.com/forum/showthread.php?t=26728 170 - http://extjs.com/learn/Ext_FAQ_Debugging