Changes between Version 43 and Version 44 of DeveloperGuidelinesS3Framework
- Timestamp:
- 08/31/10 19:13:13 (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DeveloperGuidelinesS3Framework
v43 v44 1 == Sahana Eden Framework == 1 [[TOC]] 2 = Sahana Eden Framework = 2 3 We have built an S3 framework as a higher level of abstraction on top of Web2Py.[[BR]] 3 4 This should be used by modules where possible, but if more power is needed then drop down a level of two (to T2, to base Web2Py or to raw Python). … … 17 18 This provides details on how to configure your Model & Controllers. Views may not be required, other than index.html 18 19 19 === = Custom Functions Plugged into REST ====20 === Custom Functions Plugged into REST === 20 21 In many cases it may be much easier to implement non-CRUD resource functions as REST-plugins instead of separate controllers. 21 22 * see search_simple for an example … … 34 35 and finally, XRequest remembers the record ID until the next request to the same resource in the same session, which can be helpful when producing links (especially _next's) not knowing the record ID. 35 36 36 == = Mandatory ===37 Each Controller should start like this (to populate the side navigation Menus):37 == Mandatory == 38 Each Controller should start like this: 38 39 {{{ 39 module = 'module' 40 # Current Module (for sidebar title) 41 module_name = db(db.module.name==module).select()[0].name_nice 40 module = "module" 42 41 # Options Menu (available in all Functions' Views) 43 42 response.menu_options = [ 44 [T('Home'), False, URL(r=request, f='index')], 45 [T('Add Person'), False, URL(r=request, f='person', args='create')], 46 [T('List People'), False, URL(r=request, f='person')], 47 [T('Search People'), False, URL(r=request, f='person', args='search')] 43 [T("Home"), False, URL(r=request, f="index")], [ 44 [T("Resources"), False, URL(r=request, f="resource")], 45 [T("List"), False, URL(r=request, f="person")], 46 [T("Add"), False, URL(r=request, f="resource", args="create")], 47 [T("Search People"), False, URL(r=request, f="resource", args="search")] 48 ]] 48 49 ] 49 50 }}} 50 51 51 Each function needs to return this value to the view:52 return dict(module_name=module_name)53 54 52 All tables which are user-editable need to be protected for conflict resolution & synchronization using the predefined reusable fields {{{timestamp}}} & {{{uuidstamp}}}.[[BR]] 55 These are defined in {{{models/00_ db.py}}}:53 These are defined in {{{models/00_tables.py}}}: 56 54 {{{ 57 55 # Reusable timestamp fields 58 timestamp = SQLTable(None, 'timestamp',59 db.Field('created_on','datetime',56 timestamp = SQLTable(None, "timestamp", 57 Field("created_on", "datetime", 60 58 writable=False, 61 59 default=request.now), 62 db.Field('modified_on','datetime',60 Field("modified_on", "datetime", 63 61 writable=False, 64 62 default=request.now,update=request.now)) 65 63 66 64 # We need UUIDs as part of database synchronization 67 import uuid68 uuidstamp = SQLTable(None, 'uuidstamp',69 db.Field('uuid',length=64,65 import uuid 66 uuidstamp = SQLTable(None, "uuidstamp", 67 Field("uuid", length=64, 70 68 writable=False, 71 69 default=uuid.uuid4())) 72 70 }}} 73 71 74 == = Optional ===72 == Optional == 75 73 Other reusable fields are available to add Foreign Key links to key tables, such as 'location_id' to add a GIS location for Mapping, 'person_id' to add a Person & 'organisation_id' to add a link to an Organisation. 76 74 77 75 List output are made more functional by this .represent 'widget': 78 76 {{{ 79 def shn_list_item(table, resource, action, display= 'table.name', extra=None):77 def shn_list_item(table, resource, action, display="table.name", extra=None): 80 78 if extra: 81 79 items = DIV(TR(TD(A(eval(display), _href=t2.action(resource, [action, table.id]))), TD(eval(extra)))) … … 86 84 This is called in {{{models/01_crud.py}}} in the REST Controller: 87 85 {{{ 88 db.pr_person.represent = lambda table:shn_list_item(table, resource= 'person', action='display', display='table.full_name')86 db.pr_person.represent = lambda table:shn_list_item(table, resource="person", action="display", display="table.full_name") 89 87 }}} 90 88 … … 93 91 94 92 Form field can be made to use a TEXTAREA by marking the field as being type 'text': 95 {{{Field( 'field', 'text'),}}}93 {{{Field("field", "text"),}}} 96 94 97 95 Form field can be made to use a SELECT dropdown by setting the field as a lookup to another table...linked to the 'id' field to allow [wiki:DeveloperGuidelinesDatabaseSynchronization Database Synchronization], but displaying a more user-friendly field (such as 'name'): 98 96 {{{ 99 Field( 'field', db.othertable),97 Field("field", db.othertable), 100 98 101 db.table.field.requires = IS_NULL_OR(IS_ IN_DB(db, 'othertable.id', 'othertable.name'))99 db.table.field.requires = IS_NULL_OR(IS_ONE_OF(db, "othertable.id", "othertable.name")) 102 100 }}} 103 101 … … 127 125 * .confirmation (Default response.flash is styled like this) 128 126 127 ==== Global variables ==== 128 Use session for persistent variables 129 130 Use response for one-off variables which are visible in views without explicit passing 131 129 132 === Settings === 130 System-wide settings have their default values set in {{{models/00_db.py}}}'s {{{s3_settings}}} table. 133 System-wide settings have their default values set in {{{models/000_config.py}}}. 134 135 A few settings are still in the s3_settings table. 131 136 132 137 Upon 1st run of the system, these settings are loaded into the database from where they can subsequently be edited to configure the running instance. … … 156 161 === Conflict Detection === 157 162 Sahana is a multi-user system so there is a potential for multiple users to be editing the same record at once.[[BR]] 158 We use T2to handle this for us.163 We use Web2Py's CRUD to handle this for us. 159 164 160 165 Add this reusable field to each table which needs protecting (in {{{models/module.py}}}):