31 lines
817 B
Python
31 lines
817 B
Python
"""Custom middleware."""
|
|
from django.utils import translation
|
|
from configuration.models import TranslationSettings
|
|
|
|
def get_locale(cookie_dict):
|
|
return cookie_dict.get('locale')
|
|
|
|
|
|
def get_country_code(cookie_dict):
|
|
return cookie_dict.get('country_code')
|
|
|
|
|
|
def parse_cookies(get_response):
|
|
"""Parse cookies."""
|
|
def middleware(request):
|
|
cookie_dict = request.COOKIES
|
|
|
|
# processing locale cookie
|
|
locale = get_locale(cookie_dict) or TranslationSettings.get_solo().default_language
|
|
translation.activate(locale)
|
|
request.locale = locale
|
|
|
|
# processing country country cookie
|
|
country_code = get_country_code(cookie_dict)
|
|
request.country_code = country_code
|
|
|
|
response = get_response(request)
|
|
return response
|
|
return middleware
|
|
|