I'm having a weird issue where I'm setting some values on my flask session. It took me a couple of tries to repeat this error, but this is how it goes down.
- I open my page in private mode and login.
- I fill my form and I submit, and I get an
Internal Server Error 500
. - My log file wasn't capturing the error, so I made another log file capturing the values of the session (
log_file2
). With this, I could see that all of my session values disappeared (no idea how). - I made a
log_file2
at the end ofmain()
and all of my values were there, but when I enter save, none of them are there.
Now, here is the interesting part :
If I get the error, close the window, wait a while and reopen (again in private) I can replicate the error once again. But, if after I get the error I log on again, I fill my form and I submit, it works perfectly, I don't get any errors and my session values are all there. I closed the private window, reopened after a few minutes and tried submitting my form (with the same user) and it worked perfectly. I tried again but with an user I just created (new private window) and it gave me the error. The thing is, in production, the error appears once, the user retries and he can submit perfectly up until a couple of days when the error reappears once again for one time. I really am having trouble identifying what this could be. This is my first time making a flask web application so maybe I am missing something.
Any help that you can give me will be appreciated.
This is the code:
@app.route("/main")
def main():
session.permanent = True
try:
if 'user' in session and 'client' in session and 'user_uid' in session:
session['start_date'] = datetime.now().strftime('%Y%m%d')
session['start_time'] = datetime.now().strftime('%H%M')
context = {
'discipline': session['discipline']
}
log_file2('None necessary')
return render_template('main.html', context=context)
else:
session.clear()
return redirect(url_for('login'))
except Exception as ex:
log_file(str(ex), 'main')
@app.route("/save", methods=['POST'])
def save():
session.permanent = True
log_file2(request.form.to_dict())
form_fields = Fields.get_fields_with_attr(session['client'])
values = request.form.to_dict()
try:
visit_uid = str(uuid4())
img_data = values['sig-data']
img = base64.b64decode(img_data)
with open(join(dirname(realpath(__file__)), "static/img/sigs/{0}.png".format(visit_uid)), "wb") as fh:
fh.write(img)
if 'on-site' in values:
values['on-site'] = 1
values['visit-date'] = session['start_date']
values['time-arrival'] = session['start_time']
values['time-finish'] = datetime.now().strftime('%H%M')
else:
values['on-site'] = 0
visitDate = datetime.strptime(values['visit-date'], '%Y-%m-%d')
values['visit-date'] = visitDate.strftime('%Y%m%d')
timeArrival = datetime.strptime(values['time-arrival'], '%I:%M %p')
values['time-arrival'] = timeArrival.strftime('%H%M')
timeFinish = datetime.strptime(values['time-finish'], '%I:%M %p')
values['time-finish'] = timeFinish.strftime('%H%M')
datetime_object = datetime.strptime(values['time-leave'], '%I:%M %p')
time_string = datetime_object.strftime('%H%M')
values['time-leave'] = time_string
values['emp-num'] = session['user']
Visit.prepare_fields(values, session['user_uid'], form_fields, visit_uid)
session.clear()
return redirect(url_for('login'))
except Exception as ex:
log_file(str(ex), 'save')