TurboGears

TurboGears ??

組成架構

  • CherryPy - 網賺架構的框架 (controller)
  • SQLObject - 用來對應資料庫資料 (model)
  • Kid - 主要的範本引擎,還有其他的可用 (view)
  • MochiKit - 一個 Web 2.0 Javascript/Ajax 的函式庫

架構圖

http://www.turbogears.org/about/howitallfits.jpg

看一下快速的 API 參考吧 http://compoundthinking.com/blog/wp-content/tg-0.9-quickref.pdf

圖片版權屬於 Kevin Dangoor

CherryPy 櫻桃派

  • http://www.cherrypy.org
  • 一個 Python 網頁開發框架
  • 本身有有支援許多的範本引,如 Cheetah,CherryTemplate,ClearSilver,Kid...族繁
  • 可以將 URL 對應到程式元件或是函式
  • 可以在其他的網頁伺服器後端

範例:

import cherrypy

class hiTossug:
   def index(self):
       return 'Hi Tossug'
   index.exposed = True

cherrypy.root = hiTossug()
cherrypy.server.start()

SQLObject

  • 目前有支援 MySQL,PostgreSQL,SQLite,MSSQL,Sybase,MaxDB,Firebird
  • 對應您資料庫中的資料
  • 可以用 Python 的方式來存取
  • 大量簡化,SQL 語法的使用
  • 讓資料庫的程式開發有了對應 Python 的抽象層

範例

from sqlobject import *

db = connectionForURI('sqlite:///tmp/test.db?debug=1')
sqlhub.processConnection = db

class Person(SQLObject):
    name = StringCol(length=50)
    .....
    ..... 定義一些您想定義的欄位
    .....
    info = StringCol()
Person.createTable(ifNotExists=True)

SQLObject Notes

SQLObejct 每一個 Table Objetc 都會有id 所有的 primary key 都必須是單一的,不可改

name = StringCol(alternateID=True, length=20) 其中 alernateID=True 可以作為以後搜尋的key,但這個欄位的質必須是獨一的

SQLObejct One to Many

單一資料的 Table SQLObject

欄位 = MultipleJoin('另一個SQLObject')

多筆資料對應的 Table SQLObject

欄位 = ForeignKey('另一個SQLObject')

SQLObject Many to Many

兩個 Table 的 SQLObject 都一樣

欄位 = RelatedJoin('另一個SQLObjetc')

SQLObjetc Selecting Multiple Objects

簡單的 results = SQLObject.select(SQLObject.q.某欄位=="你要的質")

要注意的是所有 q 以下的都是欄位名稱

兩個 table

例如 Person 和 Address 的關係

results = SQLObject.select(AND(Address.q.personID == Person.q.id,Address.q.zip.startswith('504'))))

會產生

1/Select : SELECT person.id, person.first_name, person.middle_initial, person.last_name FROM person, address WHERE ((address.person_id = person.id) AND (address.zip LIKE '504%')) 1/COMMIT : auto

當然也可以手寫的方式寫SQL:

> peeps = Person.select("""address.id = person.id AND
...                     address.zip LIKE '504%'""",
...                     clauseTables=['address'])

SQLObject 一些名稱的定義

class User(SQLObject):
    _table = "user_table" # 你的資料庫 table 的名稱
    _idName = "userid"    # table 的主要 id
    username = StringCol(length=20, dbName='name') # dbName 是欄位名稱

Kid

  • 是 XML 格式的範本引擎
  • 可生成不同格式的文件
  • 也可以加入 Python 的語法

範例

<?xml version='1.0' encoding='utf-8'?>
<?python
import time
title = "A Kid Template"
?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:py="http://purl.org/kid/ns#"
>
<head>
    <title py:content="title">
    This is replaced with the value of the title variable.
    </title>
</head>
<body>
<p>
    The current time is ${time.strftime('%C %c')}.
</p>
</body>
</html></p></body></title></head></html>

TurboGears 工具

  • tg-admin, 是 TurboGears 的主要工具程式

  • tg-admin toolbox,執行後,看一下您的瀏覽器吧,http://localhost:7654/

    • Widget Browser
    • WebConsole
    • ModelDesigner
    • admi18n
    • CatWalk
  • tg-admin quickstart demo, 執行後,這樣可以產生 demo 原始的框架,包括檔案,還有資料夾

  • demo-start.py, 開始的程式

  • devcfg.py, 專案開發時的設置

  • prodcfg.py, 實際執行服務時的設置

  • demo/controllers.py,定義 URL 對應程式還有處理地方

  • demo/model.py,定義 SQLObejct 和資料庫的對應

  • demo/static/,放一些靜態資料的地方

  • demo/templates/,放置範本的地方

  • 執行 python2.4 demo-start.py 後,就可以用瀏覽器指到 http://localhost:8080/

demo/
|-- README.txt
|-- demo
|   |-- __init__.py
|   |-- config.py
|   |-- controllers.py
|   |-- model.py
|   |-- sqlobject-history
|   |-- static
|   |   |-- css
|   |   |-- images
|   |   |   |-- favicon.ico
|   |   |   `-- tg_under_the_hood.png
|   |   `-- javascript
|   |-- templates
|   |   |-- __init__.py
|   |   |-- login.kid
|   |   |-- master.kid
|   |   `-- welcome.kid
|   `-- tests
|       |-- __init__.py
|       |-- test_controllers.py
|       `-- test_model.py
|-- demo.egg-info
|   |-- PKG-INFO
|   |-- SOURCES.txt
|   |-- not-zip-safe
|   |-- paster_plugins.txt
|   |-- requires.txt
|   |-- sqlobject.txt
|   `-- top_level.txt
|-- devcfg.py
|-- prodcfg.py
|-- setup.py
|-- setup.pyc
`-- start-demo.py

參考資料