Skip to main content

TurboEntity 版 SimpleBlog

· 2 min read

這幾天 TurboEntity 這個類似 ActiveMapper 的模組一出現馬上獲得不小的關注. 因為它可以用幾乎與 SQLObject 數量相當的程式碼來定義 ORM, 又能完全繼承 SQLAlchemy 的效能和彈性.

Lee McFadden 從上上週開始在他的 Blog 上連載了 3 篇 'SimpleBlog' 系列文章,引導如何使用 SQLAlchemy + TurboGears 製作簡單的 Blog 系統. 他採用了接近基本 SQLAlchemy 的設定方式,因此程式碼有點多.

我用 TurboEntity 照著 'SimpleBlog Part I' 中的範例改寫後,獲得以下程式碼 (model.py):

>
> from turboentity import *
> from docutils.core import publish_parts
>
> class Post(Entity):
> title = Column(Unicode(50))
> content = Column(Unicode)
> post_date = Column(DateTime, default=datetime.now())
> is_published = Column(Boolean, default=False)
>
> @property
> def html_content(self):
> return publish_parts(self.content,writer_name="html")["html_body"]
>

在其它部份保持不變的情形下,拿來跟原文比較可以發現使用 TurboEntity 寫的定義 ORM 程式,就程式碼行數上實在不輸 SQLObject. 難能可貴地是同樣能保持很高的可讀性.

TurboEntity 網站上還有使用 TurboEntity 版本做 TurboGears Identity Model 的例子,看來轉換到從 SQLObject 轉換到 SQLAlchemy 已經不再是太令人躊躇的事了.