from django.utils.cache import patch_vary_headers
from django.utils import translation
from account.models import Account

class LocaleMiddleware(object):
    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context depending on the user's account. This allows pages
    to be dynamically translated to the language the user desires
    (if the language is available, of course). 
    """

    def get_language_for_user(self, request):
        if request.user.is_authenticated():
            try:
                account = Account.objects.get(user=request.user)
                return account.language
            except (Account.DoesNotExist, Account.MultipleObjectsReturned):
                pass
        return translation.get_language_from_request(request)

    def process_request(self, request):
        translation.activate(self.get_language_for_user(request))
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response

from django.utils.cache import patch_vary_headers
from django.utils import translation
from account.models import Account

class LocaleMiddleware(object):
    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context depending on the user's account. This allows pages
    to be dynamically translated to the language the user desires
    (if the language is available, of course). 
    """

    def get_language_for_user(self, request):
        if request.user.is_authenticated():
            try:
                account = Account.objects.get(user=request.user)
                return account.language
            except Account.DoesNotExist:
                pass
        return translation.get_language_from_request(request)

    def process_request(self, request):
        translation.activate(self.get_language_for_user(request))
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response

Throw 500 error on multiple account in LocaleMiddleware so we can fix them.
Throw 500 error on multiple account in LocaleMiddleware so we can fix them.

  from django.utils.cache import patch_vary_headers
  from django.utils import translation
  from account.models import Account
  
  class LocaleMiddleware(object):
      """
      This is a very simple middleware that parses a request
      and decides what translation object to install in the current
      thread context depending on the user's account. This allows pages
      to be dynamically translated to the language the user desires
      (if the language is available, of course). 
      """
  
      def get_language_for_user(self, request):
          if request.user.is_authenticated():
              try:
                  account = Account.objects.get(user=request.user)
                  return account.language
-             except (Account.DoesNotExist, Account.MultipleObjectsReturned):
+             except Account.DoesNotExist:
                  pass
          return translation.get_language_from_request(request)
  
      def process_request(self, request):
          translation.activate(self.get_language_for_user(request))
          request.LANGUAGE_CODE = translation.get_language()
  
      def process_response(self, request, response):
          patch_vary_headers(response, ('Accept-Language',))
          response['Content-Language'] = translation.get_language()
          translation.deactivate()
          return response
  
Throw 500 error on multiple account in LocaleMiddleware so we can fix them.
## Code Before:
from django.utils.cache import patch_vary_headers
from django.utils import translation
from account.models import Account

class LocaleMiddleware(object):
    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context depending on the user's account. This allows pages
    to be dynamically translated to the language the user desires
    (if the language is available, of course). 
    """

    def get_language_for_user(self, request):
        if request.user.is_authenticated():
            try:
                account = Account.objects.get(user=request.user)
                return account.language
            except (Account.DoesNotExist, Account.MultipleObjectsReturned):
                pass
        return translation.get_language_from_request(request)

    def process_request(self, request):
        translation.activate(self.get_language_for_user(request))
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response

## Instruction:
Throw 500 error on multiple account in LocaleMiddleware so we can fix them.
## Code After:
from django.utils.cache import patch_vary_headers
from django.utils import translation
from account.models import Account

class LocaleMiddleware(object):
    """
    This is a very simple middleware that parses a request
    and decides what translation object to install in the current
    thread context depending on the user's account. This allows pages
    to be dynamically translated to the language the user desires
    (if the language is available, of course). 
    """

    def get_language_for_user(self, request):
        if request.user.is_authenticated():
            try:
                account = Account.objects.get(user=request.user)
                return account.language
            except Account.DoesNotExist:
                pass
        return translation.get_language_from_request(request)

    def process_request(self, request):
        translation.activate(self.get_language_for_user(request))
        request.LANGUAGE_CODE = translation.get_language()

    def process_response(self, request, response):
        patch_vary_headers(response, ('Accept-Language',))
        response['Content-Language'] = translation.get_language()
        translation.deactivate()
        return response
