Revision 5ce3ce4f snf-astakos-app/astakos/im/activation_backends.py

b/snf-astakos-app/astakos/im/activation_backends.py
47 47

  
48 48
logger = logging.getLogger(__name__)
49 49

  
50

  
50 51
def get_backend(request):
51 52
    """
52 53
    Returns an instance of an activation backend,
......
59 60
    """
60 61
    module = 'astakos.im.activation_backends'
61 62
    prefix = 'Invitations' if INVITATIONS_ENABLED else 'Simple'
62
    backend_class_name = '%sBackend' %prefix
63
    backend_class_name = '%sBackend' % prefix
63 64
    try:
64 65
        mod = import_module(module)
65 66
    except ImportError, e:
66
        raise ImproperlyConfigured('Error loading activation backend %s: "%s"' % (module, e))
67
        raise ImproperlyConfigured(
68
            'Error loading activation backend %s: "%s"' % (module, e))
67 69
    try:
68 70
        backend_class = getattr(mod, backend_class_name)
69 71
    except AttributeError:
70 72
        raise ImproperlyConfigured('Module "%s" does not define a activation backend named "%s"' % (module, backend_class_name))
71 73
    return backend_class(request)
72 74

  
75

  
73 76
class ActivationBackend(object):
74 77
    def __init__(self, request):
75 78
        self.request = request
76
    
79

  
77 80
    def _is_preaccepted(self, user):
78 81
        # return True if user email matches specific patterns
79 82
        for pattern in RE_USER_EMAIL_PATTERNS:
80 83
            if re.match(pattern, user.email):
81 84
                return True
82 85
        return False
83
    
86

  
84 87
    def get_signup_form(self, provider='local', instance=None):
85 88
        """
86 89
        Returns a form instance of the relevant class
87 90
        """
88 91
        main = provider.capitalize() if provider == 'local' else 'ThirdParty'
89
        suffix  = 'UserCreationForm'
92
        suffix = 'UserCreationForm'
90 93
        formclass = '%s%s' % (main, suffix)
91 94
        request = self.request
92 95
        initial_data = None
......
94 97
            if provider == request.POST.get('provider', ''):
95 98
                initial_data = request.POST
96 99
        return globals()[formclass](initial_data, instance=instance, request=request)
97
    
98
    def handle_activation(self, user, \
99
                          activation_template_name='im/activation_email.txt', \
100
                          greeting_template_name='im/welcome_email.txt', \
101
                          admin_email_template_name='im/account_notification.txt', \
100

  
101
    def handle_activation(self, user,
102
                          activation_template_name='im/activation_email.txt',
103
                          greeting_template_name='im/welcome_email.txt',
104
                          admin_email_template_name='im/account_notification.txt',
102 105
                          switch_accounts_email_template_name='im/switch_accounts_email.txt'):
103 106
        """
104 107
        If the user is already active returns immediately.
......
117 120
            if user.conflicting_email():
118 121
                send_verification(user, switch_accounts_email_template_name)
119 122
                return SwitchAccountsVerificationSent(user.email)
120
            
123

  
121 124
            if self._is_preaccepted(user):
122 125
                if user.email_verified:
123 126
                    activate(user, greeting_template_name)
......
128 131
            else:
129 132
                send_admin_notification(
130 133
                    template_name=admin_email_template_name,
131
                    dictionary={'user':user, 'group_creation':True},
134
                    dictionary={'user': user, 'group_creation': True},
132 135
                    subject='%s alpha2 testing account notification' % SITENAME
133 136
                )
134 137
                return NotificationSent()
......
136 139
            logger.exception(e)
137 140
            raise e
138 141

  
142

  
139 143
class InvitationsBackend(ActivationBackend):
140 144
    """
141 145
    A activation backend which implements the following workflow: a user
......
148 152
    def get_signup_form(self, provider='local', instance=None):
149 153
        """
150 154
        Returns a form instance of the relevant class
151
        
155

  
152 156
        raises Invitation.DoesNotExist and ValueError if invitation is consumed
153 157
        or invitation username is reserved.
154 158
        """
......
157 161
        initial_data = self.get_signup_initial_data(provider)
158 162
        prefix = 'Invited' if invitation else ''
159 163
        main = provider.capitalize()
160
        suffix  = 'UserCreationForm'
164
        suffix = 'UserCreationForm'
161 165
        formclass = '%s%s%s' % (prefix, main, suffix)
162 166
        return globals()[formclass](initial_data, instance=instance, request=self.request)
163 167

  
......
174 178
            if invitation:
175 179
                # create a tmp user with the invitation realname
176 180
                # to extract first and last name
177
                u = AstakosUser(realname = invitation.realname)
178
                initial_data = {'email':invitation.username,
179
                                'inviter':invitation.inviter.realname,
180
                                'first_name':u.first_name,
181
                                'last_name':u.last_name,
182
                                'provider':provider}
181
                u = AstakosUser(realname=invitation.realname)
182
                initial_data = {'email': invitation.username,
183
                                'inviter': invitation.inviter.realname,
184
                                'first_name': u.first_name,
185
                                'last_name': u.last_name,
186
                                'provider': provider}
183 187
        else:
184 188
            if provider == request.POST.get('provider', ''):
185 189
                initial_data = request.POST
......
200 204
            return True
201 205
        return False
202 206

  
207

  
203 208
class SimpleBackend(ActivationBackend):
204 209
    """
205 210
    A activation backend which implements the following workflow: a user
......
213 218
            return False
214 219
        return True
215 220

  
221

  
216 222
class ActivationResult(object):
217 223
    def __init__(self, message):
218 224
        self.message = message
219 225

  
226

  
220 227
class VerificationSent(ActivationResult):
221 228
    def __init__(self):
222 229
        message = _('Verification sent.')
223 230
        super(VerificationSent, self).__init__(message)
224 231

  
232

  
225 233
class SwitchAccountsVerificationSent(ActivationResult):
226 234
    def __init__(self, email):
227 235
        message = _('This email is already associated with another \
......
230 238
                    to %s. Otherwise just ignore it.' % email)
231 239
        super(SwitchAccountsVerificationSent, self).__init__(message)
232 240

  
241

  
233 242
class NotificationSent(ActivationResult):
234 243
    def __init__(self):
235 244
        message = _('Your request for an account was successfully received and is now pending \
......
237 246
                    your interest in ~okeanos! The GRNET team.')
238 247
        super(NotificationSent, self).__init__(message)
239 248

  
249

  
240 250
class RegistationCompleted(ActivationResult):
241 251
    def __init__(self):
242 252
        message = _('Registration completed. You can now login.')
243
        super(RegistationCompleted, self).__init__(message)
253
        super(RegistationCompleted, self).__init__(message)

Also available in: Unified diff