<s> import re <EOL> from django . conf import settings <EOL> from django . http import HttpResponseRedirect <EOL> from django . utils import translation <EOL> from django . utils . cache import patch_vary_headers <EOL> from django . utils . http import urlquote <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from account . models import Account <EOL> class LocaleMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def get_language_for_user ( self , request ) : <EOL> if request . user . is_authenticated ( ) : <EOL> try : <EOL> account = Account . objects . get ( user = request . user ) <EOL> return account . language <EOL> except Account . DoesNotExist : <EOL> pass <EOL> return translation . get_language_from_request ( request ) <EOL> def process_request ( self , request ) : <EOL> translation . activate ( self . get_language_for_user ( request ) ) <EOL> request . LANGUAGE_CODE = translation . get_language ( ) <EOL> def process_response ( self , request , response ) : <EOL> patch_vary_headers ( response , ( "<STR_LIT>" , ) ) <EOL> response [ "<STR_LIT>" ] = translation . get_language ( ) <EOL> translation . deactivate ( ) <EOL> return response <EOL> class AuthenticatedMiddleware ( object ) : <EOL> def __init__ ( self , login_url = None , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> if login_url is None : <EOL> login_url = settings . LOGIN_URL <EOL> self . redirect_field_name = redirect_field_name <EOL> self . login_url = login_url <EOL> self . exemptions = [ <EOL> r"<STR_LIT>" % settings . MEDIA_URL , <EOL> r"<STR_LIT>" % settings . STATIC_URL , <EOL> r"<STR_LIT>" % login_url , <EOL> ] + getattr ( settings , "<STR_LIT>" , [ ] ) <EOL> def process_request ( self , request ) : <EOL> for exemption in self . exemptions : <EOL> if re . match ( exemption , request . path ) : <EOL> return None <EOL> if not request . user . is_authenticated ( ) : <EOL> path = urlquote ( request . get_full_path ( ) ) <EOL> tup = ( self . login_url , self . redirect_field_name , path ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" % tup ) </s>
