app/soc/views/helper/decorators.py
author Sverre Rabbelier <sverre@rabbelier.nl>
Thu, 04 Jun 2009 22:26:24 +0200
changeset 2392 b53e1cdb0398
parent 2181 3ebe6cb63aa0
child 2393 7fe19ebfe0d3
permissions -rw-r--r--
Make the maintenance page a hardcoded response Show the maintenance message on the requested page, instead of redirecting to a specific url which then displays a page with the maintenance notice.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
#
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
"""Views decorators.
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
"""
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
__authors__ = [
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
987
6fd5c561b446 Cache sidebar with a simple caching API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 972
diff changeset
    22
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
  ]
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
import logging
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
from functools import wraps
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
from google.appengine.runtime import DeadlineExceededError
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    31
from google.appengine.runtime.apiproxy_errors import CapabilityDisabledError
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    32
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
from django import http
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    34
from django.utils.translation import ugettext
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    36
from soc.logic import dicts
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    37
from soc.views.helper import responses
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    38
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    39
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    40
DEF_DOWN_FOR_MAINTENANCE_MSG = ugettext("Down for maintenance")
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    41
DEF_IN_UNEXPECTED_MAINTENANCE_MSG = ugettext(
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    42
      "Down for unexpected maintenance.")
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    43
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    44
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    45
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    46
class Error(Exception):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
    47
  """Base class for all exceptions raised by this module.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
    48
  """
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
    49
  
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    50
  pass
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    51
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    53
def maintenance(request):
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    54
  """Returns a 'down for maintenance' view.
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    55
  """
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    56
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    57
  context = responses.getUniversalContext(request)
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    58
  context['page_name'] = ugettext('Maintenance')
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    59
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    60
  notice = context.pop('site_notice')
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    61
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    62
  if not notice:
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    63
    context['body_content'] = DEF_IN_UNEXPECTED_MAINTENANCE_MSG
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    64
  else:
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    65
    context['body_content'] = notice
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    66
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    67
  context['header_title'] = DEF_DOWN_FOR_MAINTENANCE_MSG
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    68
  context['sidebar_menu_items'] = [
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    69
      {'heading': DEF_DOWN_FOR_MAINTENANCE_MSG,
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    70
       'group': ''},
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    71
      ]
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    72
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    73
  template = 'soc/base.html'
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    74
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    75
  return responses.respond(request, template, context=context)
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    76
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    77
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
def view(func):
499
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 367
diff changeset
    79
  """Decorator that insists that exceptions are handled by view.
d22e4fe8e64b Fix missing dots in doc strings and some other doc string corrections.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 367
diff changeset
    80
  """
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    81
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    82
  from soc.logic.helper import timeline
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    83
  from soc.logic.models.site import logic as site_logic
1592
4465cda55182 Add a maintenance end date
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1585
diff changeset
    84
  from soc.logic.models.user import logic as user_logic
1761
cf39e8705fac Except and properly handle Errors in view decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1632
diff changeset
    85
  from soc.views import out_of_band
cf39e8705fac Except and properly handle Errors in view decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1632
diff changeset
    86
  from soc.views.helper import responses
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    87
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
  @wraps(func)
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    89
  def view_wrapper(request, *args, **kwds):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
    90
    """View decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
    91
    """
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    92
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
    try:
2181
3ebe6cb63aa0 Place the getSingleton call in view decorator in the try
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
    94
      site = site_logic.getSingleton()
3ebe6cb63aa0 Place the getSingleton call in view decorator in the try
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
    95
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    96
      # don't redirect admins
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    97
      redirect = not user_logic.isDeveloper()
2181
3ebe6cb63aa0 Place the getSingleton call in view decorator in the try
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
    98
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    99
      if redirect and timeline.isActivePeriod(site, 'maintenance'):
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
   100
        return maintenance(request)
2181
3ebe6cb63aa0 Place the getSingleton call in view decorator in the try
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
   101
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
   102
      return func(request, *args, **kwds)
1632
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   103
    except DeadlineExceededError, exception:
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   104
      logging.exception(exception)
