<?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>Code That Matters &#187; python</title>
	<atom:link href="http://www.codethatmatters.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codethatmatters.com</link>
	<description>Pylons, rails, jQuery and other stuff that matters</description>
	<lastBuildDate>Sat, 16 Jul 2011 10:57:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unicode in Mako using Pylons</title>
		<link>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/</link>
		<comments>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 07:18:02 +0000</pubDate>
		<dc:creator>henke</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[mako]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=72</guid>
		<description><![CDATA[Small post regarding unicode support in mako. I learned yesterday that mako not only has input and output encoding options but also an default_filter encoding. In the environment.py file, in your Pylons config folder, you need all these three to be sure that everything is enoded as unicode. Remember though that the default_filters argument can [...]]]></description>
			<content:encoded><![CDATA[<p>Small post regarding unicode support in mako. I learned yesterday that mako not only has input and output encoding options but also an default_filter encoding. In the environment.py file, in your Pylons config folder, you need all these three to be sure that everything is enoded as unicode. Remember though that the default_filters argument can have big impact on performance.</p>
<p><em>(If someone knows the configuration files for other frameworks, please write a comment about it and I&#8217;ll add it to the post)</em></p>
<p>Please read the Mako unicode chapter <a href="http://www.makotemplates.org/docs/unicode.html">here</a> if you want the gory details.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="co1">#environment.py</span>
 config<span class="br0">&#91;</span><span class="st0">'pylons.app_globals'</span><span class="br0">&#93;</span>.<span class="me1">mako_lookup</span> = TemplateLookup<span class="br0">&#40;</span>
        input_encoding=<span class="st0">'utf-8'</span>, 
        output_encoding=<span class="st0">'utf-8'</span>, 
        default_filters=<span class="br0">&#91;</span><span class="st0">'decode.utf8'</span><span class="br0">&#93;</span> <span class="br0">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2009/10/unicode-in-mako-using-pylons/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Serving images directly from database</title>
		<link>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/</link>
		<comments>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/#comments</comments>
		<pubDate>Thu, 01 Oct 2009 08:54:13 +0000</pubDate>
		<dc:creator>henke</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.codethatmatters.com/?p=26</guid>
		<description><![CDATA[Even though storing images in the database have its advantages/disadvantages, here is a small guide for those who want to store them in the database and serve them directly to the browser without having to generate a file first. This examples is based on the amazing web framework pylons. 
In this example I want to [...]]]></description>
			<content:encoded><![CDATA[<p>Even though storing images in the database have its advantages/disadvantages, here is a small guide for those who want to store them in the database and serve them directly to the browser without having to generate a file first. This examples is based on the amazing web framework <a href="http://www.pylonshq.com">pylons</a>. </p>
<p>In this example I want to serve small product images from my product controller.</p>
<p>In the product.py controller I have this method</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span class="kw1">def</span> show_image<span class="br0">&#40;</span><span class="kw2">self</span>, <span class="kw2">id</span>, name<span class="br0">&#41;</span>:
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Content-type'</span><span class="br0">&#93;</span> = <span class="st0">'image/png'</span>
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Cache-Control'</span><span class="br0">&#93;</span> = <span class="st0">'max-age=14400'</span>
    response.<span class="me1">headers</span><span class="br0">&#91;</span><span class="st0">'Pragma'</span><span class="br0">&#93;</span> = <span class="st0">''</span>
    product = Product.<span class="me1">get_by</span><span class="br0">&#40;</span><span class="kw2">id</span>=<span class="kw2">id</span><span class="br0">&#41;</span>
    <span class="kw1">return</span> product.<span class="me1">image</span></pre></div></div>

<p>Then in my mako template I have</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;img</span> <span class="re0">src</span>=<span class="st0">&quot;url(controller='product', action='show_image', id='${c.product.id}', name='c.product.image_name')&quot;</span><span class="re2">/&gt;</span></span></pre></div></div>

<p>which will generate</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span class="sc3"><span class="re1">&lt;img</span> <span class="re0">src</span>=<span class="st0">&quot;/product/show_image/1/super-glue.png&quot;</span><span class="re2">/&gt;</span></span></pre></div></div>

<p>So whats happening is that when the page is loading the image tag, the show_image() function is called which fetches the image data from the database and returns the image data buffer to the template. The key for this to work is to set the appropriate headers so that the page know that the data returned is image/png through the Content-type. The name argument is just used to set a name for the image file on the connecting client so that the cached file will have a correct name.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codethatmatters.com/2009/10/serving-images-directly-from-database/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

