librelist archives

« back to archive

Flask login with mongoengine

Flask login with mongoengine

From:
Imran Khawaja
Date:
2012-11-24 @ 19:06
Hi :
Im new to python/Flask so pls forgive me if this is a trivial question. I
have my app setup to use mongoengine and flask login.

My User Model is defined as the following :

class User(UserMixin,db.Document):
    created_at = db.DateTimeField(default=datetime.datetime.now,
required=True)
    emailAddress = db.StringField(max_length=255, required=True)
    firstName = db.StringField(max_length=255, required=False)
    lastName = db.StringField(max_length=255, required=False)
    password_hash= db.StringField(max_length=255, required=True)
    current_package= db.StringField(max_length=255,required=False)

    def __unicode__(self):
        return self.id

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return "%s/%s" % (self.id, self.emailAddress)


    def get_id(self):
        return unicode(self.id)

    meta = {
        'allow_inheritance': True,
        'indexes': ['-created_at', 'emailAddress'],
        'ordering': ['-created_at']
    }


I have form on where a user can sign up by giving an email address and
password and I call a service method to create a user.

createdUser = UserService.createUser(email,password)
login_user(createdUser)

This part works fine where my user is created and stored in mongo.
I then forward the the user to a more detailed page which asks for more
information. On this page I decorated the view with @login_required and I
try to get the current logged in user by doing :
user = current_user.

However i get the error AttributeError: 'BaseQuerySet' object has no
attribute 'is_authenticated'
originating from flask_login.py :

if not current_user.is_authenticated():



Has anyone encountered this before? I thought using UserMixin would define
is_authenticated for me or am I mistaken?
I also tried defining is_authenticated in my User model but got the same
error.

Thanks
Imran
-- 
Imran

Re: [flask] Flask login with mongoengine

From:
Sina K
Date:
2012-11-24 @ 19:53
Hi Imran,

Just guessing here, but where did you set 'current_user'?

Sina

Imran Khawaja wrote:
>
> Hi :
> Im new to python/Flask so pls forgive me if this is a trivial
> question. I have my app setup to use mongoengine and flask login.
>
> My User Model is defined as the following :
>
> class User(UserMixin,db.Document):
> created_at = db.DateTimeField(default=datetime.datetime.now,
> required=True)
> emailAddress = db.StringField(max_length=255, required=True)
> firstName = db.StringField(max_length=255, required=False)
> lastName = db.StringField(max_length=255, required=False)
> password_hash= db.StringField(max_length=255, required=True)
> current_package= db.StringField(max_length=255,required=False)
>
> def __unicode__(self):
> return self.id <http://self.id>
>
> def set_password(self, password):
> self.password_hash = generate_password_hash(password)
>
> def check_password(self, password):
> return check_password_hash(self.password_hash, password)
>
> def __repr__(self):
> return "%s/%s" % (self.id <http://self.id>, self.emailAddress)
>
>
> def get_id(self):
> return unicode(self.id <http://self.id>)
>
> meta = {
> 'allow_inheritance': True,
> 'indexes': ['-created_at', 'emailAddress'],
> 'ordering': ['-created_at']
> }
>
>
> I have form on where a user can sign up by giving an email address and
> password and I call a service method to create a user.
>
> createdUser = UserService.createUser(email,password)
> login_user(createdUser)
>
> This part works fine where my user is created and stored in mongo.
> I then forward the the user to a more detailed page which asks for
> more information. On this page I decorated the view with
> @login_required and I try to get the current logged in user by doing :
> user = current_user.
>
> However i get the error AttributeError: 'BaseQuerySet' object has no
> attribute 'is_authenticated'
> originating from flask_login.py :
> if not current_user.is_authenticated():
>
>
> Has anyone encountered this before? I thought using UserMixin would
> define is_authenticated for me or am I mistaken?
> I also tried defining is_authenticated in my User model but got the
> same error.
>
> Thanks
> Imran
> -- 
> Imran

Re: [flask] Flask login with mongoengine

From:
Simon Zimmermann
Date:
2012-11-24 @ 20:13
> However i get the error AttributeError: 'BaseQuerySet' object has no
> attribute 'is_authenticated'
> originating from flask_login.py :
>
> if not current_user.is_authenticated():

From the error it looks like you use a QuerySet object as user rather
than a User class

> Has anyone encountered this before? I thought using UserMixin would define
> is_authenticated for me or am I mistaken?
> I also tried defining is_authenticated in my User model but got the same
> error.

Your user class should implement the User interface[1].

[1]: http://packages.python.org/Flask-Login/#your-user-class

Re: [flask] Flask login with mongoengine

From:
Imran Khawaja
Date:
2012-11-24 @ 21:19
Thanks I got it working. The problem was that I  in
my @login_manager.user_loader
annotated method I did not have an instance of a User because I was doing
User.objects.(id=userid)
instead of
User.objects.get(id=userid) which gives me an actual instance of a user.

Thank you for the help!
Imran


On Sat, Nov 24, 2012 at 3:13 PM, Simon Zimmermann <simon@insmo.com> wrote:

> > However i get the error AttributeError: 'BaseQuerySet' object has no
> > attribute 'is_authenticated'
> > originating from flask_login.py :
> >
> > if not current_user.is_authenticated():
>
> >From the error it looks like you use a QuerySet object as user rather
> than a User class
>
> > Has anyone encountered this before? I thought using UserMixin would
> define
> > is_authenticated for me or am I mistaken?
> > I also tried defining is_authenticated in my User model but got the same
> > error.
>
> Your user class should implement the User interface[1].
>
> [1]: http://packages.python.org/Flask-Login/#your-user-class
>



-- 
Imran