Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.views.generic import RedirectView as DjangoRedirectView
4from django.utils.translation import ugettext_lazy as _
5from superdjango.exceptions import IMustBeMissingSomething
6from .messages import MessageMixin
7from .templates import TemplateView
9# Exports
11__all__ = (
12 "CallbackRedirectView",
13 "CountRedirectView",
14 "DelayedRedirectView",
15 "RedirectView",
16)
18# Views
21class CallbackRedirectView(DjangoRedirectView):
22 """Allows the specification of a redirect callback when the view is created."""
24 redirect_callback = None
25 """A callable that is used to issue the redirect. It should accept a request argument and return an
26 ``HTTPResponseRedirect``.
27 """
29 def get_redirect_callback(self):
30 """Get the redirect callback function."""
31 if self.redirect_callback is None or not callable(self.redirect_callback):
32 raise IMustBeMissingSomething(self.__class__.__name__, "redirect_callback", "get_redirect_callback")
34 return self.redirect_callback
36 def get_redirect_url(self, *args, **kwargs):
37 """Attempt to issue a redirect using the provided callback."""
38 callback = self.get_redirect_callback()
40 return callback(self.request)
43class CountRedirectView(MessageMixin, DjangoRedirectView):
44 """Logs a "hit" before redirecting."""
46 counter_callback = None
47 """A callable that may be used to the activity. If defined, the callback is passed the request instance and redirect
48 URL, as in ``callback(request, url)``.
49 """
51 def get_counter_callback(self):
52 """Get the callable, if any, that may be used to record model activity.
54 .. tip::
55 No error is raised if the ``counter_callback`` does not exist or is not callable.
57 """
58 if self.counter_callback is not None and callable(self.counter_callback):
59 return self.counter_callback
61 return None
63 def get_redirect_url(self, *args, **kwargs):
64 """Execute callback before returning the redirect URL."""
65 redirect_url = super().get_redirect_url(*args, **kwargs)
67 callback = self.get_counter_callback()
68 if callback is not None:
69 callback(self.request, redirect_url)
71 return redirect_url
74class DelayedRedirectView(MessageMixin, TemplateView):
75 """Redirect the user after a delay."""
77 countdown_enabled = True
78 """Indicates whether a countdown should be displayed."""
80 delayed_seconds = 5
81 """The number of seconds to wait before issuing the redirect."""
83 redirect_url = None
84 """The URL to which the user should be redirected."""
86 template_name = "superdjango/views/delayed_redirect.html"
87 """The default template to use for the redirect page."""
89 def get_context_data(self, **kwargs):
90 """Add delayed redirect variables to the context.
92 - ``countdown_enabled``
93 - ``delayed_seconds``
94 - ``redirect_url``
96 """
97 context = super().get_context_data(**kwargs)
99 context['countdown_enabled'] = self.countdown_enabled
100 context['delayed_seconds'] = self.delayed_seconds
101 context['redirect_url'] = self.get_redirect_url()
103 return context
105 def get_redirect_url(self):
106 """Get the URL to which the user should be redirected.
108 :rtype: str
110 """
111 if self.redirect_url is not None:
112 return self.redirect_url
114 raise IMustBeMissingSomething(self.__class__.__name__, "redirect_url", "get_redirect_url")
116 def get_title(self):
117 """Get the default title."""
118 if self.title is not None:
119 return self.title
121 return _("Redirect")
124class RedirectView(MessageMixin, DjangoRedirectView):
125 """A redirect view which incorporates message functionality."""
126 pass