app/soc/views/helper/decorators.py
author Daniel Hans <Daniel.M.Hans@gmail.com>
Sun, 27 Sep 2009 23:31:39 +0200
changeset 2980 cbfd8e12527a
parent 2904 395cff83dc83
child 2984 2b7ea1f2629a
permissions -rw-r--r--
Add task and iterative_task decorator Also add getBatchOfData method to the base logic and tasks logic module. Reviewed-by: Sverre Rabbelier
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 django import http
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    31
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
    32
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    33
from soc.logic import dicts
2980
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
    34
from soc.logic import tasks
2392
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    35
from soc.views.helper import responses
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    36
b53e1cdb0398 Make the maintenance page a hardcoded response
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2181
diff changeset
    37
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    38
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
    39
  """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
    40
  """
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
    41
  
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    42
  pass
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    43
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
    44
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
    45
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
    46
  """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
    47
  """
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    48
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
    49
  @wraps(func)
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    50
  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
    51
    """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
    52
    """
1585
06fb5950cb03 Make it possible to put the site in maintenance mode
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1582
diff changeset
    53
2904
395cff83dc83 Enable the new middleware and empty the view decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2834
diff changeset
    54
    return func(request, *args, **kwds)
2393
7fe19ebfe0d3 Display caught errors on the page itself
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2392
diff changeset
    55
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    56
  return view_wrapper
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    57
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    58
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    59
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
    60
  """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
    61
  """
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    62
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    63
  @wraps(func)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    64
  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
    65
    """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
    66
    """
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    67
    params = kwargs.get('params', {})
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    68
    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
    69
    return func(self, *args, **kwargs)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    70
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    71
  return wrapper
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    72
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    73
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    74
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
    75
  """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
    76
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    77
  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
    78
  '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
    79
  """
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    80
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    81
  # 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
    82
  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
    83
  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
    84
  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
    85
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    86
  @wraps(func)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    87
  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
    88
    """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
    89
    """
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    90
    params = kwargs.get('params', {})
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    91
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    92
    # Try to extract rights
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    93
    if 'rights' in params:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    94
      rights = params['rights']
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    95
    else:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    96
      rights = self._params['rights']
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
    97
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
    98
    check_kwargs = kwargs.copy()
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
    99
    context = responses.getUniversalContext(request)
1454
37eb949c3267 Add some missing useJavaScript calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   100
    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
   101
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   102
    id = context['account']
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   103
    user = context['user']
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   104
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   105
    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
   106
    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
   107
    check_kwargs['context'] = context
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 906
diff changeset
   108
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   109
    # 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
   110
    rights.setCurrentUser(id, user)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   111
873
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   112
    # Do the access check dance
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   113
    try:
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 987
diff changeset
   114
      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
   115
    except out_of_band.Error, error:
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   116
      return helper.responses.errorResponse(error, request)
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   117
    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
   118
cbb1d15ffe91 Added a merge_params and check_access decorator
Sverre Rabbelier <srabbelier@gmail.com>
parents: 499
diff changeset
   119
  return wrapper
2980
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   120
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   121
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   122
def task(func):
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   123
  """Task decorator wrapper method
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   124
  """
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   125
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   126
  @wraps(func)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   127
  def wrapper(request, *args, **kwargs):
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   128
    """Decorator wrapper method
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   129
    """
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   130
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   131
    try:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   132
      return func(request, *args, **kwargs)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   133
    except tasks.FatalTaskError, error:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   134
      logging.exception(error)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   135
      return tasks.terminateTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   136
    except Exception, exception:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   137
      logging.exception(exception)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   138
      return tasks.repeatTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   139
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   140
  return wrapper
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   141
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   142
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   143
def iterative_task(func):
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   144
  """Iterative wrapper method
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   145
  """
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   146
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   147
  @wraps(func)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   148
  def wrapper(request, *args, **kwargs):
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   149
    """Decorator wrapper method
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   150
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   151
    Params usage:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   152
      logic: name of the logic for the data model to iterate through
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   153
      filter: a dict for the properties that the entities should have
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   154
      order: a list with the sort order
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   155
      json: json object with additional parameters
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   156
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   157
    Returns:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   158
      Standard http django response
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   159
    """
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   160
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   161
    post_dict = request.POST
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   162
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   163
    if 'logic' not in post_dict:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   164
       return tasks.terminateTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   165
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   166
    _temp = __import__(post_dict['logic'], globals(), locals(), ['logic'], -1)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   167
    logic = _temp.logic
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   168
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   169
    filter = None
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   170
    if 'filter' in post_dict:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   171
      filter = simplejson.loads(post_dict['filter'])
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   172
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   173
    order = None
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   174
    if 'order' in post_dict:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   175
      order = simplejson.loads(post_dict['order'])
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   176
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   177
    start_key = None
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   178
    if 'next_key' in post_dict:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   179
      start_key = db.Key(post_dict['start_key'])
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   180
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   181
    json = None
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   182
    if 'json' in post_dict:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   183
      json = post_dict['json']
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   184
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   185
    entities, start_key = logic.getBatchOfData(filter, order, start_key)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   186
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   187
    try:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   188
      new_json = func(request, entities=entities, json=json, *args, **kwargs)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   189
    except tasks.FatalTaskError, error:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   190
      logging.error(error)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   191
      return tasks.terminateTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   192
    except Exception, exception:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   193
      logging.error(exception)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   194
      return tasks.repeatTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   195
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   196
    if start_key is None:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   197
      logging.debug('Task sucessfully completed')
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   198
    else:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   199
      context = post_dict.copy()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   200
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   201
      if 'json' in context:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   202
        del context['json']
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   203
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   204
      context.update({'start_key': start_key})
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   205
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   206
      if new_json is not None:
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   207
        context.update({'json': new_json})
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   208
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   209
      tasks.startTask(url=request.path, context=context)
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   210
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   211
    return tasks.terminateTask()
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   212
cbfd8e12527a Add task and iterative_task decorator
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 2904
diff changeset
   213
  return wrapper