Sahana Eden - Introduction to the Code

49
: Sahana Eden Emergency Development Environment 2 July 2010, Sahana Camp Fran Boon fran@ sahanafoundation.org

description

A 1/2 day tutorial on how to code in the Sahana Eden framework.(This course is still under development)

Transcript of Sahana Eden - Introduction to the Code

Page 1: Sahana Eden - Introduction to the Code

:Sahana Eden Emergency DevelopmentEnvironment

2 July 2010, Sahana CampFran Boon

fran@ sahanafoundation.org

Page 2: Sahana Eden - Introduction to the Code

: -Technology Stack Back end•Python• 2Web Py

–MVC framework–cron scheduler

•Libraries–lxml–Shapely–PyGSM–xlwt–s3*** (custom Eden modules)

Page 3: Sahana Eden - Introduction to the Code

: -Technology Stack Front end

•JavaScript–jQuery

•dataTables•colorbox

–ExtJS

–OpenLayers•GeoExt

Page 4: Sahana Eden - Introduction to the Code

Set yours e lf up

:// . . / /http eden s ahanafoundation org wiki Ins tallationGuide lines Deve loper•Install Python:

– inc lxml & Shapely

•Install Bzr•Install web2py

bzr export web2py lp:~mdipierro/web2py/devel•Download Eden

cd applicationsbzr branch lp:~sahana-eden edencd ..python web2py.py

Page 5: Sahana Eden - Introduction to the Code

: 2 Exploring Web Py s hell•Python is great for interactive exploring!•Web2Py allows this too •Install iPython to make it better!

–pyreadline needed on Windows–explore objects with tab

python web2py.py –S eden –M

Page 6: Sahana Eden - Introduction to the Code

- -Model View Controller•Models

–Database Table definitions

•Contro llers–Workflow, Logic

•Views–HTML / JS Templates parsed server-side–JS functions then run client-side in browser

•Static–no server-side processing–Images, CSS, JavaScript

Page 7: Sahana Eden - Introduction to the Code

Modules

•org - Organisation Registry•mpr - Missing Person Registry•cr - Shelter Registry•hms - Hospital Management•rms - Request Management•vol – Volunteer Management•msg - Messaging•gis - Mapping

Page 8: Sahana Eden - Introduction to the Code

Res ources• person (pr)• location (gis)• organisation (org)• hospital (hms)• shelter (cr)

•REST Controller–HTTP verbs: GET, PUT (POST), DELETE–Args: /update, /create–Representations: .html, .json, .xml, .xls, .pdf

Page 9: Sahana Eden - Introduction to the Code

eden module resource

JavaScript

URL(r=request, a=application, c=controller, f=function, args=[], vars={})

Page 10: Sahana Eden - Introduction to the Code

Models

•All executed every request–in alphabetical order–within web2py environment

•Define Tables in Database–Live Migrations

•Provide utility functions & variables which are used by multiple controllers

Page 11: Sahana Eden - Introduction to the Code

Core Models

•000_ .config py–deployment settings for easy customisation

•00_ .db py–connect to the database–instantiate classes

•00_ .s e ttings py–read deployment settings & action them

Page 12: Sahana Eden - Introduction to the Code

( )Core Models cont•00_ .tables py

–define some re-usable fields for tables–define admin tables

•00_ .utils py–patch session–utility functions

•01_ .crud py–REST Controller front-end

Page 13: Sahana Eden - Introduction to the Code

( )Core Models cont

•01_ .menu py–build the Menus

• _ 1 _ .zzz s t run py–Import default data on 1st run

(needs all tables defined 1st)

Page 14: Sahana Eden - Introduction to the Code

Controllers

•Entire Controller is executed–Manipulation done outside of functions is

visible to all functions

•Function which has been called is executed

•Functions with no arguments are automatically visible via URLs

Page 15: Sahana Eden - Introduction to the Code

Views

•Defaults to views/controller/function.html

•Views can extend & include other views– views/layout.html

•Variables only visible to views if explicitly passed in dict() or else data stored in global variables:

•request, response, sessionhttp://127.0.0.1:8000/eden/default/about

Page 16: Sahana Eden - Introduction to the Code

Coffee

Page 17: Sahana Eden - Introduction to the Code

Adding a new module

•Vehicle Tracking System•Name: vts•Resources:

–vehicle–driver–location

Page 18: Sahana Eden - Introduction to the Code

Model

models/vts.py

