10
10
class PidgeException(Exception): pass
13
from functools import update_wrapper
13
from functools import wraps
14
14
except ImportError:
15
from django.utils.functional import update_wrapper # Python 2.3, 2.4 fallback.
20
A decorator for a view, that redirects to the login page
21
if the view is being called when the user is not signed in.
23
Use thus on the line before the def of a view:
27
def wrapped_fn(request, *args, **kwargs):
28
if not request.session.get('loggedin'):
15
from django.utils.functional import wraps # Python 2.3, 2.4 fallback.
18
def decorator(request, *args, **kwargs):
20
if not u.is_authenticated():
29
21
request.session['login_message'] = "You Must Login to Access This Page"
30
return HttpResponseRedirect(reverse('Users.views.Login', args=[request.path]))
32
return fn(request, *args, **kwargs)
35
def MustLoginMessage(message):
37
@MustLoginMessage("You can only add comments if you are logged in.")
40
def decorate(view_func):
41
return _CheckLogin(view_func, message)
44
class _CheckLogin(object):
46
Class that checks that the user is logged in, redirecting to
47
the log-in page if necessary. If the test is passed, the view function
50
We use a class here so that we can define __get__. This way, when a
51
_CheckLogin object is used as a method decorator, the view function
52
is properly bound to its instance.
54
def __init__(self, view_func, message=None):
22
return HttpResponseRedirect(reverse('auth_login'))
23
return f(request, *args, **kwargs)
24
return wraps(f)(decorator)
26
class MustLoginMessage(object):
27
def __init__(self, message):
55
28
self.message = message
56
self.view_func = view_func
57
update_wrapper(self, view_func)
59
def __get__(self, obj, cls = None):
60
view_func = self.view_func.__get__(obj, cls)
61
return _CheckLogin(view_func, self.message)
63
def __call__(self, request, *args, **kwargs):
64
if not request.session.get('loggedin'):
65
request.session['login_message'] = self.message
66
return HttpResponseRedirect(reverse('Users.views.Login', args=[request.path]))
68
return self.view_func(request, *args, **kwargs)
29
def __call__(self, f):
30
def decorator(request, *args, **kwargs):
32
if not u.is_authenticated():
33
request.session['login_message'] = self.message
34
return HttpResponseRedirect(reverse('auth_login'))
35
return f(request, *args, **kwargs)
36
return wraps(f)(decorator)
71
38
def SendEmail(to, subject="", sender="", message="", headers={}):