How to create a Flask entrypoint to show party

Normally you have the session_key stored on the flask session object. From ther you can retrive the user by using the get_user method.

For example, we use the following decorator to indicate a route requires a user to be logged in:

UserSession = tryton.pool.get('web.user.session')

def login_required(func):                                                      
    @wraps(func)                                                               
    def wrapper(*args, **kwargs):                                              
        session_key = None                                                     
        if 'session_key' in session:                                           
            session_key = session['session_key']                               
        g.user = UserSession.get_user(session_key)                             
        if not g.user:                                                         
            return redirect(url_for('login', next=request.path))               
        return func(*args, **kwargs)                                           
    return wrapper    

Then you just need to use this decorator on your my-account route. For example:

@app.route('/my-account')                                
@tryton.transaction()                                                          
@login_required     
def account():
     print(g.user.name)

Note that I’m using the flask g object to store data in the application context that can be read from any function and even from html templates.

The full working example has been posted in Need some more detailed tutorial on flask tryton

1 Like