app/soc/views/helper/decorators.py
changeset 2904 395cff83dc83
parent 2834 03a1602c63f1
child 2980 cbfd8e12527a
equal deleted inserted replaced
2903:bf17c6a843dd 2904:395cff83dc83
    25 
    25 
    26 import logging
    26 import logging
    27 
    27 
    28 from functools import wraps
    28 from functools import wraps
    29 
    29 
    30 from google.appengine.runtime import DeadlineExceededError
       
    31 from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
       
    32 
       
    33 from django import http
    30 from django import http
    34 from django.utils.translation import ugettext
    31 from django.utils.translation import ugettext
    35 
    32 
    36 from soc.logic import dicts
    33 from soc.logic import dicts
    37 from soc.modules import callback
       
    38 from soc.views.helper import responses
    34 from soc.views.helper import responses
    39 
       
    40 
       
    41 DEF_DOWN_FOR_MAINTENANCE_MSG = ugettext("Down for maintenance")
       
    42 DEF_IN_UNEXPECTED_MAINTENANCE_MSG = ugettext(
       
    43       "Down for unexpected maintenance.")
       
    44 
       
    45 
    35 
    46 
    36 
    47 class Error(Exception):
    37 class Error(Exception):
    48   """Base class for all exceptions raised by this module.
    38   """Base class for all exceptions raised by this module.
    49   """
    39   """
    50   
    40   
    51   pass
    41   pass
    52 
    42 
    53 
    43 
    54 def maintenance(request):
       
    55   """Returns a 'down for maintenance' view.
       
    56   """
       
    57 
       
    58   context = responses.getUniversalContext(request)
       
    59   context['page_name'] = ugettext('Maintenance')
       
    60 
       
    61   notice = context.pop('site_notice')
       
    62 
       
    63   if not notice:
       
    64     context['body_content'] = DEF_IN_UNEXPECTED_MAINTENANCE_MSG
       
    65   else:
       
    66     context['body_content'] = notice
       
    67 
       
    68   context['header_title'] = DEF_DOWN_FOR_MAINTENANCE_MSG
       
    69   context['sidebar_menu_items'] = [
       
    70       {'heading': DEF_DOWN_FOR_MAINTENANCE_MSG,
       
    71        'group': ''},
       
    72       ]
       
    73 
       
    74   template = 'soc/base.html'
       
    75 
       
    76   return responses.respond(request, template, context=context)
       
    77 
       
    78 
       
    79 def view(func):
    44 def view(func):
    80   """Decorator that insists that exceptions are handled by view.
    45   """Decorator that insists that exceptions are handled by view.
    81   """
    46   """
    82 
       
    83   from soc.views import out_of_band
       
    84 
    47 
    85   @wraps(func)
    48   @wraps(func)
    86   def view_wrapper(request, *args, **kwds):
    49   def view_wrapper(request, *args, **kwds):
    87     """View decorator wrapper method.
    50     """View decorator wrapper method.
    88     """
    51     """
    89 
    52 
    90     core = callback.getCore()
    53     return func(request, *args, **kwds)
    91     core.startNewRequest(request)
       
    92 
       
    93     def view_wrapper_helper():
       
    94       """View wrapper helper that does all the work.
       
    95       """
       
    96 
       
    97       context = responses.getUniversalContext(request)
       
    98 
       
    99       try:
       
   100         if not context['is_admin'] and context['in_maintenance']:
       
   101           return maintenance(request)
       
   102 
       
   103         return func(request, *args, **kwds)
       
   104       except CapabilityDisabledError, exception:
       
   105         logging.exception(exception)
       
   106         # assume the site is in maintenance if we get CDE
       
   107         return maintenance(request)
       
   108       except DeadlineExceededError, exception:
       
   109         template = 'soc/deadline_exceeded.html'
       
   110       except MemoryError, exception:
       
   111         template = 'soc/memory_error.html'
       
   112       except AssertionError, exception:
       
   113         template = 'soc/assertion_error.html'
       
   114       except out_of_band.Error, error:
       
   115         return responses.errorResponse(error, request)
       
   116 
       
   117       logging.exception(exception)
       
   118       return responses.respond(request, template, context=context)
       
   119 
       
   120     result = view_wrapper_helper()
       
   121     core.endRequest(request)
       
   122     return result
       
   123 
    54 
   124   return view_wrapper
    55   return view_wrapper
   125 
    56 
   126 
    57 
   127 def merge_params(func):
    58 def merge_params(func):