app/projrev/views/helpers/access.py
changeset 12 aac4944aca52
parent 8 294ff7ac9cb6
equal deleted inserted replaced
11:dfc069c2d35d 12:aac4944aca52
    10 from functools import wraps
    10 from functools import wraps
    11 
    11 
    12 from django.shortcuts import render_to_response
    12 from django.shortcuts import render_to_response
    13 from django.template import RequestContext
    13 from django.template import RequestContext
    14 
    14 
       
    15 # Dictionary containing access checks for views.
       
    16 # Keys: View function name.
       
    17 # Values: proposer or staff
       
    18 rights = {}
       
    19  
       
    20 def register(access_type):
       
    21   """Function to register access type for a View function
       
    22   """
    15 
    23 
    16 rights = {}
    24   def wrapper(func):
    17 rights['getMicr'] = 'proposer'
    25     """The decorator for registering access checks.
       
    26     """
       
    27 
       
    28     rights['%s.%s' % (func.__module__, func.__name__)] = access_type
       
    29     return func
       
    30 
       
    31   return wrapper
    18 
    32 
    19 def checkAccess(func):
    33 def checkAccess(func):
    20   """ To check the access of the user and then return the appropriate function 
    34   """ To check the access of the user and then return the appropriate function 
    21   object.
    35   object.
    22  """
    36  """
    24   @wraps(func)
    38   @wraps(func)
    25   def wrapper(request, *args, **kwargs):
    39   def wrapper(request, *args, **kwargs):
    26     """The decorator for access check.
    40     """The decorator for access check.
    27     """
    41     """
    28 
    42 
    29     user_kind = rights[func.__name__]
    43     user_kind = rights['%s.%s' % (func.__module__, func.__name__)]
    30     user = request.user
    44     user = request.user
    31 
    45 
    32     template = 'projrev/error.html'
    46     template = 'projrev/error.html'
    33     context = {}
    47     context = {}
    34 
    48 
    35     if user.is_authenticated():
    49     if user.is_authenticated():
    36       if user_kind == 'staff':
    50       if user_kind == 'reviewer':
    37         if user.is_staff:
    51         if user.is_staff:
    38           return func(request, *args, **kwargs)
    52           return func(request, *args, **kwargs)
    39         else:
    53         else:
    40           context['not_staff'] = True 
    54           context['not_staff'] = True 
    41           return render_to_response(template, RequestContext(request, context)) 
    55           return render_to_response(template, RequestContext(request, context))