Vladu Sorin Vladu Sorin - 1 year ago 78
Python Question

flask-login: can't understand how it works

I'm trying to understand how


I see in their documentation that they use a pre-populated list of users. I want to play with a database-stored users list.

However, I don't understand some things in this

def load_user(userid):
#print 'this is executed',userid
return user(userid, 'asdf')

This code will be called at every request? This is used to load all the details of my user object?

For now, I have this code:

def make_login():
username = 'asdf'
password = 'asdf'
user_data = authenticate(username, password)
user_obj = user(user_data[0], user_data[1])
return render_template('make-login.html')

When I access /make-login, I want to log in.

My user class:

class user(object):
def __init__(self, id, username, active=True):
self.username = username
self.id = id
#self.active = active
def is_authenticated(self):
return True

def is_active(self):
return True

def is_anonymous(self):
return False

def get_id(self):
return 5

Also, I wrote another two functions for authenticate/register

def authenticate(username, password):

cursor = db.cursor()
password = md5.md5(password).hexdigest()
query = "SELECT * FROM `users` WHERE `username` = %s AND `password` = %s"
cursor.execute(query, (username, password))
results = cursor.fetchall()
#print results[0][0]
#print "here i am"
if not results:
return False
user_data = [results[0][0], results[0][1]]
return user_data
#self.authenticated = True
#self.user_id = results[0][0]
#session['username'] = results['username']
#print type(results)
except db.Error, e:
return 'There was a mysql error'

def register(username, password, email, *args):
cursor = db.cursor()
password = md5.md5(password).hexdigest()
#query = "INSERT INTO `users` (`username`, `password`, `email`) VALUES ('%s', '%s', '%s')" % (username, password, email)
query = "INSERT INTO `users` (`username`, `password`, `email`) VALUES (%s, %s, %s)"
cursor.execute(query, (username, password, email))
return True
except db.Error, e:
print 'An error has been passed. %s' %e
return False

I don't know how to make this
work with MySQL. Also, I don't know if the user are logged-in. How can I get the user ID or the username?

Anyone can explain me in some rows how this

Answer Source

Flask-login doesn't actually have a user backend, it just handles the session machinery to help you login and logout users. You have to tell it (by decorating methods), what represents a user and it is also up to you to figure out how to know if a user is "active" or not (since being "active" can mean different things in different applications).

You should read the documentation and be sure what it does and does not do. Here I am only going to concentrate on wiring it up with the db backend.

To start off with, define a user object; which represents properties for your users. This object can then query databases, or LDAP, or whatever and it is the hook that connects the login mechanism with your database backend.

I will be using the login example script for this purpose.

class User(UserMixin):
    def __init__(self, name, id, active=True):
        self.name = name
        self.id = id
        self.active = active

    def is_active(self):
        # Here you should write whatever the code is
        # that checks the database if your user is active
        return self.active

    def is_anonymous(self):
        return False

    def is_authenticated(self):
        return True

Once you have the user object created, you need to write a method that loads the user (basically, creates an instance of the User class from above). This method is called with the user id.

def load_user(id):
     # 1. Fetch against the database a user by `id` 
     # 2. Create a new object of `User` class and return it.
     u = DBUsers.query.get(id)
    return User(u.name,u.id,u.active)

Once you have these steps, your login method does this:

  1. Checks to see if the username and password match (against your database) - you need to write this code yourself.

  2. If authentication was successful, get the id of the user and pass it to login_user()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download