Tech 2 - Introduction to the Code

Post on 15-Jan-2015

302 views 2 download

Tags:

description

 

Transcript of Tech 2 - Introduction to the Code

11SahanaSahanaCampCamp Viet NamViet Nam

Introduction to the CodeFran Boon

fran@sahanafoundation.org

22SahanaSahanaCampCamp Viet NamViet Nam

Installinga

Developer Environment

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

SahanaSahanaCampCamp Viet NamViet Nam

Architecture

Operating System

Database

Programming Language

Web Framework

Sahana Eden

Web Server n/a

Application

Web2Py

Python

SQLite

Windows, Linux or Mac

SahanaSahanaCampCamp Viet NamViet Nam

Installation processWizard on Wiki to select correct instructions:http //eden.sahanafoundation.org/wiki/InstallationGuidelines:

Bootable USB

Windows Developer Installer

Eden-Python-Installer-Dev.exe

Mac, Linux Follow instructions to install from source

Virtual Machine

55SahanaSahanaCampCamp Viet NamViet Nam

SahanaSahanaCampCamp Viet NamViet Nam

Editor / DebuggerNotepad 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

SahanaSahanaCampCamp Viet NamViet 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") ), )

88SahanaSahanaCampCamp Viet NamViet Nam

Building

your own

Module!http://en.flossmanuals.net/sahana-eden/building-a-new-application/

SahanaSahanaCampCamp Viet NamViet Nam

Whitespace Matters

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

SahanaSahanaCampCamp Viet NamViet Nam

Debug Mode

models/000_config.py

settings.base.debug = True

Reloads modules/s3db every request

CSS & JavaScript unminified

SahanaSahanaCampCamp Viet NamViet Nam

Training ModuleResource Course: ' ' Name

Date / Time

Location Venue: Facilitator

Welcome Pack

SahanaSahanaCampCamp Viet NamViet 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' '

SahanaSahanaCampCamp Viet NamViet Nam

Add a Controller

controllers/training.py

def course():

return s3_rest_controller()

Functions map URLs to code& resources

SahanaSahanaCampCamp Viet NamViet Nam

Try it out!http //127.0.0.1 8000/eden/training/course: :

List

Create

Read

Update

Delete

SahanaSahanaCampCamp Viet NamViet 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

SahanaSahanaCampCamp Viet NamViet Nam

Field Types

models/training.py

Field("name"), s3base.s3_date("start"), Field("welcome_pack", "upload"),

SahanaSahanaCampCamp Viet NamViet Nam

Field Labels

models/training.py

S3base.s3_date("start", label=T("Start Date")),

SahanaSahanaCampCamp Viet NamViet Nam

Links to other Resources

models/training.py

Field("name"), s3db.pr_person_id(label="Facilitator"),

SahanaSahanaCampCamp Viet NamViet Nam

Pivot Table Reports

http //127.0.0.1 8000/eden/training/course: : report/

SahanaSahanaCampCamp Viet NamViet Nam

Mapsmodels/training.py

Field("name"),s3db.gis_location_id(),

http //127.0.0.1 8000/eden/training/course: : map/

2121SahanaSahanaCampCamp Viet NamViet Nam

Which File do I Edit?

http://en.flossmanuals.net/sahana-eden/customisation/

2222SahanaSahanaCampCamp Viet NamViet Nam

SahanaSahanaCampCamp Viet NamViet Nam

Model View Controller

Each module will have: Model : modules/s3db/modulename.py

Defines the data model

Controller : controllers/modulename.py Links URLs to resources

Views : views/modulename/index.html HTML templates with embedded python code

There may be additional view files for custom pages

SahanaSahanaCampCamp Viet NamViet Nam

Web2Py URL Mapping

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

Model : not applicable here Controller : web2py/applications/eden/controllers/default.py

Function: def index(): View : web2py/applications/eden/views/default/index.html

http://host/application/controller/function

SahanaSahanaCampCamp Viet NamViet Nam

Sahana Eden URL Mapping

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

http://host/eden/module/resource

SahanaSahanaCampCamp Viet NamViet 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

SahanaSahanaCampCamp Viet NamViet Nam

Edit a Field Label

modules/s3db/org.py

tablename = "org_organisation"... Field("donation_phone", label = T("Donation Phone Number"),

SahanaSahanaCampCamp Viet NamViet Nam

Hide a Field

modules/s3db/org.py

tablename = "org_office"... Field("type", "integer readable=False, writable=False, label=T("Type")),

SahanaSahanaCampCamp Viet NamViet Nam

Add a New Field

modules/s3db/org.py

tablename = "org_organisation"... Field("facebook", label=T("Facebook Page")),

3030SahanaSahanaCampCamp Viet NamViet Nam

Git

Controlled Updates

http://eden.sahanafoundation.org/wiki/DeveloperGuidelines/Git

SahanaSahanaCampCamp Viet NamViet Nam

Installing GitGitHub.com has excellent instructions

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

SahanaSahanaCampCamp Viet NamViet Nam

Creating your Branchhttps //github.com/flavour/eden:

Get local copy : git clone git@github.com:myname/eden.gitStay up date : git pull upstream master

SahanaSahanaCampCamp Viet NamViet Nam

Updating your ServerCommit code locally : git commit -aPush to GitHub : git pushPull on Server : git pull

SahanaSahanaCampCamp Viet NamViet Nam

Sharing your CodeCommit code locally : git commit -aPush to GitHub : git pushSubmit Pull Request to get code merged into Trunk