-
Notifications
You must be signed in to change notification settings - Fork 237
/
views.py
133 lines (108 loc) · 4.35 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Base view classes for all registration workflows.
"""
from django.conf import settings
from django.shortcuts import redirect
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView
from registration import signals
from registration.forms import RegistrationForm
class RegistrationView(FormView):
"""
Base class for user registration views.
"""
disallowed_url = 'registration_disallowed'
form_class = RegistrationForm
success_url = None
template_name = 'registration/registration_form.html'
def dispatch(self, *args, **kwargs):
"""
Check that user signup is allowed before even bothering to
dispatch or do other processing.
"""
if not self.registration_allowed():
return redirect(self.disallowed_url)
return super(RegistrationView, self).dispatch(*args, **kwargs)
def form_valid(self, form):
new_user = self.register(form)
success_url = self.get_success_url(new_user)
# success_url may be a simple string, or a tuple providing the
# full argument set for redirect(). Attempting to unpack it
# tells us which one it is.
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
def form_invalid(self, form):
# tl;dr -- this method is implemented to work around Django
# ticket #25548, which is present in the Django 1.9 release
# (but not in Django 1.8 or 1.10).
#
# The longer explanation is that in Django 1.9,
# FormMixin.form_invalid() does not pass the form instance to
# get_context_data(). This causes get_context_data() to
# construct a new form instance with the same data in order to
# put it into the template context, and then any access to
# that form's ``errors`` or ``cleaned_data`` runs that form
# instance's validation. The end result is that validation
# gets run twice on an invalid form submission, which is
# undesirable for performance reasons.
#
# Manually implementing this method, and passing the form
# instance to get_context_data(), solves this issue (which was
# fixed in Django 1.9.1 and so is not present in Django
# 1.10).
return self.render_to_response(self.get_context_data(form=form))
def registration_allowed(self):
"""
Override this to enable/disable user registration, either
globally or on a per-request basis.
"""
return getattr(settings, 'REGISTRATION_OPEN', True)
def register(self, form):
"""
Implement user-registration logic here. Access to both the
request and the registration form is available here.
"""
raise NotImplementedError
class ActivationView(TemplateView):
"""
Base class for user activation views.
"""
template_name = 'registration/activate.html'
def get(self, *args, **kwargs):
"""
The base activation logic; subclasses should leave this method
alone and implement activate(), which is called from this
method.
"""
activated_user = self.activate(*args, **kwargs)
if activated_user:
signals.user_activated.send(
sender=self.__class__,
user=activated_user,
request=self.request
)
success_url = self.get_success_url(activated_user)
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
except ValueError:
return redirect(success_url)
return super(ActivationView, self).get(*args, **kwargs)
def activate(self, *args, **kwargs):
"""
Implement account-activation logic here.
"""
raise NotImplementedError
def get_success_url(self, user):
"""
Implement this to return the URL (either a 3-tuple for
redirect(), or a simple string name of a URL pattern) to
redirect to after successful activation.
This differs from most get_success_url() methods of Django
views in that it receives an extra argument: the user whose
account was activated.
"""
raise NotImplementedError