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.http import Http404 

4from django.template.response import TemplateResponse 

5from django.utils.translation import ugettext_lazy as _ 

6from django.views.generic import View as GenericView 

7from superdjango.assets.library import JavaScript, StyleSheet 

8from superdjango.conf import SUPERDJANGO 

9from superdjango.exceptions import IMustBeMissingSomething 

10 

11__all__ = ( 

12 "BaseView", 

13 "GenericView", 

14) 

15 

16# Views 

17 

18 

19class BaseView(GenericView): 

20 """A mixin for establishing core functionality for all views.""" 

21 

22 active_page = None 

23 """The name of the active page that may be used to identify the current view within a menu.""" 

24 

25 active_subpage = None 

26 """The name of the active subpage that may be used to identify the current view within a submenu.""" 

27 

28 base_template_name = SUPERDJANGO.BASE_TEMPLATE 

29 """The base template to extend.""" 

30 

31 css = None 

32 """A list of CSS markup elements that maybe added to the output. See ``get_css()``.""" 

33 

34 js = None 

35 """A list of JavaScript markup elements that maybe added to the output. See ``get_js()``.""" 

36 

37 raise_exception = False 

38 """Indicates whether exceptions should be raised during processing or mitigated, if possible.""" 

39 

40 subtitle = None 

41 """The subtitle of the page.""" 

42 

43 template_name = None 

44 """The name of the template to use for rendering the view.""" 

45 

46 title = None 

47 """The title of the page.""" 

48 

49 # noinspection PyMethodMayBeStatic 

50 def dispatch_not_found(self, message=None): 

51 """Dispatch a 404 (page not found) error. By default, this just raises an ``Http404``. 

52 

53 :param message: The 404 message. 

54 

55 """ 

56 if message is None: 

57 message = _("This page could not be located.") 

58 

59 raise Http404(message) 

60 

61 def get_base_template(self): 

62 """Get the name of the base template to use for rendering the view. 

63 

64 :rtype: str 

65 

66 """ 

67 if self.base_template_name is not None: 

68 return self.base_template_name 

69 

70 raise IMustBeMissingSomething(self.__class__.__name__, "base_template_name", "get_base_template") 

71 

72 def get_css(self): 

73 """Get the URLs or markup of any CSS to be included in the view. 

74 

75 :rtype: Style 

76 

77 """ 

78 if self.css is not None: 

79 return self.css 

80 

81 return StyleSheet() 

82 

83 def get_context_data(self, **kwargs): 

84 """Uses keyword arguments as the base context and adds the current view instance as ``view``. 

85 

86 :rtype: dict 

87 

88 The following variables are added to the context: 

89 

90 - ``active_page``: The identifier for the active menu item. 

91 - ``active_subpage``: The identifier for the active sub-menu item. 

92 - ``base_template``: The template to extend, i.e. ``{% extends base_template %}``. 

93 - ``subtitle``: The subtitle of the page. 

94 - ``title``: The title of the page. 

95 - ``view``: The current view instance. 

96 - ``view_css``: A Style instance. 

97 - ``view_js``: A JavaScript instance. 

98 

99 """ 

100 

101 kwargs['active_page'] = self.active_page 

102 kwargs['active_subpage'] = self.active_subpage 

103 kwargs['base_template'] = self.get_base_template() 

104 kwargs['subtitle'] = self.get_subtitle() 

105 kwargs['title'] = self.get_title() 

106 kwargs['view'] = self 

107 kwargs['view_css'] = self.get_css() 

108 kwargs['view_js'] = self.get_js() 

109 

110 return kwargs 

111 

112 def get_js(self): 

113 """Get the URLs or markup of any JavaScript to be included in the view. 

114 

115 :rtype: JavaScript 

116 

117 """ 

118 if self.js is not None: 

119 return self.js 

120 

121 return JavaScript() 

122 

123 def get_subtitle(self): 

124 """Get the page subtitle. 

125 

126 :rtype: str 

127 

128 """ 

129 if self.subtitle is not None: 

130 return self.subtitle 

131 

132 return "" 

133 

134 def get_template_names(self): 

135 """Get the name of the template used to render the response. 

136 

137 :rtype: list[str] 

138 

139 """ 

140 if self.template_name is not None: 

141 return [self.template_name] 

142 

143 raise IMustBeMissingSomething(self.__class__.__name__, "template_name", "get_template_names") 

144 

145 def get_title(self): 

146 """Get the page title. 

147 

148 :rtype: str 

149 

150 """ 

151 if self.title is not None: 

152 return self.title 

153 

154 return "" 

155 

156 def render_to_response(self, context): 

157 """Render the current context. 

158 

159 :param context: The context variables to use for rendering. 

160 

161 :rtype: TemplateResponse 

162 

163 """ 

164 # noinspection PyUnresolvedReferences 

165 return TemplateResponse(self.request, self.get_template_names(), context=context)