Created
July 4, 2018 20:30
-
-
Save senaps/b92ea2a9f0737a43f4d7bed6aa7d409b to your computer and use it in GitHub Desktop.
custom flask code!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" the problem is, we want to be able to change session life time of a flask app, | |
on the fly, without restarting the app. the reason for this is, that the app was | |
being used as a UI for a networking device, customers were able to use our app | |
instead of using a terminal to config the device. so, we thought it would be a | |
better UX thing to not restart the application, on session time change. | |
i was tasked to solve the problem, and this is how i approached it: | |
i checked the source code of flask to see how it works, and set the session life | |
time. | |
#: A :class:`~datetime.timedelta` which is used to set the expiration | |
#: date of a permanent session. The default is 31 days which makes a | |
#: permanent session survive for roughly one month. | |
#: | |
#: This attribute can also be configured from the config with the | |
#: ``PERMANENT_SESSION_LIFETIME`` configuration key. Defaults to | |
#: ``timedelta(days=31)`` | |
permanent_session_lifetime = ConfigAttribute('PERMANENT_SESSION_LIFETIME', | |
get_converter=_make_timedelta) | |
the above, is what i found on the source code, so i thought, if i could change | |
this behaviour, all problems would be fixed. | |
i searched a bit more to find out how this is being used to determine the | |
session life time on each request. | |
def get_expiration_time(self, app, session): | |
"""A helper method that returns an expiration date for the session | |
or ``None`` if the session is linked to the browser session. The | |
default implementation returns now + the permanent session | |
lifetime configured on the application. | |
""" | |
if session.permanent: | |
return datetime.utcnow() + app.permanent_session_lifetime | |
i found the above code on another file, and got the idea: i would inherit the | |
base `Flask` class and edit the code on it. so: | |
class New_Flask(Flask): | |
permanent_session_lifetime = datetime.timedelta(minutes=3) | |
this obviously is not the actual code that i used, but this is the code that shows how i | |
handled the problem. there may be better ways, but i liked the way i did it, and since | |
then, the app is working just fine. (the actual code has exception handling and some | |
conditionals for where the session time is 0 or is a very large number, i only allow numbers | |
between 0-60, with 0 being infinite session life time. | |
i didn't have access to actual code that i wrote, because it's on our company's private git | |
server. i am not allowed to get out any of the code. | |
""" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment