app/projrev/views/helpers/access.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Fri, 07 Aug 2009 03:56:08 +0530
changeset 16 bed14c9685a5
parent 12 aac4944aca52
permissions -rw-r--r--
Project model changes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
"""This module contains the access checks
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
"""
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
__authors__ = [
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
  '"Madhusudan.C.S" <madhusudancs@gmail.com>',
88ae12bc6280 Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
]
7
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
     8
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
     9
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    10
from functools import wraps
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    11
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    12
from django.shortcuts import render_to_response
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    13
from django.template import RequestContext
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    14
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    15
# Dictionary containing access checks for views.
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    16
# Keys: View function name.
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    17
# Values: proposer or staff
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    18
rights = {}
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    19
 
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    20
def register(access_type):
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    21
  """Function to register access type for a View function
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    22
  """
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    23
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    24
  def wrapper(func):
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    25
    """The decorator for registering access checks.
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    26
    """
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    27
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    28
    rights['%s.%s' % (func.__module__, func.__name__)] = access_type
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    29
    return func
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    30
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    31
  return wrapper
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    32
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    33
def checkAccess(func):
7
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
    34
  """ To check the access of the user and then return the appropriate function 
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    35
  object.
7
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
    36
 """
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    37
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    38
  @wraps(func)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    39
  def wrapper(request, *args, **kwargs):
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    40
    """The decorator for access check.
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    41
    """
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    42
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    43
    user_kind = rights['%s.%s' % (func.__module__, func.__name__)]
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    44
    user = request.user
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    45
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    46
    template = 'projrev/error.html'
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    47
    context = {}
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    48
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    49
    if user.is_authenticated():
12
aac4944aca52 With ACL registers
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 8
diff changeset
    50
      if user_kind == 'reviewer':
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    51
        if user.is_staff:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    52
          return func(request, *args, **kwargs)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    53
        else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    54
          context['not_staff'] = True 
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    55
          return render_to_response(template, RequestContext(request, context)) 
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    56
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    57
      if user_kind == 'proposer':
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    58
        if not user.is_staff:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    59
          return func(request, *args, **kwargs)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    60
        else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    61
          context['not_proposer'] = True
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    62
          return render_to_response(template, RequestContext(request, context))
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    63
    else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    64
      context['not_authenticated'] = True
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    65
      return render_to_response(template, RequestContext(request, context))
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    66
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    67
  return wrapper