An error when using and executing functions using flask_tryton

Hello, I have a concern when using flask_tryton.
I have carried out all the steps as shown here. But I still get this error :

[2024-09-27 11:27:45,130] ERROR in app: Exception on /doctor/party [GET]
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python3/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/lib/python3/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/gnuhealth/.local/lib/python3.8/site-packages/flask_tryton.py", line 143, in wrapper
    tryton = current_app.extensions['Tryton']
KeyError: 'Tryton'
[details="Configuration and tests"]
from flask_tryton import Tryton

app = Flask(__name__)

app.config['TRYTON_CONFIG'] = '/home/gnuhealth/gnuhealth/tryton/server/config/trytond.conf'
app.config['TRYTON_DATABASE'] = 'PDMD_SANTE'
app.config['TRYTON_USER'] = 0

tryton = Tryton(app)
User = tryton.pool.get('res.user')

bp = Blueprint('doctors', __name__, url_prefix='/doctor/')

@tryton.default_context
def default_context():
    return User.get_preferences(context_only=True)


@bp.route('/tests')
def tests():
    return {'The Answer': "La réponses"}


@bp.route('/party')
@tryton.transaction()
def party():
    user, = User.search([('login', '=', 'admin')])
    print('Le username des Username -------- ', user)
    return {"User": user.name}
[/details]

You did not register your blueprint to the Flask application, so I guess it is using a new application which does not have the Tryton extension.
I think you should add:

bp = Blueprint('doctors', __name__, url_prefix='/doctor/')
app.register_blueprint(bp)

I did indeed save my blueprint in my main application, the file for which is as follows :


from flask import Flask

from flask_tryton import Tryton


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
    )
    print(app)

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    # a simple page that says hello
    @app.route('/hello')
    def hello():
        return 'Hello, World!'

    from . import auth, doctor, db
    from flask_tryton import Tryton
    db.init_app(app)
    app.register_blueprint(auth.bp)
    app.register_blueprint(doctor.bp)
    
    return app



In this one, Tryton is not initialized with the app.

How i do to initialize Tryton in the app please?

tryton = Tryton(app)

thank you very much and sorry for my late reply . It’s Ok

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