librelist archives

« back to archive

Get item on pyMongo

Get item on pyMongo

From:
azwar akbar
Date:
2012-05-29 @ 04:02
Hello,
I'm new on python, flask and also in pyMongo.
Today I try to use flask and pymongo.
I have problem accessing field using pyMongo.

I try to use __getitem__() but not sure about this.

It is about simple login function, beside username and password, I also
want to know it's role,
so I need to get role item.

Here is my code

from flask import Flask, request, render_template, sessionimport
pymongofrom pymongo import Connectionfrom admin_region import admin

connection *=* Connection()
db *=* connection.ship_movement

app *=* Flask(__name__)
app.secret_key *=* 'k923nk@#$0sfd#$50){'
app.register_blueprint(admin, url_prefix*=*'/admin')
@app.route('/')*def* index():
    *return* 'hello world'
@app.route('/login', methods*=*['GET', 'POST'])*def* login():
    error *=* None

    *if* request.method *==* 'GET':
        *return* render_template('login.html', page_title *=* 'Login')
    *else*:
        userauth *=*
db.userauth.find({'username':request.form['username']},
{'password':request.form['password']})
        *if* userauth.count() *==* 1:
            session['username'] *=* request.form['username']
            role *=* userauth.collection.*__getitem__*('role')
            *if* role *==* '0':
                *return* render_template('admin_region.html',
page_title *=* 'Admin')

nb: in database, role is integer number 0,1,2

When I print *role* variable, it shows
*Collection(Database(Connection('localhost',
27017), u'ship_movement'), u'userauth.role')*
How to get *role* value from *userauth* so I can check it whether it value
is 0,1 or 2 ?

Thank you for your help, and sorry for my bad English ;)

-- 


regards,
Azwar Akbar

Re: [flask] Get item on pyMongo

From:
Lix Xu
Date:
2012-05-29 @ 08:07
I think you may use db.userauth.find_one({...}) to fetch the record.
find_one() will return a dictory or None.
find() will return iter-like object then you can use for xx in xx to fetch
every record.
Or, you can get next record via userauth.next().

You should refer to pymongo document:

http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find


On Tue, May 29, 2012 at 12:02 PM, azwar akbar <azwar.akbar@gmail.com> wrote:

> Hello,
> I'm new on python, flask and also in pyMongo.
> Today I try to use flask and pymongo.
> I have problem accessing field using pyMongo.
>
> I try to use __getitem__() but not sure about this.
>
> It is about simple login function, beside username and password, I also
> want to know it's role,
> so I need to get role item.
>
> Here is my code
>
> from flask import Flask, request, render_template, sessionimport 
pymongofrom pymongo import Connectionfrom admin_region import admin
>
> connection *=* Connection()
> db *=* connection.ship_movement
>
> app *=* Flask(__name__)
> app.secret_key *=* 'k923nk@#$0sfd#$50){'
> app.register_blueprint(admin, url_prefix*=*'/admin')
> @app.route('/')*def* index():
>     *return* 'hello world'
> @app.route('/login', methods*=*['GET', 'POST'])*def* login():
>     error *=* None
>
>     *if* request.method *==* 'GET':
>         *return* render_template('login.html', page_title *=* 'Login')
>     *else*:
>         userauth *=* 
db.userauth.find({'username':request.form['username']}, 
{'password':request.form['password']})
>         *if* userauth.count() *==* 1:
>             session['username'] *=* request.form['username']
>             role *=* userauth.collection.*__getitem__*('role')
>             *if* role *==* '0':
>                 *return* render_template('admin_region.html', page_title
*=* 'Admin')
>
> nb: in database, role is integer number 0,1,2
>
> When I print *role* variable, it shows 
*Collection(Database(Connection('localhost',
> 27017), u'ship_movement'), u'userauth.role')*
> How to get *role* value from *userauth* so I can check it whether it
> value is 0,1 or 2 ?
>
> Thank you for your help, and sorry for my bad English ;)
>
> --
>
>
> regards,
> Azwar Akbar
>
>
>
>

Re: [flask] Get item on pyMongo

From:
azwar akbar
Date:
2012-05-30 @ 00:30
Hi Lix Xu, thanks for your advice, really help me to understand.
Now my code becomes:

        userauth *=*
db.userauth.find_one({'username':request.form['username'],
'password':request.form['password']})

And because it return dictionary data type, now I can access role item
using

        role = userauth['role']

Thanks again for your advice.

On Tue, May 29, 2012 at 3:07 PM, Lix Xu <xuzenglin@gmail.com> wrote:

> I think you may use db.userauth.find_one({...}) to fetch the record.
> find_one() will return a dictory or None.
> find() will return iter-like object then you can use for xx in xx to fetch
> every record.
> Or, you can get next record via userauth.next().
>
> You should refer to pymongo document:
> 
http://api.mongodb.org/python/current/api/pymongo/collection.html#pymongo.collection.Collection.find
>
>
> On Tue, May 29, 2012 at 12:02 PM, azwar akbar <azwar.akbar@gmail.com>wrote:
>
>> Hello,
>> I'm new on python, flask and also in pyMongo.
>> Today I try to use flask and pymongo.
>> I have problem accessing field using pyMongo.
>>
>> I try to use __getitem__() but not sure about this.
>>
>> It is about simple login function, beside username and password, I also
>> want to know it's role,
>> so I need to get role item.
>>
>> Here is my code
>>
>> from flask import Flask, request, render_template, sessionimport 
pymongofrom pymongo import Connectionfrom admin_region import admin
>>
>> connection *=* Connection()
>> db *=* connection.ship_movement
>>
>> app *=* Flask(__name__)
>> app.secret_key *=* 'k923nk@#$0sfd#$50){'
>> app.register_blueprint(admin, url_prefix*=*'/admin')
>> @app.route('/')*def* index():
>>     *return* 'hello world'
>> @app.route('/login', methods*=*['GET', 'POST'])*def* login():
>>     error *=* None
>>
>>     *if* request.method *==* 'GET':
>>         *return* render_template('login.html', page_title *=* 'Login')
>>     *else*:
>>         userauth *=* 
db.userauth.find({'username':request.form['username']}, 
{'password':request.form['password']})
>>         *if* userauth.count() *==* 1:
>>             session['username'] *=* request.form['username']
>>             role *=* userauth.collection.*__getitem__*('role')
>>             *if* role *==* '0':
>>                 *return* render_template('admin_region.html', 
page_title *=* 'Admin')
>>
>> nb: in database, role is integer number 0,1,2
>>
>> When I print *role* variable, it shows 
*Collection(Database(Connection('localhost',
>> 27017), u'ship_movement'), u'userauth.role')*
>> How to get *role* value from *userauth* so I can check it whether it
>> value is 0,1 or 2 ?
>>
>> Thank you for your help, and sorry for my bad English ;)
>>
>> --
>>
>>
>> regards,
>> Azwar Akbar
>>
>>
>>
>>
>


-- 


regards,
Azwar Akbar