Hide keyboard shortcuts

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 

2 

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 

8 

9# Exports 

10 

11__all__ = ( 

12 "CallbackRedirectView", 

13 "CountRedirectView", 

14 "DelayedRedirectView", 

15 "RedirectView", 

16) 

17 

18# Views 

19 

20 

21class CallbackRedirectView(DjangoRedirectView): 

22 """Allows the specification of a redirect callback when the view is created.""" 

23 

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 """ 

28 

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") 

33 

34 return self.redirect_callback 

35 

36 def get_redirect_url(self, *args, **kwargs): 

37 """Attempt to issue a redirect using the provided callback.""" 

38 callback = self.get_redirect_callback() 

39 

40 return callback(self.request) 

41 

42 

43class CountRedirectView(MessageMixin, DjangoRedirectView): 

44 """Logs a "hit" before redirecting.""" 

45 

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 """ 

50 

51 def get_counter_callback(self): 

52 """Get the callable, if any, that may be used to record model activity. 

53 

54 .. tip:: 

55 No error is raised if the ``counter_callback`` does not exist or is not callable. 

56 

57 """ 

58 if self.counter_callback is not None and callable(self.counter_callback): 

59 return self.counter_callback 

60 

61 return None 

62 

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) 

66 

67 callback = self.get_counter_callback() 

68 if callback is not None: 

69 callback(self.request, redirect_url) 

70 

71 return redirect_url 

72 

73 

74class DelayedRedirectView(MessageMixin, TemplateView): 

75 """Redirect the user after a delay.""" 

76 

77 countdown_enabled = True 

78 """Indicates whether a countdown should be displayed.""" 

79 

80 delayed_seconds = 5 

81 """The number of seconds to wait before issuing the redirect.""" 

82 

83 redirect_url = None 

84 """The URL to which the user should be redirected.""" 

85 

86 template_name = "superdjango/views/delayed_redirect.html" 

87 """The default template to use for the redirect page.""" 

88 

89 def get_context_data(self, **kwargs): 

90 """Add delayed redirect variables to the context. 

91 

92 - ``countdown_enabled`` 

93 - ``delayed_seconds`` 

94 - ``redirect_url`` 

95 

96 """ 

97 context = super().get_context_data(**kwargs) 

98 

99 context['countdown_enabled'] = self.countdown_enabled 

100 context['delayed_seconds'] = self.delayed_seconds 

101 context['redirect_url'] = self.get_redirect_url() 

102 

103 return context 

104 

105 def get_redirect_url(self): 

106 """Get the URL to which the user should be redirected. 

107 

108 :rtype: str 

109 

110 """ 

111 if self.redirect_url is not None: 

112 return self.redirect_url 

113 

114 raise IMustBeMissingSomething(self.__class__.__name__, "redirect_url", "get_redirect_url") 

115 

116 def get_title(self): 

117 """Get the default title.""" 

118 if self.title is not None: 

119 return self.title 

120 

121 return _("Redirect") 

122 

123 

124class RedirectView(MessageMixin, DjangoRedirectView): 

125 """A redirect view which incorporates message functionality.""" 

126 pass