from django.contrib.auth.models import User from django.utils import timezone from django.utils import translation import pytz class SettingsMiddleware(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # # Set Timzone # if request.user.is_authenticated: try: timezone.activate(pytz.timezone(request.user.userprofile.timezone)) except User.userprofile.RelatedObjectDoesNotExist: pass # No profile except AttributeError: pass # No User logged in else: timezone.deactivate() # # Set Language # try: language = request.user.userprofile.language_code except User.userprofile.RelatedObjectDoesNotExist: pass # No profile except AttributeError: pass # No User logged in else: translation.activate(language) request.LANGUAGE_CODE = translation.get_language() response = self.get_response(request) # Code to be executed for each request/response after # the view is called. return response