librelist archives

« back to archive

couchdb.http.ResourceNotFound

couchdb.http.ResourceNotFound

From:
Alasdair Macmillan
Date:
2010-08-19 @ 15:51
Hi

I'm trying to test whether a field exists in a CouchDB document (using the
Couchdb library not Flask-Couchdb). I have a login form that sends the 
email address. I am trying to use simple control logic that if the email 
address being posted from the form == the email field in my document

server = Server('http://localhost:5984')
db = server['database']
user = db[form_email]
form_email = request.form['email']

if form_email == user['email']:
     <start a session>
else:

     <login again with a message explaining)

However it causes an http error and I don't know how to test for the email
field being present in my couchdb instance without that happening on form 
posting?  Is there a way or do I need to handle the error and redirect 
that?

couchdb.http.ResourceNotFound
ResourceNotFound: ('not_found', 'missing')


AL

Re: [flask] couchdb.http.ResourceNotFound

From:
Ron DuPlain
Date:
2010-08-19 @ 17:09
Hi,

On Thu, Aug 19, 2010 at 11:51 AM, Alasdair Macmillan <al@atomised.coop> wrote:
> server = Server('http://localhost:5984')
> db = server['database']
> user = db[form_email]
> form_email = request.form['email']
>
> if form_email == user['email']:
>     <start a session>
> else:
>
>     <login again with a message explaining)
>
> However it causes an http error and I don't know how to test for the 
email field being present in my couchdb instance without that happening on
form posting?  Is there a way or do I need to handle the error and 
redirect that?
>

The error you are seeing is very similar to dict's KeyError.  You can
use db.get:

    db.get(form_email)

By default this returns None if your couch does not have a doc with an
id matching form_email.  You can explicitly set the default just like
in dict.get:

    db.get(form_email, return_this_if_missing)

So you can simply:

    doc = db.get(form_email)
    if doc and doc.get('email') == request.form['email']:
        <start a session>
    else:
        <don't start a session>

I'm assuming you are handling request.form cases.
Hope this helps,

Ron


help(db.get)

Help on method get in module couchdb.client:

get(self, id, default=None, **options) method of
couchdb.client.Database instance
    Return the document with the specified ID.

    :param id: the document ID
    :param default: the default value to return when the document is not
                    found
    :return: a `Row` object representing the requested document, or `None`
             if no document with the ID was found
    :rtype: `Document`