Posts tagged ‘couchdb’

CouchDB and Pylons: User Registration and Login

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’s start by creating a new pylons project and some controllers.

$ paster create -t pylons userdemo
$ cd userdemo
$ paster controller main
$ paster controller auth

Also, delete public/index.html.

For our controller main, we are going to add one action called index. 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’s add some routes for the main page, login, logout, and registration. Open up config/routing.py.

    map.connect('/', controller='main', action='index')
    map.connect('/auth/login', controller='auth', action='login', conditions=dict(method=['GET']))
    map.connect('/auth/login', controller='auth', action='login_post', conditions=dict(method=['POST']))
    map.connect('/auth/logout', controller='auth', action='logout')
    map.connect('/auth/register', controller='auth', action='register', conditions=dict(method=['GET']))
    map.connect('/auth/register', controller='auth', action='register_post', conditions=dict(method=['POST']))

For checking to see if a user is logged in, we will store a user_id in the session. Let’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’t logged in, it will redirect to the login page. Your controllers/main.py should look have this:

from decorator import decorator
 
def require_login(func, *args, **kwargs):
    """ Checks to see if user_id is in session """
    if not 'user_id' in session:
        redirect_to('/auth/login')
    return func(*args, **kwargs)
require_login = decorator(require_login)
 
class MainController(BaseController):
 
    @require_login
    def index(self):
        return 'You are logged in! Click <a href="/auth/logout">here</a> to logout.'

Continue reading ‘CouchDB and Pylons: User Registration and Login’ »

CouchDB and Pylons: Getting Started

CouchDB is very cool(it’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 — such as MySQL or PostgreSQL. I’ve decided to go a different route and integrate Pylons with CouchDB. Right now there is a Python library, couchdb-python, that will help us communicate with our CouchDB’s HTTP/JSON api.

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.

CouchDB is written in Erlang, a functional programming language with high concurrency. Let’s install Erlang.

$ wget http://erlang.org/download/otp_src_R12B-5.tar.gz
$ tar -xvzf otp_src_R12B-5.tar.gz
$ cd otp_src_R12B-5
$ ./configure
$ sudo make && make install

You should now have erlang installed. If you type erl and hit enter, you should see something like this.

$ erl
Erlang (BEAM) emulator version 5.6.3 [source] [smp:2] [async-threads:0] [kernel-poll:false]
 
Eshell V5.6.3  (abort with ^G)
1>

Continue reading ‘CouchDB and Pylons: Getting Started’ »

MoosTrax will return.

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 — so I’m going to bring it back.

I’m moving my database backend from CouchDB to MySQL, so my issues with CouchDB won’t prevent me from providing MoosTrax. I have been working a lot on migrating the site and fixing it up…so I really hope to have it up in the next week or so.

In the mean time, I’m also developing an Android client, so that those of you with a T-Mobile G1(myself included) will be able to use MoosTrax. Screenshot below.

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.

Hasta la vista, MoosTrax.

UPDATE 01/21/09:

Okay, okay. A lot of you seem to like the site…I’m going to bring it back. Moving back to MySQL though. Stay tuned.

Well, it was a fun while it lasted. Unfortunately, I’ve decided to close the doors of MoosTrax permanently.

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.

After this, I decided to redesign the site, use a new database system(CouchDB), 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’t love map/reduce).

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’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.

I thank everyone who helped me in testing BlackBerryTracker/MoosTrax and those who supported the progress of the site!

It was a great run and I hope to begin working a new project that will hopefully serve a new community.