File: data.py

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (242 lines) | stat: -rw-r--r-- 7,492 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import warnings

from django.utils.functional import cached_property

from .utils import OrderBy, OrderByTuple, segment


class TableData:
    """
    Base class for table data containers.
    """

    def __init__(self, data):
        self.data = data

    def __getitem__(self, key):
        """
        Slicing returns a new `.TableData` instance, indexing returns a single record.
        """
        return self.data[key]

    def __iter__(self):
        """
        for ... in ... default to using this. There's a bug in Django 1.3
        with indexing into QuerySets, so this side-steps that problem (as well
        as just being a better way to iterate).
        """
        return iter(self.data)

    def set_table(self, table):
        """
        `Table.__init__` calls this method to inject an instance of itself into the
        `TableData` instance.
        Good place to do additional checks if Table and TableData instance will work
        together properly.
        """
        self.table = table

    @property
    def model(self):
        return getattr(self.data, "model", None)

    @property
    def ordering(self):
        return None

    @property
    def verbose_name(self):
        return "item"

    @property
    def verbose_name_plural(self):
        return "items"

    @staticmethod
    def from_data(data):
        # allow explicit child classes of TableData to be passed to Table()
        if isinstance(data, TableData):
            return data
        if TableQuerysetData.validate(data):
            return TableQuerysetData(data)
        elif TableListData.validate(data):
            return TableListData(list(data))

        raise ValueError(
            "data must be QuerySet-like (have count() and order_by()) or support"
            f" list(data) -- {type(data).__name__} has neither"
        )


class TableListData(TableData):
    """
    Table data container for a list of dicts, for example::

    [
        {'name': 'John', 'age': 20},
        {'name': 'Brian', 'age': 25}
    ]

    .. note::

        Other structures might have worked in the past, but are not explicitly
        supported or tested.
    """

    @staticmethod
    def validate(data):
        """
        Validates `data` for use in this container
        """
        return hasattr(data, "__iter__") or (
            hasattr(data, "__len__") and hasattr(data, "__getitem__")
        )

    def __len__(self):
        return len(self.data)

    @property
    def verbose_name(self):
        return getattr(self.data, "verbose_name", super().verbose_name)

    @property
    def verbose_name_plural(self):
        return getattr(self.data, "verbose_name_plural", super().verbose_name_plural)

    def order_by(self, aliases):
        """
        Order the data based on order by aliases (prefixed column names) in the
        table.

        Arguments:
            aliases (`~.utils.OrderByTuple`): optionally prefixed names of
                columns ('-' indicates descending order) in order of
                significance with regard to data ordering.
        """
        accessors = []
        for alias in aliases:
            bound_column = self.table.columns[OrderBy(alias).bare]

            # bound_column.order_by reflects the current ordering applied to
            # the table. As such we need to check the current ordering on the
            # column and use the opposite if it doesn't match the alias prefix.
            if alias[0] != bound_column.order_by_alias[0]:
                accessors += bound_column.order_by.opposite
            else:
                accessors += bound_column.order_by

        self.data.sort(key=OrderByTuple(accessors).key)


class TableQuerysetData(TableData):
    """
    Table data container for a queryset.
    """

    @staticmethod
    def validate(data):
        """
        Validates `data` for use in this container
        """
        return (
            hasattr(data, "count")
            and callable(data.count)
            and hasattr(data, "order_by")
            and callable(data.order_by)
        )

    def __len__(self):
        """Cached data length"""
        if not hasattr(self, "_length") or self._length is None:
            if hasattr(self.table, "paginator"):
                # for paginated tables, use QuerySet.count() as we are interested in total number of records.
                self._length = self.data.count()
            else:
                # for non-paginated tables, use the length of the QuerySet
                self._length = len(self.data)

        return self._length

    def set_table(self, table):
        super().set_table(table)
        if self.model and getattr(table._meta, "model", None) and self.model != table._meta.model:
            warnings.warn(
                f"Table data is of type {self.model} but {table._meta.model} is specified in Table.Meta.model"
            )

    @property
    def ordering(self):
        """
        Returns the list of order by aliases that are enforcing ordering on the
        data.

        If the data is unordered, an empty sequence is returned. If the
        ordering can not be determined, `None` is returned.

        This works by inspecting the actual underlying data. As such it's only
        supported for querysets.
        """

        aliases = {}
        for bound_column in self.table.columns:
            aliases[bound_column.order_by_alias] = bound_column.order_by
        try:
            return next(segment(self.data.query.order_by, aliases))
        except StopIteration:
            pass

    def order_by(self, aliases):
        """
        Order the data based on order by aliases (prefixed column names) in the
        table.

        Arguments:
            aliases (`~.utils.OrderByTuple`): optionally prefixed names of
                columns ('-' indicates descending order) in order of
                significance with regard to data ordering.
        """
        modified_any = False
        accessors = []
        for alias in aliases:
            bound_column = self.table.columns[OrderBy(alias).bare]
            # bound_column.order_by reflects the current ordering applied to
            # the table. As such we need to check the current ordering on the
            # column and use the opposite if it doesn't match the alias prefix.
            if alias[0] != bound_column.order_by_alias[0]:
                accessors += bound_column.order_by.opposite
            else:
                accessors += bound_column.order_by

            if bound_column:
                queryset, modified = bound_column.order(self.data, alias[0] == "-")

                if modified:
                    self.data = queryset
                    modified_any = True

        # custom ordering
        if modified_any:
            return

        # Traditional ordering
        if accessors:
            order_by_accessors = (a.for_queryset() for a in accessors)
            self.data = self.data.order_by(*order_by_accessors)

    @cached_property
    def verbose_name(self):
        """
        The full (singular) name for the data.

        Model's `~django.db.Model.Meta.verbose_name` is honored.
        """
        return self.data.model._meta.verbose_name

    @cached_property
    def verbose_name_plural(self):
        """
        The full (plural) name for the data.

        Model's `~django.db.Model.Meta.verbose_name` is honored.
        """
        return self.data.model._meta.verbose_name_plural