35346c8c8550109adfa3cc2560472e797df6fa6f
[redakcja.git] / apps / maintenancemode / middleware.py
1 from django.conf import settings
2 from django.core import urlresolvers
3
4 # This is django-maintancemode v. 0.9.2
5
6 from django.conf.urls import defaults
7 defaults.handler503 = 'maintenancemode.views.defaults.temporary_unavailable'
8 defaults.__all__.append('handler503')
9
10 from maintenancemode.conf.settings import MAINTENANCE_MODE
11 import traceback
12     
13 class MaintenanceModeMiddleware(object):
14     def process_request(self, request):
15         # Allow access if middleware is not activated
16         if not MAINTENANCE_MODE:
17             return None
18
19         # Allow access if remote ip is in INTERNAL_IPS
20         if request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
21             return None
22         
23         # Allow acess if the user doing the request is logged in and a
24         # staff member.
25         if hasattr(request, 'user') and request.user.is_staff:
26             return None
27         
28         # Otherwise show the user the 503 page
29         resolver = urlresolvers.get_resolver(None)
30         
31         callback, param_dict = resolver._resolve_special('503')
32         return callback(request, **param_dict)
33     
34     def process_exception(self, request, exception):
35         tb_text = traceback.format_exc()
36         url = request.build_absolute_uri()
37         request.META['wsgi.errors'].write(url + '\n' + str(tb_text) + '\n')