Middleware
CorePropsMiddleware
Provides the frontend with basic system configuration information, user preference language and notifications, under the userLanguage, globalNotifications, globalSettings props.
class CorePropsMiddleware:
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
if request.user.is_authenticated:
try:
user_profile = models.UserProfile.objects.get(user=request.user)
except models.UserProfile.DoesNotExist:
language = "en-us"
else:
language = user_profile.language
translation.activate(language)
share(request, "userLanguage", language)
try:
notifications_obj = models.Notifications.objects.filter(
active=True, language=language
)
except models.Notifications.DoesNotExist:
share(request, "globalNotifications", None)
else:
notification_schema = serialiazers.NotificationSchema(many=True)
notifications = notification_schema.dump(notifications_obj)
share(request, "globalNotifications", notifications)
else:
share(request, "userLanguage", None)
share(request, "globalNotifications", None)
global_settings = models.GlobalSettings.objects.first()
if global_settings:
active_audit = global_settings.active_audit
if not utils.is_audit_installed():
active_audit = False
settings = {
"appName": global_settings.name_app
if not global_settings.name_app == ""
else "Bitmotto OTC",
"appLogo": global_settings.get_logo(),
"timeExpiredSession": global_settings.session_expire_time,
"activeAudit": active_audit,
"activeIPAuth": global_settings.active_ip_authorization,
"activeEmailProvider": global_settings.active_email_provider,
"activeRegistration": global_settings.active_registration,
"activeCaptcha": global_settings.active_captcha,
"activeSocialApp": bool(SocialApplication.objects.all()),
}
else:
settings = {
"appName": "Bitmotto OTC",
"appLogo": "/static/img/logo.png",
"timeExpiredSession": app_settings.SESSION_EXPIRE_TIME,
"activeAudit": False,
"activeIPAuth": False,
"activeEmailProvider": False,
"activeRegistration": True,
"activeCaptcha": False,
"activeSocialApp": bool(SocialApplication.objects.all()),
}
share(request, "globalSettings", settings)
response = self.get_response(request)
return response
SessionIdleTimeout
Middleware that validates the idle time of a user. If the time exceeds that established in the settings variable SESSION_EXPIRE_TIME or in the session_expire_time field of the GlobalSettings model, if it exists, the user is logged off. If it does not exceed it, the count is restarted again.
class SessionIdleTimeout(MiddlewareMixin):
"""Middleware class to timeout a session after a specified time period."""
def process_request(self, request):
# Timeout is done only for authenticated logged in users.
if request.user.is_authenticated:
current = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
idle_timeout = int(app_settings.SESSION_EXPIRE_TIME)
global_settings = models.GlobalSettings.objects.first()
if global_settings:
idle_timeout = global_settings.session_expire_time
# Timeout if idle time period is exceeded.
if "last_activity" in request.session:
last_activity = datetime.strptime(
request.session["last_activity"], "%Y-%m-%dT%H:%M:%S"
)
now = datetime.strptime(current, "%Y-%m-%dT%H:%M:%S")
if (now - last_activity).seconds > idle_timeout * 60:
logout(request)
share_flash(
request,
error="Your session has been closed due to inactivity",
errors={
"error": "Your session has been closed due to inactivity"
},
)
share(request, "message_other_view", True)
return redirect("accounts:login")
if request.accepts("text/html"):
request.session["last_activity"] = current
else:
request.session["last_activity"] = current
return None