Tech 2 - Introduction to the Code

857 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
857
On SlideShare
0
From Embeds
0
Number of Embeds
16
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tech 2 - Introduction to the Code

  1. 1. Introduction to the Code Fran Boon fran@sahanafoundation.org SahanaCamp Viet Nam 1
  2. 2. Installing a Developer Environment http://eden.sahanafoundation.org/wiki/InstallationGuidelines SahanaCamp Viet Nam 2
  3. 3. 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
  4. 4. 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
  5. 5. SahanaCamp Viet Nam 5
  6. 6. 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
  7. 7. 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
  8. 8. Building your own Module! http://en.flossmanuals.net/sahana-eden/building-a-new-application/ SahanaCamp Viet Nam 8
  9. 9. Whitespace Matters Unlike other languages which use parentheses to delimit code blocks {...}, Python instead uses White Space SahanaCamp Viet Nam
  10. 10. Debug Mode models/000_config.py settings.base.debug = True  Reloads modules/s3db every request  CSS & JavaScript unminified SahanaCamp Viet Nam
  11. 11. Training Module Resource: 'Course'      Name Date / Time Location: Venue Facilitator Welcome Pack SahanaCamp Viet Nam
  12. 12. 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
  13. 13. Add a Controller controllers/training.py def course(): return s3_rest_controller()  Functions map URLs to code & resources SahanaCamp Viet Nam
  14. 14. Try it out! http://127.0.0.1:8000/eden/training/course  List  Create  Read  Update  Delete SahanaCamp Viet Nam
  15. 15. 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
  16. 16. Field Types models/training.py Field("name"), s3base.s3_date("start"), Field("welcome_pack", "upload"), SahanaCamp Viet Nam
  17. 17. Field Labels models/training.py S3base.s3_date("start", label=T("Start Date")), SahanaCamp Viet Nam
  18. 18. Links to other Resources models/training.py Field("name"), s3db.pr_person_id(label="Facilitator"), SahanaCamp Viet Nam
  19. 19. Pivot Table Reports http://127.0.0.1:8000/eden/training/course /report SahanaCamp Viet Nam
  20. 20. Maps models/training.py Field("name"), s3db.gis_location_id(), http://127.0.0.1:8000/eden/training/course /map SahanaCamp Viet Nam
  21. 21. Which File do I Edit? http://en.flossmanuals.net/sahana-eden/customisation/ SahanaCamp Viet Nam 21
  22. 22. SahanaCamp Viet Nam 22
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. 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
  27. 27. Edit a Field Label modules/s3db/org.py tablename = "org_organisation" ... Field("donation_phone", label = T("Donation Phone Number"), SahanaCamp Viet Nam
  28. 28. Hide a Field modules/s3db/org.py tablename = "org_office" ... Field("type", "integer readable=False, writable=False, label=T("Type")), SahanaCamp Viet Nam
  29. 29. Add a New Field modules/s3db/org.py tablename = "org_organisation" ... Field("facebook", label=T("Facebook Page")), SahanaCamp Viet Nam
  30. 30. Git Controlled Updates http://eden.sahanafoundation.org/wiki/DeveloperGuidelines/Git SahanaCamp Viet Nam 30
  31. 31. Installing Git GitHub.com has excellent instructions Create SSH public/private keys ssh-keygen -t rsa -C "my_email@email.com" SahanaCamp Viet Nam
  32. 32. 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
  33. 33. Updating your Server Commit code locally: git Push to GitHub: git Pull on Server: git commit -a push pull SahanaCamp Viet Nam
  34. 34. 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

×