<?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>The Braindonor Network &#187; Mako</title>
	<atom:link href="http://www.braindonor.net/tag/mako/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.braindonor.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 01 Jun 2010 17:42:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Mako Authentication Required</title>
		<link>http://www.braindonor.net/coding-blog/mako-authentication-required/175/</link>
		<comments>http://www.braindonor.net/coding-blog/mako-authentication-required/175/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 21:11:55 +0000</pubDate>
		<dc:creator>John Hoff</dc:creator>
				<category><![CDATA[Coding Blog]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mako]]></category>
		<category><![CDATA[mod_python]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.braindonor.net/?p=175</guid>
		<description><![CDATA[I have been using my mod_python Mako handler for several months now in my personal projects. For the most part, I have been very happy with Mako and am finding it extremely useful. One issue I have had to wrap my head around has been the inability to halt template execution cleanly. A common practice [...]]]></description>
			<content:encoded><![CDATA[I have been using my <a href="http://www.braindonor.net/projects/updated-mod_python-mako-handler/127/">mod_python Mako handler</a> for several months now in my personal projects.  For the most part, I have been very happy with Mako and am finding it extremely useful.  One issue I have had to wrap my head around has been the inability to halt template execution cleanly.  A common practice in many a website has been to flush the current output buffer, display the required authentication information with a form or a redirect, and then end the request&#8212;making authentication required on a page of content.<span id="more-175"></span><br /><br />

<a href="http://www.makotemplates.org/">Mako</a> posed a significant challenge to my own thought process as I was designing page templates.  I was very used to having the content buffered and being able to perform an internal redirect or transfer.  Both mod_perl and ASP.NET offer this feature as part of the various frameworks.  Luckily, my work in PHP has helped me pay much closer attention to how I am handling the internal flow of programs.  Since most PHP configurations are set to be unbuffered, you have to pay particular attention to when and where you are delivering headers to the client browser.<br /><br />

One way to do solve this was to introduce exception handling to my mako handler.  In ASP.NET, when an internal transfer or redirect is made an exception is thrown through the stack.  In almost all cases, this exception can be cleanly ignored.  To introduce this behavior, I would need to add acceptable exception conditions to my handler.  Further, I would have to code my specific authentication methods into my generic handler.  This was something I did not wish to do.<br /><br />

Instead of working with exceptions, I looked to the template inheritance of Mako to solve the problem.  I start with a non-authenticated template which is in turn inherited by an authenticated template.  The page requested inherits the template that corresponds to its need for authentication.<br /><br />

The following example pages illustrate how inheritance can be used to introduce the required authentication.<br /><br />

<b>template.html</b><br />
<code>&lt;%
    self.page_executed = False
%&gt;
&lt;html&gt;&lt;body&gt;
${next.body()}
&lt;br /&gt;&lt;br /&gt;
Page Executed: ${self.page_executed}
&lt;/body&gt;&lt;/html&gt;</code><br />

<b>require_auth_template.html</b><br />
<code>&lt;%inherit file="template.html" /&gt;
&lt;%!
    from my_library import Account
%&gt;
&lt;%
    self.account = Account.fetch_from_request(req)
%&gt;
% if self.account is None:
    You must log in to continue.
% else:
    ${next.body()}
% endif</code><br />

<b>page1.html</b><br />
<code>&lt;%inherit file="template.html" /&gt;
&lt;%
    self.page_executed = True
%&gt;
This page will always be displayed in the template.</code><br />

<b>page2.html</b><br />
<code>&lt;%inherit file="require_auth_template.html" /&gt;
&lt;%
    self.page_executed = True
%&gt;
This page will only be displayed in the template
if the user has logged in.</code><br />

<b>Output of page1.html</b><br />
<code>This page will always be displayed in the template.

Page Executed: True</code><br />

<b>Output of page2.html</b><br />
<code>You must log in to continue.

Page Executed: False</code><br />

Because the user is not logged in, the inheritance chain of page2.html ends the request and sends the login content to the top-level template without even calling the template of page2.html.<br /><br />

This technique can be further expanded to introduce exception handling by wrapping the next.body() call in a try block.  This would keep all of the necessary exception and authentication logic outside of template handler and in the templates where they belong.]]></content:encoded>
			<wfw:commentRss>http://www.braindonor.net/coding-blog/mako-authentication-required/175/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updated mod_python Mako Handler</title>
		<link>http://www.braindonor.net/projects/updated-mod_python-mako-handler/127/</link>
		<comments>http://www.braindonor.net/projects/updated-mod_python-mako-handler/127/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 17:43:43 +0000</pubDate>
		<dc:creator>John Hoff</dc:creator>
				<category><![CDATA[Coding Blog]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mako]]></category>
		<category><![CDATA[mod_python]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>

		<guid isPermaLink="false">http://www.braindonor.net/?p=127</guid>
		<description><![CDATA[I have been using the mod_python handler to parse Mako templates for about a month now in a personal project. As I have done more and more development on my project, I have naturally encountered shortcomings and errors in my handler. The first thing I encountered when using the handler was the incorrect status being [...]]]></description>
			<content:encoded><![CDATA[I have been using the mod_python handler to parse Mako templates for about a month now in a personal project.  As I have done more and more development on my project, I have naturally encountered shortcomings and errors in my handler.<span id="more-127"></span><br /><br />

The first thing I encountered when using the handler was the incorrect status being returned after a template was parsed.  Initially, I was not aware of the 500 status attached to all of the responses because the content came through correctly.  Once I started to implement some AJAX features, everything came crashing down.  While content was indeed returned, the ajax request looked at the status in the response header first, and started erring out all of my AJAX calls.  I tracked this down to how I was previously setting the status so that it could be updated inside of the template:<br /><br />

<code>req.status = apache.OK
req.write(template.render(req=req))
return req.status</code><br />

After some digging, I discovered that the reason for the 500 status was that I was attempting to set the status of the request.  Instead of attempting to use the request status, I needed to make a temporary place-holder:<br /><br />

<code>req.mako_status = apache.OK
req.write(template.render(req=req))
return req.mako_status</code><br />

After that fix, it was smooth sailing until I started integrating <a href="http://www.sqlalchemy.org/" target="_blank">SQLAlchemy</a> into my application.  The moment I imported SQLAlchemy into my template file, mod_python started throwing error pages concerning the directory to cache eggs into.  Because I had already been testing my ORM objects through the command line, this came as a surprise.  The quick fix was to create a directory for it, and add it to the environment from within the handler.  Once that was done, everything imported cleanly.<br /><br />

Now that I had my ORM objects imported, it was time to use them.  As i was implementing a form-handling routine, I ran into another issue.  When I passed in the raw value from the util.FieldStorage object provided by mod_python, I started receiving ProgrammingError exceptions from SQLAlchemy indicating that it was unable to adapt the select statements.  After some digging, I found one little reference pointing back to the behavior of FieldStorage.  I found the reason for the error <a href="http://osdir.com/ml/python.sqlalchemy.user/2006-08/msg00084.html" target="_blank">here</a>.  FieldStorage was returning StringField objects instead of raw strings when I parsed the form inputs.  When I placed those into the orm calls, it was unable to perform the needed type-matches.  For the solution, I created a wrapper object for FieldStorage to implement some additional methods that <i>did</i> return string values.<br /><br />]]></content:encoded>
			<wfw:commentRss>http://www.braindonor.net/projects/updated-mod_python-mako-handler/127/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>mod_python Handler for Mako</title>
		<link>http://www.braindonor.net/projects/mod_python-handler-for-mako/116/</link>
		<comments>http://www.braindonor.net/projects/mod_python-handler-for-mako/116/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 06:32:49 +0000</pubDate>
		<dc:creator>John Hoff</dc:creator>
				<category><![CDATA[Coding Blog]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Apache]]></category>
		<category><![CDATA[Mako]]></category>
		<category><![CDATA[mod_python]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.braindonor.net/?p=116</guid>
		<description><![CDATA[One of my personal goals this Winter has been to learn and start using Python. Learning new languages and putting them to use is something I try to do about every year. Like so many others out there, messing around with Perl in college really has had an impact on my professional career. Being a [...]]]></description>
			<content:encoded><![CDATA[One of my personal goals this Winter has been to learn and start using <a href="http://www.python.org" target="_blank">Python</a>.  Learning new languages and putting them to use is something I try to do about every year.  Like so many others out there, <a href="http://xkcd.com/519/" target="_blank">messing around with Perl</a> in college really has had an impact on my professional career.  Being a professional web developer who uses Perl&#8212;especially through Apache/mod_perl&#8212;it was only natural that I began looking at building out a small site using <a href="http://www.modpython.org/" target="_blank">mod_python</a>. Little did I understand how much of a minefield I was stepping into!<span id="more-116"></span><br /><br />

Whenever I'm learning a new language, I inevitably explore how others are using it.  This means trying out many of the Python-based web development frameworks that are out there.  One of the biggest surprises for me was the reliance that nearly all of the frameworks place on custom Python application servers, bypassing Apache.  This also extends to most of the template libraries.  It all really depends on a pure Python application server.  That would be fine if my goal was to learn Django or Zope. I'm interested in learning Python...and learning a new language for me has always been very dependent on reinventing a known wheel.  For me, that really meant focusing on mod_python.<br /><br />

So I dive in, get mod_python installed on a system of mine and start working.  Time from apt-get install mod_python to a working Hello World handler: about 15 minutes.  All confident in my ability to get things moving, I start taking another look at Python template libraries.  Everything grinds to a halt again for me almost immediately.  The libraries all have documentation on how to call them from within python programs, but very little information on how to call them from within mod_python.  Two exceptions stood out: <a href="http://www.cheetahtemplate.org/">Cheetah</a> and <a href="" target="_blank">Mako</a>.  Cheetah had a couple of handler examples on their wiki...but I was never able to get any of them to work well.  Mako had a working WSGI handler that I was able to study and learn very quickly how to use their library from within mod_python.<br /><br />

Now, everything is up and running, and I'm building a couple of small web applications using mod_python and Mako.  To help fill some of the vacuum out there with respect to mod_python handlers, I decided that I wanted to share the handler I have been working with.  I wouldn't call it production-worthy, but I am enjoying using it a lot.  The Apache configurations required are below and I have attached the handler Python script.  Enjoy!<br /><br />

Apache Configuration:<br />
<code>&lt;Files ~ "\.html$"&gt;
    PythonPath "sys.path+['/var/www/braindonor.net/lib']"
    PythonDebug On
    SetHandler mod_python
    PythonHandler MakoHandler
&lt;/Files&gt;</code>]]></content:encoded>
			<wfw:commentRss>http://www.braindonor.net/projects/mod_python-handler-for-mako/116/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
