Revision 18ffbee1 snf-astakos-app/astakos/im/activation_backends.py
b/snf-astakos-app/astakos/im/activation_backends.py | ||
---|---|---|
35 | 35 |
from django.core.exceptions import ImproperlyConfigured |
36 | 36 |
from django.core.mail import send_mail |
37 | 37 |
from django.template.loader import render_to_string |
38 |
from django.utils.translation import ugettext as _ |
|
39 | 38 |
from django.contrib.sites.models import Site |
40 | 39 |
from django.contrib import messages |
41 |
from django.db import transaction |
|
42 | 40 |
from django.core.urlresolvers import reverse |
41 |
from django.utils.translation import ugettext as _ |
|
42 |
from django.db import transaction |
|
43 | 43 |
|
44 | 44 |
from urlparse import urljoin |
45 | 45 |
|
... | ... | |
57 | 57 |
|
58 | 58 |
def get_backend(request): |
59 | 59 |
""" |
60 |
Returns an instance of a registration backend,
|
|
60 |
Returns an instance of an activation backend,
|
|
61 | 61 |
according to the INVITATIONS_ENABLED setting |
62 | 62 |
(if True returns ``astakos.im.activation_backends.InvitationsBackend`` and if False |
63 | 63 |
returns ``astakos.im.activation_backends.SimpleBackend``). |
... | ... | |
71 | 71 |
try: |
72 | 72 |
mod = import_module(module) |
73 | 73 |
except ImportError, e: |
74 |
raise ImproperlyConfigured('Error loading registration backend %s: "%s"' % (module, e))
|
|
74 |
raise ImproperlyConfigured('Error loading activation backend %s: "%s"' % (module, e))
|
|
75 | 75 |
try: |
76 | 76 |
backend_class = getattr(mod, backend_class_name) |
77 | 77 |
except AttributeError: |
78 |
raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr))
|
|
78 |
raise ImproperlyConfigured('Module "%s" does not define a activation backend named "%s"' % (module, attr))
|
|
79 | 79 |
return backend_class(request) |
80 | 80 |
|
81 | 81 |
class SignupBackend(object): |
... | ... | |
88 | 88 |
|
89 | 89 |
class InvitationsBackend(SignupBackend): |
90 | 90 |
""" |
91 |
A registration backend which implements the following workflow: a user
|
|
91 |
A activation backend which implements the following workflow: a user
|
|
92 | 92 |
supplies the necessary registation information, if the request contains a valid |
93 | 93 |
inivation code the user is automatically activated otherwise an inactive user |
94 | 94 |
account is created and the user is going to receive an email as soon as an |
... | ... | |
110 | 110 |
invitation = self.invitation |
111 | 111 |
initial_data = self.get_signup_initial_data(provider) |
112 | 112 |
prefix = 'Invited' if invitation else '' |
113 |
main = provider.capitalize() if provider == 'local' else 'ThirdParty'
|
|
113 |
main = provider.capitalize() |
|
114 | 114 |
suffix = 'UserCreationForm' |
115 | 115 |
formclass = '%s%s%s' % (prefix, main, suffix) |
116 | 116 |
ip = self.request.META.get('REMOTE_ADDR', |
... | ... | |
119 | 119 |
|
120 | 120 |
def get_signup_initial_data(self, provider): |
121 | 121 |
""" |
122 |
Returns the necassary registration form depending the user is invited or not
|
|
122 |
Returns the necassary activation form depending the user is invited or not
|
|
123 | 123 |
|
124 | 124 |
Throws Invitation.DoesNotExist in case ``code`` is not valid. |
125 | 125 |
""" |
... | ... | |
131 | 131 |
# create a tmp user with the invitation realname |
132 | 132 |
# to extract first and last name |
133 | 133 |
u = AstakosUser(realname = invitation.realname) |
134 |
print '>>>', invitation, invitation.inviter |
|
134 | 135 |
initial_data = {'email':invitation.username, |
135 | 136 |
'inviter':invitation.inviter.realname, |
136 | 137 |
'first_name':u.first_name, |
... | ... | |
155 | 156 |
return True |
156 | 157 |
return False |
157 | 158 |
|
158 |
@transaction.commit_manually |
|
159 | 159 |
def handle_activation(self, user, verification_template_name='im/activation_email.txt', greeting_template_name='im/welcome_email.txt', admin_email_template_name='im/admin_notification.txt'): |
160 | 160 |
""" |
161 | 161 |
Initially creates an inactive user account. If the user is preaccepted |
... | ... | |
166 | 166 |
The method uses commit_manually decorator in order to ensure the user |
167 | 167 |
will be created only if the procedure has been completed successfully. |
168 | 168 |
""" |
169 |
result = None |
|
170 | 169 |
try: |
170 |
if user.is_active: |
|
171 |
return RegistationCompleted() |
|
171 | 172 |
if self._is_preaccepted(user): |
172 | 173 |
if user.email_verified: |
173 | 174 |
activate(user, greeting_template_name) |
174 |
result = RegistationCompleted()
|
|
175 |
return RegistationCompleted()
|
|
175 | 176 |
else: |
176 | 177 |
send_verification(user, verification_template_name) |
177 |
result = VerificationSent()
|
|
178 |
return VerificationSent()
|
|
178 | 179 |
else: |
179 | 180 |
send_admin_notification(user, admin_email_template_name) |
180 |
result = NotificationSent()
|
|
181 |
return NotificationSent()
|
|
181 | 182 |
except Invitation.DoesNotExist, e: |
182 | 183 |
raise InvitationCodeError() |
183 |
else: |
|
184 |
return result |
|
184 |
except BaseException, e: |
|
185 |
logger.exception(e) |
|
186 |
raise e |
|
185 | 187 |
|
186 | 188 |
class SimpleBackend(SignupBackend): |
187 | 189 |
""" |
188 |
A registration backend which implements the following workflow: a user
|
|
190 |
A activation backend which implements the following workflow: a user
|
|
189 | 191 |
supplies the necessary registation information, an incative user account is |
190 | 192 |
created and receives an email in order to activate his/her account. |
191 | 193 |
""" |
... | ... | |
238 | 240 |
* DEFAULT_CONTACT_EMAIL: service support email |
239 | 241 |
* DEFAULT_FROM_EMAIL: from email |
240 | 242 |
""" |
241 |
result = None |
|
242 |
if not self._is_preaccepted(user): |
|
243 |
send_admin_notification(user, admin_email_template_name) |
|
244 |
result = NotificationSent() |
|
243 |
try: |
|
244 |
if user.is_active: |
|
245 |
return RegistrationCompeted() |
|
246 |
if not self._is_preaccepted(user): |
|
247 |
send_admin_notification(user, admin_email_template_name) |
|
248 |
return NotificationSent() |
|
249 |
else: |
|
250 |
send_verification(user, email_template_name) |
|
251 |
return VerificationSend() |
|
252 |
except SendEmailError, e: |
|
253 |
transaction.rollback() |
|
254 |
raise e |
|
255 |
except BaseException, e: |
|
256 |
logger.exception(e) |
|
257 |
raise e |
|
245 | 258 |
else: |
246 |
send_verification(user, email_template_name) |
|
247 |
result = VerificationSend() |
|
248 |
return result |
|
259 |
transaction.commit() |
|
249 | 260 |
|
250 | 261 |
class ActivationResult(object): |
251 | 262 |
def __init__(self, message): |
... | ... | |
266 | 277 |
class RegistationCompleted(ActivationResult): |
267 | 278 |
def __init__(self): |
268 | 279 |
message = _('Registration completed. You can now login.') |
269 |
super(RegistationCompleted, self).__init__(message) |
|
270 |
|
|
271 |
|
|
280 |
super(RegistationCompleted, self).__init__(message) |
Also available in: Unified diff