* Table's __unicode__ method no longer returns as_html()
[django-tables2.git] / django_tables2 / views.py
1 from django.core.exceptions import ImproperlyConfigured
2 from django.views.generic.base import TemplateResponseMixin
3 from django.views.generic.list import BaseListView
4
5
6 class SingleTableMixin(object):
7     """
8     Adds a Table object to the context. Typically used with
9     ``TemplateResponseMixin``.
10
11     :param table_class: table class
12     :type table_class: subclass of ``django_tables2.Table``
13
14     :param table_data: data used to populate the table
15     :type table_data: any compatible data source
16
17     :param context_table_name: name of the table's template variable (default:
18         "table")
19     :type context_table_name: ``string``
20
21     This mixin plays nice with the Django's ``MultipleObjectMixin`` by using
22     ``get_queryset()`` as a fallback for the table data source.
23     """
24     table_class = None
25     table_data = None
26     context_table_name = None
27
28     def get_table(self):
29         """
30         Return a table object to use. The table has automatic support for
31         sorting and pagination.
32         """
33         table_class = self.get_table_class()
34         table = table_class(self.get_table_data(),
35                             order_by=self.request.GET.get("sort"))
36         table.paginate(page=self.request.GET.get("page", 1))
37         return table
38
39     def get_table_class(self):
40         """
41         Return the class to use for the table.
42         """
43         if self.table_class:
44             return self.table_class
45         raise ImproperlyConfigured(u"A table class was not specified. Define"
46                                    u"%(cls)s.table_class"
47                                    % {"cls": self.__class__.__name__})
48
49     def get_context_table_name(self, table):
50         """
51         Get the name to use for the table's template variable.
52         """
53         return self.context_table_name or "table"
54
55     def get_table_data(self):
56         """
57         Return the table data that should be used to populate the rows.
58         """
59         if self.table_data:
60             return self.table_data
61         elif hasattr(self, "get_queryset"):
62             return self.get_queryset()
63         raise ImproperlyConfigured(u"Table data was not specified. Define "
64                                    u"%(cls)s.table_data"
65                                    % {"cls": self.__class__.__name__})
66
67     def get_context_data(self, **kwargs):
68         """
69         Overriden version of ``TemplateResponseMixin`` to inject the table into
70         the template's context.
71         """
72         context = super(SingleTableMixin, self).get_context_data(**kwargs)
73         table = self.get_table()
74         context[self.get_context_table_name(table)] = table
75         return context
76
77
78 class SingleTableView(SingleTableMixin, TemplateResponseMixin, BaseListView):
79     """
80     Generic view that renders a template and passes in a ``Table`` object.
81     """