Add management command for localizing contrib apps.
[fnpdjango.git] / fnpdjango / middleware.py
1 from django.utils import translation
2 from django.conf import settings
3 from django.http import Http404
4 from . import app_settings
5
6
7 class SetRemoteAddrFromXRealIP(object):
8     """Sets REMOTE_ADDR from the X-Real-IP header, as set by Nginx."""
9     def process_request(self, request):
10         if app_settings.REALIP:
11             try:
12                 request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP']
13             except KeyError:
14                 pass
15         return None
16
17
18 class URLLocaleMiddleware(object):
19     """Decides which translation to use, based on path only."""
20
21     def process_request(self, request):
22         language = translation.get_language_from_path(request.path_info)
23         if language == settings.LANGUAGE_CODE:
24             raise Http404
25         if language:
26             translation.activate(language)
27         request.LANGUAGE_CODE = translation.get_language()
28
29     def process_response(self, request, response):
30         language = translation.get_language()
31         translation.deactivate()
32         if 'Content-Language' not in response:
33             response['Content-Language'] = language
34         return response