old_console/core/middleware.py
2024-11-02 14:12:45 +03:00

59 lines
1.7 KiB
Python

import logging
import pytz
from django.conf import settings
from django.utils import timezone
from license_info.tools import get_license_info
from users.services.userinfo import UserStatusService
from license_info.exeptions import LicenseException
_log = logging.getLogger(__name__)
class TimezoneMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if request.user.is_authenticated:
timezone_name = getattr(getattr(request.user, 'userinfo', None), 'timezone', None)
timezone_name = timezone_name or getattr(settings, 'DEFAULT_CURRENT_TIMEZONE', None) or 'UTC'
timezone.activate(pytz.timezone(timezone_name))
service = UserStatusService(request.user)
service.set_status()
else:
timezone.deactivate()
response = self.get_response(request)
timezone.deactivate()
return response
class LicenseMiddleware:
"""Check if user activate license"""
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Get license
info = None
try:
info = get_license_info()
except LicenseException:
_log.error("Can't get license information")
# Append it to request
request.license = info
# Return request
response = self.get_response(request)
# # Allow only license urls
# # Don't use reverse here, because we need several URL's to allow
# if info is None and "license" not in request.path:
# return LicenseInfoAPIView.as_view(request)
return response