1582
f2f352ad193f Redirect to an error page when a GAE exception occurs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   105
      return http.HttpResponseRedirect('/soc/content/deadline_exceeded.html')
1632
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   106
    except CapabilityDisabledError, exception:
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   107
      logging.exception(exception)
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
   108
      # assume the site is in maintenance if we get CDE
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
   109
      return http.HttpResponseRedirect('/maintenance')
1632
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   110
    except MemoryError, exception:
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   111
      logging.exception(exception)
1582
f2f352ad193f Redirect to an error page when a GAE exception occurs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   112
      return http.HttpResponseRedirect('/soc/content/memory_error.html')
1632
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   113
    except AssertionError, exception:
73a9bc6a85d9 Remove unused imports and rename too short variable names in soc.views.helper.decorators module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1592
diff changeset
   114
      logging.exception(exception)
1582
f2f352ad193f Redirect to an error page when a GAE exception occurs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1454
diff changeset
   115
      return http.HttpResponseRedirect('/soc/content/assertion_error.html')
1761
cf39e8705fac Except and properly handle Errors in view decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1632
diff changeset
   116
    except out_of_band.Error, error:
cf39e8705fac Except and properly handle Errors in view decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1632
diff changeset
   117
      return responses.errorResponse(error, request)
365
74dec172944e Create decorators module in view/helper and add view decorator that catches exceptions like DeadlineExceededError, MemoryError, AssertionError (this code is being moved from respond function). Add view decorator to all view functions. In addition remove not needed imports from all affected files and fix too long lines.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   118
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   119
  return view_wrapper
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   120
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   121
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   122
def merge_params(func):
906
275d2f457c50 Fix missing dots in soc.views.helper.decoratos module docstrings.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 873
diff changeset
   123
  """Decorator that merges 'params' with self._params.
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   124
  """
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   125
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   126
  @wraps(func)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   127
  def wrapper(self, *args, **kwargs):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
   128
    """Decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
   129
    """
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   130
    params = kwargs.get('params', {})
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   131
    kwargs['params'] = dicts.merge(params, self._params)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   132
    return func(self, *args, **kwargs)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   133
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   134
  return wrapper
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   135
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   136
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   137
def check_access(func):
906
275d2f457c50 Fix missing dots in soc.views.helper.decoratos module docstrings.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 873
diff changeset
   138
  """This decorator does access checks for the specified view method.
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   139
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   140
  The rights dictionary is extracted from 'params', or, if either 'params' or
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   141
  'rights' do not exist, from self._params['rights'].
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   142
  """
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   143
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   144
  # Do not pollute helper.decorators with access specific imports
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   145
  from soc.views import out_of_band
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   146
  from soc.views import helper
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   147
  from soc.views.helper import responses
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   148
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   149
  @wraps(func)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   150
  def wrapper(self, request, access_type, *args, **kwargs):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
   151
    """Decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1761
diff changeset
   152
    """
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   153
    params = kwargs.get('params', {})
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   154
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   155
    # Try to extract rights
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   156
    if 'rights' in params:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   157
      rights = params['rights']
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   158
    else:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   159
      rights = self._params['rights']
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   160
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   161
    check_kwargs = kwargs.copy()
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   162
    context = responses.getUniversalContext(request)
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   163
    responses.useJavaScript(context, self._params['js_uses_all'])
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   164
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   165
    id = context['account']
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   166
    user = context['user']
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   167
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   168
    check_kwargs['GET'] = request.GET
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   169
    check_kwargs['POST'] = request.POST
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   170
    check_kwargs['context'] = context
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   171
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   172
    # reset and pre-fill the Checker's cache
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   173
    rights.setCurrentUser(id, user)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   174
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   175
    # Do the access check dance
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   176
    try:
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   177
      rights.checkAccess(access_type, check_kwargs)
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   178
    except out_of_band.Error, error:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   179
      return helper.responses.errorResponse(error, request)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   180
    return func(self, request, access_type, *args, **kwargs)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   181
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   182
  return wrapper