<?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>chris moos&#039;s blog &#187; pylons</title>
	<atom:link href="http://chrismoos.com/tag/pylons/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrismoos.com</link>
	<description>developer by day, developer by night</description>
	<lastBuildDate>Fri, 30 Jul 2010 04:52:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pylons Worker&#160;Threads</title>
		<link>http://chrismoos.com/2009/03/04/pylons-worker-threads/</link>
		<comments>http://chrismoos.com/2009/03/04/pylons-worker-threads/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 07:03:53 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[threads]]></category>
		<category><![CDATA[worker]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=258</guid>
		<description><![CDATA[I was reading a thread over at pylons-discuss about worker threads in pylons. Worker threads are useful if you need to execute a long running task, but want to return to the user immediately. You could run a new thread per request, but if you have many requests, it is probably better to queue things [...]]]></description>
			<content:encoded><![CDATA[<p>I was reading a <a href="http://groups.google.com/group/pylons-discuss/browse_thread/thread/4cbf14a040689cee?hl=en#">thread</a> over at <a href="http://groups.google.com/group/pylons-discuss/topics?hl=en">pylons-discuss</a> about worker threads in pylons. Worker threads are useful if you need to execute a long running task, but want to return to the user immediately. You could run a new thread per request, but if you have many requests, it is probably better to queue things up.</p>
<p>To do this, you start a thread and use python&#8217;s <a href="http://docs.python.org/library/queue.html">Queue</a> for managing the tasks. Here is a very basic implementation:</p>
<p><em>config/environment.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># PYLONS IMPORTS</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">lib</span>.<span style="color: black;">myworker</span> <span style="color: #ff7700;font-weight:bold;">import</span> start_myworker
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> load_environment<span style="color: black;">&#40;</span>global_conf, app_conf<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Configure the Pylons environment via the ``pylons.config``
    object
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># PYLONS STUFF GOES HERE</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># start worker</span>
    start_myworker<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><em>lib/myworker.py</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">Queue</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
&nbsp;
worker_q = <span style="color: #dc143c;">Queue</span>.<span style="color: #dc143c;">Queue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyWorkerThread<span style="color: black;">&#40;</span><span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Worker thread is running.'</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            msg = worker_q.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #808080; font-style: italic;"># do a long running task...</span>
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'We got %s, do something with it!'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
                <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Unable to process in worker thread: '</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>e<span style="color: black;">&#41;</span>
            worker_q.<span style="color: black;">task_done</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>                
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> start_myworker<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    worker = MyWorkerThread<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    worker.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And in your controller&#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> myapp.<span style="color: black;">lib</span>.<span style="color: black;">myworker</span> <span style="color: #ff7700;font-weight:bold;">import</span> worker_q
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> WorkerController<span style="color: black;">&#40;</span>BaseController<span style="color: black;">&#41;</span>:    
    <span style="color: #ff7700;font-weight:bold;">def</span> do_task<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
      worker_q.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>: generate_some_unique_id<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'msg'</span>: <span style="color: #483d8b;">'your data goes here'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/03/04/pylons-worker-threads/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CouchDB and Pylons: User Registration and&#160;Login</title>
		<link>http://chrismoos.com/2009/02/21/couchdb-and-pylons-user-registration-and-login/</link>
		<comments>http://chrismoos.com/2009/02/21/couchdb-and-pylons-user-registration-and-login/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 01:47:08 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[formencode]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=200</guid>
		<description><![CDATA[In the previous tutorial, we learned how to get CouchDB and Pylons up and running, as well as create a simple page counter. Now we are going to implement a simple user authentication system. This tutorial will teach you how to use formencode to validate forms and CouchDB to store our user data.
Let&#8217;s start by [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://chrismoos.com/2009/02/03/couchdb-and-pylons-getting-started/">previous tutorial</a>, we learned how to get CouchDB and Pylons up and running, as well as create a simple page counter. Now we are going to implement a simple user authentication system. This tutorial will teach you how to use <a href="http://www.formencode.org/">formencode</a> to validate forms and CouchDB to store our user data.</p>
<p>Let&#8217;s start by creating a new pylons project and some controllers.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster create <span style="color: #660033;">-t</span> pylons userdemo
$ <span style="color: #7a0874; font-weight: bold;">cd</span> userdemo
$ paster controller main
$ paster controller auth</pre></div></div>

<p>Also, delete public/index.html.</p>
<p>For our controller main, we are going to add one action called <em>index</em>. This will be the main page for the site and will only be accessible for logged in users. If a user is not logged in, they will be redirected to the login page. Let&#8217;s add some routes for the main page, login, logout, and registration. Open up config/routing.py.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span>, controller=<span style="color: #483d8b;">'main'</span>, action=<span style="color: #483d8b;">'index'</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/login'</span>, controller=<span style="color: #483d8b;">'auth'</span>, action=<span style="color: #483d8b;">'login'</span>, conditions=<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>method=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'GET'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/login'</span>, controller=<span style="color: #483d8b;">'auth'</span>, action=<span style="color: #483d8b;">'login_post'</span>, conditions=<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>method=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'POST'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/logout'</span>, controller=<span style="color: #483d8b;">'auth'</span>, action=<span style="color: #483d8b;">'logout'</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/register'</span>, controller=<span style="color: #483d8b;">'auth'</span>, action=<span style="color: #483d8b;">'register'</span>, conditions=<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>method=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'GET'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/register'</span>, controller=<span style="color: #483d8b;">'auth'</span>, action=<span style="color: #483d8b;">'register_post'</span>, conditions=<span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>method=<span style="color: black;">&#91;</span><span style="color: #483d8b;">'POST'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>For checking to see if a user is logged in, we will store a <em>user_id</em> in the session. Let&#8217;s make a decorator that we can add to our index action that will check to see if a user is logged in. If the user isn&#8217;t logged in, it will redirect to the login page. Your <em>controllers/main.py</em> should look have this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> decorator <span style="color: #ff7700;font-weight:bold;">import</span> decorator
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> require_login<span style="color: black;">&#40;</span>func, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Checks to see if user_id is in session &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #483d8b;">'user_id'</span> <span style="color: #ff7700;font-weight:bold;">in</span> session:
        redirect_to<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/auth/login'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> func<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
require_login = decorator<span style="color: black;">&#40;</span>require_login<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MainController<span style="color: black;">&#40;</span>BaseController<span style="color: black;">&#41;</span>:
&nbsp;
    @require_login
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'You are logged in! Click &lt;a href=&quot;/auth/logout&quot;&gt;here&lt;/a&gt; to logout.'</span></pre></div></div>

<p><span id="more-200"></span></p>
<p>If you start the server now and go to <a href="http://localhost:5000">http://localhost:5000</a> you will be redirected to /auth/login. Good. Let&#8217;s get into CouchDB now&#8230;</p>
<p>Open up <a href="http://localhost:5984/_utils">http://localhost:5984/_utils</a> and create a new database, and call it <em>userdemo</em>.</p>
<p>Now we are going to define our User schema in <em>model/__init__.py</em>. A schema describes a certain type of document, and in this case, it will be a User document. This example is very simple, so we only need username, password, and salt. Note the other field, <em>type</em>. This will specify that the document is for users, and it will be automatically set with the default parameter when we store a document. We are also going to create a simple helper function that will return an instance of our database(from couchdb-python).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> couchdb <span style="color: #ff7700;font-weight:bold;">import</span> Server
<span style="color: #ff7700;font-weight:bold;">from</span> couchdb <span style="color: #ff7700;font-weight:bold;">import</span> schema
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    server = Server<span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://localhost:5984/'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> server<span style="color: black;">&#91;</span><span style="color: #483d8b;">'userdemo'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> User<span style="color: black;">&#40;</span>schema.<span style="color: black;">Document</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot; Simple user document &quot;&quot;&quot;</span>
    username = schema.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    password = schema.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    salt = schema.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">type</span> = schema.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span>default=<span style="color: #483d8b;">'user'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Next, we are going to create a simple template for auth/login. This will prompt the user to login, or register if the user does not have an account. (I assume you are using <a href="http://www.makotemplates.org/">mako</a> for your template engine).</p>
<p><em>templates/login.mak</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Login<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Login<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;form</span> <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;/auth/login&quot;</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;post&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Username:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/th<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${h.text('username')}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;th<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Password:<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/th<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;td<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${h.password('password')}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/td<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/tr<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Login&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/form<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
% if c.invalid_user:
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>*** An invalid username or password was entered.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
% endif
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Don't have an account? Click <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;/auth/register&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>here<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> to register.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Notice that we used h.text() and h.password(). These helpers create our input boxes and will also display an error when we get into form validation. Make sure to import those functions in <em>lib/helpers.py</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><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> text, password</pre></div></div>

<p>Now that we&#8217;ve created or login template, let&#8217;s implement our action auth/login in <em>controllers/auth.py</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> AuthController<span style="color: black;">&#40;</span>BaseController<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'login.mak'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Okay, let&#8217;s run the server and try to go to <a href="http://localhost:5000">http://localhost:5000</a>. We are redirect to our new login page.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster serve <span style="color: #660033;">--reload</span> development.ini</pre></div></div>

<p><img src="http://chrismoos.com/wp-content/uploads/2009/02/login.png" /></p>
<p>Okay, now let&#8217;s implement our login action. This is under <em>controllers/auth.py</em>, and the action is login_post. The first thing we will do is add a formencode schema for our login form. This will make sure the username and/or password is not empty, and it will display an error accordingly. Here is what our new auth.py file might look like.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> formencode
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> LoginForm<span style="color: black;">&#40;</span>formencode.<span style="color: black;">Schema</span><span style="color: black;">&#41;</span>:
    username = formencode.<span style="color: black;">validators</span>.<span style="color: black;">String</span><span style="color: black;">&#40;</span>not_empty=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    password = formencode.<span style="color: black;">validators</span>.<span style="color: black;">String</span><span style="color: black;">&#40;</span>not_empty=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AuthController<span style="color: black;">&#40;</span>BaseController<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'login.mak'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AuthController<span style="color: black;">&#40;</span>BaseController<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> login<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'login.mak'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> login_post<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            form_result = LoginForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #dc143c;">user</span> = authenticate_user<span style="color: black;">&#40;</span>form_result<span style="color: black;">&#91;</span><span style="color: #483d8b;">'username'</span><span style="color: black;">&#93;</span>, form_result<span style="color: black;">&#91;</span><span style="color: #483d8b;">'password'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
                session<span style="color: black;">&#91;</span><span style="color: #483d8b;">'user_id'</span><span style="color: black;">&#93;</span> = <span style="color: #dc143c;">user</span>.<span style="color: #008000;">id</span>
                session.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                redirect_to<span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> InvalidUser:
                c.<span style="color: black;">invalid_user</span> = <span style="color: #008000;">True</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'login.mak'</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> formencode.<span style="color: black;">Invalid</span>, err:
            html = render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'login.mak'</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> formencode.<span style="color: black;">htmlfill</span>.<span style="color: black;">render</span><span style="color: black;">&#40;</span>html, errors=err.<span style="color: black;">error_dict</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This login_post action first checks to see if the form is valid, if not, it will display the login form with the appropriate errors. If the form is valid, we attempt to call the function <em>authenticate_user</em>, which will return a valid User document if the login is successful, or raise an Exception if the login information is invalid. If the login is valid, we save the user.id in our session. If not, we set the invalid_user context variable and return the login page.</p>
<p>Okay, so we have our login_post action defined, but we don&#8217;t have an authenticate_user method yet. Let&#8217;s implement that. But before we do that, we need to know a little bit more about <a href="http://wiki.apache.org/couchdb/HTTP_view_API">CouchDB views</a>. A view is a way to query data from our database. A view is defined by implementing map and reduce functions. </p>
<p>Let&#8217;s start by adding a user view, that will let us query user data. To create the view, go to <a href="http://localhost:5984/_utils">http://localhost:5984/_utils</a> and select the userdemo database. Go to <em>Create Document</em>. We are going to create a <em>design document</em> which is where the views are defined. The document ID should be <em>_design/user</em>. This view is accessible via http://localhost:5984/userdemo/_view/user/VIEW_FUNC. To define our view functions, open the newly created _design/user document and add a field called <em>views</em>. For now, just put <em>{}</em> for the value. Hit save document. </p>
<p>Each view function defines a map and optionally a reduce function. This lets us limit and control what documents our view will return. For now, we just want a simple view that allows us to query the database and get a user by the username. Remember that CouchDB returns data with a key/value. The key we want to return is the username. </p>
<p>Let&#8217;s talk about <em>map</em> for a second. A map function is passed a CouchDB document, and then <em>emits</em>, or adds, key/value pairs. CouchDB uses javascript as the default view server. We will call our view function <em>by_username</em>, and it will return the username as the key and the user document as the value. Open your _design/user document and put this in for the views field.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;by_username&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;function(doc) { if(doc.type == 'user') emit(doc.username, doc); }&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>CouchDB should look like this:<br />
<img src="http://chrismoos.com/wp-content/uploads/2009/02/couchdb.png"/></p>
<p>Notice that we check doc.type. This is because all documents are stored under one namespace, so we need a way to differentiate between different documents. We do this by setting a type field for every user, with the value &#8220;user&#8221;. You can access this view by going here: <a href="http://localhost:5984/userdemo/_view/user/by_username">http://localhost:5984/userdemo/_view/user/by_username</a>. </p>
<p>Alright, now that we have our view defined, let&#8217;s implement the authenticate_user function. We also create a custom exception class that we use for an invalid user. We use hashlib to generate a sha256 hash of the user&#8217;s password and a random salt. <em>gen_hash_password</em> is used later for our registration functions, for creating a new salt and getting a hash. Add this above your controller in <em>controllers/auth.py</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> userdemo.<span style="color: black;">model</span> <span style="color: #ff7700;font-weight:bold;">import</span> get_db, User
<span style="color: #ff7700;font-weight:bold;">import</span> hashlib
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> InvalidUser<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> hash_password<span style="color: black;">&#40;</span>password, salt<span style="color: black;">&#41;</span>:
    m = hashlib.<span style="color: black;">sha256</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    m.<span style="color: black;">update</span><span style="color: black;">&#40;</span>password<span style="color: black;">&#41;</span>
    m.<span style="color: black;">update</span><span style="color: black;">&#40;</span>salt<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> m.<span style="color: black;">hexdigest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gen_hash_password<span style="color: black;">&#40;</span>password<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
    letters = <span style="color: #483d8b;">'abcdefghijklmnopqrstuvwxyz0123456789'</span>
    p = <span style="color: #483d8b;">''</span>
    <span style="color: #dc143c;">random</span>.<span style="color: black;">seed</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">32</span><span style="color: black;">&#41;</span>:
        p += letters<span style="color: black;">&#91;</span><span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>letters<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> hash_password<span style="color: black;">&#40;</span>password, p<span style="color: black;">&#41;</span>, p
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> authenticate_user<span style="color: black;">&#40;</span>username, password<span style="color: black;">&#41;</span>:
    result = User.<span style="color: black;">view</span><span style="color: black;">&#40;</span>get_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'_view/user/by_username'</span>, key=username<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> InvalidUser<span style="color: black;">&#40;</span><span style="color: #483d8b;">'bad username'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #dc143c;">user</span> = result.<span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># check password</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> hash_password<span style="color: black;">&#40;</span>password, <span style="color: #dc143c;">user</span>.<span style="color: black;">salt</span><span style="color: black;">&#41;</span> == <span style="color: #dc143c;">user</span>.<span style="color: black;">password</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> InvalidUser<span style="color: black;">&#40;</span><span style="color: #483d8b;">'bad password'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">user</span></pre></div></div>

<p>Okay, now our site can login a user. Let&#8217;s get into registration. Create the following functions for our registration action in <em>controllers/auth.py</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> register<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'register.mak'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> register_post<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            form_result = RegisterForm<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">user</span> = User<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">user</span>.<span style="color: black;">username</span> = form_result<span style="color: black;">&#91;</span><span style="color: #483d8b;">'username'</span><span style="color: black;">&#93;</span>
            <span style="color: #dc143c;">pwd</span>, salt = gen_hash_password<span style="color: black;">&#40;</span>form_result<span style="color: black;">&#91;</span><span style="color: #483d8b;">'password'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">user</span>.<span style="color: black;">password</span> = <span style="color: #dc143c;">pwd</span>
            <span style="color: #dc143c;">user</span>.<span style="color: black;">salt</span> = salt
            <span style="color: #dc143c;">user</span>.<span style="color: black;">store</span><span style="color: black;">&#40;</span>get_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'You are registered. Click &lt;a href=&quot;/auth/login&quot;&gt;here&lt;/a&gt; to login.'</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> formencode.<span style="color: black;">Invalid</span>, err:
            html = render<span style="color: black;">&#40;</span><span style="color: #483d8b;">'register.mak'</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> formencode.<span style="color: black;">htmlfill</span>.<span style="color: black;">render</span><span style="color: black;">&#40;</span>html, errors=err.<span style="color: black;">error_dict</span><span style="color: black;">&#41;</span></pre></div></div>

<p>These functions are similar to the login ones, except here we store a new user into the database, using the User schema document class that we defined in <em>model/__init__.py</em>. We also need to implement the RegisterForm, and also a custom validator, which checks to see if a username is taken.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> user_exists<span style="color: black;">&#40;</span>username<span style="color: black;">&#41;</span>:
    result = User.<span style="color: black;">view</span><span style="color: black;">&#40;</span>get_db<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'_view/user/by_username'</span>, key=username<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> UsernameValidator<span style="color: black;">&#40;</span>formencode.<span style="color: black;">validators</span>.<span style="color: black;">String</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> validate_python<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value, state<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> user_exists<span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> formencode.<span style="color: black;">Invalid</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Username already taken'</span>, value, state<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> RegisterForm<span style="color: black;">&#40;</span>formencode.<span style="color: black;">Schema</span><span style="color: black;">&#41;</span>:
    username = UsernameValidator<span style="color: black;">&#40;</span>not_empty=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    password = formencode.<span style="color: black;">validators</span>.<span style="color: black;">String</span><span style="color: black;">&#40;</span>not_empty=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></div></div>

<p>And that&#8217;s it! You can now register an account, login, and view the protected page(with the require_login decorator). Now you just need to add require_login to any function that is only for logged in users. In the next tutorial, I will go into some more advanced CouchDB topics and some cool map/reduce views. </p>
<p>You can download the pylons userdemo project <a href="http://chrismoos.com/wp-content/uploads/2009/02/userdemo.zip">here</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/02/21/couchdb-and-pylons-user-registration-and-login/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CouchDB and Pylons: Getting&#160;Started</title>
		<link>http://chrismoos.com/2009/02/03/couchdb-and-pylons-getting-started/</link>
		<comments>http://chrismoos.com/2009/02/03/couchdb-and-pylons-getting-started/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 01:25:33 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[pylons]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=139</guid>
		<description><![CDATA[CouchDB is very cool(it&#8217;s built on erlang), and with Pylons, it is even cooler.
Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API.
Normally a Pylons web application will use some sort of RDBMS for storing data and persistence &#8212; such as MySQL or PostgreSQL. I&#8217;ve decided to go a [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://couchdb.apache.org/">CouchDB</a> is very cool(it&#8217;s built on erlang), and with <a href="http://pylonshq.com">Pylons</a>, it is even cooler.</p>
<blockquote><p>Apache CouchDB is a distributed, fault-tolerant and schema-free document-oriented database accessible via a RESTful HTTP/JSON API.</p></blockquote>
<p>Normally a Pylons web application will use some sort of RDBMS for storing data and persistence &#8212; such as <a href="http://mysql.com">MySQL</a> or <a href="www.postgresql.org">PostgreSQL</a>. I&#8217;ve decided to go a different route and integrate Pylons with CouchDB. Right now there is a Python library, <a href="http://code.google.com/p/couchdb-python/">couchdb-python</a>, that will help us communicate with our CouchDB&#8217;s HTTP/JSON api.</p>
<p>For this article, I will assume you have Pylons installed and you are somewhat familiar with it. We are going to create a simple page counter application.</p>
<p>CouchDB is written in Erlang, a functional programming language with high concurrency. Let&#8217;s install Erlang.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>erlang.org<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>otp_src_R12B-5.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> otp_src_R12B-5.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> otp_src_R12B-<span style="color: #000000;">5</span>
$ .<span style="color: #000000; font-weight: bold;">/</span>configure
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>You should now have erlang installed. If you type <strong>erl</strong> and hit enter, you should see something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ erl
Erlang <span style="color: #7a0874; font-weight: bold;">&#40;</span>BEAM<span style="color: #7a0874; font-weight: bold;">&#41;</span> emulator version 5.6.3 <span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #7a0874; font-weight: bold;">source</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>smp:<span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>async-threads:<span style="color: #000000;">0</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>kernel-poll:<span style="color: #c20cb9; font-weight: bold;">false</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
Eshell V5.6.3  <span style="color: #7a0874; font-weight: bold;">&#40;</span>abort with ^G<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000;">1</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p><span id="more-139"></span><br />
Now let&#8217;s install CouchDB. The latest version can be found on <a href="http://couchdb.apache.org/downloads.html">this page</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>download.filehat.com<span style="color: #000000; font-weight: bold;">/</span>apache<span style="color: #000000; font-weight: bold;">/</span>incubator<span style="color: #000000; font-weight: bold;">/</span>couchdb<span style="color: #000000; font-weight: bold;">/</span>0.8.1-incubating<span style="color: #000000; font-weight: bold;">/</span>apache-couchdb-0.8.1-incubating.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> apache-couchdb-0.8.1-incubating.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> apache-couchdb-0.8.1-incubating
$ .<span style="color: #000000; font-weight: bold;">/</span>configure
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Okay. CouchDB should now be installed. Let&#8217;s start it up.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ couchdb
Apache CouchDB 0.9.0a721128-incubating <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">LogLevel</span>=info<span style="color: #7a0874; font-weight: bold;">&#41;</span> is starting.
Apache CouchDB has started. Time to relax.</pre></div></div>

<p>Alright, we are almost there. Let&#8217;s install the <a href="http://code.google.com/p/couchdb-python/">couchdb-python</a> library.</p>
<p>easy_install:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install <span style="color: #007800;">CouchDB</span>==<span style="color: #000000;">0.5</span></pre></div></div>

<p>or the latest development version:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">svn</span> checkout http:<span style="color: #000000; font-weight: bold;">//</span>couchdb-python.googlecode.com<span style="color: #000000; font-weight: bold;">/</span>svn<span style="color: #000000; font-weight: bold;">/</span>trunk<span style="color: #000000; font-weight: bold;">/</span> couchdb-python-read-only
$ <span style="color: #7a0874; font-weight: bold;">cd</span> couchdb-python-read-only
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>

<p>Now that we&#8217;ve got CouchDB all setup and running, let&#8217;s open up the web interface and create a database. CouchDB has a great web interface, located at <a href="http://localhost:5984/_utils/">http://localhost:5984/_utils/</a>. </p>
<p>When you are at the web interface, go to create database, and let&#8217;s name it <strong>tutorial</strong>. After you have created the new database, you are redirected and are shown an empty database with no documents.</p>
<p>We now have created our CouchDB database and we are ready to start adding new documents.</p>
<p>Let&#8217;s create a new Pylons project named <strong>tut1</strong>, delete the default index.html, and a create a controller called <strong>main</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster create <span style="color: #660033;">-t</span> pylons tut1
$ <span style="color: #7a0874; font-weight: bold;">cd</span> tut1
$ <span style="color: #c20cb9; font-weight: bold;">rm</span> tut1<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>index.html
$ paster controller main</pre></div></div>

<p>For this tutorial, we aren&#8217;t really going to go into too much detail. We are just going to make one page with a simple page counter. The next tutorial will be about some more advanced topics.</p>
<p>Okay, open up your <strong>config/routing.py</strong> file so we can add a route to our index page in the main controller.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/'</span>, controller=<span style="color: #483d8b;">'main'</span>, action=<span style="color: #483d8b;">'index'</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/{controller}/{action}'</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">map</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/{controller}/{action}/{id}'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Now let&#8217;s open up the file <strong>controllers/main.py</strong>. Let&#8217;s add an import for the couchdb-python module.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> couchdb.<span style="color: black;">client</span></pre></div></div>

<p>For the counter to work, we are going to just create a simple document that will store the visits.</p>
<p>Here is what our method for index will look like.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        db = couchdb.<span style="color: black;">client</span>.<span style="color: black;">Database</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'http://localhost:5984/tutorial'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># if counter does not exist, let's initialize it</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #483d8b;">'counter'</span> <span style="color: #ff7700;font-weight:bold;">in</span> db:
            db<span style="color: black;">&#91;</span><span style="color: #483d8b;">'counter'</span><span style="color: black;">&#93;</span> = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'visits'</span>: <span style="color: #ff4500;">1</span><span style="color: black;">&#125;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'You are the first user!'</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            counter = db<span style="color: black;">&#91;</span><span style="color: #483d8b;">'counter'</span><span style="color: black;">&#93;</span>
            counter<span style="color: black;">&#91;</span><span style="color: #483d8b;">'visits'</span><span style="color: black;">&#93;</span> = counter<span style="color: black;">&#91;</span><span style="color: #483d8b;">'visits'</span><span style="color: black;">&#93;</span> + <span style="color: #ff4500;">1</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># save</span>
            db<span style="color: black;">&#91;</span><span style="color: #483d8b;">'counter'</span><span style="color: black;">&#93;</span> = counter
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'You are user #%d.'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>counter<span style="color: black;">&#91;</span><span style="color: #483d8b;">'visits'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>First, a server object is created, and then the database is selected. After, we check to see if the counter document exists, and if it doesn&#8217;t, we initialize it. CouchDB stores a documents as a dictionary. A document can have as many key/value pairs as it wants. For this example, we just have one key, visits, which stores an integer.</p>
<p>Now that our index method is done, let&#8217;s run our pylons application.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ paster serve <span style="color: #660033;">--reload</span> development.ini</pre></div></div>

<p>Open up the site in a web browser, at <a href="http://localhost:5000">http://localhost:5000</a>.</p>
<p>You should see the following on your first visit.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">You are the first user!</pre></div></div>

<p>And on subsequent visits&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">You are user #2.</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">You are user #3.</pre></div></div>

<p>You have successfully created a CouchDB powered page counter! Now, this isn&#8217;t meant to be a production page counter, but it should give you a basic example of how CouchDB stores and retrieves documents.</p>
<p>In the next tutorial, we will integrate CouchDB with Pylons more, use Forms, CouchDB-Python&#8217;s Schemas, and more.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/02/03/couchdb-and-pylons-getting-started/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Pylons (X)HTML Validator&#160;Middleware</title>
		<link>http://chrismoos.com/2008/01/29/pylons-xhtml-validator-middleware/</link>
		<comments>http://chrismoos.com/2008/01/29/pylons-xhtml-validator-middleware/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 20:29:20 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[validator]]></category>
		<category><![CDATA[w3]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://chrismoos.com/2008/01/29/pylons-xhtml-validator-middleware/</guid>
		<description><![CDATA[This middleware will use the W3 Validator Service to check if your page is valid HTML. It will only check each URI once so that it doesn&#8217;t slow down your site by validating every page load. It does this by placing the result of each URI into a memory cache(using Beaker). You can then call [...]]]></description>
			<content:encoded><![CDATA[<p>This middleware will use the W3 Validator Service to check if your page is valid HTML. It will only check each URI once so that it doesn&#8217;t slow down your site by validating every page load. It does this by placing the result of each URI into a memory cache(using Beaker). You can then call <strong>cache_is_valid_xhtml()</strong> in your template to determine if the current page is valid.</p>
<p><em>Note</em>: The first time your page loads it will not display if the page is valid or not(It is validated AFTER the first response).</p>
<p>Download Link: <a href="http://www.tech9computers.com/W3ValidatorMiddleware-0.1-py2.5.egg">http://www.tech9computers.com/W3ValidatorMiddleware-0.1-py2.5.egg</a></p>
<p>Source Download(gzipped): <a href="http://www.tech9computers.com/w3validator.tar.gz">http://www.tech9computers.com/w3validator.tar.gz</a></p>
<p><strong>Install</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>www.tech9computers.com<span style="color: #000000; font-weight: bold;">/</span>W3ValidatorMiddleware-<span style="color: #000000;">0.1</span>-py2.5.egg
<span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install W3ValidatorMiddleware-<span style="color: #000000;">0.1</span>-py2.5.egg</pre></div></div>

<p><strong>Configuration</strong></p>
<p><em>config/middleware.py</em></p>
<p>At the top of your middleware.py import the following</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> w3validator <span style="color: #ff7700;font-weight:bold;">import</span> ValidatorMiddleware</pre></td></tr></table></div>

<p>Then right below <strong>CUSTOM MIDDLEWARE HERE<br />
</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #808080; font-style: italic;"># CUSTOM MIDDLEWARE HERE (filtered by error handling middlewares)</span>
    app = ValidatorMiddleware<span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><em>lib/helpers.py</em></p>
<p>At the top you need to import cache_is_valid_xhtml()</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> w3validator <span style="color: #ff7700;font-weight:bold;">import</span> cache_is_valid_xhtml</pre></td></tr></table></div>

<p>I then created the following method that I call from my templates to return a string saying if the page is valid or not.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> xhtml_str<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    valid = cache_is_valid_xhtml<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> valid == <span style="color: #008000;">True</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'&lt;em&gt;Valid&lt;/em&gt; XHTML 1.0'</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> valid == <span style="color: #008000;">False</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'&lt;em&gt;NOT&lt;/em&gt; Valid XHTML 1.0'</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #808080; font-style: italic;"># hasn't been validated yet</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'XHTML 1.0'</span></pre></div></div>

<p><strong>Template Configuration</strong></p>
<p>If you have a base template, you might want to the footer of every page to display if it is valid (X)HTML or not.</p>
<p><em>Mako</em></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My XHTML Site<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;footer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        ${h.xhtml_str()}
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>That&#8217;s all you need to do to add validation support to your site. You can check the middleware out in action on my site at <a href="http://blackberrytracker.com">http://blackberrytracker.com</a>. Scroll to the bottom of the page to see where I have the validation result.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2008/01/29/pylons-xhtml-validator-middleware/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python Web Frameworks: 2 Down, 1 to&#160;go</title>
		<link>http://chrismoos.com/2007/10/17/python-web-frameworks-2-down-1-to-go/</link>
		<comments>http://chrismoos.com/2007/10/17/python-web-frameworks-2-down-1-to-go/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 21:45:21 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[genshi]]></category>
		<category><![CDATA[pylons]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[turbogears]]></category>

		<guid isPermaLink="false">http://chrismoos.com/2007/10/17/python-web-frameworks-2-down-1-to-go/</guid>
		<description><![CDATA[On my trial to find the best python web framework(all around), I&#8217;ve just finished learning Pylons and experiencing everything it has to offer. Genshi is amazing and kills any text template engine(Django, Mako). SQLAlchemy is much different than Django&#8217;s ORM and has its advantages and disadvantages. Django&#8217;s ORM feels more natural and is much easier [...]]]></description>
			<content:encoded><![CDATA[<p>On my trial to find the best python web framework(all around), I&#8217;ve just finished learning Pylons and experiencing everything it has to offer. Genshi is amazing and kills any text template engine(Django, Mako). SQLAlchemy is much different than Django&#8217;s ORM and has its advantages and disadvantages. Django&#8217;s ORM feels more natural and is much easier to learn at first. I believe that SQLAlchemy is much more powerful but the learning curve is much bigger. </p>
<p>Django is a great web framework and I think it is perfect for people that want to build news/content sites. Everything in Django works very well together which makes it a one-stop shop. </p>
<p>Pylons is awesome too but compared to Django is lacking in a lot of documentation. You need to look in a lot of different places to learn all the aspects that Pylons has to offer. I will say that Genshi + SQLAlchemy + Pylons + Routes is very very powerful.</p>
<p>I&#8217;m going to be moving on to TurboGears now to see what is has different. I&#8217;ve read that the documentation is more complete but for the most part the components in each framework are pretty much the same(or can be).</p>
<p>Here it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2007/10/17/python-web-frameworks-2-down-1-to-go/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
