librelist archives

« back to archive

flash a message from trigger

flash a message from trigger

From:
sonic steph
Date:
2014-04-26 @ 17:03
Hi,

I am using Flask with SqlAlachemy (and MYSQL). I have made a trigger
that check if the name of a user is already used. If it is used once,
the trigger raised an error with a message.

I want to display this message with a Flash message :

try:
    db.session.commit()
except Exception as e:
    app.logger.error(e.message)
    db.session.flush()

as you can read, I can already catch the message and log it :

(OperationalError) (1644, 'this name (Linux) is already used')

I have the same issue with my form in flask-admin. But in this case, I
haven't seen yet what is the method to override...

cheers,

st├ęphane

Re: [flask] flash a message from trigger

From:
sonic steph
Date:
2014-04-27 @ 12:27
In fact, this is an encoding issues. The sqlalchemy connection is
initialized with :

SQLALCHEMY_DATABASE_URI = 'mysql://{}:{}@{}/{}?charset=utf8'.\
            format(user,passwd,host,dbName)

logging or flash messages from trigger gives this :

(OperationalError) (1644, 'Il existe d\xc3\xa9j\xc3\xa0 un amapien r\xc3
\xa9pondant au nom ...')

In my first message, I translate into english, which was a bad idea,
caused you can't see this encoding issue.. Even if I,

try:
    db.session.commit()
except Exception as e:
    flash(u"%s" % e.message)
    app.logger.error(unicode(e.message).encode("utf-8"))
    db.session.flush()

I can't have a user friendly correct string error message from the
trigger..

cheers

st├ęphane