Revision 8316698a snf-astakos-app/astakos/im/backends.py
b/snf-astakos-app/astakos/im/backends.py | ||
---|---|---|
48 | 48 |
from astakos.im.models import AstakosUser, Invitation |
49 | 49 |
from astakos.im.forms import * |
50 | 50 |
from astakos.im.util import get_invitation |
51 |
from astakos.im.settings import INVITATIONS_ENABLED, DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, MODERATION_ENABLED, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL |
|
51 |
from astakos.im.settings import INVITATIONS_ENABLED, DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, MODERATION_ENABLED, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL, RE_USER_EMAIL_PATTERNS
|
|
52 | 52 |
|
53 | 53 |
import socket |
54 | 54 |
import logging |
55 |
import re |
|
55 | 56 |
|
56 | 57 |
logger = logging.getLogger(__name__) |
57 | 58 |
|
... | ... | |
78 | 79 |
raise ImproperlyConfigured('Module "%s" does not define a registration backend named "%s"' % (module, attr)) |
79 | 80 |
return backend_class(request) |
80 | 81 |
|
81 |
class InvitationsBackend(object): |
|
82 |
class SignupBackend(object): |
|
83 |
def _is_preaccepted(self, user): |
|
84 |
# return True if user email matches specific patterns |
|
85 |
for pattern in RE_USER_EMAIL_PATTERNS: |
|
86 |
if re.match(pattern, user.email): |
|
87 |
return True |
|
88 |
return False |
|
89 |
|
|
90 |
class InvitationsBackend(SignupBackend): |
|
82 | 91 |
""" |
83 | 92 |
A registration backend which implements the following workflow: a user |
84 | 93 |
supplies the necessary registation information, if the request contains a valid |
... | ... | |
93 | 102 |
""" |
94 | 103 |
self.request = request |
95 | 104 |
self.invitation = get_invitation(request) |
105 |
super(InvitationsBackend, self).__init__() |
|
96 | 106 |
|
97 | 107 |
def get_signup_form(self, provider): |
98 | 108 |
""" |
... | ... | |
136 | 146 |
If there is a valid, not-consumed invitation code for the specific user |
137 | 147 |
returns True else returns False. |
138 | 148 |
""" |
149 |
if super(InvitationsBackend, self)._is_preaccepted(user): |
|
150 |
return True |
|
139 | 151 |
invitation = self.invitation |
140 | 152 |
if not invitation: |
141 | 153 |
return False |
... | ... | |
145 | 157 |
return False |
146 | 158 |
|
147 | 159 |
@transaction.commit_manually |
148 |
def signup(self, form, admin_email_template_name='im/admin_notification.txt'): |
|
160 |
def signup(self, form, email_template_name='im/activation_email.txt', admin_email_template_name='im/admin_notification.txt'):
|
|
149 | 161 |
""" |
150 | 162 |
Initially creates an inactive user account. If the user is preaccepted |
151 | 163 |
(has a valid invitation code) the user is activated and if the request |
... | ... | |
159 | 171 |
try: |
160 | 172 |
user = form.save() |
161 | 173 |
if self._is_preaccepted(user): |
162 |
user.is_active = True |
|
163 |
user.save() |
|
164 |
message = _('Registration completed. You can now login.') |
|
174 |
if user.email_verified: |
|
175 |
user.is_active = True |
|
176 |
user.save() |
|
177 |
message = _('Registration completed. You can now login.') |
|
178 |
else: |
|
179 |
try: |
|
180 |
_send_verification(self.request, user, email_template_name) |
|
181 |
message = _('Verification sent to %s' % user.email) |
|
182 |
except (SMTPException, socket.error) as e: |
|
183 |
status = messages.ERROR |
|
184 |
name = 'strerror' |
|
185 |
message = getattr(e, name) if hasattr(e, name) else e |
|
165 | 186 |
else: |
166 | 187 |
_send_notification(user, admin_email_template_name) |
167 | 188 |
message = _('Your request for an account was successfully sent \ |
... | ... | |
183 | 204 |
transaction.commit() |
184 | 205 |
return status, message, user |
185 | 206 |
|
186 |
class SimpleBackend(object):
|
|
207 |
class SimpleBackend(SignupBackend):
|
|
187 | 208 |
""" |
188 | 209 |
A registration backend which implements the following workflow: a user |
189 | 210 |
supplies the necessary registation information, an incative user account is |
... | ... | |
191 | 212 |
""" |
192 | 213 |
def __init__(self, request): |
193 | 214 |
self.request = request |
215 |
super(SimpleBackend, self).__init__() |
|
194 | 216 |
|
195 | 217 |
def get_signup_form(self, provider): |
196 | 218 |
""" |
... | ... | |
207 | 229 |
ip = self.request.META.get('REMOTE_ADDR', |
208 | 230 |
self.request.META.get('HTTP_X_REAL_IP', None)) |
209 | 231 |
return globals()[formclass](initial_data, ip=ip) |
210 |
|
|
232 |
|
|
233 |
def _is_preaccepted(self, user): |
|
234 |
if super(SimpleBackend, self)._is_preaccepted(user): |
|
235 |
return True |
|
236 |
if MODERATION_ENABLED: |
|
237 |
return False |
|
238 |
return True |
|
239 |
|
|
211 | 240 |
@transaction.commit_manually |
212 | 241 |
def signup(self, form, email_template_name='im/activation_email.txt', admin_email_template_name='im/admin_notification.txt'): |
213 | 242 |
""" |
... | ... | |
233 | 262 |
""" |
234 | 263 |
user = form.save() |
235 | 264 |
status = messages.SUCCESS |
236 |
if MODERATION_ENABLED:
|
|
265 |
if not self._is_preaccepted(user):
|
|
237 | 266 |
try: |
238 | 267 |
_send_notification(user, admin_email_template_name) |
239 | 268 |
message = _('Your request for an account was successfully sent \ |
Also available in: Unified diff