How to use flask_tryton

Hi,

I was trying to make a flask app that send info to tryton using flask_tryton.

The code is the follow:

from flask import render_template, flash, redirect, url_for, session
from app import app
from app import tryton 

Party = tryton.pool.get('party.party')

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

@app.route('/', methods=['GET', 'POST'])
@tryton.transaction(readonly=False)
def index():
    form = LoginForm() 
	if form.validate_on_submit():
		firstname = form.firstname.data
		party, = Party.create([{
			'name': firstname,
			}])

I have already setting these variables:

    TRYTON_DATABASE: the Tryton’s database to connect.
    TRYTON_USER: the Tryton user id to use, by default 0 (aka root).
    TRYTON_CONFIG: the optional path to the Tryton’s configuration.

But when I send the info I receive this error:

  File "/testlib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/testlib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/testlib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/testlib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/testlib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/testlib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/testlib/python3.5/site-packages/flask_tryton.py", line 97, in wrapper
    with Transaction().start(database, 0):
  File "/testlib/python3.5/site-packages/trytond/transaction.py", line 96, in start
    autocommit=autocommit)
  File "/testlib/python3.5/site-packages/trytond/backend/postgresql/database.py", line 158, in get_connection
    conn.set_isolation_level(ISOLATION_LEVEL_REPEATABLE_READ)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 84, in check_async_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 273, in set_isolation_level
    prev = self.isolation_level
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 43, in check_closed_
    return func(self, *args, **kwargs)
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 265, in isolation_level
    name = self._get_guc('default_transaction_isolation')
  File "/testlib/python3.5/site-packages/psycopg2cffi/_impl/connection.py", line 234, in _get_guc
    raise exceptions.OperationalError("can't fetch %s" % name)
psycopg2cffi._impl.exceptions.OperationalError: can't fetch default_transaction_isolation

I’m not sure what thing I miss something but I based the code in flask-tryton · PyPI

Thanks in advance!

Do you have the same issue with psycopg2 (and not psycopg2cffi) ?

Well, I don’t use psycopg2 'cause can run trytond with it, I only have psycopg2cffi for database usage, it works fine, so I’m not sure if I need some special config with flask_tryton?

There is no special configuration regarding the database for flask_tryton except the variables you’re setting. That’s why the most probable issue look like psycopg2cffi, it would be interesting anyway to understand why you can’t run tryton with psycopg2 (are you using pypy ?) and it would help to rule out an eventual issue in flask_tryton.

I use ubuntu or debian for diffent apps, but in no one I can use psycopg2, at the moment I don’t have an instance running without psycopg2 but the most common trouble log is with isolation transaction.I use pypi to to install flask_tryton, I’m trying to migrate to docker but first I need to see that it works fine in a tradicional enviroment.

I couldn’t not find the code of flask_tryton but I create one on github.com/iehoshia/flask, so maybe I miss some config according to https://github.com/iehoshia/flask_tryton/blob/master/flask_tryton.py#L69.

Thanks for your help!

I am sorry but I don’t understand a word you say. Do you or do you not use psycopg2 ?

Also pypi ≠ pypy. The first one is the python package index, the second one is an implementation of the python interpreter in python.

Well, I have install psycopg and psycopg2cffi 'cause without it tryton does not run, I don’t know why. Well, I don’t use pypy.

This is really strange it shouldn’t happen. You have messed up something in your installation.

I need to add the follow lines to the route, after it works well.
@app.route(’/’, methods=[‘GET’, ‘POST’])
@tryton.transaction(readonly=False,user=1)

After some time, the previous message make it works, but sometimes the app downs, so I uninstall psycopg2cffi and try the follow post https://stackoverflow.com/questions/22752521/uwsgi-flask-sqlalchemy-and-postgres-ssl-error-decryption-failed-or-bad-reco/22753269#22753269, it works fine for me now.

# the fix
lazy = true
lazy-apps = true
1 Like

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