emmett-framework / emmett

The web framework for inventors
Other
1.04k stars 70 forks source link

flash() does not reset messages #298

Closed advh67 closed 3 years ago

advh67 commented 3 years ago

At the controller python file: flash('Welcome, advh67')

My template file "include_flash.html":

    {{__alerts=current.response.alerts()}}
    {{if len(__alerts)>0:}}
        <style>
         ...mystyle
        </style>
        <div id="auth_message" class="divMessage error_animation" style="box-shadow: 5px 10px 8px darkgray;">
        {{='. '.join([str(__x) for __x in __alerts])}}
        alerts={{=current.response.alerts()}}
        {{current.session['newsessionkey']='newsessionvalue'}}
        new session={{=current.session}}
        </div>
        <!-- <div class="divMessage error_animation" style="top:1em;background-color:gray;color:lightgray;text-align:left;">Notificaci&oacute;n</div> -->
    {{pass}}

This code works well, at first sight:

Welcome, advh67
alerts=[]
new session=<sdict {'auth': <sdict {'user': 'advh67', 'data': {'first_name': 'advh67'}, 'last_visit': datetime.datetime(2020, 11, 4, 8, 11, 34, 523456), 'last_dbcheck': datetime.datetime(2020, 11, 4, 8, 11, 34, 523456), 'expiration': 2592000, 'remember': False}>, '_flashes': [], 'newsessionkey': 'newsessionvalue'}> 

The "session" values in template looks updated, but the session variable in main code does not get updated. The '_flashes' value is not emptyed and shows the same messages at every page refresh. The 'newsessionkey' becomes inexistent, too. This code worked in emmett 2.0.2

gi0baro commented 3 years ago

@advh67 a couple of things:

advh67 commented 3 years ago

The sessions implementation in app.pipeline is: [SessionManager.cookies('mykey'),...]

I put a second response.alerts() to check if It become an empty list. The emmett function helpers.get_flashed_messages() is working. If I comment out the

alerts={{=current.response.alerts()}}

the result is the same, the value of current.session['_flashes'] in the template becomes [] after reading with response.alerts().

The key '_flashes' contains an empty list but the main 'session' does not seem to update the changes, keeping the '_flashes' list intact.

Adding prints in my pipeline:

class MyAuthPipe(Pipe):
    .... code
   async def close(self):
        print('session.close:')
        print(self.auth.session)
        print('----------------')

shows in my linux terminal:

session.close:
<sdict {'_flashes': [('message', 'Session finished'), ('message', 'Welcome, advh67')], 'auth': <sdict {'user': 'advh67', 'data': {'first_name': 'advh67'}, 'last_visit': datetime.datetime(2020, 11, 5, 7, 24, 48, 602188), 'last_dbcheck': datetime.datetime(2020, 11, 5, 7, 24, 48, 602188), 'expiration': 2592000, 'remember': False}>}>

The "Session finished" message coming from previous logout page does not get erased, neither the "Welcome User" one. Any session['newsessionkey'] value set into the template is shown at the page, but never got updated in the "true" session.

gi0baro commented 3 years ago

@advh67 I confirm this, is a side effect of a flow change I made between 2.0 and 2.1 I should be able to publish a 2.1.3 in the next 48 hours, I'm sorry for the inconvenient.

advh67 commented 3 years ago

Nice.

Thanks

gi0baro commented 3 years ago

This is fixed both in master and 2.1.3 🎉