app/projrev/views/helpers/access.py
changeset 8 294ff7ac9cb6
parent 7 3e6916eb3d2c
child 12 aac4944aca52
equal deleted inserted replaced
7:3e6916eb3d2c 8:294ff7ac9cb6
     5 __authors__ = [
     5 __authors__ = [
     6   '"Madhusudan.C.S" <madhusudancs@gmail.com>',
     6   '"Madhusudan.C.S" <madhusudancs@gmail.com>',
     7 ]
     7 ]
     8 
     8 
     9 
     9 
    10 def checkAccess(func, request):
    10 from functools import wraps
       
    11 
       
    12 from django.shortcuts import render_to_response
       
    13 from django.template import RequestContext
       
    14 
       
    15 
       
    16 rights = {}
       
    17 rights['getMicr'] = 'proposer'
       
    18 
       
    19 def checkAccess(func):
    11   """ To check the access of the user and then return the appropriate function 
    20   """ To check the access of the user and then return the appropriate function 
    12 object
    21   object.
    13  """
    22  """
    14   user_kind = rights[func.__name__]
    23 
    15   if user.is_authenticated():
    24   @wraps(func)
    16     if user_kind == 'staff':
    25   def wrapper(request, *args, **kwargs):
    17       if user.is_staff:
    26     """The decorator for access check.
    18         return func(request)
    27     """
    19       else:
    28 
    20         return 
    29     user_kind = rights[func.__name__]
    21         
    30     user = request.user
    22     if user_kind == 'proposer':
    31 
    23       if not user.is_staff:
    32     template = 'projrev/error.html'
    24         return func(request)
    33     context = {}
    25       else:
    34 
    26         return
    35     if user.is_authenticated():
    27     
    36       if user_kind == 'staff':
       
    37         if user.is_staff:
       
    38           return func(request, *args, **kwargs)
       
    39         else:
       
    40           context['not_staff'] = True 
       
    41           return render_to_response(template, RequestContext(request, context)) 
       
    42 
       
    43       if user_kind == 'proposer':
       
    44         if not user.is_staff:
       
    45           return func(request, *args, **kwargs)
       
    46         else:
       
    47           context['not_proposer'] = True
       
    48           return render_to_response(template, RequestContext(request, context))
       
    49     else:
       
    50       context['not_authenticated'] = True
       
    51       return render_to_response(template, RequestContext(request, context))
       
    52 
       
    53   return wrapper