# Vehicle resourcetable = db.define_table( “vts_vehicle”,

Field("registration"),Field("make"),Field("model"),Field("supplier"),)

Page 19: Sahana Eden - Introduction to the Code

Controller

controllers/vts.py

def vehicle():return shn_rest_controller("vts", "vehicle")

Page 20: Sahana Eden - Introduction to the Code

View

None needed at 1st – can RAD without them•then polish later

Try:http://127.0.0.1:8000/eden/vts/vehicle

Page 21: Sahana Eden - Introduction to the Code

( )Model revis ited

models/vts.pytable = db.define_table("vts_vehicle",

Field("registration", unique=True),Field("make"),Field("model"),Field("purchase_date", "date",

default=request.utcnow),Field("supplier"),)

table.registration.requires = IS_NOT_IN_DB(db, table.registration)table.purchase_date.requires = IS_DATE()

Page 22: Sahana Eden - Introduction to the Code

( )Controller revis itedcontrollers/vts.pymodule = request.controllerdef vehicle():

"REST Controller for the Vehicle Resource"resource = request.functiontablename = "%s_%s" % (module, resource)table = db[tablename]

table.license.label = T("License Plate") table.license.comment = SPAN("*", _class="req")

return shn_rest_controller(module, resource)

Page 23: Sahana Eden - Introduction to the Code

: Controller CRUD Stringscontrollers/vts.pydef vehicle():

…s3.crud_strings[tablename] = Storage(

title_create = T("Add Vehicle"), title_display = T("Vehicle Details"), title_list = T("List Vehicles"), title_update = T("Edit Vehicle"), label_create_button = T("Add Vehicle"), msg_record_created = T("Vehicle added"),

…)…

Page 24: Sahana Eden - Introduction to the Code

: Controller Action buttonscontrollers/vts.pydef vehicle():

…def veh_postp(jr, output):

shn_action_buttons(jr)return output

response.s3.postp = veh_postp

output = shn_rest_controller(module, resource)return output

Page 25: Sahana Eden - Introduction to the Code

: Controller prepcontrollers/org.pydef organisation():

…def org_prep(jr):

if jr.representation == "html":crud.settings.create_next =

URL(r=request, f="dashboard") crud.settings.update_next =

URL(r=request, f="dashboard") return True

response.s3.prep = org_prep

output = shn_rest_controller(module, resource)return output

Page 26: Sahana Eden - Introduction to the Code

Documentation

•Examples from other modules•Developer Guidelines on Wiki•But the best… ?

Page 27: Sahana Eden - Introduction to the Code

, !Us e the Source LukeMany resources and tricks on the Internet find you will, but solutions to all technical issues only in the source lie

Page 28: Sahana Eden - Introduction to the Code

3 2S is built on Web Py

FORM -> SQLFORM -> CRUD -> RESTweb2py/gluon/tools.pyself.settings.create_onvalidation = None self.settings.update_onvalidation = None self.settings.delete_onvalidation = None self.settings.create_onaccept = None self.settings.update_onaccept = None self.settings.update_ondelete = None self.settings.delete_onaccept = None

•onvalidation: before DB I/O•onaccept: after DB I/O

Page 29: Sahana Eden - Introduction to the Code

Menus

•Modules menu–Enable Module in 000_config–Create an ‘index’ function & view–01_menu.py will add it to default menu–default/index.html will add it to front page

Page 30: Sahana Eden - Introduction to the Code

: Controller Menucontrollers/vts.pydef index():

"Custom View"module_name =

deployment_settings.modules[module].name_nicereturn dict(module_name=module_name)

Page 31: Sahana Eden - Introduction to the Code

Enable Modulemodels/000_config.pydeployment_settings.modules = Storage(

…vts = Storage(

name_nice = "Vehicle Tracking System", description = "Track vehicles",

module_type = 4),…

)

Page 32: Sahana Eden - Introduction to the Code

: Controller Menucontrollers/vts.pyresponse.menu_options = [ [T("Vehicles"), False, URL(r=request, f="vehicle"),[

[T("List"), False, URL(r=request, f="vehicle")],[T("Add"), False, URL(r=request, f="vehicle", args="create")] ] ]]

Page 33: Sahana Eden - Introduction to the Code

