~pidgeon690/pidge-groups/trunk

« back to all changes in this revision

Viewing changes to Apps/basic/funcs.py

  • Committer: Fergus Ross Ferrier
  • Date: 2009-05-25 22:10:29 UTC
  • mfrom: (273.2.4 refactor+user)
  • Revision ID: hello@fergusrossferrier.co.uk-20090525221029-gqdycg3rfhxujqpz
Merged user-refactor fun.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
class PidgeException(Exception): pass
11
11
 
12
12
try:
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.
16
 
 
17
 
 
18
 
def MustLogin(fn):
19
 
    """
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. 
22
 
    
23
 
    Use thus on the line before the def of a view:
24
 
    @MustLogin
25
 
    """
26
 
 
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.
 
16
 
 
17
def MustLogin(f):
 
18
    def decorator(request, *args, **kwargs):
 
19
        u = request.user
 
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]))
31
 
        else:
32
 
            return fn(request, *args, **kwargs)
33
 
    return wrapped_fn
34
 
    
35
 
def MustLoginMessage(message):
36
 
    """
37
 
    @MustLoginMessage("You can only add comments if you are logged in.")
38
 
 
39
 
    """
40
 
    def decorate(view_func):
41
 
        return _CheckLogin(view_func, message)
42
 
    return decorate
43
 
 
44
 
class _CheckLogin(object):
45
 
    """
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
48
 
    is invoked. 
49
 
    
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.
53
 
    """
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)
 
25
 
 
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)
58
 
        
59
 
    def __get__(self, obj, cls = None):
60
 
        view_func = self.view_func.__get__(obj, cls)
61
 
        return _CheckLogin(view_func, self.message)
62
 
    
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]))
67
 
        else:
68
 
            return self.view_func(request, *args, **kwargs)
69
 
            
 
29
    def __call__(self, f):
 
30
        def decorator(request, *args, **kwargs):
 
31
            u = request.user
 
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)
70
37
 
71
38
def SendEmail(to, subject="", sender="", message="", headers={}):
72
39
    if not sender:
74
41
    
75
42
    from django.core.mail import EmailMessage
76
43
    email = EmailMessage(subject=subject, from_email=sender, body=message, to=to, headers=headers)
77
 
    email.send()
 
 
b'\\ No newline at end of file'
 
44
    email.send()