Revision 111f3da6 snf-astakos-app/astakos/im/functions.py
b/snf-astakos-app/astakos/im/functions.py | ||
---|---|---|
40 | 40 |
from django.core.urlresolvers import reverse |
41 | 41 |
from django.core.exceptions import ValidationError |
42 | 42 |
from django.template import Context, loader |
43 |
from django.contrib.auth import login as auth_login, logout as auth_logout |
|
44 |
from django.http import HttpRequest |
|
43 | 45 |
|
44 | 46 |
from urllib import quote |
45 | 47 |
from urlparse import urljoin |
46 | 48 |
from smtplib import SMTPException |
47 | 49 |
from datetime import datetime |
50 |
from functools import wraps |
|
48 | 51 |
|
49 |
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL |
|
52 |
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, \ |
|
53 |
SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL, LOGGING_LEVEL |
|
50 | 54 |
from astakos.im.models import Invitation, AstakosUser |
51 | 55 |
|
52 | 56 |
logger = logging.getLogger(__name__) |
53 | 57 |
|
58 |
def logged(func, msg): |
|
59 |
@wraps(func) |
|
60 |
def with_logging(*args, **kwargs): |
|
61 |
email = '' |
|
62 |
user = None |
|
63 |
if len(args) == 2 and isinstance(args[1], AstakosUser): |
|
64 |
user = args[1] |
|
65 |
elif len(args) == 1 and isinstance(args[0], HttpRequest): |
|
66 |
request = args[0] |
|
67 |
user = request.user |
|
68 |
email = user.email if user and user.is_authenticated() else '' |
|
69 |
r = func(*args, **kwargs) |
|
70 |
logger._log(LOGGING_LEVEL, msg % email, []) |
|
71 |
return r |
|
72 |
return with_logging |
|
73 |
|
|
74 |
login = logged(auth_login, '%s logged in.') |
|
75 |
logout = logged(auth_logout, '%s logged out.') |
|
76 |
|
|
54 | 77 |
def send_verification(user, template_name='im/activation_email.txt'): |
55 | 78 |
""" |
56 | 79 |
Send email to user to verify his/her email and activate his/her account. |
... | ... | |
73 | 96 |
logger.exception(e) |
74 | 97 |
raise SendVerificationError() |
75 | 98 |
else: |
76 |
logger.info('Sent activation %s', user) |
|
99 |
msg = 'Sent activation %s' % user.email |
|
100 |
logger._log(LOGGING_LEVEL, msg, []) |
|
77 | 101 |
|
78 | 102 |
def send_activation(user, template_name='im/activation_email.txt'): |
79 | 103 |
send_verification(user, template_name) |
... | ... | |
100 | 124 |
logger.exception(e) |
101 | 125 |
raise SendNotificationError() |
102 | 126 |
else: |
103 |
logger.info('Sent admin notification for user %s', user) |
|
127 |
msg = 'Sent admin notification for user %s' % user.email |
|
128 |
logger._log(LOGGING_LEVEL, msg, []) |
|
104 | 129 |
|
105 | 130 |
def send_invitation(invitation, template_name='im/invitation.txt'): |
106 | 131 |
""" |
... | ... | |
123 | 148 |
logger.exception(e) |
124 | 149 |
raise SendInvitationError() |
125 | 150 |
else: |
126 |
logger.info('Sent invitation %s', invitation) |
|
151 |
msg = 'Sent invitation %s' % invitation |
|
152 |
logger._log(LOGGING_LEVEL, msg, []) |
|
127 | 153 |
|
128 | 154 |
def send_greeting(user, email_template_name='im/welcome_email.txt'): |
129 | 155 |
""" |
... | ... | |
145 | 171 |
logger.exception(e) |
146 | 172 |
raise SendGreetingError() |
147 | 173 |
else: |
148 |
logger.info('Sent greeting %s', user) |
|
174 |
msg = 'Sent greeting %s' % user.email |
|
175 |
logger._log(LOGGING_LEVEL, msg, []) |
|
149 | 176 |
|
150 | 177 |
def send_feedback(msg, data, user, email_template_name='im/feedback_mail.txt'): |
151 | 178 |
subject = _("Feedback from %s alpha2 testing" % SITENAME) |
... | ... | |
161 | 188 |
logger.exception(e) |
162 | 189 |
raise SendFeedbackError() |
163 | 190 |
else: |
164 |
logger.info('Sent feedback from %s', user.email) |
|
191 |
msg = 'Sent feedback from %s' % user.email |
|
192 |
logger._log(LOGGING_LEVEL, msg, []) |
|
165 | 193 |
|
166 | 194 |
def send_change_email(ec, request, email_template_name='registration/email_change_email.txt'): |
167 | 195 |
try: |
... | ... | |
177 | 205 |
logger.exception(e) |
178 | 206 |
raise ChangeEmailError() |
179 | 207 |
else: |
180 |
logger.info('Sent change email for %s', ec.user.email) |
|
208 |
msg = 'Sent change email for %s' % ec.user.email |
|
209 |
logger._log(LOGGING_LEVEL, msg, []) |
|
181 | 210 |
|
182 | 211 |
def activate(user, email_template_name='im/welcome_email.txt'): |
183 | 212 |
""" |
Also available in: Unified diff