How to create a Flask entrypoint to show party

Thank you for the example.
I am having some trouble getting an entry point to the web user from where I can begin to show the user/party data, for example in /my-account route.
What would the right way to get the web_user object/id?

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

The g object is what I needed in order to have the context where needed. Thank you Sergi.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.