Skip to content

Commit

Permalink
Make success_url attribute work on ActivationView, too.
Browse files Browse the repository at this point in the history
  • Loading branch information
ubernostrum committed May 31, 2017
1 parent ea4a232 commit 5d397a5
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
10 changes: 10 additions & 0 deletions docs/views.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ classes, for use in writing your own custom subclasses.
Useful places to override or customize on an ``ActivationView``
subclass are:

.. attribute:: success_url

The URL to redirect to after successful activation. A string
containing a (relative) URL, or a string name of a URL pattern,
or a 3-tuple of arguments suitable for passing to Django's
`redirect shortcut
<https://docs.djangoproject.com/en/stable/topics/http/shortcuts/#redirect>`_. Can
be overridden on a per-request basis (see below). Default value
is ``None``, so that per-request customization is used instead.

.. attribute:: template_name

The template to use for user activation. Should be a
Expand Down
5 changes: 2 additions & 3 deletions registration/backends/hmac/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class ActivationView(BaseActivationView):
couldn't be activated.
"""
success_url = 'registration_activation_complete'

def activate(self, *args, **kwargs):
# This is safe even if, somehow, there's no activation key,
# because unsign() will raise BadSignature rather than
Expand All @@ -118,9 +120,6 @@ def activate(self, *args, **kwargs):
return user
return False

def get_success_url(self, user):
return ('registration_activation_complete', (), {})

def validate_key(self, activation_key):
"""
Verify that the activation key is valid and within the
Expand Down
5 changes: 2 additions & 3 deletions registration/backends/model_activation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ class ActivationView(BaseActivationView):
couldn't be activated.
"""
success_url = 'registration_activation_complete'

def activate(self, *args, **kwargs):
activation_key = kwargs.get('activation_key')
activated_user = RegistrationProfile.objects.activate_user(
activation_key
)
return activated_user

def get_success_url(self, user):
return ('registration_activation_complete', (), {})
2 changes: 1 addition & 1 deletion registration/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ def test_activation(self):
kwargs={'activation_key': profile.activation_key}
)
)
self.assertRedirects(resp, '/')
self.assertRedirects(resp, '/activate/complete/')
4 changes: 2 additions & 2 deletions registration/tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from registration.backends.model_activation.views import RegistrationView

from .views import ActivateWithSimpleRedirect
from .views import ActivateWithComplexRedirect


urlpatterns = [
Expand All @@ -30,7 +30,7 @@
# the view; that way it can return a sensible "invalid key"
# message instead of a confusing 404.
url(r'^activate/(?P<activation_key>\w+)/$',
ActivateWithSimpleRedirect.as_view(),
ActivateWithComplexRedirect.as_view(),
name='registration_activate'),
url(r'^register/$',
RegistrationView.as_view(),
Expand Down
10 changes: 2 additions & 8 deletions registration/tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,5 @@
from registration.backends.model_activation.views import ActivationView


class ActivateWithSimpleRedirect(ActivationView):
def get_success_url(self, user):
"""
Returns a string URL to redirect to on success, rather than a
(view, args, kwargs) 3-tuple, to test handling of that case.
"""
return '/'
class ActivateWithComplexRedirect(ActivationView):
success_url = ('registration_activation_complete', (), {})
19 changes: 5 additions & 14 deletions registration/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ActivationView(TemplateView):
Base class for user activation views.
"""
success_url = None
template_name = 'registration/activate.html'

def get(self, *args, **kwargs):
Expand All @@ -86,7 +87,10 @@ def get(self, *args, **kwargs):
user=activated_user,
request=self.request
)
success_url = self.get_success_url(activated_user)
success_url = self.get_success_url(activated_user) if \
(hasattr(self, 'get_success_url') and
callable(self.success_url)) else \
self.success_url
try:
to, args, kwargs = success_url
return redirect(to, *args, **kwargs)
Expand All @@ -100,16 +104,3 @@ def activate(self, *args, **kwargs):
"""
raise NotImplementedError

def get_success_url(self, user):
"""
Implement this to return the URL (either a 3-tuple for
redirect(), or a 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

0 comments on commit 5d397a5

Please sign in to comment.