1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
from __future__ import unicode_literals |
3
|
|
|
""" |
4
|
|
|
Class based views for django-inspectional-registration |
5
|
|
|
""" |
6
|
|
|
__author__ = 'Alisue <[email protected]>' |
7
|
|
|
from django.http import Http404 |
8
|
|
|
from django.shortcuts import redirect |
9
|
|
|
from django.views.generic import TemplateView |
10
|
|
|
from django.views.generic.edit import ProcessFormView |
11
|
|
|
from django.views.generic.edit import FormMixin |
12
|
|
|
from django.views.generic.base import TemplateResponseMixin |
13
|
|
|
from django.views.generic.detail import SingleObjectMixin |
14
|
|
|
from django.utils.text import ugettext_lazy as _ |
15
|
|
|
|
16
|
|
|
from registration.backends import get_backend |
17
|
|
|
from registration.models import RegistrationProfile |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class RegistrationCompleteView(TemplateView): |
21
|
|
|
"""A simple template view for registration complete""" |
22
|
|
|
template_name = r'registration/registration_complete.html' |
23
|
|
|
|
24
|
|
|
def get_context_data(self, **kwargs): |
25
|
|
|
context = super(RegistrationCompleteView, |
26
|
|
|
self).get_context_data(**kwargs) |
27
|
|
|
# get registration_profile instance from the session |
28
|
|
|
if 'registration_profile_pk' in self.request.session: |
29
|
|
|
profile_pk = self.request.session.pop('registration_profile_pk') |
30
|
|
|
profile = RegistrationProfile.objects.get(pk=profile_pk) |
31
|
|
|
else: |
32
|
|
|
profile = None |
33
|
|
|
context['registration_profile'] = profile |
34
|
|
|
return context |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
class RegistrationClosedView(TemplateView): |
38
|
|
|
"""A simple template view for registraion closed |
39
|
|
|
|
40
|
|
|
This view is called when user accessed to RegistrationView |
41
|
|
|
with REGISTRATION_OPEN = False |
42
|
|
|
""" |
43
|
|
|
template_name = r'registration/registration_closed.html' |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
class ActivationCompleteView(TemplateView): |
47
|
|
|
"""A simple template view for activation complete""" |
48
|
|
|
template_name = r'registration/activation_complete.html' |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
class ActivationView(TemplateResponseMixin, FormMixin, |
52
|
|
|
SingleObjectMixin, ProcessFormView): |
53
|
|
|
"""A complex view for activation |
54
|
|
|
|
55
|
|
|
GET: |
56
|
|
|
Display an ActivationForm which has ``password1`` and ``password2`` |
57
|
|
|
for activation user who has ``activation_key`` |
58
|
|
|
``password1`` and ``password2`` should be equal to prepend typo |
59
|
|
|
|
60
|
|
|
POST: |
61
|
|
|
Activate the user who has ``activation_key`` with passed ``password1`` |
62
|
|
|
""" |
63
|
|
|
template_name = r'registration/activation_form.html' |
64
|
|
|
model = RegistrationProfile |
65
|
|
|
|
66
|
|
|
def __init__(self, *args, **kwargs): |
67
|
|
|
self.backend = get_backend() |
68
|
|
|
super(ActivationView, self).__init__(*args, **kwargs) |
69
|
|
|
|
70
|
|
|
def get_queryset(self): |
71
|
|
|
"""get ``RegistrationProfile`` queryset which status is 'accepted'""" |
72
|
|
|
return self.model.objects.filter(_status='accepted') |
73
|
|
|
|
74
|
|
|
def get_object(self, queryset=None): |
75
|
|
|
"""get ``RegistrationProfile`` instance by ``activation_key`` |
76
|
|
|
|
77
|
|
|
``activation_key`` should be passed by URL |
78
|
|
|
""" |
79
|
|
|
queryset = queryset or self.get_queryset() |
80
|
|
|
try: |
81
|
|
|
obj = queryset.get(activation_key=self.kwargs['activation_key']) |
82
|
|
|
if obj.activation_key_expired(): |
83
|
|
|
raise Http404(_('Activation key has expired')) |
84
|
|
|
except self.model.DoesNotExist: |
85
|
|
|
raise Http404(_('An invalid activation key has passed')) |
86
|
|
|
return obj |
87
|
|
|
|
88
|
|
|
def get_success_url(self): |
89
|
|
|
"""get activation complete url via backend""" |
90
|
|
|
return self.backend.get_activation_complete_url(self.activated_user) |
91
|
|
|
|
92
|
|
|
def get_form_class(self): |
93
|
|
|
"""get activation form class via backend""" |
94
|
|
|
return self.backend.get_activation_form_class() |
95
|
|
|
|
96
|
|
|
def form_valid(self, form): |
97
|
|
|
"""activate user who has ``activation_key`` with ``password1`` |
98
|
|
|
|
99
|
|
|
this method is called when form validation has successed. |
100
|
|
|
""" |
101
|
|
|
profile = self.get_object() |
102
|
|
|
password = form.cleaned_data['password1'] |
103
|
|
|
self.activated_user = self.backend.activate( |
104
|
|
|
profile.activation_key, self.request, password=password) |
105
|
|
|
return super(ActivationView, self).form_valid(form) |
106
|
|
|
|
107
|
|
|
def get(self, request, *args, **kwargs): |
108
|
|
|
# self.object have to be set |
109
|
|
|
self.object = self.get_object() |
110
|
|
|
return super(ActivationView, self).get(request, *args, **kwargs) |
111
|
|
|
|
112
|
|
|
def post(self, request, *args, **kwargs): |
113
|
|
|
# self.object have to be set |
114
|
|
|
self.object = self.get_object() |
115
|
|
|
return super(ActivationView, self).post(request, *args, **kwargs) |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
class RegistrationView(FormMixin, TemplateResponseMixin, ProcessFormView): |
119
|
|
|
"""A complex view for registration |
120
|
|
|
|
121
|
|
|
GET: |
122
|
|
|
Display an RegistrationForm which has ``username``, ``email1`` and ``email2`` |
123
|
|
|
for registration. |
124
|
|
|
``email1`` and ``email2`` should be equal to prepend typo. |
125
|
|
|
|
126
|
|
|
``form`` and ``supplement_form`` is in context to display these form. |
127
|
|
|
|
128
|
|
|
POST: |
129
|
|
|
Register the user with passed ``username`` and ``email1`` |
130
|
|
|
""" |
131
|
|
|
template_name = r'registration/registration_form.html' |
132
|
|
|
|
133
|
|
|
def __init__(self, *args, **kwargs): |
134
|
|
|
self.backend = get_backend() |
135
|
|
|
super(RegistrationView, self).__init__(*args, **kwargs) |
136
|
|
|
|
137
|
|
|
def get_success_url(self): |
138
|
|
|
"""get registration complete url via backend""" |
139
|
|
|
return self.backend.get_registration_complete_url(self.new_user) |
140
|
|
|
|
141
|
|
|
def get_disallowed_url(self): |
142
|
|
|
"""get registration closed url via backend""" |
143
|
|
|
return self.backend.get_registration_closed_url() |
144
|
|
|
|
145
|
|
|
def get_form_class(self): |
146
|
|
|
"""get registration form class via backend""" |
147
|
|
|
return self.backend.get_registration_form_class() |
148
|
|
|
|
149
|
|
|
def get_supplement_form_class(self): |
150
|
|
|
"""get registration supplement form class via backend""" |
151
|
|
|
return self.backend.get_supplement_form_class() |
152
|
|
|
|
153
|
|
|
def get_supplement_form(self, supplement_form_class): |
154
|
|
|
"""get registration supplement form instance""" |
155
|
|
|
if not supplement_form_class: |
156
|
|
|
return None |
157
|
|
|
return supplement_form_class(**self.get_form_kwargs()) |
158
|
|
|
|
159
|
|
|
def form_valid(self, form, supplement_form=None): |
160
|
|
|
"""register user with ``username`` and ``email1`` |
161
|
|
|
|
162
|
|
|
this method is called when form validation has successed. |
163
|
|
|
""" |
164
|
|
|
username = form.cleaned_data['username'] |
165
|
|
|
email = form.cleaned_data['email1'] |
166
|
|
|
if supplement_form: |
167
|
|
|
supplement = supplement_form.save(commit=False) |
168
|
|
|
else: |
169
|
|
|
supplement = None |
170
|
|
|
self.new_user = self.backend.register(username, email, |
171
|
|
|
self.request, |
172
|
|
|
supplement=supplement) |
173
|
|
|
profile = self.new_user.registration_profile |
174
|
|
|
# save the profile on the session so that the RegistrationCompleteView |
175
|
|
|
# can refer the profile instance. |
176
|
|
|
# this instance is automatically removed when the user accessed |
177
|
|
|
# RegistrationCompleteView |
178
|
|
|
self.request.session['registration_profile_pk'] = profile.pk |
179
|
|
|
return super(RegistrationView, self).form_valid(form) |
180
|
|
|
|
181
|
|
|
def form_invalid(self, form, supplement_form=None): |
182
|
|
|
context = self.get_context_data( |
183
|
|
|
form=form, |
184
|
|
|
supplement_form=supplement_form |
185
|
|
|
) |
186
|
|
|
return self.render_to_response(context) |
187
|
|
|
|
188
|
|
|
def get(self, request, *args, **kwargs): |
189
|
|
|
form_class = self.get_form_class() |
190
|
|
|
form = self.get_form(form_class) |
191
|
|
|
supplement_form_class = self.get_supplement_form_class() |
192
|
|
|
supplement_form = self.get_supplement_form(supplement_form_class) |
193
|
|
|
context = self.get_context_data( |
194
|
|
|
form=form, supplement_form=supplement_form) |
195
|
|
|
return self.render_to_response(context) |
196
|
|
|
|
197
|
|
|
def post(self, request, *args, **kwargs): |
198
|
|
|
form_class = self.get_form_class() |
199
|
|
|
form = self.get_form(form_class) |
200
|
|
|
supplement_form_class = self.get_supplement_form_class() |
201
|
|
|
supplement_form = self.get_supplement_form(supplement_form_class) |
202
|
|
|
if form.is_valid() and (not supplement_form or supplement_form.is_valid()): |
203
|
|
|
return self.form_valid(form, supplement_form) |
204
|
|
|
else: |
205
|
|
|
return self.form_invalid(form, supplement_form) |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
def dispatch(self, request, *args, **kwargs): |
209
|
|
|
if not self.backend.registration_allowed(): |
210
|
|
|
# registraion has closed |
211
|
|
|
return redirect(self.get_disallowed_url()) |
212
|
|
|
return super(RegistrationView, self).dispatch(request, *args, **kwargs) |
213
|
|
|
|