app/projrev/views/helpers/access.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 06 Aug 2009 20:20:36 +0530
changeset 8 294ff7ac9cb6
parent 7 3e6916eb3d2c
child 12 aac4944aca52
permissions -rw-r--r--
Added new set of files.
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
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    15
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    16
rights = {}
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    17
rights['getMicr'] = 'proposer'
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    18
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    19
def checkAccess(func):
7
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
    20
  """ 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
    21
  object.
7
3e6916eb3d2c Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 5
diff changeset
    22
 """
8
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    23
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    24
  @wraps(func)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    25
  def wrapper(request, *args, **kwargs):
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    26
    """The decorator for access check.
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    27
    """
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    28
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    29
    user_kind = rights[func.__name__]
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    30
    user = request.user
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    31
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    32
    template = 'projrev/error.html'
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    33
    context = {}
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    34
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    35
    if user.is_authenticated():
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    36
      if user_kind == 'staff':
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    37
        if user.is_staff:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    38
          return func(request, *args, **kwargs)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    39
        else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    40
          context['not_staff'] = True 
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    41
          return render_to_response(template, RequestContext(request, context)) 
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    42
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    43
      if user_kind == 'proposer':
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    44
        if not user.is_staff:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    45
          return func(request, *args, **kwargs)
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    46
        else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    47
          context['not_proposer'] = True
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    48
          return render_to_response(template, RequestContext(request, context))
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    49
    else:
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    50
      context['not_authenticated'] = True
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    51
      return render_to_response(template, RequestContext(request, context))
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    52
294ff7ac9cb6 Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 7
diff changeset
    53
  return wrapper