31 lines
935 B
Python
31 lines
935 B
Python
"""
|
|||
|
|
Utility functions for the Flask job application system.
|
||
|
|
"""
|
||
|
|
from datetime import datetime
|
||
|
|
from flask import session, current_app
|
||
|
|
|
||
|
|
|
||
|
|
def check_rate_limit():
|
||
|
|
"""
|
||
|
|
Check if user is submitting forms too quickly.
|
||
|
|
Returns (allowed: bool, wait_seconds: int)
|
||
|
|
"""
|
||
|
|
now = datetime.now()
|
||
|
|
last_submit_str = session.get('last_submission_time')
|
||
|
|
|
||
|
|
if last_submit_str:
|
||
|
|
try:
|
||
|
|
last_submit = datetime.fromisoformat(last_submit_str)
|
||
|
|
elapsed = (now - last_submit).total_seconds()
|
||
|
|
|
||
|
|
if elapsed < current_app.config['RATE_LIMIT_SECONDS']:
|
||
|
|
wait_seconds = int(current_app.config['RATE_LIMIT_SECONDS'] - elapsed) + 1
|
||
|
|
return False, wait_seconds
|
||
|
|
except (ValueError, TypeError):
|
||
|
|
# Invalid timestamp, allow submission
|
||
|
|
pass
|
||
|
|
|
||
|
|
# Update last submission time
|
||
|
|
session['last_submission_time'] = now.isoformat()
|
||
|
|
return True, 0
|