Introduction to the Code
Fran Boon

fran@sahanafoundation.org

SahanaCamp Viet Nam

1
Installing
a
Developer Environment
http://eden.sahanafoundation.org/wiki/InstallationGuidelines
SahanaCamp Viet Nam

2
Architecture
Web Server

n/a

Application

Sahana Eden

Web Framework

Web2Py

Programming Language

Python

Database

SQLite

Operating System

Windows, Linux or Mac

SahanaCamp Viet Nam
Installation process
Wizard on Wiki to select correct instructions:
http://eden.sahanafoundation.org/wiki/InstallationGuidelines



Bootable USB
Windows


Developer Installer




Mac, Linux




Eden-Python-Installer-Dev.exe

Follow instructions to install from source

Virtual Machine

SahanaCamp Viet Nam
SahanaCamp Viet Nam

5
Editor / Debugger
Notepad+ good basic editor for Windows
+
Eclipse allows advanced debugging:


set breakpoints and step through code

http://eden.sahanafoundation.org/wiki/DeveloperGuidelinesEclipse

Firebug allows advanced debugging of CSS and
JavaScript





view AJAX requests
edit CSS in realtime
browse DOM
set breakpoints and step through code
SahanaCamp Viet Nam
Edit the Menus
modules/s3menus.py
def org(self):
""" ORG / Organization Registry """
return M(c="org")(
M("Organizations", f="organisation")(
M("New", m="create"),
M("List All"),
M("Search", m="search"),
M("Import", m="import")
),
M("Venues", f="office")(
M("New", m="create"),
M("List All"),
#M("Search", m="search"),
M("Import", m="import")
),
)
SahanaCamp Viet Nam
Building
your own
Module!
http://en.flossmanuals.net/sahana-eden/building-a-new-application/
SahanaCamp Viet Nam

8
Whitespace Matters
Unlike other languages which use parentheses to
delimit code blocks {...}, Python instead uses White
Space

SahanaCamp Viet Nam
Debug Mode
models/000_config.py
settings.base.debug = True



Reloads modules/s3db every request



CSS & JavaScript unminified

SahanaCamp Viet Nam
Training Module
Resource: 'Course'






Name
Date / Time
Location: Venue
Facilitator
Welcome Pack

SahanaCamp Viet Nam
Define the Basic Data Model
models/training.py
tablename = "training_course"
db.define_table(tablename,
Field("name"),
Field("start"),
Field("facilitator"),
)


Table Name: module_resource



Database 'migrated' automatically

SahanaCamp Viet Nam
Add a Controller
controllers/training.py
def course():
return s3_rest_controller()


Functions map URLs to code
& resources

SahanaCamp Viet Nam
Try it out!
http://127.0.0.1:8000/eden/training/course


List



Create



Read



Update



Delete

SahanaCamp Viet Nam
Other Formats
http://127.0.0.1:8000/eden/training/course .xls
http://127.0.0.1:8000/eden/training/course .xml
http://127.0.0.1:8000/eden/training/course .json

SahanaCamp Viet Nam
Field Types
models/training.py
Field("name"),
s3base.s3_date("start"),
Field("welcome_pack", "upload"),

SahanaCamp Viet Nam
Field Labels
models/training.py
S3base.s3_date("start",
label=T("Start Date")),

SahanaCamp Viet Nam
Links to other Resources
models/training.py
Field("name"),
s3db.pr_person_id(label="Facilitator"),

SahanaCamp Viet Nam
Pivot Table Reports
http://127.0.0.1:8000/eden/training/course /report

SahanaCamp Viet Nam
Maps
models/training.py
Field("name"),
s3db.gis_location_id(),

http://127.0.0.1:8000/eden/training/course /map
SahanaCamp Viet Nam
Which File do I Edit?

http://en.flossmanuals.net/sahana-eden/customisation/
SahanaCamp Viet Nam

21
SahanaCamp Viet Nam

22
Model View Controller
Each module will have:


Model: modules/s3db/modulename.py




Controller: controllers/modulename.py




Defines the data model
Links URLs to resources

Views: views/modulename/index.html


HTML templates with embedded python code

There may be additional view files for custom pages

SahanaCamp Viet Nam
Web2Py URL Mapping
http://host/application/controller/function

e.g.: http://127.0.0.1:8000/eden/default/index


Model: not



Controller: web2py/applications/eden/controllers/default.py




applicable here

Function: def

index():

View: web2py/applications/eden/views/default/index.html

SahanaCamp Viet Nam
Sahana Eden URL Mapping
http://host/eden/module/resource
eg: http://127.0.0.1:8000/eden/org/site


Model: web2py/applications/eden/modules/s3db/org.py




tablename = "org_site"

Controller: web2py/applications/eden/controllers/org.py


Function: def

site():
return s3_rest_controller()



View: not

applicable here

SahanaCamp Viet Nam
Views


HTML with Python inside {{..}}



Extend layout.html for basic look and feel

views/training/index.html
{{extend "layout.html"}}
<H1>Welcome to the Training Module</H1>
<A href='{{=URL(f="course")}}'>Browse the Course
List</A>


Resource Controllers use Generic Views:

web2py/applications/eden/views/_list_create.html



Can customise these in the template:

web2py/applications/eden/templates/template/views/_list_create.html

SahanaCamp Viet Nam
Edit a Field Label
modules/s3db/org.py
tablename = "org_organisation"
...
Field("donation_phone",
label = T("Donation Phone Number"),

SahanaCamp Viet Nam
Hide a Field
modules/s3db/org.py
tablename = "org_office"
...
Field("type",
"integer
readable=False,
writable=False,
label=T("Type")),

SahanaCamp Viet Nam
Add a New Field
modules/s3db/org.py
tablename = "org_organisation"
...
Field("facebook", label=T("Facebook Page")),

SahanaCamp Viet Nam
Git
Controlled Updates
http://eden.sahanafoundation.org/wiki/DeveloperGuidelines/Git
SahanaCamp Viet Nam

30
Installing Git
GitHub.com has excellent instructions

Create SSH public/private keys
ssh-keygen -t rsa -C "my_email@email.com"

SahanaCamp Viet Nam
Creating your Branch
https://github.com/flavour/eden
Get local copy: git
Stay up date: git

clone git@github.com:myname/eden.git

pull upstream master

SahanaCamp Viet Nam
Updating your Server
Commit code locally: git
Push to GitHub: git
Pull on Server: git

commit -a

push

pull

SahanaCamp Viet Nam
Sharing your Code
Commit code locally: git
Push to GitHub: git

commit -a

push

Submit Pull Request to get code merged into Trunk

SahanaCamp Viet Nam
Tech 2 - Introduction to the Code

Tech 2 - Introduction to the Code