Your SlideShare is downloading. ×
0
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
GHC Participant Training
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

GHC Participant Training

702

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
702
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • S3 is a codename for Sahana Eden Server & Client can be on the same machine Each part needs to be debugged separately: Eclipse works well for the Server-side Python, whilst Firebug is excellent for the generated HTML/CSS/JavaScript JavaScript libraries we use include jQuery & ExtJS
  • There are notes on the Wiki for installing a developer environment on Windows, Mac or Linux machines if you wish to run in other environments later.
  • Look around the filesystem in web2py/applications/eden folder Similar layout structure to other MVC frameworks, such as Ruby-on-Rails & Django
  • JavaScript which doesn’t require server-side parsing put into static Python Libraries available: System Python, Web2Py, Eden (S3*)
  • The problems faced by both the victims of and responders to disasters are numerous. Beyond the trauma, this involves having to coordinate, manage and track things. In countries at risk of being affected by large scale sudden onset disasters, these problems can be massive and overwhelming.
  • Normally you work in a local branch on your PC. You Merge changes in regularly from Trunk. You Push your changes to your branch on LaunchPad (You Pull from LaunchPad to update your server sites)
  • Minimise the time taken to process Manage dependencies No need to import most of the libraries in ‘gluon/’
  • Create a new file in the models folder called inc.py & type in the text above – be careful to use the correct case for Field!
  • Create a new file in the controllers folder called inc.py & type in the text above – be careful to get the indentation correct!
  • Note that you’ll need to Register yourself as a user on the system before you’re allowed to create a resource. The 1 st user to register gets the Administrator role by default.
  • Create a new folder in the views folder called ‘ inc ’ & inside that put a new file ‘ incident_list_create.html’ . Type in the text above. Refresh the incident page to see the new text: http://127.0.0.1:8000/eden/inc/ incident No need to learn another macro language Defaults (non-REST) to views/controller/function.html Open the About page ( http://127.0.0.1:8000/eden/default/about ) & browse the variables We store our S3 variables in response.s3.*, session.s3.* so as not to pollute the Web2Py namespace
  • unique=True is a SQL-level constraint. Open your inc.py from the models directory & add the extra code above. Validators are DAL-level constraints which produce nice error messages instead of tickets! Validators provide server-side validation & some also provide client-side rendering & validation Web2Py automatically adds a Validator when the SQL constraint is set.
  • Fields default to type ‘string’. ‘ date’ is the SQL-level field type & also provides the class in the HTML which means we get a date widget Add the extra code above to your file & refresh the page, now enter a date.
  • Add the extra code above to your file & refresh the page to see the default value
  • Add this to your model (underneath the table definition) & try it out
  • You can check out the languages file, if you wish: http://127.0.0.1:8000/admin/default/design/eden#languages Update all languages
  • Add this to your model (underneath the table definition) & try it out Comment is rendered as ‘column3’ (although can be moved using ‘formstyle’) We also use the same space for Help buttons & ‘Add New Resource’ links. NB For optimal performance, we normally add such comments in the Controller instead & put them inside a ‘prep’ to tale effect just when ‘r.representation in shn_interactive_view_formats’
  • Add this to your model (underneath the table definition) & try it out Comment is rendered as ‘column3’ (although can be moved using ‘formstyle’) We also use the same space for Help buttons & ‘Add New Resource’ links. NB For optimal performance, we normally add such comments in the Controller instead & put them inside a ‘prep’ to tale effect just when ‘r.representation in shn_interactive_view_formats’
  • Add this to your model (underneath the table definition) & try it out Comment is rendered as ‘column3’ (although can be moved using ‘formstyle’) We also use the same space for Help buttons & ‘Add New Resource’ links. NB For optimal performance, we normally add such comments in the Controller instead & put them inside a ‘prep’ to tale effect just when ‘r.representation in shn_interactive_view_formats’
  • This should be available as a script or an alias ‘w2p’ iPython needs to be installed for object exploring.
  • Add this text to the file 000_config.py in the models folder. Navigate to the home page to see the module appear both there & on the menu. Try navigating to the module. Module Type 10 means appears in the ‘more’ section of the default modules menu. (Most deployments will create a fully-customised menu anyway)
  • Add this text to your controller file & now open the module. “ Custom View” is a Doc String One should be added to all functions for automatic documentation generators & interactive browsing of docs You can try this out in the Interactive Shell: w2p execfile("applications/eden/controllers/vts.py", globals()) help(index)
  • Create a file called ‘index.html’ inside the views/inc folder See plain HTML being interspersed with the server-side parsed Python Look at another module’s index.html What’s bad about this index?
  • Add this text to your controller (outside the functions – e.g. at the top) & see what effect this has on your module.
  • Copt this text into your model. Note the use of string substitution to define the tablename
  • Tells SQL that these fields are ‘reference’ fields (i.e. Foreign Keys) - They store integers which are a pointer to the ID field in the other tables.
  • Copy this text into your Model & refresh the presence page Reusable field defined in models/03_gis.py Includes additional useful configuration such as .requires & .represent
  • Copt this text into your controller. Try it out. Note the automation of the resource name to minimise the editing after copy/paste of new functions
  • Copy this text into your model & see what it does for the page IS_ONE_OF() will provide us a dropdown showing license plates, even though we actually store the ID. This is an S3 validator which extends Web2Py’s IS_IN_DB() to support the ‘deleted’ status & also allow the use of lambdas as formatting options.
  • We can add additional fields into the link table.
  • IS_ONE_OF provides the representation in the dropdown table.field.represent provides the representation in Display/List views A lambda is a Python feature for defining anonymous functions on the fly: http://diveintopython.org/power_of_introspection/lambda_functions.html
  • Optimise the query – we know we only want a single record & a single field from that record
  • Add the optimised query to your model.
  • Add this to your model underneath the definition of the Presence table & try out the URL. No need for dedicated presence() controller
  • Add this Resource Header definition to your Controller. Be careful to get the end brackets & commas right! ‘ r’ is the resource
  • Then plug it into the Controller
  • Take a look at the HTML structure of the page in Firebug.
  • Web2Py source code very accessible – small enough to be able to grasp the whole thing after not too long FORM defined in gluon/html.py: Dumb HTML elements SQLFORM defined in gluon/sqlhtml.py: Self-processing forms which update the database CRUD defined in gluon/tools.py: Wrappers around SQLFORM - We actually change CRUD a lot & are looking to remove it completely – building straight on top of SQLFORM() REST defined in modules/s3xrc.py: RESTful interface & XML representations If you drop down a level, then need to do things more manually & need to take care of framework issues yourself.
  • Whilst we spend a lot of time updating developer documentation, the actual codebase always moves faster, so the source is always the most authoritative source of information.
  • Transcript

    • 1. Sahana Eden: Emergency Development Environment 30 September 2010, Grace Hopper Celebration Fran Boon [email_address]
    • 2. Agenda
      • Stack Overview
      • Getting Set up
      • Model View Controller
        • Filesystem Layout
      • Build a New Module
        • S3 REST Controller
    • 3. Stack Overview
      • Server
      Client Browser Eclipse Firebug Sahana Eden (S3) Web2Py Python HTML JavaScript CSS
    • 4. Installation: Virtual Machine
      • Simplest way to start:
      • http://eden.sahanafoundation.org/wiki/InstallationGuidelinesVirtualMachine
      • Full Developer Environment:
      • Eclipse
      • Codebase (Web2Py/Sahana Eden)
      • Launch Eclipse to get started…
    • 5. Model-View-Controller
      • web2py/applications/ eden /
      • controllers
      • models
      • views
    • 6. Model-View-Controller
      • Models
        • Define Tables in the Database
      • Controllers
        • Workflow, Logic
      • Views
        • HTML / JS Templates parsed server-side
        • JS functions then run client-side in browser
    • 7. Model-View-Controller
      • Static
        • no server-side processing
        • Images
        • CSS
        • JavaScript
      • Modules
        • Python libraries
    • 8. Modules
      • Sahana concept
        • Logical grouping of user-facing functionality
        • Not to be confused with Python modules
          • i.e. not web2py/applications/eden/modules
      • Consist of:
        • Model(s)
        • Controller
        • Views
    • 9. Sahana Modules
      • Organisation Registry : org
      • Person Finder : pr, mpr, dvi
      • Request Management : rms
      • Inventory : inv
      • Shelter Registry : cr
      • Mapping : gis
      • Messaging : msg
    • 10. Branches LaunchPad Merge proposal Trunk Local Branch Your Branch bzr merge bzr push
    • 11. Request processing
      • Web2Py environment setup
      • All Models executed (alphabetical order)
      • Controller executed
      • Controller Function executed
      • View parsed (explicit variable passing)
      • HTML/JS returned to browser
    • 12. Emergency
      • We have to build a module before bed!
    • 13. Software Development Cycle
    • 14. Incident Reporting System
      • Data :
      • Location
      • Date
      • Reporter
      • Details
      • Screens :
      • Report Incident
      • View Incidents
      • Update Incident
    • 15. Define Data
      • models/ inc .py
      • table = db.define_table("inc_incident",
          • location_id(),
          • Field("date", "date"),
          • person_id(),
          • comments()
      • )
    • 16. Forms for CRUD
      • controllers/ inc .py
      • def incident():
      • return shn_ rest _controller("inc",
      • "incident")
    • 17. We’re done!
      • http://127.0.0.1:8000/eden/inc/incident
      • Create an incident.
      • View in different formats:
        • .json, .xml, .xls, .pdf
        • http:// 127.0.0.1:8000/eden/inc/incident/1.json
    • 18. Views: Custom REST
      • views/ inc /incident_ list_create .html
      • {{extend " layout .html"}}
      • <p>A Normal HTML Paragraph</p>
      • {{rheader=&quot;A Paragraph inserted inside the page&quot;}}
      • {{include &quot;_ list_create .html&quot;}}
      • {{include &quot;gis/location_autocomplete.js&quot;}}
      • Python code inside {{ }} is parsed server-side
      • Views can extend (one) & include (many) other views
      • Extend ‘ layout ’ for overall look/feel
      • Variables only visible to views if:
        • explicitly passed in return dict()
        • stored in global variables: request , response , session
    • 19. SQL constraints / Validators
      • models/inc.py
      • table = db.define_table(&quot;inc_incident&quot;,
      • Field(&quot;code&quot;, unique=True ),
      • )
      • DAL supports Live Migrations
    • 20. Field Types
      • models/inc.py
      • table = db.define_table (&quot;inc_incident&quot;,
      • Field(&quot;date&quot;, &quot; date &quot;),
      • )
    • 21. Default Values
      • models/inc.py
      • table = db.define_table(&quot;inc_incident&quot;,
      • Field(&quot;date_date&quot;, &quot;date&quot;, default=request.utcnow ),
      • )
    • 22. Labels
      • models/inc.py
      • table.person_id. label = &quot;Reporter&quot;
    • 23. Internationalisation
      • models/inc.py
      • table.person_id. label = T(&quot;Reporter“)
    • 24. Comments
      • models/inc.py
      • table.code. comment = SPAN(&quot;*&quot;, _class=&quot;req&quot;)
    • 25. Requires
      • models/03_gis.py
      • db.gis_location. comment = …
      • db.gis_location. requires = IS_ONE_OF (…)
    • 26. Represent
      • models/03_gis.py
      • db.gis_location. represent = shn_location_represent
    • 27. Exploring: Web2Py shell
      • Python is great for interactive exploring!
        • Web2Py allows this too 
      • w2p
      • python web2py.py –S eden –M
      • Explore objects with tab
      • db.
      • gis.
    • 28. DAL
      • Try these in the shell: w2p
      • db.define_table(&quot;person&quot;, Field(&quot;name&quot;))
      • id = db.person.insert(name=&quot;max&quot;) query = (db.person.id == id)
      • db(query).count()
      • db(query).update(name=&quot;Max&quot;)
      • rows = db(query).select(orderby=db.person.name) for row in rows:
      • print row.name
      • db(query).delete()
    • 29. End
    • 30. Enable Module
      • models/000_config.py
      • deployment_settings.modules = Storage(
      • #irs = Storage(
      • inc = Storage(
      • name_nice = &quot;Incident Reporting System&quot;, description = &quot;Report Incidents&quot;,
      • module_type = 3
      • ),
      • )
    • 31. Index page
      • controllers/inc.py
      • module = request.controller
      • def index ():
      • &quot;Custom View&quot;
      • module_name = deployment_settings.modules[module].name_nice
      • return dict(module_name=module_name)
    • 32. View
      • views/inc/index.html
      • {{extend &quot;layout.html&quot;}}
      • {{=H2(module_name)}}
      • <p>This module allows users to track their vehicles</p>
      • {{=LI(A(&quot;List Vehicles&quot;, _href=URL(r=request, f=&quot;vehicle&quot;)))}}
    • 33. Controller: Menu
      • controllers/inc.py
      • response.menu_options = [
      • [T(&quot;Incidents&quot;), False, URL(r=request, f=&quot;incident&quot;),[ [ T(&quot;List&quot;), False, URL(r=request, f=&quot;incident&quot;)], [T(&quot;Add&quot;), False, URL(r=request, f=&quot;incident&quot;, args=&quot;create&quot;)] ]]]
    • 34. Joined Resources
      • So far:
        • Resource = Single Table
      • Reality:
        • Resource spread out over multiple Tables
    • 35. Joined Resources: Model
      • Link Vehicle to Location
        • vts_presence
    • 36. Joined Resources: Model
      • models/vts.py
      • module = &quot;vts&quot;
      • resource = &quot;presence&quot;
      • tablename = &quot;%s_%s&quot; % (module, resource)
      • table = db.define_table(tablename,
      • Field(&quot;vehicle_id&quot;),
      • Field(&quot;location_id&quot;),
      • )
    • 37. Joined Resources: Model
      • models/vts.py
      • table = db.define_table(tablename,
      • Field(&quot;vehicle_id&quot;, db.vts_vehicle ),
      • Field(&quot;location_id&quot;, db.gis_location ),
      • )
    • 38. Joined Resources: Model
      • models/vts.py
      • table = db.define_table(tablename,
      • Field(&quot;vehicle_id&quot;, db.vts_vehicle),
      • location_id() ,
      • )
    • 39. Joined Resources: Controller
      • controllers/vts.py
      • def presence ():
      • resource = request.function
      • return shn_rest_controller(module, resource)
      • http://127.0.0.1:8000/eden/vts/presence
    • 40. Joined Resources: Model
      • models/vts.py
      • table = db.define_table(tablename,
      • Field(&quot;vehicle_id&quot;, db.vts_vehicle),
      • location_id(),
      • )
      • table.vehicle_id. requires = IS_ONE_OF (db,
      • &quot;vts_vehicle.id&quot;,
      • &quot;vts_vehicle.registration&quot;)
    • 41. Joined Resources: Model
      • models/vts.py
      • table = db.define_table(tablename,
      • Field(&quot;vehicle_id&quot;, db.vts_vehicle),
      • location_id(),
      • Field(&quot;timestmp&quot;, &quot;datetime&quot;) ,
      • )
      • table.vehicle_id.requires = IS_ONE_OF(db, &quot;vts_vehicle.id&quot;, &quot;vts_vehicle.registration&quot;)
      • table.timestmp.requires = IS_DATETIME()
    • 42. Represent
      • table.vehicle_id. represent = lambda id: function
    • 43. DAL: Optimised SQL queries
      • “ Find the vehicle with this ID”:
      • db(db.vts_vehicle.id == id).select()
      • db(db.vts_vehicle.id == id).select().first()
      • “ Return the Registration”:
      • db(db.vts_vehicle.id == id).select().first().registration
      • db(db.vts_vehicle.id == id).
      • select( limitby =(0,1)).first().registration
      • db(db.vts_vehicle.id == id).
      • select( db.vts_vehicle.registration ,
      • limitby=(0,1)).first().registration
    • 44. JR: Represent
      • models/vts.py
      • table.vehicle_id. represent = lambda id: db(db.vts_vehicle.id == id). select(db.vts_vehicle.registration, limitby=(0,1)).first().registration
    • 45. Components
      • models/vts.py
      • # Presence as component of vehicle
      • s3xrc.model.add_component(module,
      • resource,
      • multiple=True,
      • joinby=dict(vts_vehicle=&quot;vehicle_id&quot;),
      • deletable=True,
      • editable=True)
      • http://127.0.0.1:8000/eden/vts/vehicle/1/presence
    • 46. RHeader
      • controllers/vts.py
      • def shn_vts_rheader(r, tabs=[]):
      • if r.representation == &quot;html&quot;:
      • rheader_tabs = shn_rheader_tabs(r, tabs)
      • vehicle = r.record
      • rheader = DIV(TABLE(
      • TR(TH(T(&quot;Vehicle: &quot;)), vehicle.registration)
      • ),
      • rheader_tabs
      • )
      • return rheader
      • return None
    • 47. RHeader Tabs
      • controllers/vts.py
      • def vehicle():
      • output = shn_rest_controller(module, &quot;vehicle&quot;,
      • rheader=lambda r: shn_vts_rheader(r,
      • tabs = [(T(&quot;Basic Details&quot;), None),
      • (T(&quot;Presence&quot;), &quot;presence&quot;)
      • ]))
      • return output
      • http://127.0.0.1:8000/eden/vts/vehicle/1/presence
    • 48. RHeader Tabs
    • 49. S3 is built on Web2Py REST CRUD SQLFORM FORM
    • 50. Form submission
      • Use Firebug’s Net Panel to look at a form submission.
      • Each field has an entry in form.vars
      • Can add additional vars to the form in a custom View
      • Can process these within our Controller
        • onvalidation: Before DB I/O
        • onaccept: After DB I/O
    • 51. Documentation
      • Examples from other modules
      • Developer Guidelines on Wiki:
      • http://eden.sahanafoundation.org/wiki/DeveloperGuidelines
      • But the best…?
    • 52. Use the Source, Luke!
      • Many resources and tricks on the Internet find you will, but the ultimate answers only in the source lie

    ×