: Controller Menucontrollers/vts.pyresponse.menu_options = [ [T("Vehicles"), False, URL(r=request, f="vehicle"),[

[T("List"), False, URL(r=request, f="vehicle")],[T("Add"), False, URL(r=request, f="vehicle", args="create")] ]]

Page 34: Sahana Eden - Introduction to the Code

View

views/vts/index.html

{{extend "layout.html"}}{{=H2(T(module_name))}}<p>This module allows users to track their vehicles</p>{{=A("List Vehicles", URL(r=request, f="vehicle"))}}

Page 35: Sahana Eden - Introduction to the Code

( )View REST

•REST has defaults– create.html, display.html, list.html, update.html

•They can also be customised:views/vts/vehicle_create.html

{{extend "layout.html"}}{{rheader="Some extra text in my REST view"}}{{include "_create.html"}}

Page 36: Sahana Eden - Introduction to the Code

Lunch

Page 37: Sahana Eden - Introduction to the Code

: Joined Res ources Model

•Link to Location

models/vts.pytable = db.define_table("vts_presence",

Field("vehicle_id", db.vts_vehicle),location_id,Field("time", "datetime"),Field("comments"),)

table.time.requires = IS_DATETIME()table.vehicle_id.requires = \IS_ONE_OF(db, "vts_vehicle.id", "%(registration)s")

Page 38: Sahana Eden - Introduction to the Code

: Joined Res ources Controller

controllers/vts.pydef presence():

return shn_rest_controller("vts", "presence")

Page 39: Sahana Eden - Introduction to the Code

:DAL Databas e Abs traction Layer

http://web2py.com/examples/default/dal

db.define_table("person", Field("name"))id = db.person.insert(name="max")query = (db.person.id == id)db(query).count()db(query).delete()db(query).update(name="Max")rows = db(query).select(orderby=db.person.name)for row in rows: print row.namedb.commit()

Page 40: Sahana Eden - Introduction to the Code

( )JR Model cont

table.vehicle_id.represent = lambda id: db(db.vts_vehicle.id == id).select() \ .first().registration

table.vehicle_id.represent = lambda id: db(db.vts_vehicle.id == id) \.select(limitby=(0, 1)).first().registration

table.vehicle_id.represent = lambda id: db(db.vts_vehicle.id == id).select(db.vts_vehicle. registration, limitby=(0, 1)).first().registration

Page 41: Sahana Eden - Introduction to the Code

: JR Model Components

# Component of vehicle entity s3xrc.model.add_component(module, resource,

multiple=True,joinby=dict(vts_vehicle="vehicle_id"),deletable=True,editable=True,main="time",extra="location_details")

http://127.0.0.1:8000/eden/vts/vehicle/presence

rheader allows main resource details to be visible in component views

Page 42: Sahana Eden - Introduction to the Code

!Dis play on Map

•Map displays Feature Groups•Feature Groups contain Feature Classes•Locations have Feature Classes•“Vehicles” are set up ready to go

Page 43: Sahana Eden - Introduction to the Code

: Joined Res ources View

views/gis/location_popup.html

{{if "vts_vehicle" in request.vars.caller:}}{{fc = db(db.gis_feature_class.name == "Vehicle").select(db.gis_feature_class.id, limitby=(0, 1)).first().id}}// Hide unnecessary fields…// Pre-populate the FeatureClass & Name…// HTML5 GeoLocation support…{{pass}}

Page 44: Sahana Eden - Introduction to the Code

3S XRC

•Export–1st build native XML tree–Then transform to output format using XSLT

•Import–1st transform the input format using XSLT –Then import native XML tree

Page 45: Sahana Eden - Introduction to the Code

:Sahana Eden Community Deve lopment

Proces s2 July 2010, Sahana Camp

Fran Boonfran@ sahanafoundation.org

Page 46: Sahana Eden - Introduction to the Code

Blue Prints

•Drafted on Wiki•Discussed on IRC & Mailing List•Functional Specs

–User Requirements–User Stories–Wireframes

•Technical Specs

Page 47: Sahana Eden - Introduction to the Code

Mes s aging

Open Data Kit

JavaRosa

Page 48: Sahana Eden - Introduction to the Code

Development Proces s

•Branches on LaunchPad–Distributed Version Control (bzr)–Merge Early, Merge Often

•Demo sites

•Developer Guidelines•User Guidelines

Page 49: Sahana Eden - Introduction to the Code

Sahana Community Tools

Email List sahana-eden@ googlegroups.com

IRC Chat #sahana-edenWiki /BugTracker

eden.sahanafoundation.org

LaunchPad launchpad.net/sahana-edenDimDimTwitter @ SahanaFOSS