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.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
11__all__ = (
12 "BaseView",
13 "GenericView",
14)
16# Views
19class BaseView(GenericView):
20 """A mixin for establishing core functionality for all views."""
22 active_page = None
23 """The name of the active page that may be used to identify the current view within a menu."""
25 active_subpage = None
26 """The name of the active subpage that may be used to identify the current view within a submenu."""
28 base_template_name = SUPERDJANGO.BASE_TEMPLATE
29 """The base template to extend."""
31 css = None
32 """A list of CSS markup elements that maybe added to the output. See ``get_css()``."""
34 js = None
35 """A list of JavaScript markup elements that maybe added to the output. See ``get_js()``."""
37 raise_exception = False
38 """Indicates whether exceptions should be raised during processing or mitigated, if possible."""
40 subtitle = None
41 """The subtitle of the page."""
43 template_name = None
44 """The name of the template to use for rendering the view."""
46 title = None
47 """The title of the page."""
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``.
53 :param message: The 404 message.
55 """
56 if message is None:
57 message = _("This page could not be located.")
59 raise Http404(message)
61 def get_base_template(self):
62 """Get the name of the base template to use for rendering the view.
64 :rtype: str
66 """
67 if self.base_template_name is not None:
68 return self.base_template_name
70 raise IMustBeMissingSomething(self.__class__.__name__, "base_template_name", "get_base_template")
72 def get_css(self):
73 """Get the URLs or markup of any CSS to be included in the view.
75 :rtype: Style
77 """
78 if self.css is not None:
79 return self.css
81 return StyleSheet()
83 def get_context_data(self, **kwargs):
84 """Uses keyword arguments as the base context and adds the current view instance as ``view``.
86 :rtype: dict
88 The following variables are added to the context:
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.
99 """
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()
110 return kwargs
112 def get_js(self):
113 """Get the URLs or markup of any JavaScript to be included in the view.
115 :rtype: JavaScript
117 """
118 if self.js is not None:
119 return self.js
121 return JavaScript()
123 def get_subtitle(self):
124 """Get the page subtitle.
126 :rtype: str
128 """
129 if self.subtitle is not None:
130 return self.subtitle
132 return ""
134 def get_template_names(self):
135 """Get the name of the template used to render the response.
137 :rtype: list[str]
139 """
140 if self.template_name is not None:
141 return [self.template_name]
143 raise IMustBeMissingSomething(self.__class__.__name__, "template_name", "get_template_names")
145 def get_title(self):
146 """Get the page title.
148 :rtype: str
150 """
151 if self.title is not None:
152 return self.title
154 return ""
156 def render_to_response(self, context):
157 """Render the current context.
159 :param context: The context variables to use for rendering.
161 :rtype: TemplateResponse
163 """
164 # noinspection PyUnresolvedReferences
165 return TemplateResponse(self.request, self.get_template_names(), context=context)