<?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; couchdb</title>
	<atom:link href="http://chrismoos.com/tag/couchdb/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>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>MoosTrax will&#160;return.</title>
		<link>http://chrismoos.com/2009/01/24/moostrax-will-return/</link>
		<comments>http://chrismoos.com/2009/01/24/moostrax-will-return/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 05:21:50 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[moostrax]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=125</guid>
		<description><![CDATA[Yes, I recently made a post saying that MoosTrax will be going away forever. Due to all the comments and feedback, it is obvious to me that there are many people that enjoyed the service &#8212; so I&#8217;m going to bring it back.
I&#8217;m moving my database backend from CouchDB to MySQL, so my issues with [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, I recently made a <a href="http://chrismoos.com/2009/01/20/hasta-la-vista-moostrax/">post</a> saying that MoosTrax will be going away forever. Due to all the comments and feedback, it is obvious to me that there are many people that enjoyed the service &#8212; so I&#8217;m going to bring it back.</p>
<p>I&#8217;m moving my database backend from <a href="http://couchdb.apache.org/">CouchDB</a> to MySQL, so my issues with CouchDB won&#8217;t prevent me from providing MoosTrax. I have been working a lot on migrating the site and fixing it up&#8230;so I really hope to have it up in the next week or so.</p>
<p>In the mean time, I&#8217;m also developing an <a href="http://code.google.com/android/">Android</a> client, so that those of you with a <a href="http://www.t-mobileg1.com/">T-Mobile G1</a>(myself included) will be able to use MoosTrax. Screenshot below.</p>
<p>After the site is up and running and the Android client is stable, I want to make an iPhone client the next priority. Also, if there are enough people that want a Windows Mobile client, I would consider that as well. Please e-mail moostraxsupport@gmail.com if you are interested in a Windows Mobile client.</p>
<p><img src="http://www.tech9computers.com/moostrax.png" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/01/24/moostrax-will-return/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Hasta la vista,&#160;MoosTrax.</title>
		<link>http://chrismoos.com/2009/01/20/hasta-la-vista-moostrax/</link>
		<comments>http://chrismoos.com/2009/01/20/hasta-la-vista-moostrax/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 23:42:56 +0000</pubDate>
		<dc:creator>Chris Moos</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[blackberrytracker]]></category>
		<category><![CDATA[couchdb]]></category>
		<category><![CDATA[moostrax]]></category>

		<guid isPermaLink="false">http://chrismoos.com/?p=117</guid>
		<description><![CDATA[UPDATE 01/21/09: 
Okay, okay. A lot of you seem to like the site&#8230;I&#8217;m going to bring it back. Moving back to MySQL though. Stay tuned.
Well, it was a fun while it lasted. Unfortunately, I&#8217;ve decided to close the doors of MoosTrax permanently. 
It started with BlackBerryTracker.com, which I had set up in Fall of 2007, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE 01/21/09:</strong> </p>
<blockquote><p>Okay, okay. A lot of you seem to like the site&#8230;I&#8217;m going to bring it back. Moving back to MySQL though. Stay tuned.</p></blockquote>
<p>Well, it was a fun while it lasted. Unfortunately, I&#8217;ve decided to close the doors of MoosTrax permanently. </p>
<p>It started with BlackBerryTracker.com, which I had set up in Fall of 2007, to provide a site to track BlackBerry GPS-enabled devices. Prior to starting the site I had got my first BlackBerry, the 8800, and also my first GPS enabled phone. I decided to to learn a little J2ME and BlackBerry device programming, while trying out the GPS functionality of the phone. The end product of this was BlackBerryTracker which was relatively well received by the community and I believe provided a good service to a lot of people. The site grew to just over 10,000 people in the following year. </p>
<p>After this, I decided to redesign the site, use a new database system(<a href="http://couchdb.apache.org/">CouchDB</a>), and choose a new, less specific name(MoosTrax). I was very happy with the new site, and especially the database, CouchDB, because of its less relational nature and document-oriented architecture(and who doesn&#8217;t love map/reduce). </p>
<p>Unfortunately, with CouchDB came extremely large database sizes. So large, in fact, that my 80GB hard drive became quickly filled. It filled so fast that I couldn&#8217;t even compact it, or make it smaller. My only option at this point was to move the 80GB database to another, remote server, compact it, delete it from the server, and replace it with the smaller copy. This proved to be more work than it was worth, and my interests in MoosTrax had been declining, so I have now decided to end it all here. </p>
<p>I thank everyone who helped me in testing BlackBerryTracker/MoosTrax and those who supported the progress of the site!</p>
<p>It was a great run and I hope to begin working a new project that will hopefully serve a new community.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismoos.com/2009/01/20/hasta-la-vista-moostrax/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
