mini curso

web2py
@lucadavila
framework web python
# opensource
# seguro
# model view controller
# windows, mac, unix/linux, GAE
# SQLite, PostgreSQL, MySQL, Oracle, GAE...
# apache, lighttpd, cherokee, *mod_python ...
# compatibilidade reversa
# curva aprendizado pequena
# interface administrativa web
/models/db.py
#database definition
db = DAL('sqlite://storage.sqlite')
#model definition
Person = db.define_table('persons',
  Field('name', label='Your name'),
  Field('email'),
)


#validators
Person.name.requires = IS_NOT_EMPTY()
Person.email.requires = IS_EMPTY_OR(IS_EMAIL())
#insert (without validating)
Person.insert(name="Lucas D'Avila", email="lucassdvl@gmail.com")
Person.insert(name="", email="foo")

#validate and insert
Person.validate_and_insert(name="", email="foo")
<Row {'errors': <Row {'name': <lazyT 'enter a value'>,
'email': <lazyT 'enter a valid email address'>}>, 'id': None}>
#save changes
db.commit()

#undo changes
db.rollback()
#count
db(Person.id > 0).count()


#select
persons = db(Person.id > 0).select()
person_by_id = Person(1)
#update set
db(Person.id > 0).update(name="Luke")

#update record
Person(1).update_record(name="Lucas")
#delete set
db(Person.id > 0).delete()

#delete record
Person(1).delete_record()

#truncate table (restart id sequence)
Person.truncate()
/controllers/persons.py
#action returning a string
def say_hello():
  return dict(some_var = "Hello world!")

#action returning a set of database records
def persons() :
  persons = db(Person.id > 0).select()
  return locals()
/views/persons/say_hello.html
#view
<h1>web2py views</h1>
<p>Controller says: {{=some_var}}</p>
#url mapping
http://hostname /app/controller/action/arg/arg2/...?var=lucas
#dispatching
download
web2py.com/examples/default/download
docs
web2py.com/examples/default/documentation
web2py.com/book
Obrigado!

Web2py