Need some more detailed tutorial on flask tryton

Hey guys
Can anyone teach me(or point me to a site) on how to do flask tryton in more detail than what is on the py website?

1 Like

I think there is no such documentation (or at least we are not aware of it).

If you want to learn how to use flask-tryton I will recomend learing first the flask basics and also the tryton basics. Once you are familiar with both concepts using flask-tryton is easier.

What basics do i need to learn in tryton?

You need to know how the tryton ORM works as normally flask tryton interact with tryton models to use its data.

Once you have this it depends on the application but you will need to know how the modules you have activated in the database are designed in order to follow it’s usages from flask tryton.

Here is an example which uses flask-tryton and web_user for authentication:

Click to show the example
from flask import Flask
from flask_tryton import Tryton
from flask.ext.babel import Babel

app = Flask(__name__)      
babel = Babel(app)                                                             
app.config['TRYTON_DATABASE'] = os.environ.get('DB_NAME', 'tryton') 
tryton = Tryton(app, configure_jinja=True)

WebUser = tryton.pool.get('web.user')                                          
Session = 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']                               
        user = Session.get_user(session_key)                                   
        if not user:                                                           
            return redirect(url_for('login', next=request.path))               
        return func(*args, **kwargs)                                           
    return wrapper                                                             


class LoginForm(FlaskForm):                                                    
    email = StringField('Correo Electrónico',                                  
        validators=[DataRequired()],                                           
        render_kw={'placeholder': 'Correo Electrónico'})                       
    password = PasswordField('Contraseña',                                     
        validators=[DataRequired()],                                           
        render_kw={'placeholder': 'Contraseña'})                               


@app.route('/login', methods=['GET', 'POST'])                                  
@tryton.transaction()                                                          
def login():                                                                   
    form = LoginForm()                                                         
    if request.method == 'POST' and form.validate_on_submit():                 
        user = WebUser.authenticate(form.email.data, form.password.data)       
        if user:                                                               
            session['session_key'] = WebUser.new_session(user)                 
            flash('Ahora estas identificado.', 'success')                      
            return redirect(request.form.get('next', url_for('index')))        
        flash('Email o contraseña incorrectos.', 'error')                      
    return render_template(                                                    
        'login.html',                                                          
        form=form,                                                             
        next=request.args.get('next'))                                         


@app.route('/logout')                                                          
@tryton.transaction(readonly=False)                                            
@login_required                                                                
def logout():                                                                  
    if session['session_key']:                                                 
        Session.delete(Session.search(                                         
                ('key', '=', session['session_key']),                          
                ))                                                             
        session.pop('session_key', None)                                       
        flash("Se ha desconectado tu sessión", 'success')                      
    return redirect(url_for('index')) 


@app.route('/')                                                                
@tryton.transaction()                                                          
def index():                                                                   
    return render_template('index.html')    

                                                          
@app.route('/my-account')                                                       
@tryton.transaction()                                                          
@login_required                                                                
def account():                                                                 
    return render_template('account.html')                                           
2 Likes

This example was mentioned on #tryton IRC channel by @pokoli.