<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Here comes the Sun &#187; pylons</title>
	<atom:link href="http://sunng.info/blog/tag/pylons/feed/" rel="self" type="application/rss+xml" />
	<link>http://sunng.info/blog</link>
	<description>Homemade Clojure Geek</description>
	<lastBuildDate>Fri, 11 May 2012 02:24:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>迁移pylonsbook chapter 8到pylons1.0</title>
		<link>http://sunng.info/blog/2010/09/%e8%bf%81%e7%a7%bbpylonsbook-chapter-8%e5%88%b0pylons1-0/</link>
		<comments>http://sunng.info/blog/2010/09/%e8%bf%81%e7%a7%bbpylonsbook-chapter-8%e5%88%b0pylons1-0/#comments</comments>
		<pubDate>Sun, 12 Sep 2010 13:23:18 +0000</pubDate>
		<dc:creator>sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://sunng.info/blog/2010/09/%e8%bf%81%e7%a7%bbpylonsbook-chaptor-8%e5%88%b0pylons1-0/</guid>
		<description><![CDATA[Pylonsbook的第八章，是一个完整的CRUD程序例子。pylons升级到1.0之后，有一些代码不能正常工作了。 首先查看你使用的各种库的版本： paster shell development.ini import pylons pylons.__version__ import sqlalchemy sqlalchemy.__version__ import webhelpers webhelpers.__version__ Pylons 1.0开始鼓励通过继承Base类的声明式ORM（pylonsbook介绍的是pylons 0.9x，sqlalchemy 0.5x，使用mapper的方式） 对pylons1.0，mode/__init__.py中声明model的代码如下： from sqlalchemy import * from sqlalchemy.orm import relationship class PageTag&#40;Base&#41;: &#160; &#160; __tablename__ = 'pagetag' &#160; &#160; id = Column&#40;'id', Integer, &#8230; <a href="http://sunng.info/blog/2010/09/%e8%bf%81%e7%a7%bbpylonsbook-chapter-8%e5%88%b0pylons1-0/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Pylonsbook的<a href="http://pylonsbook.com/en/1.1/starting-the-simplesite-tutorial.html">第八章</a>，是一个完整的CRUD程序例子。pylons升级到1.0之后，有一些代码不能正常工作了。</p>
<p>首先查看你使用的各种库的版本：<br />
<i>paster shell development.ini</i></p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">import</span> pylons<br />
pylons.__version__<br />
<span style="color: #ff7700;font-weight:bold;">import</span> sqlalchemy<br />
sqlalchemy.__version__<br />
<span style="color: #ff7700;font-weight:bold;">import</span> webhelpers<br />
webhelpers.__version__</div></div>
<p>Pylons 1.0开始鼓励通过继承Base类的声明式ORM（pylonsbook介绍的是pylons 0.9x，sqlalchemy 0.5x，使用mapper的方式）<br />
对pylons1.0，mode/__init__.py中声明model的代码如下：</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">orm</span> <span style="color: #ff7700;font-weight:bold;">import</span> relationship<br />
<br />
<span style="color: #ff7700;font-weight:bold;">class</span> PageTag<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; __tablename__ <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'pagetag'</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'id'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> Sequence<span style="color: black;">&#40;</span><span style="color: #483d8b;">'pagetag_seq_id'</span><span style="color: #66cc66;">,</span> optional<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> primary_key<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pageid <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'pageid'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> ForeignKey<span style="color: black;">&#40;</span><span style="color: #483d8b;">'page.id'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; tagid <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tagid'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> ForeignKey<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tag.id'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">class</span> Page<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;docstring for Page&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; __tablename__ <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'page'</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'id'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> Sequence<span style="color: black;">&#40;</span><span style="color: #483d8b;">'page_seq_id'</span><span style="color: #66cc66;">,</span> optional<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> primary_key<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; content <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'content'</span><span style="color: #66cc66;">,</span> Text<span style="color: #66cc66;">,</span> nullable<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; posted <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'posted'</span><span style="color: #66cc66;">,</span> DateTime<span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span>now<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; title <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'title'</span><span style="color: #66cc66;">,</span> Unicode<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span>u<span style="color: #483d8b;">'Untitled Page'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; heading <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'heading'</span><span style="color: #66cc66;">,</span> Unicode<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; tags <span style="color: #66cc66;">=</span> relationship<span style="color: black;">&#40;</span>PageTag<span style="color: #66cc66;">,</span> backref<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'page'</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; comments <span style="color: #66cc66;">=</span> relationship<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Comment&quot;</span><span style="color: #66cc66;">,</span> backref<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'page'</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">class</span> Tag<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">&quot;&quot;&quot;docstring for Tag&quot;&quot;&quot;</span><br />
&nbsp; &nbsp; __tablename__ <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'tag'</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'id'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> Sequence<span style="color: black;">&#40;</span><span style="color: #483d8b;">'tag_seq_id'</span><span style="color: #66cc66;">,</span> optional<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> primary_key<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; name <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span><span style="color: #66cc66;">,</span> Unicode<span style="color: black;">&#40;</span><span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> nullable<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: #66cc66;">,</span> unique<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">class</span> Comment<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; __tablename__ <span style="color: #66cc66;">=</span> <span style="color: #483d8b;">'comment'</span><br />
&nbsp; &nbsp; <span style="color: #008000;">id</span> <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'id'</span><span style="color: #66cc66;">,</span> Integer<span style="color: #66cc66;">,</span> Sequence<span style="color: black;">&#40;</span><span style="color: #483d8b;">'comment_seq_id'</span><span style="color: #66cc66;">,</span> optional<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> primary_key<span style="color: #66cc66;">=</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; content <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'content'</span><span style="color: #66cc66;">,</span> Text<span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span>u<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; name <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'name'</span><span style="color: #66cc66;">,</span> Unicode<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #dc143c;">email</span> <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'email'</span><span style="color: #66cc66;">,</span> Unicode<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> nullable<span style="color: #66cc66;">=</span><span style="color: #008000;">False</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; created <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span><span style="color: #483d8b;">'created'</span><span style="color: #66cc66;">,</span> TIMESTAMP<span style="color: #66cc66;">,</span> default<span style="color: #66cc66;">=</span>now<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; pageid <span style="color: #66cc66;">=</span> Column<span style="color: black;">&#40;</span>Integer<span style="color: #66cc66;">,</span> ForeignKey<span style="color: black;">&#40;</span><span style="color: #483d8b;">'page.id'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p><em>init_model</em> 方法的内容也不相同，但是对Session的配置都是在Session.configure里进行。 如果设置autocommit=True，即使设置了autoflush=True仍然需要Session.flush()一下才能把变更写入数据库。否则，则需要显示调用Session.commit()</p>
<p>在写controller的部分，pylonsbook大量用了</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">page_q <span style="color: #66cc66;">=</span> model.<span style="color: black;">meta</span>.<span style="color: black;">Session</span>.<span style="color: black;">query</span><span style="color: black;">&#40;</span>model.<span style="color: black;">Page</span><span style="color: black;">&#41;</span><br />
c.<span style="color: black;">page</span> <span style="color: #66cc66;">=</span> page_q.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div></div>
<p>或</p>
<div class="codecolorer-container python twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">page_q <span style="color: #66cc66;">=</span> model.<span style="color: black;">meta</span>.<span style="color: black;">Session</span>.<span style="color: black;">query</span><span style="color: black;">&#40;</span>model.<span style="color: black;">Page</span><span style="color: black;">&#41;</span><br />
c.<span style="color: black;">page</span> <span style="color: #66cc66;">=</span> page_q.<span style="color: black;">filter_by</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: #66cc66;">=</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span>.<span style="color: black;">first</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>在新版本里只需要<br />
<code class="codecolorer python default"><span class="python">c.<span style="color: black;">page</span><span style="color: #66cc66;">=</span>Session.<span style="color: black;">query</span><span style="color: black;">&#40;</span>model.<span style="color: black;">Page</span><span style="color: black;">&#41;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span></span></code><br />
即可。</p>
<p>在模板部分，pylons现在默认不依赖formbuild，所以文中提到的helper方法要额外安装formbuild库。不过这些也可以用过webhelper来实现，在lib/helpers.py里：<br />
<code class="codecolorer python default"><span class="python"><span style="color: #ff7700;font-weight:bold;">from</span> webhelpers.<span style="color: black;">html</span>.<span style="color: black;">tags</span> <span style="color: #ff7700;font-weight:bold;">import</span> *</span></code></p>
<p>在field.html中</p>
<div class="codecolorer-container html4strict twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span>Heading: ${h.text(<br />
&nbsp; &nbsp; &quot;heading&quot;<br />
)}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
Title: ${h.text(<br />
&nbsp; &nbsp; &quot;title&quot;<br />
)}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
<br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span><br />
Content: ${h.textarea(<br />
&nbsp; &nbsp; &quot;content&quot;, cols=40, rows=10<br />
)}<br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/p.html"><span style="color: #000000; font-weight: bold;">p</span></a>&gt;</span></div></div>
<p>模板中一个非常常用的方法url_for在pylons1.0里被改为pylons.url，需要修改/lib/helpers.py<br />
<code class="codecolorer python default"><span class="python"><span style="color: #ff7700;font-weight:bold;">from</span> pylons <span style="color: #ff7700;font-weight:bold;">import</span> url</span></code></p>
<p>书中所有的重定向都是直接设置response相关属性实现的，实际可以用redirect，结合url：<br />
<code class="codecolorer python default"><span class="python">redirect<span style="color: black;">&#40;</span>url<span style="color: black;">&#40;</span>controller<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'page'</span><span style="color: #66cc66;">,</span> action<span style="color: #66cc66;">=</span><span style="color: #483d8b;">'view'</span><span style="color: #66cc66;">,</span> <span style="color: #008000;">id</span><span style="color: #66cc66;">=</span>page.<span style="color: #008000;">id</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></span></code></p>
<p>分页部分，如果你使用的是sqlalchemy 0.6的话，随pylons 1.0发布的Webhelpers 0.6还不支持 sqlalchemy 0.6的query object，需要手动升级到1.2. (<i>easy_install -U webhelpers</i>)</p>
<p>最后关于flash状态，webhelpers里有一个专门的实现，不过还是不太好用：<br />
<a href="http://pylonshq.com/docs/en/1.0/thirdparty/webhelpers/pylonslib/">http://pylonshq.com/docs/en/1.0/thirdparty/webhelpers/pylonslib/</a></p>
<p>The post is brought to you by <a href="http://fedorahosted.org/lekhonee">lekhonee</a> v0.7</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2010/09/%e8%bf%81%e7%a7%bbpylonsbook-chapter-8%e5%88%b0pylons1-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Pylons Tips</title>
		<link>http://sunng.info/blog/2009/07/pylons-tips/</link>
		<comments>http://sunng.info/blog/2009/07/pylons-tips/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 13:09:36 +0000</pubDate>
		<dc:creator>sunng</dc:creator>
				<category><![CDATA[手艺]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://www.classicning.com/blog/?p=87</guid>
		<description><![CDATA[All scripts created by paster(paster create, paster controller and etc.) uses 4 spaces as indent while many editors(vim) uses tabs automatically. Different indent in one file will cause fatal error. h.url_for is a popular helper function in many books and &#8230; <a href="http://sunng.info/blog/2009/07/pylons-tips/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>All scripts created by paster(paster create, paster controller and etc.) uses 4 spaces as indent while many editors(vim) uses tabs automatically. Different indent in one file will cause fatal error.</p>
<p><em>h.url_for</em> is a popular helper function in many books and tutorials which to solve url mapping. This tool is no longer available by default since 0.97. If you got error message like &#8220;AttributeError: &#8216;module&#8217; object has no attribute &#8216;url_for&#8217;&#8221;, just add</p>
<p>[codesyntax lang="python"]from routes import url_for[/codesyntax]<br />
to<em> yourapp/libs/helper.py</em> .</p>
]]></content:encoded>
			<wfw:commentRss>http://sunng.info/blog/2009/07/pylons-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

