app/soc/middleware/maintenance.py
changeset 2902 23e00d707255
child 2905 378833eb5a95
equal deleted inserted replaced
2901:6c8ba67289a6 2902:23e00d707255
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2009 the Melange authors.
       
     4 #
       
     5 # Licensed under the Apache License, Version 2.0 (the "License");
       
     6 # you may not use this file except in compliance with the License.
       
     7 # You may obtain a copy of the License at
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 # Unless required by applicable law or agreed to in writing, software
       
    12 # distributed under the License is distributed on an "AS IS" BASIS,
       
    13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    14 # See the License for the specific language governing permissions and
       
    15 # limitations under the License.
       
    16 
       
    17 """Middleware to handle exceptions.
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
       
    22   ]
       
    23 
       
    24 
       
    25 from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
       
    26 
       
    27 from django import http
       
    28 from django.utils.translation import ugettext
       
    29 
       
    30 from soc.views.helper import responses
       
    31 
       
    32 
       
    33 DEF_DOWN_FOR_MAINTENANCE_MSG = ugettext("Down for maintenance")
       
    34 DEF_IN_UNEXPECTED_MAINTENANCE_MSG = ugettext(
       
    35       "Down for unexpected maintenance.")
       
    36 
       
    37 
       
    38 class MaintenanceMiddleware(object):
       
    39   """Middleware to handle maintenance mode.
       
    40   """
       
    41 
       
    42   def maintenance(self, request):
       
    43     """Returns a 'down for maintenance' view.
       
    44     """
       
    45 
       
    46     context = responses.getUniversalContext(request)
       
    47     context['page_name'] = ugettext('Maintenance')
       
    48 
       
    49     notice = context.pop('site_notice')
       
    50 
       
    51     if not notice:
       
    52       context['body_content'] = DEF_IN_UNEXPECTED_MAINTENANCE_MSG
       
    53     else:
       
    54       context['body_content'] = notice
       
    55 
       
    56     context['header_title'] = DEF_DOWN_FOR_MAINTENANCE_MSG
       
    57     context['sidebar_menu_items'] = [
       
    58         {'heading': DEF_DOWN_FOR_MAINTENANCE_MSG,
       
    59          'group': ''},
       
    60         ]
       
    61 
       
    62     template = 'soc/base.html'
       
    63 
       
    64     return responses.respond(request, template, context=context)
       
    65 
       
    66   def process_request(self, request):
       
    67     context = responses.getUniversalContext(request)
       
    68 
       
    69     if not context['is_admin'] and context['in_maintenance']:
       
    70       return self.maintenance(request)
       
    71 
       
    72   def process_exception(self, request, exception):
       
    73     if isinstance(exception, CapabilityDisabledError):
       
    74       # assume the site is in maintenance if we get CDE
       
    75       return maintenance(request)
       
    76 
       
    77     # let the exception handling middleware handle it
       
    78     return None